1 /* vi: set sw=4 ts=4: */
3 * ash shell port for busybox
5 * This code is derived from software contributed to Berkeley by
8 * Original BSD copyright notice is retained at the end of this file.
10 * Copyright (c) 1989, 1991, 1993, 1994
11 * The Regents of the University of California. All rights reserved.
13 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
14 * was re-ported from NetBSD and debianized.
16 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
19 //config: bool "ash (77 kb)"
21 //config: depends on !NOMMU
23 //config: The most complete and most pedantically correct shell included with
24 //config: busybox. This shell is actually a derivative of the Debian 'dash'
25 //config: shell (by Herbert Xu), which was created by porting the 'ash' shell
26 //config: (written by Kenneth Almquist) from NetBSD.
28 //config:# ash options
29 //config:# note: Don't remove !NOMMU part in the next line; it would break
30 //config:# menuconfig's indenting.
31 //config:if !NOMMU && (ASH || SH_IS_ASH || BASH_IS_ASH)
33 //config:config ASH_OPTIMIZE_FOR_SIZE
34 //config: bool "Optimize for size instead of speed"
36 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
38 //config:config ASH_INTERNAL_GLOB
39 //config: bool "Use internal glob() implementation"
40 //config: default y # Y is bigger, but because of uclibc glob() bug, let Y be default for now
41 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
43 //config: Do not use glob() function from libc, use internal implementation.
44 //config: Use this if you are getting "glob.h: No such file or directory"
45 //config: or similar build errors.
46 //config: Note that as of now (2017-01), uclibc and musl glob() both have bugs
47 //config: which would break ash if you select N here.
49 //config:config ASH_BASH_COMPAT
50 //config: bool "bash-compatible extensions"
52 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
54 //config:config ASH_JOB_CONTROL
55 //config: bool "Job control"
57 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
59 //config:config ASH_ALIAS
60 //config: bool "Alias support"
62 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
64 //config:config ASH_RANDOM_SUPPORT
65 //config: bool "Pseudorandom generator and $RANDOM variable"
67 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
69 //config: Enable pseudorandom generator and dynamic variable "$RANDOM".
70 //config: Each read of "$RANDOM" will generate a new pseudorandom value.
71 //config: You can reset the generator by using a specified start value.
72 //config: After "unset RANDOM" the generator will switch off and this
73 //config: variable will no longer have special treatment.
75 //config:config ASH_EXPAND_PRMT
76 //config: bool "Expand prompt string"
78 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
80 //config: $PS# may contain volatile content, such as backquote commands.
81 //config: This option recreates the prompt string from the environment
82 //config: variable each time it is displayed.
84 //config:config ASH_IDLE_TIMEOUT
85 //config: bool "Idle timeout variable $TMOUT"
87 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
89 //config: Enable bash-like auto-logout after $TMOUT seconds of idle time.
91 //config:config ASH_MAIL
92 //config: bool "Check for new mail in interactive shell"
94 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
96 //config: Enable "check for new mail" function:
97 //config: if set, $MAIL file and $MAILPATH list of files
98 //config: are checked for mtime changes, and "you have mail"
99 //config: message is printed if change is detected.
101 //config:config ASH_ECHO
102 //config: bool "echo builtin"
104 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
106 //config:config ASH_PRINTF
107 //config: bool "printf builtin"
109 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
111 //config:config ASH_TEST
112 //config: bool "test builtin"
114 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
116 //config:config ASH_HELP
117 //config: bool "help builtin"
119 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
121 //config:config ASH_GETOPTS
122 //config: bool "getopts builtin"
124 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
126 //config:config ASH_CMDCMD
127 //config: bool "command builtin"
129 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
131 //config: Enable support for the 'command' builtin, which allows
132 //config: you to run the specified command or builtin,
133 //config: even when there is a function with the same name.
135 //config:endif # ash options
137 //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP))
138 // APPLET_ODDNAME:name main location suid_type help
139 //applet:IF_SH_IS_ASH( APPLET_ODDNAME(sh, ash, BB_DIR_BIN, BB_SUID_DROP, ash))
140 //applet:IF_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, BB_DIR_BIN, BB_SUID_DROP, ash))
142 //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o
143 //kbuild:lib-$(CONFIG_SH_IS_ASH) += ash.o ash_ptr_hack.o shell_common.o
144 //kbuild:lib-$(CONFIG_BASH_IS_ASH) += ash.o ash_ptr_hack.o shell_common.o
145 //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o
148 * DEBUG=1 to compile in debugging ('set -o debug' turns on)
149 * DEBUG=2 to compile in and turn on debugging.
150 * When debugging is on ("set -o debug" was executed, or DEBUG=2),
151 * debugging info is written to ./trace, quit signal generates core dump.
154 /* Tweak debug output verbosity here */
158 #define DEBUG_INTONOFF 0
162 #define JOBS ENABLE_ASH_JOB_CONTROL
166 #include <sys/times.h>
167 #include <sys/utsname.h> /* for setting $HOSTNAME */
168 #include "busybox.h" /* for applet_names */
170 /* So far, all bash compat is controlled by one config option */
171 /* Separate defines document which part of code implements what */
172 /* function keyword */
173 #define BASH_FUNCTION ENABLE_ASH_BASH_COMPAT
174 #define IF_BASH_FUNCTION IF_ASH_BASH_COMPAT
176 #define BASH_REDIR_OUTPUT ENABLE_ASH_BASH_COMPAT
177 #define IF_BASH_REDIR_OUTPUT IF_ASH_BASH_COMPAT
179 #define BASH_DOLLAR_SQUOTE ENABLE_ASH_BASH_COMPAT
180 #define IF_BASH_DOLLAR_SQUOTE IF_ASH_BASH_COMPAT
181 #define BASH_PATTERN_SUBST ENABLE_ASH_BASH_COMPAT
182 #define IF_BASH_PATTERN_SUBST IF_ASH_BASH_COMPAT
183 #define BASH_SUBSTR ENABLE_ASH_BASH_COMPAT
184 #define IF_BASH_SUBSTR IF_ASH_BASH_COMPAT
186 #define BASH_TEST2 (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST)
187 #define BASH_SOURCE ENABLE_ASH_BASH_COMPAT
188 #define BASH_PIPEFAIL ENABLE_ASH_BASH_COMPAT
189 #define BASH_HOSTNAME_VAR ENABLE_ASH_BASH_COMPAT
190 #define BASH_SHLVL_VAR ENABLE_ASH_BASH_COMPAT
192 #if defined(__ANDROID_API__) && __ANDROID_API__ <= 24
193 /* Bionic at least up to version 24 has no glob() */
194 # undef ENABLE_ASH_INTERNAL_GLOB
195 # define ENABLE_ASH_INTERNAL_GLOB 1
198 #if !ENABLE_ASH_INTERNAL_GLOB && defined(__UCLIBC__)
199 # error uClibc glob() is buggy, use ASH_INTERNAL_GLOB.
200 # error The bug is: for "$PWD"/<pattern> ash will escape e.g. dashes in "$PWD"
201 # error with backslash, even ones which do not need to be: "/a-b" -> "/a\-b"
202 # error glob() should unbackslash them and match. uClibc does not unbackslash,
203 # error fails to match dirname, subsequently not expanding <pattern> in it.
205 // if (glob("/etc/polkit\\-1", 0, NULL, &pglob)) - this returns 0 on uclibc, no bug
206 // if (glob("/etc/polkit\\-1/*", 0, NULL, &pglob)) printf("uclibc bug!\n");
209 #if !ENABLE_ASH_INTERNAL_GLOB
214 #include "shell_common.h"
215 #if ENABLE_FEATURE_SH_MATH
218 typedef long arith_t;
219 # define ARITH_FMT "%ld"
221 #if ENABLE_ASH_RANDOM_SUPPORT
224 # define CLEAR_RANDOM_T(rnd) ((void)0)
227 #include "NUM_APPLETS.h"
229 /* STANDALONE does not make sense, and won't compile */
230 # undef CONFIG_FEATURE_SH_STANDALONE
231 # undef ENABLE_FEATURE_SH_STANDALONE
232 # undef IF_FEATURE_SH_STANDALONE
233 # undef IF_NOT_FEATURE_SH_STANDALONE
234 # define ENABLE_FEATURE_SH_STANDALONE 0
235 # define IF_FEATURE_SH_STANDALONE(...)
236 # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
240 # define PIPE_BUF 4096 /* amount of buffering in a pipe */
244 # error "Do not even bother, ash will not run on NOMMU machine"
248 /* ============ Hash table sizes. Configurable. */
252 #define CMDTABLESIZE 31 /* should be prime */
255 /* ============ Shell options */
257 static const char *const optletters_optnames[] = {
281 #define optletters(n) optletters_optnames[n][0]
282 #define optnames(n) (optletters_optnames[n] + 1)
284 enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
287 /* ============ Misc data */
289 #define msg_illnum "Illegal number: %s"
292 * We enclose jmp_buf in a structure so that we can declare pointers to
293 * jump locations. The global variable handler contains the location to
294 * jump to when an exception occurs, and the global variable exception_type
295 * contains a code identifying the exception. To implement nested
296 * exception handlers, the user should save the value of handler on entry
297 * to an inner scope, set handler to point to a jmploc structure for the
298 * inner scope, and restore handler on exit from the scope.
304 struct globals_misc {
305 uint8_t exitstatus; /* exit status of last command */
306 uint8_t back_exitstatus;/* exit status of backquoted command */
307 smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */
308 int rootpid; /* pid of main shell */
309 /* shell level: 0 for the main shell, 1 for its children, and so on */
311 #define rootshell (!shlvl)
312 char *minusc; /* argument to -c option */
314 char *curdir; // = nullstr; /* current working directory */
315 char *physdir; // = nullstr; /* physical working directory */
317 char *arg0; /* value of $0 */
319 struct jmploc *exception_handler;
321 volatile int suppress_int; /* counter */
322 volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */
323 volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */
324 volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */
325 smallint exception_type; /* kind of exception (0..5) */
327 #define EXINT 0 /* SIGINT received */
328 #define EXERROR 1 /* a generic error */
329 #define EXEXIT 4 /* exit the shell */
331 char nullstr[1]; /* zero length string */
334 #define eflag optlist[0]
335 #define fflag optlist[1]
336 #define Iflag optlist[2]
337 #define iflag optlist[3]
338 #define mflag optlist[4]
339 #define nflag optlist[5]
340 #define sflag optlist[6]
341 #define xflag optlist[7]
342 #define vflag optlist[8]
343 #define Cflag optlist[9]
344 #define aflag optlist[10]
345 #define bflag optlist[11]
346 #define uflag optlist[12]
347 #define viflag optlist[13]
349 # define pipefail optlist[14]
354 # define nolog optlist[14 + BASH_PIPEFAIL]
355 # define debug optlist[15 + BASH_PIPEFAIL]
358 /* trap handler commands */
360 * Sigmode records the current value of the signal handlers for the various
361 * modes. A value of zero means that the current handler is not known.
362 * S_HARD_IGN indicates that the signal was ignored on entry to the shell.
364 char sigmode[NSIG - 1];
365 #define S_DFL 1 /* default signal handling (SIG_DFL) */
366 #define S_CATCH 2 /* signal is caught */
367 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
368 #define S_HARD_IGN 4 /* signal is ignored permanently */
370 /* indicates specified signal received */
371 uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */
372 uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */
374 char **trap_ptr; /* used only by "trap hack" */
376 /* Rarely referenced stuff */
377 #if ENABLE_ASH_RANDOM_SUPPORT
380 pid_t backgndpid; /* pid of last background process */
382 extern struct globals_misc *const ash_ptr_to_globals_misc;
383 #define G_misc (*ash_ptr_to_globals_misc)
384 #define exitstatus (G_misc.exitstatus )
385 #define back_exitstatus (G_misc.back_exitstatus )
386 #define job_warning (G_misc.job_warning)
387 #define rootpid (G_misc.rootpid )
388 #define shlvl (G_misc.shlvl )
389 #define minusc (G_misc.minusc )
390 #define curdir (G_misc.curdir )
391 #define physdir (G_misc.physdir )
392 #define arg0 (G_misc.arg0 )
393 #define exception_handler (G_misc.exception_handler)
394 #define exception_type (G_misc.exception_type )
395 #define suppress_int (G_misc.suppress_int )
396 #define pending_int (G_misc.pending_int )
397 #define got_sigchld (G_misc.got_sigchld )
398 #define pending_sig (G_misc.pending_sig )
399 #define nullstr (G_misc.nullstr )
400 #define optlist (G_misc.optlist )
401 #define sigmode (G_misc.sigmode )
402 #define gotsig (G_misc.gotsig )
403 #define may_have_traps (G_misc.may_have_traps )
404 #define trap (G_misc.trap )
405 #define trap_ptr (G_misc.trap_ptr )
406 #define random_gen (G_misc.random_gen )
407 #define backgndpid (G_misc.backgndpid )
408 #define INIT_G_misc() do { \
409 (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
417 /* ============ DEBUG */
419 static void trace_printf(const char *fmt, ...);
420 static void trace_vprintf(const char *fmt, va_list va);
421 # define TRACE(param) trace_printf param
422 # define TRACEV(param) trace_vprintf param
423 # define close(fd) do { \
425 if (close(dfd) < 0) \
426 bb_error_msg("bug on %d: closing %d(0x%x)", \
427 __LINE__, dfd, dfd); \
430 # define TRACE(param)
431 # define TRACEV(param)
435 /* ============ Utility functions */
436 #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
437 #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
440 isdigit_str9(const char *str)
442 int maxlen = 9 + 1; /* max 9 digits: 999999999 */
443 while (--maxlen && isdigit(*str))
445 return (*str == '\0');
449 var_end(const char *var)
458 /* ============ Interrupts / exceptions */
460 static void exitshell(void) NORETURN;
463 * These macros allow the user to suspend the handling of interrupt signals
464 * over a period of time. This is similar to SIGHOLD or to sigblock, but
465 * much more efficient and portable. (But hacking the kernel is so much
466 * more fun than worrying about efficiency and portability. :-))
469 # define INT_OFF do { \
470 TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \
475 # define INT_OFF do { \
482 * Called to raise an exception. Since C doesn't include exceptions, we
483 * just do a longjmp to the exception handler. The type of exception is
484 * stored in the global variable "exception_type".
486 static void raise_exception(int) NORETURN;
488 raise_exception(int e)
491 if (exception_handler == NULL)
496 longjmp(exception_handler->loc, 1);
499 #define raise_exception(e) do { \
500 TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \
501 raise_exception(e); \
506 * Called when a SIGINT is received. (If the user specifies
507 * that SIGINT is to be trapped or ignored using the trap builtin, then
508 * this routine is not called.) Suppressint is nonzero when interrupts
509 * are held using the INT_OFF macro. (The test for iflag is just
510 * defensive programming.)
512 static void raise_interrupt(void) NORETURN;
514 raise_interrupt(void)
517 /* Signal is not automatically unmasked after it is raised,
518 * do it ourself - unmask all signals */
519 sigprocmask_allsigs(SIG_UNBLOCK);
520 /* pending_sig = 0; - now done in signal_handler() */
522 if (!(rootshell && iflag)) {
523 /* Kill ourself with SIGINT */
524 signal(SIGINT, SIG_DFL);
527 /* bash: ^C even on empty command line sets $? */
528 exitstatus = SIGINT + 128;
529 raise_exception(EXINT);
533 #define raise_interrupt() do { \
534 TRACE(("raising interrupt on line %d\n", __LINE__)); \
539 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
543 if (--suppress_int == 0 && pending_int) {
548 # define INT_ON do { \
549 TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \
553 # define INT_ON int_on()
555 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
563 #define FORCE_INT_ON force_int_on()
565 #define SAVE_INT(v) ((v) = suppress_int)
567 #define RESTORE_INT(v) do { \
569 suppress_int = (v); \
570 if (suppress_int == 0 && pending_int) \
575 /* ============ Stdout/stderr output */
578 outstr(const char *p, FILE *file)
586 flush_stdout_stderr(void)
593 /* Was called outcslow(c,FILE*), but c was always '\n' */
595 newline_and_flush(FILE *dest)
603 static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2)));
605 out1fmt(const char *fmt, ...)
612 r = vprintf(fmt, ap);
618 static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4)));
620 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
627 ret = vsnprintf(outbuf, length, fmt, ap);
634 out1str(const char *p)
640 out2str(const char *p)
643 flush_stdout_stderr();
647 /* ============ Parser structures */
649 /* control characters in argument strings */
650 #define CTL_FIRST CTLESC
651 #define CTLESC ((unsigned char)'\201') /* escape next character */
652 #define CTLVAR ((unsigned char)'\202') /* variable defn */
653 #define CTLENDVAR ((unsigned char)'\203')
654 #define CTLBACKQ ((unsigned char)'\204')
655 #define CTLARI ((unsigned char)'\206') /* arithmetic expression */
656 #define CTLENDARI ((unsigned char)'\207')
657 #define CTLQUOTEMARK ((unsigned char)'\210')
658 #define CTL_LAST CTLQUOTEMARK
660 /* variable substitution byte (follows CTLVAR) */
661 #define VSTYPE 0x0f /* type of variable substitution */
662 #define VSNUL 0x10 /* colon--treat the empty string as unset */
664 /* values of VSTYPE field */
665 #define VSNORMAL 0x1 /* normal variable: $var or ${var} */
666 #define VSMINUS 0x2 /* ${var-text} */
667 #define VSPLUS 0x3 /* ${var+text} */
668 #define VSQUESTION 0x4 /* ${var?message} */
669 #define VSASSIGN 0x5 /* ${var=text} */
670 #define VSTRIMRIGHT 0x6 /* ${var%pattern} */
671 #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */
672 #define VSTRIMLEFT 0x8 /* ${var#pattern} */
673 #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */
674 #define VSLENGTH 0xa /* ${#var} */
676 #define VSSUBSTR 0xc /* ${var:position:length} */
678 #if BASH_PATTERN_SUBST
679 #define VSREPLACE 0xd /* ${var/pattern/replacement} */
680 #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */
683 static const char dolatstr[] ALIGN1 = {
684 CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0'
686 #define DOLATSTRLEN 6
705 #if BASH_REDIR_OUTPUT
722 smallint type; /* Nxxxx */
725 union node *redirect;
730 smallint pipe_backgnd;
731 struct nodelist *cmdlist;
737 union node *redirect;
750 union node *elsepart;
777 struct nodelist *backquote;
780 /* nfile and ndup layout must match!
781 * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight
782 * that it is actually NTO2 (>&file), and change its type.
799 char *_unused_expfname;
818 struct nredir nredir;
819 struct nbinary nbinary;
823 struct nclist nclist;
832 * NODE_EOF is returned by parsecmd when it encounters an end of file.
833 * It must be distinct from NULL.
835 #define NODE_EOF ((union node *) -1L)
838 struct nodelist *next;
851 freefunc(struct funcnode *f)
853 if (f && --f->count < 0)
858 /* ============ Debugging output */
862 static FILE *tracefile;
865 trace_printf(const char *fmt, ...)
872 fprintf(tracefile, "%u ", (int) time(NULL));
874 fprintf(tracefile, "[%u] ", (int) getpid());
876 fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int);
878 vfprintf(tracefile, fmt, va);
883 trace_vprintf(const char *fmt, va_list va)
887 vfprintf(tracefile, fmt, va);
888 fprintf(tracefile, "\n");
892 trace_puts(const char *s)
900 trace_puts_quoted(char *s)
907 putc('"', tracefile);
908 for (p = s; *p; p++) {
909 switch ((unsigned char)*p) {
910 case '\n': c = 'n'; goto backslash;
911 case '\t': c = 't'; goto backslash;
912 case '\r': c = 'r'; goto backslash;
913 case '\"': c = '\"'; goto backslash;
914 case '\\': c = '\\'; goto backslash;
915 case CTLESC: c = 'e'; goto backslash;
916 case CTLVAR: c = 'v'; goto backslash;
917 case CTLBACKQ: c = 'q'; goto backslash;
919 putc('\\', tracefile);
923 if (*p >= ' ' && *p <= '~')
926 putc('\\', tracefile);
927 putc((*p >> 6) & 03, tracefile);
928 putc((*p >> 3) & 07, tracefile);
929 putc(*p & 07, tracefile);
934 putc('"', tracefile);
938 trace_puts_args(char **ap)
945 trace_puts_quoted(*ap);
947 putc('\n', tracefile);
950 putc(' ', tracefile);
965 /* leave open because libedit might be using it */
968 strcpy(s, "./trace");
970 if (!freopen(s, "a", tracefile)) {
971 fprintf(stderr, "Can't re-open %s\n", s);
976 tracefile = fopen(s, "a");
977 if (tracefile == NULL) {
978 fprintf(stderr, "Can't open %s\n", s);
984 flags = fcntl(fileno(tracefile), F_GETFL);
986 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
988 setlinebuf(tracefile);
989 fputs("\nTracing started.\n", tracefile);
993 indent(int amount, char *pfx, FILE *fp)
997 for (i = 0; i < amount; i++) {
998 if (pfx && i == amount - 1)
1004 /* little circular references here... */
1005 static void shtree(union node *n, int ind, char *pfx, FILE *fp);
1008 sharg(union node *arg, FILE *fp)
1011 struct nodelist *bqlist;
1012 unsigned char subtype;
1014 if (arg->type != NARG) {
1015 out1fmt("<node type %d>\n", arg->type);
1018 bqlist = arg->narg.backquote;
1019 for (p = arg->narg.text; *p; p++) {
1020 switch ((unsigned char)*p) {
1029 if (subtype == VSLENGTH)
1037 if (subtype & VSNUL)
1040 switch (subtype & VSTYPE) {
1066 case VSTRIMRIGHTMAX:
1073 out1fmt("<subtype %d>", subtype);
1082 shtree(bqlist->n, -1, NULL, fp);
1093 shcmd(union node *cmd, FILE *fp)
1101 for (np = cmd->ncmd.args; np; np = np->narg.next) {
1107 for (np = cmd->ncmd.redirect; np; np = np->nfile.next) {
1111 switch (np->nfile.type) {
1112 case NTO: s = ">>"+1; dftfd = 1; break;
1113 case NCLOBBER: s = ">|"; dftfd = 1; break;
1114 case NAPPEND: s = ">>"; dftfd = 1; break;
1115 #if BASH_REDIR_OUTPUT
1118 case NTOFD: s = ">&"; dftfd = 1; break;
1119 case NFROM: s = "<"; break;
1120 case NFROMFD: s = "<&"; break;
1121 case NFROMTO: s = "<>"; break;
1122 default: s = "*error*"; break;
1124 if (np->nfile.fd != dftfd)
1125 fprintf(fp, "%d", np->nfile.fd);
1127 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1128 fprintf(fp, "%d", np->ndup.dupfd);
1130 sharg(np->nfile.fname, fp);
1137 shtree(union node *n, int ind, char *pfx, FILE *fp)
1139 struct nodelist *lp;
1145 indent(ind, pfx, fp);
1147 if (n == NODE_EOF) {
1162 shtree(n->nbinary.ch1, ind, NULL, fp);
1165 shtree(n->nbinary.ch2, ind, NULL, fp);
1173 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
1174 shtree(lp->n, 0, NULL, fp);
1178 if (n->npipe.pipe_backgnd)
1184 fprintf(fp, "<node type %d>", n->type);
1192 showtree(union node *n)
1194 trace_puts("showtree called\n");
1195 shtree(n, 1, NULL, stderr);
1201 /* ============ Parser data */
1204 * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up.
1207 struct strlist *next;
1214 struct strpush *prev; /* preceding string on stack */
1216 int prev_left_in_line;
1217 #if ENABLE_ASH_ALIAS
1218 struct alias *ap; /* if push was associated with an alias */
1220 char *string; /* remember the string since it may change */
1222 /* Remember last two characters for pungetc. */
1225 /* Number of outstanding calls to pungetc. */
1230 struct parsefile *prev; /* preceding file on stack */
1231 int linno; /* current line */
1232 int pf_fd; /* file descriptor (or -1 if string) */
1233 int left_in_line; /* number of chars left in this line */
1234 int left_in_buffer; /* number of chars left in this buffer past the line */
1235 char *next_to_pgetc; /* next char in buffer */
1236 char *buf; /* input buffer */
1237 struct strpush *strpush; /* for pushing strings at this level */
1238 struct strpush basestrpush; /* so pushing one is fast */
1240 /* Remember last two characters for pungetc. */
1243 /* Number of outstanding calls to pungetc. */
1247 static struct parsefile basepf; /* top level input file */
1248 static struct parsefile *g_parsefile = &basepf; /* current input file */
1249 static int startlinno; /* line # where last token started */
1250 static char *commandname; /* currently executing command */
1253 /* ============ Message printing */
1256 ash_vmsg(const char *msg, va_list ap)
1258 fprintf(stderr, "%s: ", arg0);
1260 if (strcmp(arg0, commandname))
1261 fprintf(stderr, "%s: ", commandname);
1262 if (!iflag || g_parsefile->pf_fd > 0)
1263 fprintf(stderr, "line %d: ", startlinno);
1265 vfprintf(stderr, msg, ap);
1266 newline_and_flush(stderr);
1270 * Exverror is called to raise the error exception. If the second argument
1271 * is not NULL then error prints an error message using printf style
1272 * formatting. It then raises the error exception.
1274 static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN;
1276 ash_vmsg_and_raise(int cond, const char *msg, va_list ap)
1280 TRACE(("ash_vmsg_and_raise(%d):", cond));
1283 TRACE(("ash_vmsg_and_raise(%d):NULL\n", cond));
1288 flush_stdout_stderr();
1289 raise_exception(cond);
1293 static void ash_msg_and_raise_error(const char *, ...) NORETURN;
1295 ash_msg_and_raise_error(const char *msg, ...)
1302 ash_vmsg_and_raise(EXERROR, msg, ap);
1308 * Use '%m' to append error string on platforms that support it, '%s' and
1309 * strerror() on those that don't.
1311 * 'fmt' must be a string literal.
1313 #ifdef HAVE_PRINTF_PERCENTM
1314 #define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": %m", ##__VA_ARGS__)
1316 #define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": %s", ##__VA_ARGS__, strerror(errno))
1319 static void raise_error_syntax(const char *) NORETURN;
1321 raise_error_syntax(const char *msg)
1323 ash_msg_and_raise_error("syntax error: %s", msg);
1327 static void ash_msg_and_raise(int, const char *, ...) NORETURN;
1329 ash_msg_and_raise(int cond, const char *msg, ...)
1334 ash_vmsg_and_raise(cond, msg, ap);
1340 * error/warning routines for external builtins
1343 ash_msg(const char *fmt, ...)
1353 * Return a string describing an error. The returned string may be a
1354 * pointer to a static buffer that will be overwritten on the next call.
1355 * Action describes the operation that got the error.
1358 errmsg(int e, const char *em)
1360 if (e == ENOENT || e == ENOTDIR) {
1367 /* ============ Memory allocation */
1370 /* I consider these wrappers nearly useless:
1371 * ok, they return you to nearest exception handler, but
1372 * how much memory do you leak in the process, making
1373 * memory starvation worse?
1376 ckrealloc(void * p, size_t nbytes)
1378 p = realloc(p, nbytes);
1380 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1385 ckmalloc(size_t nbytes)
1387 return ckrealloc(NULL, nbytes);
1391 ckzalloc(size_t nbytes)
1393 return memset(ckmalloc(nbytes), 0, nbytes);
1397 ckstrdup(const char *s)
1399 char *p = strdup(s);
1401 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1405 /* Using bbox equivalents. They exit if out of memory */
1406 # define ckrealloc xrealloc
1407 # define ckmalloc xmalloc
1408 # define ckzalloc xzalloc
1409 # define ckstrdup xstrdup
1413 * It appears that grabstackstr() will barf with such alignments
1414 * because stalloc() will return a string allocated in a new stackblock.
1416 #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE)
1418 /* Most machines require the value returned from malloc to be aligned
1419 * in some way. The following macro will get this right
1420 * on many machines. */
1421 SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,
1422 /* Minimum size of a block */
1423 MINSIZE = SHELL_ALIGN(504),
1426 struct stack_block {
1427 struct stack_block *prev;
1428 char space[MINSIZE];
1432 struct stack_block *stackp;
1438 struct globals_memstack {
1439 struct stack_block *g_stackp; // = &stackbase;
1440 char *g_stacknxt; // = stackbase.space;
1441 char *sstrend; // = stackbase.space + MINSIZE;
1442 size_t g_stacknleft; // = MINSIZE;
1443 struct stack_block stackbase;
1445 extern struct globals_memstack *const ash_ptr_to_globals_memstack;
1446 #define G_memstack (*ash_ptr_to_globals_memstack)
1447 #define g_stackp (G_memstack.g_stackp )
1448 #define g_stacknxt (G_memstack.g_stacknxt )
1449 #define sstrend (G_memstack.sstrend )
1450 #define g_stacknleft (G_memstack.g_stacknleft)
1451 #define stackbase (G_memstack.stackbase )
1452 #define INIT_G_memstack() do { \
1453 (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
1455 g_stackp = &stackbase; \
1456 g_stacknxt = stackbase.space; \
1457 g_stacknleft = MINSIZE; \
1458 sstrend = stackbase.space + MINSIZE; \
1462 #define stackblock() ((void *)g_stacknxt)
1463 #define stackblocksize() g_stacknleft
1466 * Parse trees for commands are allocated in lifo order, so we use a stack
1467 * to make this more efficient, and also to avoid all sorts of exception
1468 * handling code to handle interrupts in the middle of a parse.
1470 * The size 504 was chosen because the Ultrix malloc handles that size
1474 stalloc(size_t nbytes)
1479 aligned = SHELL_ALIGN(nbytes);
1480 if (aligned > g_stacknleft) {
1483 struct stack_block *sp;
1485 blocksize = aligned;
1486 if (blocksize < MINSIZE)
1487 blocksize = MINSIZE;
1488 len = sizeof(struct stack_block) - MINSIZE + blocksize;
1489 if (len < blocksize)
1490 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1493 sp->prev = g_stackp;
1494 g_stacknxt = sp->space;
1495 g_stacknleft = blocksize;
1496 sstrend = g_stacknxt + blocksize;
1501 g_stacknxt += aligned;
1502 g_stacknleft -= aligned;
1507 stzalloc(size_t nbytes)
1509 return memset(stalloc(nbytes), 0, nbytes);
1516 if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) {
1517 write(STDERR_FILENO, "stunalloc\n", 10);
1521 g_stacknleft += g_stacknxt - (char *)p;
1526 * Like strdup but works with the ash stack.
1529 sstrdup(const char *p)
1531 size_t len = strlen(p) + 1;
1532 return memcpy(stalloc(len), p, len);
1536 grabstackblock(size_t len)
1542 pushstackmark(struct stackmark *mark, size_t len)
1544 mark->stackp = g_stackp;
1545 mark->stacknxt = g_stacknxt;
1546 mark->stacknleft = g_stacknleft;
1547 grabstackblock(len);
1551 setstackmark(struct stackmark *mark)
1553 pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase);
1557 popstackmark(struct stackmark *mark)
1559 struct stack_block *sp;
1565 while (g_stackp != mark->stackp) {
1567 g_stackp = sp->prev;
1570 g_stacknxt = mark->stacknxt;
1571 g_stacknleft = mark->stacknleft;
1572 sstrend = mark->stacknxt + mark->stacknleft;
1577 * When the parser reads in a string, it wants to stick the string on the
1578 * stack and only adjust the stack pointer when it knows how big the
1579 * string is. Stackblock (defined in stack.h) returns a pointer to a block
1580 * of space on top of the stack and stackblocklen returns the length of
1581 * this block. Growstackblock will grow this space by at least one byte,
1582 * possibly moving it (like realloc). Grabstackblock actually allocates the
1583 * part of the block that has been used.
1586 growstackblock(void)
1590 newlen = g_stacknleft * 2;
1591 if (newlen < g_stacknleft)
1592 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1596 if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) {
1597 struct stack_block *sp;
1598 struct stack_block *prevstackp;
1603 prevstackp = sp->prev;
1604 grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
1605 sp = ckrealloc(sp, grosslen);
1606 sp->prev = prevstackp;
1608 g_stacknxt = sp->space;
1609 g_stacknleft = newlen;
1610 sstrend = sp->space + newlen;
1613 char *oldspace = g_stacknxt;
1614 size_t oldlen = g_stacknleft;
1615 char *p = stalloc(newlen);
1617 /* free the space we just allocated */
1618 g_stacknxt = memcpy(p, oldspace, oldlen);
1619 g_stacknleft += newlen;
1624 * The following routines are somewhat easier to use than the above.
1625 * The user declares a variable of type STACKSTR, which may be declared
1626 * to be a register. The macro STARTSTACKSTR initializes things. Then
1627 * the user uses the macro STPUTC to add characters to the string. In
1628 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
1629 * grown as necessary. When the user is done, she can just leave the
1630 * string there and refer to it using stackblock(). Or she can allocate
1631 * the space for it using grabstackstr(). If it is necessary to allow
1632 * someone else to use the stack temporarily and then continue to grow
1633 * the string, the user should use grabstack to allocate the space, and
1634 * then call ungrabstr(p) to return to the previous mode of operation.
1636 * USTPUTC is like STPUTC except that it doesn't check for overflow.
1637 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
1638 * is space for at least one character.
1643 size_t len = stackblocksize();
1645 return (char *)stackblock() + len;
1649 * Called from CHECKSTRSPACE.
1652 makestrspace(size_t newlen, char *p)
1654 size_t len = p - g_stacknxt;
1660 size = stackblocksize();
1662 if (nleft >= newlen)
1666 return (char *)stackblock() + len;
1670 stack_nputstr(const char *s, size_t n, char *p)
1672 p = makestrspace(n, p);
1673 p = (char *)mempcpy(p, s, n);
1678 stack_putstr(const char *s, char *p)
1680 return stack_nputstr(s, strlen(s), p);
1684 _STPUTC(int c, char *p)
1692 #define STARTSTACKSTR(p) ((p) = stackblock())
1693 #define STPUTC(c, p) ((p) = _STPUTC((c), (p)))
1694 #define CHECKSTRSPACE(n, p) do { \
1697 size_t m = sstrend - q; \
1699 (p) = makestrspace(l, q); \
1701 #define USTPUTC(c, p) (*(p)++ = (c))
1702 #define STACKSTRNUL(p) do { \
1703 if ((p) == sstrend) \
1704 (p) = growstackstr(); \
1707 #define STUNPUTC(p) (--(p))
1708 #define STTOPC(p) ((p)[-1])
1709 #define STADJUST(amount, p) ((p) += (amount))
1711 #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock())
1712 #define ungrabstackstr(s, p) stunalloc(s)
1713 #define stackstrend() ((void *)sstrend)
1716 /* ============ String helpers */
1719 * prefix -- see if pfx is a prefix of string.
1722 prefix(const char *string, const char *pfx)
1725 if (*pfx++ != *string++)
1728 return (char *) string;
1732 * Check for a valid number. This should be elsewhere.
1735 is_number(const char *p)
1740 } while (*++p != '\0');
1745 * Convert a string of digits to an integer, printing an error message on
1749 number(const char *s)
1752 ash_msg_and_raise_error(msg_illnum, s);
1757 * Produce a single quoted string suitable as input to the shell.
1758 * The return string is allocated on the stack.
1761 single_quote(const char *s)
1771 len = strchrnul(s, '\'') - s;
1773 q = p = makestrspace(len + 3, p);
1776 q = (char *)mempcpy(q, s, len);
1785 do len++; while (*++s == '\'');
1787 q = p = makestrspace(len + 3, p);
1790 q = (char *)mempcpy(q, s - len, len);
1798 return stackblock();
1802 * Produce a possibly single quoted string suitable as input to the shell.
1803 * If quoting was done, the return string is allocated on the stack,
1804 * otherwise a pointer to the original string is returned.
1807 maybe_single_quote(const char *s)
1812 /* Assuming ACSII */
1813 /* quote ctrl_chars space !"#$%&'()* */
1817 if (*p >= ';' && *p <= '?')
1826 /* quote {|}~ DEL and high bytes */
1829 /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */
1830 /* TODO: maybe avoid quoting % */
1836 return single_quote(s);
1840 /* ============ nextopt */
1842 static char **argptr; /* argument list for builtin commands */
1843 static char *optionarg; /* set by nextopt (like getopt) */
1844 static char *optptr; /* used by nextopt */
1847 * XXX - should get rid of. Have all builtins use getopt(3).
1848 * The library getopt must have the BSD extension static variable
1849 * "optreset", otherwise it can't be used within the shell safely.
1851 * Standard option processing (a la getopt) for builtin routines.
1852 * The only argument that is passed to nextopt is the option string;
1853 * the other arguments are unnecessary. It returns the character,
1854 * or '\0' on end of input.
1857 nextopt(const char *optstring)
1864 if (p == NULL || *p == '\0') {
1865 /* We ate entire "-param", take next one */
1871 if (*++p == '\0') /* just "-" ? */
1874 if (LONE_DASH(p)) /* "--" ? */
1876 /* p => next "-param" */
1878 /* p => some option char in the middle of a "-param" */
1880 for (q = optstring; *q != c;) {
1882 ash_msg_and_raise_error("illegal option -%c", c);
1890 ash_msg_and_raise_error("no arg for -%c option", c);
1900 /* ============ Shell variables */
1903 * The parsefile structure pointed to by the global variable parsefile
1904 * contains information about the current file being read.
1907 int nparam; /* # of positional parameters (without $0) */
1908 #if ENABLE_ASH_GETOPTS
1909 int optind; /* next parameter to be processed by getopts */
1910 int optoff; /* used by getopts */
1912 unsigned char malloced; /* if parameter list dynamically allocated */
1913 char **p; /* parameter list */
1917 * Free the list of positional parameters.
1920 freeparam(volatile struct shparam *param)
1922 if (param->malloced) {
1924 ap = ap1 = param->p;
1931 #if ENABLE_ASH_GETOPTS
1932 static void FAST_FUNC getoptsreset(const char *value);
1936 struct var *next; /* next entry in hash list */
1937 int flags; /* flags are defined above */
1938 const char *var_text; /* name=value */
1939 void (*var_func)(const char *) FAST_FUNC; /* function to be called when */
1940 /* the variable gets set/unset */
1944 struct localvar *next; /* next local variable in list */
1945 struct var *vp; /* the variable that was made local */
1946 int flags; /* saved flags */
1947 const char *text; /* saved text */
1951 #define VEXPORT 0x01 /* variable is exported */
1952 #define VREADONLY 0x02 /* variable cannot be modified */
1953 #define VSTRFIXED 0x04 /* variable struct is statically allocated */
1954 #define VTEXTFIXED 0x08 /* text is statically allocated */
1955 #define VSTACK 0x10 /* text is allocated on the stack */
1956 #define VUNSET 0x20 /* the variable is not set */
1957 #define VNOFUNC 0x40 /* don't call the callback function */
1958 #define VNOSET 0x80 /* do not set variable - just readonly test */
1959 #define VNOSAVE 0x100 /* when text is on the heap before setvareq */
1960 #if ENABLE_ASH_RANDOM_SUPPORT
1961 # define VDYNAMIC 0x200 /* dynamic variable */
1967 /* Need to be before varinit_data[] */
1968 #if ENABLE_LOCALE_SUPPORT
1969 static void FAST_FUNC
1970 change_lc_all(const char *value)
1972 if (value && *value != '\0')
1973 setlocale(LC_ALL, value);
1975 static void FAST_FUNC
1976 change_lc_ctype(const char *value)
1978 if (value && *value != '\0')
1979 setlocale(LC_CTYPE, value);
1983 static void chkmail(void);
1984 static void changemail(const char *var_value) FAST_FUNC;
1986 # define chkmail() ((void)0)
1988 static void changepath(const char *) FAST_FUNC;
1989 #if ENABLE_ASH_RANDOM_SUPPORT
1990 static void change_random(const char *) FAST_FUNC;
1993 static const struct {
1995 const char *var_text;
1996 void (*var_func)(const char *) FAST_FUNC;
1997 } varinit_data[] = {
1999 * Note: VEXPORT would not work correctly here for NOFORK applets:
2000 * some environment strings may be constant.
2002 { VSTRFIXED|VTEXTFIXED , defifsvar , NULL },
2004 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail },
2005 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail },
2007 { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath },
2008 { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL },
2009 { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL },
2010 { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL },
2011 #if ENABLE_ASH_GETOPTS
2012 { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset },
2014 #if ENABLE_ASH_RANDOM_SUPPORT
2015 { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random },
2017 #if ENABLE_LOCALE_SUPPORT
2018 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all },
2019 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype },
2021 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
2022 { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL },
2028 struct globals_var {
2029 struct shparam shellparam; /* $@ current positional parameters */
2030 struct redirtab *redirlist;
2031 int preverrout_fd; /* stderr fd: usually 2, unless redirect moved it */
2032 struct var *vartab[VTABSIZE];
2033 struct var varinit[ARRAY_SIZE(varinit_data)];
2035 extern struct globals_var *const ash_ptr_to_globals_var;
2036 #define G_var (*ash_ptr_to_globals_var)
2037 #define shellparam (G_var.shellparam )
2038 //#define redirlist (G_var.redirlist )
2039 #define preverrout_fd (G_var.preverrout_fd)
2040 #define vartab (G_var.vartab )
2041 #define varinit (G_var.varinit )
2042 #define INIT_G_var() do { \
2044 (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
2046 for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
2047 varinit[i].flags = varinit_data[i].flags; \
2048 varinit[i].var_text = varinit_data[i].var_text; \
2049 varinit[i].var_func = varinit_data[i].var_func; \
2053 #define vifs varinit[0]
2055 # define vmail (&vifs)[1]
2056 # define vmpath (&vmail)[1]
2057 # define vpath (&vmpath)[1]
2059 # define vpath (&vifs)[1]
2061 #define vps1 (&vpath)[1]
2062 #define vps2 (&vps1)[1]
2063 #define vps4 (&vps2)[1]
2064 #if ENABLE_ASH_GETOPTS
2065 # define voptind (&vps4)[1]
2066 # if ENABLE_ASH_RANDOM_SUPPORT
2067 # define vrandom (&voptind)[1]
2070 # if ENABLE_ASH_RANDOM_SUPPORT
2071 # define vrandom (&vps4)[1]
2076 * The following macros access the values of the above variables.
2077 * They have to skip over the name. They return the null string
2078 * for unset variables.
2080 #define ifsval() (vifs.var_text + 4)
2081 #define ifsset() ((vifs.flags & VUNSET) == 0)
2083 # define mailval() (vmail.var_text + 5)
2084 # define mpathval() (vmpath.var_text + 9)
2085 # define mpathset() ((vmpath.flags & VUNSET) == 0)
2087 #define pathval() (vpath.var_text + 5)
2088 #define ps1val() (vps1.var_text + 4)
2089 #define ps2val() (vps2.var_text + 4)
2090 #define ps4val() (vps4.var_text + 4)
2091 #if ENABLE_ASH_GETOPTS
2092 # define optindval() (voptind.var_text + 7)
2095 #if ENABLE_ASH_GETOPTS
2096 static void FAST_FUNC
2097 getoptsreset(const char *value)
2099 shellparam.optind = number(value) ?: 1;
2100 shellparam.optoff = -1;
2105 * Compares two strings up to the first = or '\0'. The first
2106 * string must be terminated by '='; the second may be terminated by
2107 * either '=' or '\0'.
2110 varcmp(const char *p, const char *q)
2114 while ((c = *p) == (d = *q)) {
2115 if (c == '\0' || c == '=')
2129 * Find the appropriate entry in the hash table from the name.
2131 static struct var **
2132 hashvar(const char *p)
2136 hashval = ((unsigned char) *p) << 4;
2137 while (*p && *p != '=')
2138 hashval += (unsigned char) *p++;
2139 return &vartab[hashval % VTABSIZE];
2143 vpcmp(const void *a, const void *b)
2145 return varcmp(*(const char **)a, *(const char **)b);
2149 * This routine initializes the builtin variables.
2159 * PS1 depends on uid
2161 #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT
2162 vps1.var_text = "PS1=\\w \\$ ";
2165 vps1.var_text = "PS1=# ";
2168 end = vp + ARRAY_SIZE(varinit);
2170 vpp = hashvar(vp->var_text);
2173 } while (++vp < end);
2176 static struct var **
2177 findvar(struct var **vpp, const char *name)
2179 for (; *vpp; vpp = &(*vpp)->next) {
2180 if (varcmp((*vpp)->var_text, name) == 0) {
2188 * Find the value of a variable. Returns NULL if not set.
2190 static const char* FAST_FUNC
2191 lookupvar(const char *name)
2195 v = *findvar(hashvar(name), name);
2197 #if ENABLE_ASH_RANDOM_SUPPORT
2199 * Dynamic variables are implemented roughly the same way they are
2200 * in bash. Namely, they're "special" so long as they aren't unset.
2201 * As soon as they're unset, they're no longer dynamic, and dynamic
2202 * lookup will no longer happen at that point. -- PFM.
2204 if (v->flags & VDYNAMIC)
2207 if (!(v->flags & VUNSET))
2208 return var_end(v->var_text);
2213 #if ENABLE_UNICODE_SUPPORT
2215 reinit_unicode_for_ash(void)
2217 /* Unicode support should be activated even if LANG is set
2218 * _during_ shell execution, not only if it was set when
2219 * shell was started. Therefore, re-check LANG every time:
2221 if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
2222 || ENABLE_UNICODE_USING_LOCALE
2224 const char *s = lookupvar("LC_ALL");
2225 if (!s) s = lookupvar("LC_CTYPE");
2226 if (!s) s = lookupvar("LANG");
2231 # define reinit_unicode_for_ash() ((void)0)
2235 * Search the environment of a builtin command.
2237 static ALWAYS_INLINE const char *
2238 bltinlookup(const char *name)
2240 return lookupvar(name);
2244 * Same as setvar except that the variable and value are passed in
2245 * the first argument as name=value. Since the first argument will
2246 * be actually stored in the table, it should not be a string that
2248 * Called with interrupts off.
2251 setvareq(char *s, int flags)
2253 struct var *vp, **vpp;
2256 flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
2257 vpp = findvar(vpp, s);
2260 if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) {
2263 if (flags & VNOSAVE)
2267 ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n);
2273 if (vp->var_func && !(flags & VNOFUNC))
2274 vp->var_func(var_end(s));
2276 if (!(vp->flags & (VTEXTFIXED|VSTACK)))
2277 free((char*)vp->var_text);
2279 if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) {
2283 if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE)
2288 flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET);
2290 /* variable s is not found */
2293 if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
2295 vp = ckzalloc(sizeof(*vp));
2297 /*vp->func = NULL; - ckzalloc did it */
2300 if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
2310 * Set the value of a variable. The flags argument is ored with the
2311 * flags of the variable. If val is NULL, the variable is unset.
2314 setvar(const char *name, const char *val, int flags)
2323 q = endofname(name);
2324 p = strchrnul(q, '=');
2326 if (!namelen || p != q)
2327 ash_msg_and_raise_error("%.*s: bad variable name", namelen, name);
2332 vallen = strlen(val);
2336 nameeq = ckmalloc(namelen + vallen + 2);
2337 p = mempcpy(nameeq, name, namelen);
2340 p = mempcpy(p, val, vallen);
2343 vp = setvareq(nameeq, flags | VNOSAVE);
2349 static void FAST_FUNC
2350 setvar0(const char *name, const char *val)
2352 setvar(name, val, 0);
2356 * Unset the specified variable.
2359 unsetvar(const char *s)
2365 * Process a linked list of variable assignments.
2368 listsetvar(struct strlist *list_set_var, int flags)
2370 struct strlist *lp = list_set_var;
2376 setvareq(lp->text, flags);
2383 * Generate a list of variables satisfying the given conditions.
2386 listvars(int on, int off, char ***end)
2397 for (vp = *vpp; vp; vp = vp->next) {
2398 if ((vp->flags & mask) == on) {
2399 if (ep == stackstrend())
2400 ep = growstackstr();
2401 *ep++ = (char*)vp->var_text;
2404 } while (++vpp < vartab + VTABSIZE);
2405 if (ep == stackstrend())
2406 ep = growstackstr();
2410 return grabstackstr(ep);
2414 /* ============ Path search helper
2416 * The variable path (passed by reference) should be set to the start
2417 * of the path before the first call; path_advance will update
2418 * this value as it proceeds. Successive calls to path_advance will return
2419 * the possible path expansions in sequence. If an option (indicated by
2420 * a percent sign) appears in the path entry then the global variable
2421 * pathopt will be set to point to it; otherwise pathopt will be set to
2424 static const char *pathopt; /* set by path_advance */
2427 path_advance(const char **path, const char *name)
2437 for (p = start; *p && *p != ':' && *p != '%'; p++)
2439 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
2440 while (stackblocksize() < len)
2444 q = mempcpy(q, start, p - start);
2451 while (*p && *p != ':')
2458 return stalloc(len);
2462 /* ============ Prompt */
2464 static smallint doprompt; /* if set, prompt the user */
2465 static smallint needprompt; /* true if interactive and at start of line */
2467 #if ENABLE_FEATURE_EDITING
2468 static line_input_t *line_input_state;
2469 static const char *cmdedit_prompt;
2471 putprompt(const char *s)
2473 if (ENABLE_ASH_EXPAND_PRMT) {
2474 free((char*)cmdedit_prompt);
2475 cmdedit_prompt = ckstrdup(s);
2482 putprompt(const char *s)
2488 /* expandstr() needs parsing machinery, so it is far away ahead... */
2489 static const char *expandstr(const char *ps, int syntax_type);
2490 /* Values for syntax param */
2491 #define BASESYNTAX 0 /* not in quotes */
2492 #define DQSYNTAX 1 /* in double quotes */
2493 #define SQSYNTAX 2 /* in single quotes */
2494 #define ARISYNTAX 3 /* in arithmetic */
2495 #if ENABLE_ASH_EXPAND_PRMT
2496 # define PSSYNTAX 4 /* prompt. never passed to SIT() */
2498 /* PSSYNTAX expansion is identical to DQSYNTAX, except keeping '\$' as '\$' */
2501 * called by editline -- any expansions to the prompt should be added here.
2504 setprompt_if(smallint do_set, int whichprompt)
2507 IF_ASH_EXPAND_PRMT(struct stackmark smark;)
2514 switch (whichprompt) {
2524 #if ENABLE_ASH_EXPAND_PRMT
2525 pushstackmark(&smark, stackblocksize());
2526 putprompt(expandstr(prompt, PSSYNTAX));
2527 popstackmark(&smark);
2534 /* ============ The cd and pwd commands */
2536 #define CD_PHYSICAL 1
2546 while ((i = nextopt("LP")) != '\0') {
2548 flags ^= CD_PHYSICAL;
2557 * Update curdir (the name of the current directory) in response to a
2561 updatepwd(const char *dir)
2568 cdcomppath = sstrdup(dir);
2571 if (curdir == nullstr)
2573 new = stack_putstr(curdir, new);
2575 new = makestrspace(strlen(dir) + 2, new);
2576 lim = (char *)stackblock() + 1;
2580 if (new > lim && *lim == '/')
2585 if (dir[1] == '/' && dir[2] != '/') {
2591 p = strtok(cdcomppath, "/");
2595 if (p[1] == '.' && p[2] == '\0') {
2607 new = stack_putstr(p, new);
2610 p = strtok(NULL, "/");
2615 return stackblock();
2619 * Find out what the current directory is. If we already know the current
2620 * directory, this routine returns immediately.
2625 char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */
2626 return dir ? dir : nullstr;
2630 setpwd(const char *val, int setold)
2634 oldcur = dir = curdir;
2637 setvar("OLDPWD", oldcur, VEXPORT);
2640 if (physdir != nullstr) {
2641 if (physdir != oldcur)
2645 if (oldcur == val || !val) {
2651 dir = ckstrdup(val);
2652 if (oldcur != dir && oldcur != nullstr) {
2657 setvar("PWD", dir, VEXPORT);
2660 static void hashcd(void);
2663 * Actually do the chdir. We also call hashcd to let other routines
2664 * know that the current directory has changed.
2667 docd(const char *dest, int flags)
2669 const char *dir = NULL;
2672 TRACE(("docd(\"%s\", %d) called\n", dest, flags));
2675 if (!(flags & CD_PHYSICAL)) {
2676 dir = updatepwd(dest);
2690 static int FAST_FUNC
2691 cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
2703 dest = bltinlookup("HOME");
2704 else if (LONE_DASH(dest)) {
2705 dest = bltinlookup("OLDPWD");
2727 path = bltinlookup("CDPATH");
2730 p = path_advance(&path, dest);
2731 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
2735 if (!docd(p, flags))
2746 ash_msg_and_raise_error("can't cd to %s", dest);
2749 if (flags & CD_PRINT)
2750 out1fmt("%s\n", curdir);
2754 static int FAST_FUNC
2755 pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
2758 const char *dir = curdir;
2762 if (physdir == nullstr)
2766 out1fmt("%s\n", dir);
2771 /* ============ ... */
2774 #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024)
2776 /* Syntax classes */
2777 #define CWORD 0 /* character is nothing special */
2778 #define CNL 1 /* newline character */
2779 #define CBACK 2 /* a backslash character */
2780 #define CSQUOTE 3 /* single quote */
2781 #define CDQUOTE 4 /* double quote */
2782 #define CENDQUOTE 5 /* a terminating quote */
2783 #define CBQUOTE 6 /* backwards single quote */
2784 #define CVAR 7 /* a dollar sign */
2785 #define CENDVAR 8 /* a '}' character */
2786 #define CLP 9 /* a left paren in arithmetic */
2787 #define CRP 10 /* a right paren in arithmetic */
2788 #define CENDFILE 11 /* end of file */
2789 #define CCTL 12 /* like CWORD, except it must be escaped */
2790 #define CSPCL 13 /* these terminate a word */
2791 #define CIGN 14 /* character should be ignored */
2794 #if ENABLE_ASH_ALIAS
2798 #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE
2800 #if ENABLE_FEATURE_SH_MATH
2801 # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12))
2803 # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8))
2805 static const uint16_t S_I_T[] ALIGN2 = {
2806 #if ENABLE_ASH_ALIAS
2807 SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */
2809 SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */
2810 SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */
2811 SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */
2812 SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */
2813 SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */
2814 SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */
2815 SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */
2816 SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */
2817 SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */
2818 SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */
2819 SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */
2820 #if !USE_SIT_FUNCTION
2821 SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */
2822 SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */
2823 SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */
2827 /* Constants below must match table above */
2829 #if ENABLE_ASH_ALIAS
2830 CSPCL_CIGN_CIGN_CIGN , /* 0 */
2832 CSPCL_CWORD_CWORD_CWORD , /* 1 */
2833 CNL_CNL_CNL_CNL , /* 2 */
2834 CWORD_CCTL_CCTL_CWORD , /* 3 */
2835 CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */
2836 CVAR_CVAR_CWORD_CVAR , /* 5 */
2837 CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */
2838 CSPCL_CWORD_CWORD_CLP , /* 7 */
2839 CSPCL_CWORD_CWORD_CRP , /* 8 */
2840 CBACK_CBACK_CCTL_CBACK , /* 9 */
2841 CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */
2842 CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */
2843 CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */
2844 CWORD_CWORD_CWORD_CWORD , /* 13 */
2845 CCTL_CCTL_CCTL_CCTL , /* 14 */
2848 /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF,
2849 * caller must ensure proper cast on it if c is *char_ptr!
2851 #if USE_SIT_FUNCTION
2854 SIT(int c, int syntax)
2856 /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */
2857 static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~";
2859 * This causes '/' to be prepended with CTLESC in dquoted string,
2860 * making "./file"* treated incorrectly because we feed
2861 * ".\/file*" string to glob(), confusing it (see expandmeta func).
2862 * The "homegrown" glob implementation is okay with that,
2863 * but glibc one isn't. With '/' always treated as CWORD,
2866 # if ENABLE_ASH_ALIAS
2867 static const uint8_t syntax_index_table[] ALIGN1 = {
2868 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */
2869 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */
2870 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */
2874 static const uint8_t syntax_index_table[] ALIGN1 = {
2875 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */
2876 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */
2877 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */
2886 # if ENABLE_ASH_ALIAS
2892 /* Cast is purely for paranoia here,
2893 * just in case someone passed signed char to us */
2894 if ((unsigned char)c >= CTL_FIRST
2895 && (unsigned char)c <= CTL_LAST
2899 s = strchrnul(spec_symbls, c);
2902 indx = syntax_index_table[s - spec_symbls];
2904 return (S_I_T[indx] >> (syntax*4)) & 0xf;
2907 #else /* !USE_SIT_FUNCTION */
2909 static const uint8_t syntax_index_table[] ALIGN1 = {
2910 /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
2911 /* 0 */ CWORD_CWORD_CWORD_CWORD,
2912 /* 1 */ CWORD_CWORD_CWORD_CWORD,
2913 /* 2 */ CWORD_CWORD_CWORD_CWORD,
2914 /* 3 */ CWORD_CWORD_CWORD_CWORD,
2915 /* 4 */ CWORD_CWORD_CWORD_CWORD,
2916 /* 5 */ CWORD_CWORD_CWORD_CWORD,
2917 /* 6 */ CWORD_CWORD_CWORD_CWORD,
2918 /* 7 */ CWORD_CWORD_CWORD_CWORD,
2919 /* 8 */ CWORD_CWORD_CWORD_CWORD,
2920 /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD,
2921 /* 10 "\n" */ CNL_CNL_CNL_CNL,
2922 /* 11 */ CWORD_CWORD_CWORD_CWORD,
2923 /* 12 */ CWORD_CWORD_CWORD_CWORD,
2924 /* 13 */ CWORD_CWORD_CWORD_CWORD,
2925 /* 14 */ CWORD_CWORD_CWORD_CWORD,
2926 /* 15 */ CWORD_CWORD_CWORD_CWORD,
2927 /* 16 */ CWORD_CWORD_CWORD_CWORD,
2928 /* 17 */ CWORD_CWORD_CWORD_CWORD,
2929 /* 18 */ CWORD_CWORD_CWORD_CWORD,
2930 /* 19 */ CWORD_CWORD_CWORD_CWORD,
2931 /* 20 */ CWORD_CWORD_CWORD_CWORD,
2932 /* 21 */ CWORD_CWORD_CWORD_CWORD,
2933 /* 22 */ CWORD_CWORD_CWORD_CWORD,
2934 /* 23 */ CWORD_CWORD_CWORD_CWORD,
2935 /* 24 */ CWORD_CWORD_CWORD_CWORD,
2936 /* 25 */ CWORD_CWORD_CWORD_CWORD,
2937 /* 26 */ CWORD_CWORD_CWORD_CWORD,
2938 /* 27 */ CWORD_CWORD_CWORD_CWORD,
2939 /* 28 */ CWORD_CWORD_CWORD_CWORD,
2940 /* 29 */ CWORD_CWORD_CWORD_CWORD,
2941 /* 30 */ CWORD_CWORD_CWORD_CWORD,
2942 /* 31 */ CWORD_CWORD_CWORD_CWORD,
2943 /* 32 " " */ CSPCL_CWORD_CWORD_CWORD,
2944 /* 33 "!" */ CWORD_CCTL_CCTL_CWORD,
2945 /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD,
2946 /* 35 "#" */ CWORD_CWORD_CWORD_CWORD,
2947 /* 36 "$" */ CVAR_CVAR_CWORD_CVAR,
2948 /* 37 "%" */ CWORD_CWORD_CWORD_CWORD,
2949 /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD,
2950 /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD,
2951 /* 40 "(" */ CSPCL_CWORD_CWORD_CLP,
2952 /* 41 ")" */ CSPCL_CWORD_CWORD_CRP,
2953 /* 42 "*" */ CWORD_CCTL_CCTL_CWORD,
2954 /* 43 "+" */ CWORD_CWORD_CWORD_CWORD,
2955 /* 44 "," */ CWORD_CWORD_CWORD_CWORD,
2956 /* 45 "-" */ CWORD_CCTL_CCTL_CWORD,
2957 /* 46 "." */ CWORD_CWORD_CWORD_CWORD,
2958 /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */
2959 /* 47 "/" */ CWORD_CWORD_CWORD_CWORD,
2960 /* 48 "0" */ CWORD_CWORD_CWORD_CWORD,
2961 /* 49 "1" */ CWORD_CWORD_CWORD_CWORD,
2962 /* 50 "2" */ CWORD_CWORD_CWORD_CWORD,
2963 /* 51 "3" */ CWORD_CWORD_CWORD_CWORD,
2964 /* 52 "4" */ CWORD_CWORD_CWORD_CWORD,
2965 /* 53 "5" */ CWORD_CWORD_CWORD_CWORD,
2966 /* 54 "6" */ CWORD_CWORD_CWORD_CWORD,
2967 /* 55 "7" */ CWORD_CWORD_CWORD_CWORD,
2968 /* 56 "8" */ CWORD_CWORD_CWORD_CWORD,
2969 /* 57 "9" */ CWORD_CWORD_CWORD_CWORD,
2970 /* 58 ":" */ CWORD_CCTL_CCTL_CWORD,
2971 /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD,
2972 /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD,
2973 /* 61 "=" */ CWORD_CCTL_CCTL_CWORD,
2974 /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD,
2975 /* 63 "?" */ CWORD_CCTL_CCTL_CWORD,
2976 /* 64 "@" */ CWORD_CWORD_CWORD_CWORD,
2977 /* 65 "A" */ CWORD_CWORD_CWORD_CWORD,
2978 /* 66 "B" */ CWORD_CWORD_CWORD_CWORD,
2979 /* 67 "C" */ CWORD_CWORD_CWORD_CWORD,
2980 /* 68 "D" */ CWORD_CWORD_CWORD_CWORD,
2981 /* 69 "E" */ CWORD_CWORD_CWORD_CWORD,
2982 /* 70 "F" */ CWORD_CWORD_CWORD_CWORD,
2983 /* 71 "G" */ CWORD_CWORD_CWORD_CWORD,
2984 /* 72 "H" */ CWORD_CWORD_CWORD_CWORD,
2985 /* 73 "I" */ CWORD_CWORD_CWORD_CWORD,
2986 /* 74 "J" */ CWORD_CWORD_CWORD_CWORD,
2987 /* 75 "K" */ CWORD_CWORD_CWORD_CWORD,
2988 /* 76 "L" */ CWORD_CWORD_CWORD_CWORD,
2989 /* 77 "M" */ CWORD_CWORD_CWORD_CWORD,
2990 /* 78 "N" */ CWORD_CWORD_CWORD_CWORD,
2991 /* 79 "O" */ CWORD_CWORD_CWORD_CWORD,
2992 /* 80 "P" */ CWORD_CWORD_CWORD_CWORD,
2993 /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD,
2994 /* 82 "R" */ CWORD_CWORD_CWORD_CWORD,
2995 /* 83 "S" */ CWORD_CWORD_CWORD_CWORD,
2996 /* 84 "T" */ CWORD_CWORD_CWORD_CWORD,
2997 /* 85 "U" */ CWORD_CWORD_CWORD_CWORD,
2998 /* 86 "V" */ CWORD_CWORD_CWORD_CWORD,
2999 /* 87 "W" */ CWORD_CWORD_CWORD_CWORD,
3000 /* 88 "X" */ CWORD_CWORD_CWORD_CWORD,
3001 /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD,
3002 /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD,
3003 /* 91 "[" */ CWORD_CCTL_CCTL_CWORD,
3004 /* 92 "\" */ CBACK_CBACK_CCTL_CBACK,
3005 /* 93 "]" */ CWORD_CCTL_CCTL_CWORD,
3006 /* 94 "^" */ CWORD_CWORD_CWORD_CWORD,
3007 /* 95 "_" */ CWORD_CWORD_CWORD_CWORD,
3008 /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE,
3009 /* 97 "a" */ CWORD_CWORD_CWORD_CWORD,
3010 /* 98 "b" */ CWORD_CWORD_CWORD_CWORD,
3011 /* 99 "c" */ CWORD_CWORD_CWORD_CWORD,
3012 /* 100 "d" */ CWORD_CWORD_CWORD_CWORD,
3013 /* 101 "e" */ CWORD_CWORD_CWORD_CWORD,
3014 /* 102 "f" */ CWORD_CWORD_CWORD_CWORD,
3015 /* 103 "g" */ CWORD_CWORD_CWORD_CWORD,
3016 /* 104 "h" */ CWORD_CWORD_CWORD_CWORD,
3017 /* 105 "i" */ CWORD_CWORD_CWORD_CWORD,
3018 /* 106 "j" */ CWORD_CWORD_CWORD_CWORD,
3019 /* 107 "k" */ CWORD_CWORD_CWORD_CWORD,
3020 /* 108 "l" */ CWORD_CWORD_CWORD_CWORD,
3021 /* 109 "m" */ CWORD_CWORD_CWORD_CWORD,
3022 /* 110 "n" */ CWORD_CWORD_CWORD_CWORD,
3023 /* 111 "o" */ CWORD_CWORD_CWORD_CWORD,
3024 /* 112 "p" */ CWORD_CWORD_CWORD_CWORD,
3025 /* 113 "q" */ CWORD_CWORD_CWORD_CWORD,
3026 /* 114 "r" */ CWORD_CWORD_CWORD_CWORD,
3027 /* 115 "s" */ CWORD_CWORD_CWORD_CWORD,
3028 /* 116 "t" */ CWORD_CWORD_CWORD_CWORD,
3029 /* 117 "u" */ CWORD_CWORD_CWORD_CWORD,
3030 /* 118 "v" */ CWORD_CWORD_CWORD_CWORD,
3031 /* 119 "w" */ CWORD_CWORD_CWORD_CWORD,
3032 /* 120 "x" */ CWORD_CWORD_CWORD_CWORD,
3033 /* 121 "y" */ CWORD_CWORD_CWORD_CWORD,
3034 /* 122 "z" */ CWORD_CWORD_CWORD_CWORD,
3035 /* 123 "{" */ CWORD_CWORD_CWORD_CWORD,
3036 /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD,
3037 /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR,
3038 /* 126 "~" */ CWORD_CCTL_CCTL_CWORD,
3039 /* 127 del */ CWORD_CWORD_CWORD_CWORD,
3040 /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD,
3041 /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL,
3042 /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL,
3043 /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL,
3044 /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL,
3045 /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL,
3046 /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL,
3047 /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL,
3048 /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL,
3049 /* 137 */ CWORD_CWORD_CWORD_CWORD,
3050 /* 138 */ CWORD_CWORD_CWORD_CWORD,
3051 /* 139 */ CWORD_CWORD_CWORD_CWORD,
3052 /* 140 */ CWORD_CWORD_CWORD_CWORD,
3053 /* 141 */ CWORD_CWORD_CWORD_CWORD,
3054 /* 142 */ CWORD_CWORD_CWORD_CWORD,
3055 /* 143 */ CWORD_CWORD_CWORD_CWORD,
3056 /* 144 */ CWORD_CWORD_CWORD_CWORD,
3057 /* 145 */ CWORD_CWORD_CWORD_CWORD,
3058 /* 146 */ CWORD_CWORD_CWORD_CWORD,
3059 /* 147 */ CWORD_CWORD_CWORD_CWORD,
3060 /* 148 */ CWORD_CWORD_CWORD_CWORD,
3061 /* 149 */ CWORD_CWORD_CWORD_CWORD,
3062 /* 150 */ CWORD_CWORD_CWORD_CWORD,
3063 /* 151 */ CWORD_CWORD_CWORD_CWORD,
3064 /* 152 */ CWORD_CWORD_CWORD_CWORD,
3065 /* 153 */ CWORD_CWORD_CWORD_CWORD,
3066 /* 154 */ CWORD_CWORD_CWORD_CWORD,
3067 /* 155 */ CWORD_CWORD_CWORD_CWORD,
3068 /* 156 */ CWORD_CWORD_CWORD_CWORD,
3069 /* 157 */ CWORD_CWORD_CWORD_CWORD,
3070 /* 158 */ CWORD_CWORD_CWORD_CWORD,
3071 /* 159 */ CWORD_CWORD_CWORD_CWORD,
3072 /* 160 */ CWORD_CWORD_CWORD_CWORD,
3073 /* 161 */ CWORD_CWORD_CWORD_CWORD,
3074 /* 162 */ CWORD_CWORD_CWORD_CWORD,
3075 /* 163 */ CWORD_CWORD_CWORD_CWORD,
3076 /* 164 */ CWORD_CWORD_CWORD_CWORD,
3077 /* 165 */ CWORD_CWORD_CWORD_CWORD,
3078 /* 166 */ CWORD_CWORD_CWORD_CWORD,
3079 /* 167 */ CWORD_CWORD_CWORD_CWORD,
3080 /* 168 */ CWORD_CWORD_CWORD_CWORD,
3081 /* 169 */ CWORD_CWORD_CWORD_CWORD,
3082 /* 170 */ CWORD_CWORD_CWORD_CWORD,
3083 /* 171 */ CWORD_CWORD_CWORD_CWORD,
3084 /* 172 */ CWORD_CWORD_CWORD_CWORD,
3085 /* 173 */ CWORD_CWORD_CWORD_CWORD,
3086 /* 174 */ CWORD_CWORD_CWORD_CWORD,
3087 /* 175 */ CWORD_CWORD_CWORD_CWORD,
3088 /* 176 */ CWORD_CWORD_CWORD_CWORD,
3089 /* 177 */ CWORD_CWORD_CWORD_CWORD,
3090 /* 178 */ CWORD_CWORD_CWORD_CWORD,
3091 /* 179 */ CWORD_CWORD_CWORD_CWORD,
3092 /* 180 */ CWORD_CWORD_CWORD_CWORD,
3093 /* 181 */ CWORD_CWORD_CWORD_CWORD,
3094 /* 182 */ CWORD_CWORD_CWORD_CWORD,
3095 /* 183 */ CWORD_CWORD_CWORD_CWORD,
3096 /* 184 */ CWORD_CWORD_CWORD_CWORD,
3097 /* 185 */ CWORD_CWORD_CWORD_CWORD,
3098 /* 186 */ CWORD_CWORD_CWORD_CWORD,
3099 /* 187 */ CWORD_CWORD_CWORD_CWORD,
3100 /* 188 */ CWORD_CWORD_CWORD_CWORD,
3101 /* 189 */ CWORD_CWORD_CWORD_CWORD,
3102 /* 190 */ CWORD_CWORD_CWORD_CWORD,
3103 /* 191 */ CWORD_CWORD_CWORD_CWORD,
3104 /* 192 */ CWORD_CWORD_CWORD_CWORD,
3105 /* 193 */ CWORD_CWORD_CWORD_CWORD,
3106 /* 194 */ CWORD_CWORD_CWORD_CWORD,
3107 /* 195 */ CWORD_CWORD_CWORD_CWORD,
3108 /* 196 */ CWORD_CWORD_CWORD_CWORD,
3109 /* 197 */ CWORD_CWORD_CWORD_CWORD,
3110 /* 198 */ CWORD_CWORD_CWORD_CWORD,
3111 /* 199 */ CWORD_CWORD_CWORD_CWORD,
3112 /* 200 */ CWORD_CWORD_CWORD_CWORD,
3113 /* 201 */ CWORD_CWORD_CWORD_CWORD,
3114 /* 202 */ CWORD_CWORD_CWORD_CWORD,
3115 /* 203 */ CWORD_CWORD_CWORD_CWORD,
3116 /* 204 */ CWORD_CWORD_CWORD_CWORD,
3117 /* 205 */ CWORD_CWORD_CWORD_CWORD,
3118 /* 206 */ CWORD_CWORD_CWORD_CWORD,
3119 /* 207 */ CWORD_CWORD_CWORD_CWORD,
3120 /* 208 */ CWORD_CWORD_CWORD_CWORD,
3121 /* 209 */ CWORD_CWORD_CWORD_CWORD,
3122 /* 210 */ CWORD_CWORD_CWORD_CWORD,
3123 /* 211 */ CWORD_CWORD_CWORD_CWORD,
3124 /* 212 */ CWORD_CWORD_CWORD_CWORD,
3125 /* 213 */ CWORD_CWORD_CWORD_CWORD,
3126 /* 214 */ CWORD_CWORD_CWORD_CWORD,
3127 /* 215 */ CWORD_CWORD_CWORD_CWORD,
3128 /* 216 */ CWORD_CWORD_CWORD_CWORD,
3129 /* 217 */ CWORD_CWORD_CWORD_CWORD,
3130 /* 218 */ CWORD_CWORD_CWORD_CWORD,
3131 /* 219 */ CWORD_CWORD_CWORD_CWORD,
3132 /* 220 */ CWORD_CWORD_CWORD_CWORD,
3133 /* 221 */ CWORD_CWORD_CWORD_CWORD,
3134 /* 222 */ CWORD_CWORD_CWORD_CWORD,
3135 /* 223 */ CWORD_CWORD_CWORD_CWORD,
3136 /* 224 */ CWORD_CWORD_CWORD_CWORD,
3137 /* 225 */ CWORD_CWORD_CWORD_CWORD,
3138 /* 226 */ CWORD_CWORD_CWORD_CWORD,
3139 /* 227 */ CWORD_CWORD_CWORD_CWORD,
3140 /* 228 */ CWORD_CWORD_CWORD_CWORD,
3141 /* 229 */ CWORD_CWORD_CWORD_CWORD,
3142 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3143 /* 231 */ CWORD_CWORD_CWORD_CWORD,
3144 /* 232 */ CWORD_CWORD_CWORD_CWORD,
3145 /* 233 */ CWORD_CWORD_CWORD_CWORD,
3146 /* 234 */ CWORD_CWORD_CWORD_CWORD,
3147 /* 235 */ CWORD_CWORD_CWORD_CWORD,
3148 /* 236 */ CWORD_CWORD_CWORD_CWORD,
3149 /* 237 */ CWORD_CWORD_CWORD_CWORD,
3150 /* 238 */ CWORD_CWORD_CWORD_CWORD,
3151 /* 239 */ CWORD_CWORD_CWORD_CWORD,
3152 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3153 /* 241 */ CWORD_CWORD_CWORD_CWORD,
3154 /* 242 */ CWORD_CWORD_CWORD_CWORD,
3155 /* 243 */ CWORD_CWORD_CWORD_CWORD,
3156 /* 244 */ CWORD_CWORD_CWORD_CWORD,
3157 /* 245 */ CWORD_CWORD_CWORD_CWORD,
3158 /* 246 */ CWORD_CWORD_CWORD_CWORD,
3159 /* 247 */ CWORD_CWORD_CWORD_CWORD,
3160 /* 248 */ CWORD_CWORD_CWORD_CWORD,
3161 /* 249 */ CWORD_CWORD_CWORD_CWORD,
3162 /* 250 */ CWORD_CWORD_CWORD_CWORD,
3163 /* 251 */ CWORD_CWORD_CWORD_CWORD,
3164 /* 252 */ CWORD_CWORD_CWORD_CWORD,
3165 /* 253 */ CWORD_CWORD_CWORD_CWORD,
3166 /* 254 */ CWORD_CWORD_CWORD_CWORD,
3167 /* 255 */ CWORD_CWORD_CWORD_CWORD,
3168 /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
3169 # if ENABLE_ASH_ALIAS
3170 /* PEOA */ CSPCL_CIGN_CIGN_CIGN,
3175 # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf)
3176 #else /* debug version, caught one signed char bug */
3177 # define SIT(c, syntax) \
3179 if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \
3180 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3181 if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \
3182 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3183 ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \
3187 #endif /* !USE_SIT_FUNCTION */
3190 /* ============ Alias handling */
3192 #if ENABLE_ASH_ALIAS
3194 #define ALIASINUSE 1
3205 static struct alias **atab; // [ATABSIZE];
3206 #define INIT_G_alias() do { \
3207 atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
3211 static struct alias **
3212 __lookupalias(const char *name)
3214 unsigned int hashval;
3221 ch = (unsigned char)*p;
3225 ch = (unsigned char)*++p;
3227 app = &atab[hashval % ATABSIZE];
3229 for (; *app; app = &(*app)->next) {
3230 if (strcmp(name, (*app)->name) == 0) {
3238 static struct alias *
3239 lookupalias(const char *name, int check)
3241 struct alias *ap = *__lookupalias(name);
3243 if (check && ap && (ap->flag & ALIASINUSE))
3248 static struct alias *
3249 freealias(struct alias *ap)
3253 if (ap->flag & ALIASINUSE) {
3254 ap->flag |= ALIASDEAD;
3266 setalias(const char *name, const char *val)
3268 struct alias *ap, **app;
3270 app = __lookupalias(name);
3274 if (!(ap->flag & ALIASINUSE)) {
3277 ap->val = ckstrdup(val);
3278 ap->flag &= ~ALIASDEAD;
3281 ap = ckzalloc(sizeof(struct alias));
3282 ap->name = ckstrdup(name);
3283 ap->val = ckstrdup(val);
3284 /*ap->flag = 0; - ckzalloc did it */
3285 /*ap->next = NULL;*/
3292 unalias(const char *name)
3296 app = __lookupalias(name);
3300 *app = freealias(*app);
3311 struct alias *ap, **app;
3315 for (i = 0; i < ATABSIZE; i++) {
3317 for (ap = *app; ap; ap = *app) {
3318 *app = freealias(*app);
3328 printalias(const struct alias *ap)
3330 out1fmt("%s=%s\n", ap->name, single_quote(ap->val));
3334 * TODO - sort output
3336 static int FAST_FUNC
3337 aliascmd(int argc UNUSED_PARAM, char **argv)
3346 for (i = 0; i < ATABSIZE; i++) {
3347 for (ap = atab[i]; ap; ap = ap->next) {
3353 while ((n = *++argv) != NULL) {
3354 v = strchr(n+1, '=');
3355 if (v == NULL) { /* n+1: funny ksh stuff */
3356 ap = *__lookupalias(n);
3358 fprintf(stderr, "%s: %s not found\n", "alias", n);
3371 static int FAST_FUNC
3372 unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
3376 while (nextopt("a") != '\0') {
3380 for (i = 0; *argptr; argptr++) {
3381 if (unalias(*argptr)) {
3382 fprintf(stderr, "%s: %s not found\n", "unalias", *argptr);
3390 #endif /* ASH_ALIAS */
3393 /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */
3396 #define FORK_NOJOB 2
3398 /* mode flags for showjob(s) */
3399 #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */
3400 #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */
3401 #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */
3402 #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */
3405 * A job structure contains information about a job. A job is either a
3406 * single process or a set of processes contained in a pipeline. In the
3407 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
3411 pid_t ps_pid; /* process id */
3412 int ps_status; /* last process status from wait() */
3413 char *ps_cmd; /* text of command being run */
3417 struct procstat ps0; /* status of process */
3418 struct procstat *ps; /* status or processes when more than one */
3420 int stopstatus; /* status of a stopped job */
3422 unsigned nprocs; /* number of processes */
3424 #define JOBRUNNING 0 /* at least one proc running */
3425 #define JOBSTOPPED 1 /* all procs are stopped */
3426 #define JOBDONE 2 /* all procs are completed */
3430 sigint: 1, /* job was killed by SIGINT */
3431 jobctl: 1, /* job running under job control */
3433 waited: 1, /* true if this entry has been waited for */
3434 used: 1, /* true if this entry is in used */
3435 changed: 1; /* true if status has changed */
3436 struct job *prev_job; /* previous job */
3439 static struct job *makejob(/*union node *,*/ int);
3440 static int forkshell(struct job *, union node *, int);
3441 static int waitforjob(struct job *);
3444 enum { doing_jobctl = 0 };
3445 #define setjobctl(on) do {} while (0)
3447 static smallint doing_jobctl; //references:8
3448 static void setjobctl(int);
3455 ignoresig(int signo)
3457 /* Avoid unnecessary system calls. Is it already SIG_IGNed? */
3458 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
3459 /* No, need to do it */
3460 signal(signo, SIG_IGN);
3462 sigmode[signo - 1] = S_HARD_IGN;
3466 * Only one usage site - in setsignal()
3469 signal_handler(int signo)
3471 if (signo == SIGCHLD) {
3477 gotsig[signo - 1] = 1;
3478 pending_sig = signo;
3480 if (signo == SIGINT && !trap[SIGINT]) {
3481 if (!suppress_int) {
3483 raise_interrupt(); /* does not return */
3490 * Set the signal handler for the specified signal. The routine figures
3491 * out what it should be set to.
3494 setsignal(int signo)
3497 char cur_act, new_act;
3498 struct sigaction act;
3502 if (t != NULL) { /* trap for this sig is set */
3504 if (t[0] == '\0') /* trap is "": ignore this sig */
3508 if (rootshell && new_act == S_DFL) {
3511 if (iflag || minusc || sflag == 0)
3520 * "In all cases, bash ignores SIGQUIT. Non-builtin
3521 * commands run by bash have signal handlers
3522 * set to the values inherited by the shell
3523 * from its parent". */
3539 //TODO: if !rootshell, we reset SIGQUIT to DFL,
3540 //whereas we have to restore it to what shell got on entry
3541 //from the parent. See comment above
3543 if (signo == SIGCHLD)
3546 t = &sigmode[signo - 1];
3549 /* current setting is not yet known */
3550 if (sigaction(signo, NULL, &act)) {
3551 /* pretend it worked; maybe we should give a warning,
3552 * but other shells don't. We don't alter sigmode,
3553 * so we retry every time.
3554 * btw, in Linux it never fails. --vda */
3557 if (act.sa_handler == SIG_IGN) {
3558 cur_act = S_HARD_IGN;
3560 && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)
3562 cur_act = S_IGN; /* don't hard ignore these */
3566 if (cur_act == S_HARD_IGN || cur_act == new_act)
3569 act.sa_handler = SIG_DFL;
3572 act.sa_handler = signal_handler;
3575 act.sa_handler = SIG_IGN;
3579 /* flags and mask matter only if !DFL and !IGN, but we do it
3580 * for all cases for more deterministic behavior:
3583 sigfillset(&act.sa_mask);
3585 sigaction_set(signo, &act);
3590 /* mode flags for set_curjob */
3591 #define CUR_DELETE 2
3592 #define CUR_RUNNING 1
3593 #define CUR_STOPPED 0
3596 /* pgrp of shell on invocation */
3597 static int initialpgrp; //references:2
3598 static int ttyfd = -1; //5
3601 static struct job *jobtab; //5
3603 static unsigned njobs; //4
3605 static struct job *curjob; //lots
3606 /* number of presumed living untracked jobs */
3607 static int jobless; //4
3610 /* Bash has a feature: it restores termios after a successful wait for
3611 * a foreground job which had at least one stopped or sigkilled member.
3612 * The probable rationale is that SIGSTOP and SIGKILL can preclude task from
3613 * properly restoring tty state. Should we do this too?
3614 * A reproducer: ^Z an interactive python:
3617 * Python 2.7.12 (...)
3619 * { python leaves tty in -icanon -echo state. We do survive that... }
3620 * [1]+ Stopped python
3621 * { ...however, next program (python #2) does not survive it well: }
3623 * Python 2.7.12 (...)
3624 * >>> Traceback (most recent call last):
3625 * { above, I typed "qwerty<CR>", but -echo state is still in effect }
3626 * File "<stdin>", line 1, in <module>
3627 * NameError: name 'qwerty' is not defined
3629 * The implementation below is modeled on bash code and seems to work.
3630 * However, I'm not sure we should do this. For one: what if I'd fg
3631 * the stopped python instead? It'll be confused by "restored" tty state.
3633 static struct termios shell_tty_info;
3637 if (rootshell && ttyfd >= 0)
3638 tcgetattr(ttyfd, &shell_tty_info);
3643 /* if (rootshell) - caller ensures this */
3645 tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info);
3648 job_signal_status(struct job *jp)
3652 struct procstat *ps = jp->ps;
3653 for (i = 0; i < jp->nprocs; i++) {
3654 status = ps[i].ps_status;
3655 if (WIFSIGNALED(status) || WIFSTOPPED(status))
3661 restore_tty_if_stopped_or_signaled(struct job *jp)
3663 //TODO: check what happens if we come from waitforjob() in expbackq()
3665 int s = job_signal_status(jp);
3666 if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */
3671 # define get_tty_state() ((void)0)
3672 # define restore_tty_if_stopped_or_signaled(jp) ((void)0)
3676 set_curjob(struct job *jp, unsigned mode)
3679 struct job **jpp, **curp;
3681 /* first remove from list */
3682 jpp = curp = &curjob;
3687 jpp = &jp1->prev_job;
3689 *jpp = jp1->prev_job;
3691 /* Then re-insert in correct position */
3699 /* job being deleted */
3702 /* newly created job or backgrounded job,
3703 * put after all stopped jobs.
3708 if (!jp1 || jp1->state != JOBSTOPPED)
3711 jpp = &jp1->prev_job;
3717 /* newly stopped job - becomes curjob */
3718 jp->prev_job = *jpp;
3726 jobno(const struct job *jp)
3728 return jp - jobtab + 1;
3733 * Convert a job name to a job structure.
3736 #define getjob(name, getctl) getjob(name)
3739 getjob(const char *name, int getctl)
3743 const char *err_msg = "%s: no such job";
3747 char *(*match)(const char *, const char *);
3762 if (c == '+' || c == '%') {
3764 err_msg = "No current job";
3770 err_msg = "No previous job";
3780 if (num > 0 && num <= njobs) {
3781 jp = jobtab + num - 1;
3796 if (match(jp->ps[0].ps_cmd, p)) {
3800 err_msg = "%s: ambiguous";
3810 err_msg = "job %s not created under job control";
3811 if (getctl && jp->jobctl == 0)
3816 ash_msg_and_raise_error(err_msg, name);
3820 * Mark a job structure as unused.
3823 freejob(struct job *jp)
3825 struct procstat *ps;
3829 for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
3830 if (ps->ps_cmd != nullstr)
3833 if (jp->ps != &jp->ps0)
3836 set_curjob(jp, CUR_DELETE);
3842 xtcsetpgrp(int fd, pid_t pgrp)
3844 if (tcsetpgrp(fd, pgrp))
3845 ash_msg_and_raise_perror("can't set tty process group");
3849 * Turn job control on and off.
3851 * Note: This code assumes that the third arg to ioctl is a character
3852 * pointer, which is true on Berkeley systems but not System V. Since
3853 * System V doesn't have job control yet, this isn't a problem now.
3855 * Called with interrupts off.
3863 if (on == doing_jobctl || rootshell == 0)
3867 ofd = fd = open(_PATH_TTY, O_RDWR);
3869 /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails.
3870 * That sometimes helps to acquire controlling tty.
3871 * Obviously, a workaround for bugs when someone
3872 * failed to provide a controlling tty to bash! :) */
3878 /* fd is a tty at this point */
3879 fd = fcntl(fd, F_DUPFD, 10);
3880 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */
3883 goto out; /* F_DUPFD failed */
3884 close_on_exec_on(fd);
3885 while (1) { /* while we are in the background */
3886 pgrp = tcgetpgrp(fd);
3889 ash_msg("can't access tty; job control turned off");
3893 if (pgrp == getpgrp())
3904 xtcsetpgrp(fd, pgrp);
3906 /* turning job control off */
3909 /* was xtcsetpgrp, but this can make exiting ash
3910 * loop forever if pty is already deleted */
3911 tcsetpgrp(fd, pgrp);
3925 static int FAST_FUNC
3926 killcmd(int argc, char **argv)
3928 if (argv[1] && strcmp(argv[1], "-l") != 0) {
3931 if (argv[i][0] == '%') {
3933 * "kill %N" - job kill
3934 * Converting to pgrp / pid kill
3940 jp = getjob(argv[i], 0);
3942 * In jobs started under job control, we signal
3943 * entire process group by kill -PGRP_ID.
3944 * This happens, f.e., in interactive shell.
3946 * Otherwise, we signal each child via
3947 * kill PID1 PID2 PID3.
3949 * sh -c 'sleep 1|sleep 1 & kill %1'
3950 * sh -c 'true|sleep 2 & sleep 1; kill %1'
3951 * sh -c 'true|sleep 1 & sleep 2; kill %1'
3953 n = jp->nprocs; /* can't be 0 (I hope) */
3956 dst = alloca(n * sizeof(int)*4);
3958 for (j = 0; j < n; j++) {
3959 struct procstat *ps = &jp->ps[j];
3960 /* Skip non-running and not-stopped members
3961 * (i.e. dead members) of the job
3963 if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status))
3966 * kill_main has matching code to expect
3967 * leading space. Needed to not confuse
3968 * negative pids with "kill -SIGNAL_NO" syntax
3970 dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid);
3974 } while (argv[++i]);
3976 return kill_main(argc, argv);
3980 showpipe(struct job *jp /*, FILE *out*/)
3982 struct procstat *ps;
3983 struct procstat *psend;
3985 psend = jp->ps + jp->nprocs;
3986 for (ps = jp->ps + 1; ps < psend; ps++)
3987 printf(" | %s", ps->ps_cmd);
3988 newline_and_flush(stdout);
3989 flush_stdout_stderr();
3994 restartjob(struct job *jp, int mode)
3996 struct procstat *ps;
4002 if (jp->state == JOBDONE)
4004 jp->state = JOBRUNNING;
4005 pgid = jp->ps[0].ps_pid;
4006 if (mode == FORK_FG) {
4008 xtcsetpgrp(ttyfd, pgid);
4010 killpg(pgid, SIGCONT);
4014 if (WIFSTOPPED(ps->ps_status)) {
4020 status = (mode == FORK_FG) ? waitforjob(jp) : 0;
4025 static int FAST_FUNC
4026 fg_bgcmd(int argc UNUSED_PARAM, char **argv)
4032 mode = (**argv == 'f') ? FORK_FG : FORK_BG;
4036 jp = getjob(*argv, 1);
4037 if (mode == FORK_BG) {
4038 set_curjob(jp, CUR_RUNNING);
4039 printf("[%d] ", jobno(jp));
4041 out1str(jp->ps[0].ps_cmd);
4042 showpipe(jp /*, stdout*/);
4043 retval = restartjob(jp, mode);
4044 } while (*argv && *++argv);
4050 sprint_status48(char *s, int status, int sigonly)
4056 if (!WIFEXITED(status)) {
4058 if (WIFSTOPPED(status))
4059 st = WSTOPSIG(status);
4062 st = WTERMSIG(status);
4064 if (st == SIGINT || st == SIGPIPE)
4067 if (WIFSTOPPED(status))
4072 //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata
4073 col = fmtstr(s, 32, strsignal(st));
4074 if (WCOREDUMP(status)) {
4075 strcpy(s + col, " (core dumped)");
4076 col += sizeof(" (core dumped)")-1;
4078 } else if (!sigonly) {
4079 st = WEXITSTATUS(status);
4080 col = fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st);
4087 wait_block_or_sig(int *status)
4094 /* Poll all children for changes in their state */
4096 /* if job control is active, accept stopped processes too */
4097 pid = waitpid(-1, status, doing_jobctl ? (WNOHANG|WUNTRACED) : WNOHANG);
4099 break; /* Error (e.g. EINTR, ECHILD) or pid */
4101 /* Children exist, but none are ready. Sleep until interesting signal */
4104 sigprocmask(SIG_SETMASK, &mask, &mask);
4105 while (!got_sigchld && !pending_sig)
4107 sigprocmask(SIG_SETMASK, &mask, NULL);
4108 #else /* unsafe: a signal can set pending_sig after check, but before pause() */
4109 while (!got_sigchld && !pending_sig)
4113 /* If it was SIGCHLD, poll children again */
4114 } while (got_sigchld);
4119 #define DOWAIT_NONBLOCK 0
4120 #define DOWAIT_BLOCK 1
4121 #define DOWAIT_BLOCK_OR_SIG 2
4124 dowait(int block, struct job *job)
4129 struct job *thisjob = NULL;
4131 TRACE(("dowait(0x%x) called\n", block));
4133 /* It's wrong to call waitpid() outside of INT_OFF region:
4134 * signal can arrive just after syscall return and handler can
4135 * longjmp away, losing stop/exit notification processing.
4136 * Thus, for "jobs" builtin, and for waiting for a fg job,
4137 * we call waitpid() (blocking or non-blocking) inside INT_OFF.
4139 * However, for "wait" builtin it is wrong to simply call waitpid()
4140 * in INT_OFF region: "wait" needs to wait for any running job
4141 * to change state, but should exit on any trap too.
4142 * In INT_OFF region, a signal just before syscall entry can set
4143 * pending_sig variables, but we can't check them, and we would
4144 * either enter a sleeping waitpid() (BUG), or need to busy-loop.
4146 * Because of this, we run inside INT_OFF, but use a special routine
4147 * which combines waitpid() and sigsuspend().
4148 * This is the reason why we need to have a handler for SIGCHLD:
4149 * SIG_DFL handler does not wake sigsuspend().
4152 if (block == DOWAIT_BLOCK_OR_SIG) {
4153 pid = wait_block_or_sig(&status);
4156 if (block == DOWAIT_NONBLOCK)
4157 wait_flags = WNOHANG;
4158 /* if job control is active, accept stopped processes too */
4160 wait_flags |= WUNTRACED;
4161 /* NB: _not_ safe_waitpid, we need to detect EINTR */
4162 pid = waitpid(-1, &status, wait_flags);
4164 TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
4165 pid, status, errno, strerror(errno)));
4170 for (jp = curjob; jp; jp = jp->prev_job) {
4172 struct procstat *ps;
4173 struct procstat *psend;
4174 if (jp->state == JOBDONE)
4178 psend = ps + jp->nprocs;
4180 if (ps->ps_pid == pid) {
4181 TRACE(("Job %d: changing status of proc %d "
4182 "from 0x%x to 0x%x\n",
4183 jobno(jp), pid, ps->ps_status, status));
4184 ps->ps_status = status;
4187 if (ps->ps_status == -1)
4188 jobstate = JOBRUNNING;
4190 if (jobstate == JOBRUNNING)
4192 if (WIFSTOPPED(ps->ps_status)) {
4193 jp->stopstatus = ps->ps_status;
4194 jobstate = JOBSTOPPED;
4197 } while (++ps < psend);
4201 /* Found the job where one of its processes changed its state.
4202 * Is there at least one live and running process in this job? */
4203 if (jobstate != JOBRUNNING) {
4204 /* No. All live processes in the job are stopped
4205 * (JOBSTOPPED) or there are no live processes (JOBDONE)
4207 thisjob->changed = 1;
4208 if (thisjob->state != jobstate) {
4209 TRACE(("Job %d: changing state from %d to %d\n",
4210 jobno(thisjob), thisjob->state, jobstate));
4211 thisjob->state = jobstate;
4213 if (jobstate == JOBSTOPPED)
4214 set_curjob(thisjob, CUR_STOPPED);
4220 /* The process wasn't found in job list */
4222 if (!WIFSTOPPED(status))
4228 if (thisjob && thisjob == job) {
4232 len = sprint_status48(s, status, 1);
4244 showjob(struct job *jp, int mode)
4246 struct procstat *ps;
4247 struct procstat *psend;
4250 char s[16 + 16 + 48];
4251 FILE *out = (mode & SHOW_STDERR ? stderr : stdout);
4255 if (mode & SHOW_ONLY_PGID) { /* jobs -p */
4256 /* just output process (group) id of pipeline */
4257 fprintf(out, "%d\n", ps->ps_pid);
4261 col = fmtstr(s, 16, "[%d] ", jobno(jp));
4266 else if (curjob && jp == curjob->prev_job)
4269 if (mode & SHOW_PIDS)
4270 col += fmtstr(s + col, 16, "%d ", ps->ps_pid);
4272 psend = ps + jp->nprocs;
4274 if (jp->state == JOBRUNNING) {
4275 strcpy(s + col, "Running");
4276 col += sizeof("Running") - 1;
4278 int status = psend[-1].ps_status;
4279 if (jp->state == JOBSTOPPED)
4280 status = jp->stopstatus;
4281 col += sprint_status48(s + col, status, 0);
4283 /* By now, "[JOBID]* [maybe PID] STATUS" is printed */
4285 /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line
4286 * or prints several "PID | <cmdN>" lines,
4287 * depending on SHOW_PIDS bit.
4288 * We do not print status of individual processes
4289 * between PID and <cmdN>. bash does it, but not very well:
4290 * first line shows overall job status, not process status,
4291 * making it impossible to know 1st process status.
4295 /* for each process */
4298 if (mode & SHOW_PIDS)
4299 col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1;
4301 fprintf(out, "%s%*c%s%s",
4303 33 - col >= 0 ? 33 - col : 0, ' ',
4304 ps == jp->ps ? "" : "| ",
4307 } while (++ps != psend);
4308 newline_and_flush(out);
4312 if (jp->state == JOBDONE) {
4313 TRACE(("showjob: freeing job %d\n", jobno(jp)));
4319 * Print a list of jobs. If "change" is nonzero, only print jobs whose
4320 * statuses have changed since the last call to showjobs.
4327 TRACE(("showjobs(0x%x) called\n", mode));
4329 /* Handle all finished jobs */
4330 while (dowait(DOWAIT_NONBLOCK, NULL) > 0)
4333 for (jp = curjob; jp; jp = jp->prev_job) {
4334 if (!(mode & SHOW_CHANGED) || jp->changed) {
4340 static int FAST_FUNC
4341 jobscmd(int argc UNUSED_PARAM, char **argv)
4346 while ((m = nextopt("lp")) != '\0') {
4350 mode |= SHOW_ONLY_PGID;
4356 showjob(getjob(*argv, 0), mode);
4366 /* Called only on finished or stopped jobs (no members are running) */
4368 getstatus(struct job *job)
4372 struct procstat *ps;
4374 /* Fetch last member's status */
4375 ps = job->ps + job->nprocs - 1;
4376 status = ps->ps_status;
4378 /* "set -o pipefail" mode: use last _nonzero_ status */
4379 while (status == 0 && --ps >= job->ps)
4380 status = ps->ps_status;
4383 retval = WEXITSTATUS(status);
4384 if (!WIFEXITED(status)) {
4386 retval = WSTOPSIG(status);
4387 if (!WIFSTOPPED(status))
4390 /* XXX: limits number of signals */
4391 retval = WTERMSIG(status);
4393 if (retval == SIGINT)
4399 TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n",
4400 jobno(job), job->nprocs, status, retval));
4404 static int FAST_FUNC
4405 waitcmd(int argc UNUSED_PARAM, char **argv)
4416 /* wait for all jobs */
4420 if (!jp) /* no running procs */
4422 if (jp->state == JOBRUNNING)
4428 * "When bash is waiting for an asynchronous command via
4429 * the wait builtin, the reception of a signal for which a trap
4430 * has been set will cause the wait builtin to return immediately
4431 * with an exit status greater than 128, immediately after which
4432 * the trap is executed."
4434 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4435 /* if child sends us a signal *and immediately exits*,
4436 * dowait() returns pid > 0. Check this case,
4437 * not "if (dowait() < 0)"!
4446 if (**argv != '%') {
4447 pid_t pid = number(*argv);
4452 if (job->ps[job->nprocs - 1].ps_pid == pid)
4454 job = job->prev_job;
4457 job = getjob(*argv, 0);
4459 /* loop until process terminated or stopped */
4460 while (job->state == JOBRUNNING) {
4461 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4466 retval = getstatus(job);
4473 retval = 128 + pending_sig;
4482 struct job *jp, *jq;
4484 len = njobs * sizeof(*jp);
4486 jp = ckrealloc(jq, len + 4 * sizeof(*jp));
4488 offset = (char *)jp - (char *)jq;
4490 /* Relocate pointers */
4493 jq = (struct job *)((char *)jq + l);
4497 #define joff(p) ((struct job *)((char *)(p) + l))
4498 #define jmove(p) (p) = (void *)((char *)(p) + offset)
4499 if (joff(jp)->ps == &jq->ps0)
4500 jmove(joff(jp)->ps);
4501 if (joff(jp)->prev_job)
4502 jmove(joff(jp)->prev_job);
4512 jp = (struct job *)((char *)jp + len);
4516 } while (--jq >= jp);
4521 * Return a new job structure.
4522 * Called with interrupts off.
4525 makejob(/*union node *node,*/ int nprocs)
4530 for (i = njobs, jp = jobtab; ; jp++) {
4537 if (jp->state != JOBDONE || !jp->waited)
4546 memset(jp, 0, sizeof(*jp));
4548 /* jp->jobctl is a bitfield.
4549 * "jp->jobctl |= doing_jobctl" likely to give awful code */
4553 jp->prev_job = curjob;
4558 jp->ps = ckmalloc(nprocs * sizeof(struct procstat));
4560 TRACE(("makejob(%d) returns %%%d\n", nprocs,
4567 * Return a string identifying a command (to be printed by the
4570 static char *cmdnextc;
4573 cmdputs(const char *s)
4575 static const char vstype[VSTYPE + 1][3] = {
4576 "", "}", "-", "+", "?", "=",
4577 "%", "%%", "#", "##"
4578 IF_BASH_SUBSTR(, ":")
4579 IF_BASH_PATTERN_SUBST(, "/", "//")
4582 const char *p, *str;
4586 unsigned char subtype = 0;
4590 nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
4592 while ((c = *p++) != '\0') {
4600 if ((subtype & VSTYPE) == VSLENGTH)
4606 str = "\"}" + !(quoted & 1);
4613 #if ENABLE_FEATURE_SH_MATH
4628 if ((subtype & VSTYPE) != VSNORMAL)
4630 str = vstype[subtype & VSTYPE];
4631 if (subtype & VSNUL)
4640 /* These can only happen inside quotes */
4646 // [1]+ Done true ${\$} <<=== BUG: ${\$} is not a valid way to write $$ (${$} would be ok)
4657 while ((c = *str++) != '\0') {
4660 } /* while *p++ not NUL */
4663 USTPUTC('"', nextc);
4669 /* cmdtxt() and cmdlist() call each other */
4670 static void cmdtxt(union node *n);
4673 cmdlist(union node *np, int sep)
4675 for (; np; np = np->narg.next) {
4679 if (sep && np->narg.next)
4685 cmdtxt(union node *n)
4688 struct nodelist *lp;
4699 lp = n->npipe.cmdlist;
4717 cmdtxt(n->nbinary.ch1);
4733 cmdtxt(n->nif.test);
4735 if (n->nif.elsepart) {
4736 cmdtxt(n->nif.ifpart);
4738 n = n->nif.elsepart;
4756 cmdtxt(n->nbinary.ch1);
4766 cmdputs(n->nfor.var);
4768 cmdlist(n->nfor.args, 1);
4773 cmdputs(n->narg.text);
4777 cmdlist(n->ncmd.args, 1);
4778 cmdlist(n->ncmd.redirect, 0);
4791 cmdputs(n->ncase.expr->narg.text);
4793 for (np = n->ncase.cases; np; np = np->nclist.next) {
4794 cmdtxt(np->nclist.pattern);
4796 cmdtxt(np->nclist.body);
4810 #if BASH_REDIR_OUTPUT
4825 cmdputs(utoa(n->nfile.fd));
4827 if (n->type == NTOFD || n->type == NFROMFD) {
4828 if (n->ndup.dupfd >= 0)
4829 cmdputs(utoa(n->ndup.dupfd));
4840 commandtext(union node *n)
4844 STARTSTACKSTR(cmdnextc);
4846 name = stackblock();
4847 TRACE(("commandtext: name %p, end %p\n", name, cmdnextc));
4848 return ckstrdup(name);
4853 * Fork off a subshell. If we are doing job control, give the subshell its
4854 * own process group. Jp is a job structure that the job is to be added to.
4855 * N is the command that will be evaluated by the child. Both jp and n may
4856 * be NULL. The mode parameter can be one of the following:
4857 * FORK_FG - Fork off a foreground process.
4858 * FORK_BG - Fork off a background process.
4859 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
4860 * process group even if job control is on.
4862 * When job control is turned off, background processes have their standard
4863 * input redirected to /dev/null (except for the second and later processes
4866 * Called with interrupts off.
4869 * Clear traps on a fork.
4877 for (tp = trap; tp < &trap[NSIG]; tp++) {
4878 if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */
4879 if (trap_ptr == trap)
4881 /* else: it "belongs" to trap_ptr vector, don't free */
4883 if ((tp - trap) != 0)
4884 setsignal(tp - trap);
4891 /* Lives far away from here, needed for forkchild */
4892 static void closescript(void);
4894 /* Called after fork(), in child */
4895 /* jp and n are NULL when called by openhere() for heredoc support */
4896 static NOINLINE void
4897 forkchild(struct job *jp, union node *n, int mode)
4901 TRACE(("Child shell %d\n", getpid()));
4905 /* man bash: "Non-builtin commands run by bash have signal handlers
4906 * set to the values inherited by the shell from its parent".
4907 * Do we do it correctly? */
4911 if (mode == FORK_NOJOB /* is it `xxx` ? */
4912 && n && n->type == NCMD /* is it single cmd? */
4913 /* && n->ncmd.args->type == NARG - always true? */
4914 && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0
4915 && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */
4916 /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */
4918 TRACE(("Trap hack\n"));
4919 /* Awful hack for `trap` or $(trap).
4921 * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
4922 * contains an example where "trap" is executed in a subshell:
4924 * save_traps=$(trap)
4926 * eval "$save_traps"
4928 * Standard does not say that "trap" in subshell shall print
4929 * parent shell's traps. It only says that its output
4930 * must have suitable form, but then, in the above example
4931 * (which is not supposed to be normative), it implies that.
4933 * bash (and probably other shell) does implement it
4934 * (traps are reset to defaults, but "trap" still shows them),
4935 * but as a result, "trap" logic is hopelessly messed up:
4938 * trap -- 'echo Ho' SIGWINCH <--- we have a handler
4939 * # (trap) <--- trap is in subshell - no output (correct, traps are reset)
4940 * # true | trap <--- trap is in subshell - no output (ditto)
4941 * # echo `true | trap` <--- in subshell - output (but traps are reset!)
4942 * trap -- 'echo Ho' SIGWINCH
4943 * # echo `(trap)` <--- in subshell in subshell - output
4944 * trap -- 'echo Ho' SIGWINCH
4945 * # echo `true | (trap)` <--- in subshell in subshell in subshell - output!
4946 * trap -- 'echo Ho' SIGWINCH
4948 * The rules when to forget and when to not forget traps
4949 * get really complex and nonsensical.
4951 * Our solution: ONLY bare $(trap) or `trap` is special.
4953 /* Save trap handler strings for trap builtin to print */
4954 trap_ptr = xmemdup(trap, sizeof(trap));
4955 /* Fall through into clearing traps */
4959 /* do job control only in root shell */
4961 if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) {
4964 if (jp->nprocs == 0)
4967 pgrp = jp->ps[0].ps_pid;
4968 /* this can fail because we are doing it in the parent also */
4970 if (mode == FORK_FG)
4971 xtcsetpgrp(ttyfd, pgrp);
4976 if (mode == FORK_BG) {
4977 /* man bash: "When job control is not in effect,
4978 * asynchronous commands ignore SIGINT and SIGQUIT" */
4981 if (jp->nprocs == 0) {
4983 if (open(bb_dev_null, O_RDONLY) != 0)
4984 ash_msg_and_raise_error("can't open '%s'", bb_dev_null);
4988 if (iflag) { /* why if iflag only? */
4993 * "In all cases, bash ignores SIGQUIT. Non-builtin
4994 * commands run by bash have signal handlers
4995 * set to the values inherited by the shell
4997 * Take care of the second rule: */
5001 if (n && n->type == NCMD
5002 && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0
5004 TRACE(("Job hack\n"));
5005 /* "jobs": we do not want to clear job list for it,
5006 * instead we remove only _its_ own_ job from job list.
5007 * This makes "jobs .... | cat" more useful.
5013 for (jp = curjob; jp; jp = jp->prev_job)
5018 /* Called after fork(), in parent */
5020 #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid)
5023 forkparent(struct job *jp, union node *n, int mode, pid_t pid)
5025 TRACE(("In parent shell: child = %d\n", pid));
5027 /* jp is NULL when called by openhere() for heredoc support */
5028 while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0)
5034 if (mode != FORK_NOJOB && jp->jobctl) {
5037 if (jp->nprocs == 0)
5040 pgrp = jp->ps[0].ps_pid;
5041 /* This can fail because we are doing it in the child also */
5045 if (mode == FORK_BG) {
5046 backgndpid = pid; /* set $! */
5047 set_curjob(jp, CUR_RUNNING);
5050 struct procstat *ps = &jp->ps[jp->nprocs++];
5053 ps->ps_cmd = nullstr;
5055 if (doing_jobctl && n)
5056 ps->ps_cmd = commandtext(n);
5061 /* jp and n are NULL when called by openhere() for heredoc support */
5063 forkshell(struct job *jp, union node *n, int mode)
5067 TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode));
5070 TRACE(("Fork failed, errno=%d", errno));
5073 ash_msg_and_raise_error("can't fork");
5076 CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */
5077 forkchild(jp, n, mode);
5079 forkparent(jp, n, mode, pid);
5085 * Wait for job to finish.
5087 * Under job control we have the problem that while a child process
5088 * is running interrupts generated by the user are sent to the child
5089 * but not to the shell. This means that an infinite loop started by
5090 * an interactive user may be hard to kill. With job control turned off,
5091 * an interactive user may place an interactive program inside a loop.
5092 * If the interactive program catches interrupts, the user doesn't want
5093 * these interrupts to also abort the loop. The approach we take here
5094 * is to have the shell ignore interrupt signals while waiting for a
5095 * foreground process to terminate, and then send itself an interrupt
5096 * signal if the child process was terminated by an interrupt signal.
5097 * Unfortunately, some programs want to do a bit of cleanup and then
5098 * exit on interrupt; unless these processes terminate themselves by
5099 * sending a signal to themselves (instead of calling exit) they will
5100 * confuse this approach.
5102 * Called with interrupts off.
5105 waitforjob(struct job *jp)
5109 TRACE(("waitforjob(%%%d) called\n", jobno(jp)));
5112 while (jp->state == JOBRUNNING) {
5113 /* In non-interactive shells, we _can_ get
5114 * a keyboard signal here and be EINTRed,
5115 * but we just loop back, waiting for command to complete.
5118 * "If bash is waiting for a command to complete and receives
5119 * a signal for which a trap has been set, the trap
5120 * will not be executed until the command completes."
5122 * Reality is that even if trap is not set, bash
5123 * will not act on the signal until command completes.
5124 * Try this. sleep5intoff.c:
5125 * #include <signal.h>
5126 * #include <unistd.h>
5129 * sigemptyset(&set);
5130 * sigaddset(&set, SIGINT);
5131 * sigaddset(&set, SIGQUIT);
5132 * sigprocmask(SIG_BLOCK, &set, NULL);
5136 * $ bash -c './sleep5intoff; echo hi'
5137 * ^C^C^C^C <--- pressing ^C once a second
5139 * $ bash -c './sleep5intoff; echo hi'
5140 * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT)
5143 dowait(DOWAIT_BLOCK, jp);
5150 xtcsetpgrp(ttyfd, rootpid);
5151 restore_tty_if_stopped_or_signaled(jp);
5154 * This is truly gross.
5155 * If we're doing job control, then we did a TIOCSPGRP which
5156 * caused us (the shell) to no longer be in the controlling
5157 * session -- so we wouldn't have seen any ^C/SIGINT. So, we
5158 * intuit from the subprocess exit status whether a SIGINT
5159 * occurred, and if so interrupt ourselves. Yuck. - mycroft
5161 if (jp->sigint) /* TODO: do the same with all signals */
5162 raise(SIGINT); /* ... by raise(jp->sig) instead? */
5164 if (jp->state == JOBDONE)
5171 * return 1 if there are stopped jobs, otherwise 0
5183 if (jp && jp->state == JOBSTOPPED) {
5184 out2str("You have stopped jobs.\n");
5194 * Code for dealing with input/output redirection.
5199 #define EMPTY -2 /* marks an unused slot in redirtab */
5200 #define CLOSED -1 /* marks a slot of previously-closed fd */
5203 * Handle here documents. Normally we fork off a process to write the
5204 * data to a pipe. If the document is short, we can stuff the data in
5205 * the pipe without forking.
5207 /* openhere needs this forward reference */
5208 static void expandhere(union node *arg, int fd);
5210 openhere(union node *redir)
5216 ash_msg_and_raise_error("pipe call failed");
5217 if (redir->type == NHERE) {
5218 len = strlen(redir->nhere.doc->narg.text);
5219 if (len <= PIPE_BUF) {
5220 full_write(pip[1], redir->nhere.doc->narg.text, len);
5224 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
5227 ignoresig(SIGINT); //signal(SIGINT, SIG_IGN);
5228 ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN);
5229 ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN);
5230 ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN);
5231 signal(SIGPIPE, SIG_DFL);
5232 if (redir->type == NHERE)
5233 full_write(pip[1], redir->nhere.doc->narg.text, len);
5235 expandhere(redir->nhere.doc, pip[1]);
5236 _exit(EXIT_SUCCESS);
5244 openredirect(union node *redir)
5250 switch (redir->nfile.type) {
5251 /* Can't happen, our single caller does this itself */
5257 return openhere(redir);
5260 /* For N[X]HERE, reading redir->nfile.expfname would touch beyond
5261 * allocated space. Do it only when we know it is safe.
5263 fname = redir->nfile.expfname;
5265 switch (redir->nfile.type) {
5271 f = open(fname, O_RDONLY);
5276 f = open(fname, O_RDWR|O_CREAT, 0666);
5281 #if BASH_REDIR_OUTPUT
5284 /* Take care of noclobber mode. */
5286 if (stat(fname, &sb) < 0) {
5287 f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666);
5290 } else if (!S_ISREG(sb.st_mode)) {
5291 f = open(fname, O_WRONLY, 0666);
5294 if (fstat(f, &sb) < 0 && S_ISREG(sb.st_mode)) {
5307 f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5312 f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666);
5320 ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory"));
5322 ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file"));
5326 * Copy a file descriptor to be >= 10. Throws exception on error.
5334 newfd = fcntl(from, F_DUPFD, 10);
5335 err = newfd < 0 ? errno : 0;
5338 ash_msg_and_raise_perror("%d", from);
5340 fcntl(newfd, F_SETFD, FD_CLOEXEC);
5346 dup2_or_raise(int from, int to)
5350 newfd = (from != to) ? dup2(from, to) : to;
5352 /* Happens when source fd is not open: try "echo >&99" */
5353 ash_msg_and_raise_perror("%d", from);
5358 fcntl_F_DUPFD(int fd, int avoid_fd)
5362 newfd = fcntl(fd, F_DUPFD, avoid_fd + 1);
5372 xdup_CLOEXEC_and_close(int fd, int avoid_fd)
5376 newfd = fcntl(fd, F_DUPFD, avoid_fd + 1);
5382 /* fd was not open? */
5385 ash_msg_and_raise_perror("%d", newfd);
5387 fcntl(newfd, F_SETFD, FD_CLOEXEC);
5392 /* Struct def and variable are moved down to the first usage site */
5398 struct redirtab *next;
5400 struct squirrel two_fd[];
5402 #define redirlist (G_var.redirlist)
5405 add_squirrel_closed(struct redirtab *sq, int fd)
5412 for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
5413 /* If we collide with an already moved fd... */
5414 if (fd == sq->two_fd[i].orig_fd) {
5416 * "echo 3>FILE 3>&- 3>FILE"
5417 * "echo 3>&- 3>FILE"
5418 * No need for last redirect to insert
5419 * another "need to close 3" indicator.
5421 TRACE(("redirect_fd %d: already moved or closed\n", fd));
5425 TRACE(("redirect_fd %d: previous fd was closed\n", fd));
5426 sq->two_fd[i].orig_fd = fd;
5427 sq->two_fd[i].moved_to = CLOSED;
5431 save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq)
5435 if (avoid_fd < 9) /* the important case here is that it can be -1 */
5440 /* Testcase: "ls -l /proc/$$/fd 10>&-" should work */
5441 ttyfd = xdup_CLOEXEC_and_close(ttyfd, avoid_fd);
5442 TRACE(("redirect_fd %d: matches ttyfd, moving it to %d\n", fd, ttyfd));
5443 return 1; /* "we closed fd" */
5446 /* Are we called from redirect(0)? E.g. redirect
5447 * in a forked child. No need to save fds,
5448 * we aren't going to use them anymore, ok to trash.
5453 /* If this one of script's fds? */
5455 struct parsefile *pf = g_parsefile;
5457 /* We skip fd == 0 case because of the following:
5458 * $ ash # running ash interactively
5460 * and in script.sh: "exec 9>&0".
5461 * Even though top-level pf_fd _is_ 0,
5462 * it's still ok to use it: "read" builtin uses it,
5463 * why should we cripple "exec" builtin?
5465 if (fd == pf->pf_fd) {
5466 pf->pf_fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
5467 return 1; /* "we closed fd" */
5473 /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */
5475 /* First: do we collide with some already moved fds? */
5476 for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
5477 /* If we collide with an already moved fd... */
5478 if (fd == sq->two_fd[i].moved_to) {
5479 new_fd = fcntl_F_DUPFD(fd, avoid_fd);
5480 sq->two_fd[i].moved_to = new_fd;
5481 TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd));
5482 if (new_fd < 0) /* what? */
5484 return 0; /* "we did not close fd" */
5486 if (fd == sq->two_fd[i].orig_fd) {
5487 /* Example: echo Hello >/dev/null 1>&2 */
5488 TRACE(("redirect_fd %d: already moved\n", fd));
5489 return 0; /* "we did not close fd" */
5493 /* If this fd is open, we move and remember it; if it's closed, new_fd = CLOSED (-1) */
5494 new_fd = fcntl_F_DUPFD(fd, avoid_fd);
5495 TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd));
5499 /* new_fd = CLOSED; - already is -1 */
5501 sq->two_fd[i].moved_to = new_fd;
5502 sq->two_fd[i].orig_fd = fd;
5504 /* if we move stderr, let "set -x" code know */
5505 if (fd == preverrout_fd)
5506 preverrout_fd = new_fd;
5508 return 0; /* "we did not close fd" */
5512 internally_opened_fd(int fd, struct redirtab *sq)
5519 /* If this one of script's fds? */
5521 struct parsefile *pf = g_parsefile;
5523 if (fd == pf->pf_fd)
5529 if (sq) for (i = 0; i < sq->pair_count && sq->two_fd[i].orig_fd != EMPTY; i++) {
5530 if (fd == sq->two_fd[i].moved_to)
5537 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
5538 * old file descriptors are stashed away so that the redirection can be
5539 * undone by calling popredir.
5541 /* flags passed to redirect */
5542 #define REDIR_PUSH 01 /* save previous values of file descriptors */
5544 redirect(union node *redir, int flags)
5546 struct redirtab *sv;
5555 if (flags & REDIR_PUSH)
5563 fd = redir->nfile.fd;
5564 if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) {
5565 //bb_error_msg("doing %d > %d", fd, newfd);
5566 newfd = redir->ndup.dupfd;
5569 newfd = openredirect(redir); /* always >= 0 */
5571 /* open() gave us precisely the fd we wanted.
5572 * This means that this fd was not busy
5573 * (not opened to anywhere).
5574 * Remember to close it on restore:
5576 add_squirrel_closed(sv, fd);
5585 /* if "N>FILE": move newfd to fd */
5586 /* if "N>&M": dup newfd to fd */
5587 /* if "N>&-": close fd (newfd is -1) */
5589 IF_BASH_REDIR_OUTPUT(redirect_more:)
5591 closed = save_fd_on_redirect(fd, /*avoid:*/ newfd, sv);
5593 /* "N>&-" means "close me" */
5595 /* ^^^ optimization: saving may already
5596 * have closed it. If not... */
5600 /* if newfd is a script fd or saved fd, simulate EBADF */
5601 if (internally_opened_fd(newfd, sv)) {
5603 ash_msg_and_raise_perror("%d", newfd);
5605 dup2_or_raise(newfd, fd);
5606 if (close_fd >= 0) /* "N>FILE" or ">&FILE" or heredoc? */
5608 #if BASH_REDIR_OUTPUT
5609 if (redir->nfile.type == NTO2 && fd == 1) {
5610 /* ">&FILE". we already redirected to 1, now copy 1 to 2 */
5618 } while ((redir = redir->nfile.next) != NULL);
5621 //dash:#define REDIR_SAVEFD2 03 /* set preverrout */
5622 #define REDIR_SAVEFD2 0
5623 // dash has a bug: since REDIR_SAVEFD2=3 and REDIR_PUSH=1, this test
5624 // triggers for pure REDIR_PUSH too. Thus, this is done almost always,
5625 // not only for calls with flags containing REDIR_SAVEFD2.
5626 // We do this unconditionally (see save_fd_on_redirect()).
5627 //if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0)
5628 // preverrout_fd = copied_fd2;
5632 redirectsafe(union node *redir, int flags)
5635 volatile int saveint;
5636 struct jmploc *volatile savehandler = exception_handler;
5637 struct jmploc jmploc;
5640 /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */
5641 err = setjmp(jmploc.loc); /* was = setjmp(jmploc.loc) * 2; */
5643 exception_handler = &jmploc;
5644 redirect(redir, flags);
5646 exception_handler = savehandler;
5647 if (err && exception_type != EXERROR)
5648 longjmp(exception_handler->loc, 1);
5649 RESTORE_INT(saveint);
5653 static struct redirtab*
5654 pushredir(union node *redir)
5656 struct redirtab *sv;
5665 #if BASH_REDIR_OUTPUT
5666 if (redir->nfile.type == NTO2)
5669 redir = redir->nfile.next;
5672 sv = ckzalloc(sizeof(*sv) + i * sizeof(sv->two_fd[0]));
5675 sv->two_fd[i].orig_fd = sv->two_fd[i].moved_to = EMPTY;
5676 sv->next = redirlist;
5682 * Undo the effects of the last redirection.
5687 struct redirtab *rp;
5690 if (redirlist == NULL)
5694 for (i = 0; i < rp->pair_count; i++) {
5695 int fd = rp->two_fd[i].orig_fd;
5696 int copy = rp->two_fd[i].moved_to;
5697 if (copy == CLOSED) {
5702 if (copy != EMPTY) {
5705 dup2_or_raise(copy, fd);
5710 redirlist = rp->next;
5716 unwindredir(struct redirtab *stop)
5718 while (redirlist != stop)
5719 popredir(/*drop:*/ 0);
5723 /* ============ Routines to expand arguments to commands
5725 * We have to deal with backquotes, shell variables, and file metacharacters.
5728 #if ENABLE_FEATURE_SH_MATH
5730 ash_arith(const char *s)
5732 arith_state_t math_state;
5735 math_state.lookupvar = lookupvar;
5736 math_state.setvar = setvar0;
5737 //math_state.endofname = endofname;
5740 result = arith(&math_state, s);
5741 if (math_state.errmsg)
5742 ash_msg_and_raise_error(math_state.errmsg);
5752 #define EXP_FULL 0x1 /* perform word splitting & file globbing */
5753 #define EXP_TILDE 0x2 /* do normal tilde expansion */
5754 #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */
5755 #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */
5756 /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt"
5757 * POSIX says for this case:
5758 * Pathname expansion shall not be performed on the word by a
5759 * non-interactive shell; an interactive shell may perform it, but shall
5760 * do so only when the expansion would result in one word.
5761 * Currently, our code complies to the above rule by never globbing
5762 * redirection filenames.
5763 * Bash performs globbing, unless it is non-interactive and in POSIX mode.
5764 * (this means that on a typical Linux distro, bash almost always
5765 * performs globbing, and thus diverges from what we do).
5767 #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */
5768 #define EXP_QPAT 0x20 /* pattern in quoted parameter expansion */
5769 #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */
5770 #define EXP_WORD 0x80 /* expand word in parameter expansion */
5771 #define EXP_QUOTED 0x100 /* expand word in double quotes */
5775 #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */
5776 #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */
5777 #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */
5778 #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */
5779 #define RMESCAPE_SLASH 0x20 /* Stop globbing after slash */
5781 /* Add CTLESC when necessary. */
5782 #define QUOTES_ESC (EXP_FULL | EXP_CASE | EXP_QPAT)
5783 /* Do not skip NUL characters. */
5784 #define QUOTES_KEEPNUL EXP_TILDE
5787 * Structure specifying which parts of the string should be searched
5788 * for IFS characters.
5791 struct ifsregion *next; /* next region in list */
5792 int begoff; /* offset of start of region */
5793 int endoff; /* offset of end of region */
5794 int nulonly; /* search for nul bytes only */
5798 struct strlist *list;
5799 struct strlist **lastp;
5802 /* output of current string */
5803 static char *expdest;
5804 /* list of back quote expressions */
5805 static struct nodelist *argbackq;
5806 /* first struct in list of ifs regions */
5807 static struct ifsregion ifsfirst;
5808 /* last struct in list */
5809 static struct ifsregion *ifslastp;
5810 /* holds expanded arg list */
5811 static struct arglist exparg;
5815 * cvtnum() is used even if math support is off (to prepare $? values and such).
5822 /* 32-bit and wider ints require buffer size of bytes*3 (or less) */
5823 len = sizeof(arith_t) * 3;
5824 /* If narrower: worst case, 1-byte ints: need 5 bytes: "-127<NUL>" */
5825 if (sizeof(arith_t) < 4) len += 2;
5827 expdest = makestrspace(len, expdest);
5828 len = fmtstr(expdest, len, ARITH_FMT, num);
5829 STADJUST(len, expdest);
5834 * Break the argument string into pieces based upon IFS and add the
5835 * strings to the argument list. The regions of the string to be
5836 * searched for IFS characters have been stored by recordregion.
5839 ifsbreakup(char *string, struct arglist *arglist)
5841 struct ifsregion *ifsp;
5846 const char *ifs, *realifs;
5851 if (ifslastp != NULL) {
5854 realifs = ifsset() ? ifsval() : defifs;
5857 p = string + ifsp->begoff;
5858 nulonly = ifsp->nulonly;
5859 ifs = nulonly ? nullstr : realifs;
5861 while (p < string + ifsp->endoff) {
5863 if ((unsigned char)*p == CTLESC)
5865 if (!strchr(ifs, *p)) {
5870 ifsspc = (strchr(defifs, *p) != NULL);
5871 /* Ignore IFS whitespace at start */
5872 if (q == start && ifsspc) {
5878 sp = stzalloc(sizeof(*sp));
5880 *arglist->lastp = sp;
5881 arglist->lastp = &sp->next;
5885 if (p >= string + ifsp->endoff) {
5889 if ((unsigned char)*p == CTLESC)
5891 if (strchr(ifs, *p) == NULL) {
5895 if (strchr(defifs, *p) == NULL) {
5910 } while (ifsp != NULL);
5919 sp = stzalloc(sizeof(*sp));
5921 *arglist->lastp = sp;
5922 arglist->lastp = &sp->next;
5928 struct ifsregion *p = ifsfirst.next;
5935 struct ifsregion *ifsp;
5940 ifsfirst.next = NULL;
5947 esclen(const char *start, const char *p)
5951 while (p > start && (unsigned char)*--p == CTLESC) {
5958 * Remove any CTLESC characters from a string.
5961 rmescapes(char *str, int flag)
5963 static const char qchars[] ALIGN1 = {
5964 IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' };
5968 unsigned protect_against_glob;
5970 IF_BASH_PATTERN_SUBST(unsigned slash = flag & RMESCAPE_SLASH;)
5972 p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash));
5978 if (flag & RMESCAPE_ALLOC) {
5979 size_t len = p - str;
5980 size_t fulllen = len + strlen(p) + 1;
5982 if (flag & RMESCAPE_GROW) {
5983 int strloc = str - (char *)stackblock();
5984 r = makestrspace(fulllen, expdest);
5985 /* p and str may be invalidated by makestrspace */
5986 str = (char *)stackblock() + strloc;
5988 } else if (flag & RMESCAPE_HEAP) {
5989 r = ckmalloc(fulllen);
5991 r = stalloc(fulllen);
5995 q = (char *)mempcpy(q, str, len);
6000 globbing = flag & RMESCAPE_GLOB;
6001 protect_against_glob = globbing;
6003 if ((unsigned char)*p == CTLQUOTEMARK) {
6004 // Note: both inquotes and protect_against_glob only affect whether
6005 // CTLESC,<ch> gets converted to <ch> or to \<ch>
6006 inquotes = ~inquotes;
6008 protect_against_glob = globbing;
6011 if ((unsigned char)*p == CTLESC) {
6015 ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)");
6017 if (protect_against_glob) {
6019 * We used to trust glob() and fnmatch() to eat
6020 * superfluous escapes (\z where z has no
6021 * special meaning anyway). But this causes
6022 * bugs such as string of one greek letter rho
6023 * (unicode-encoded as two bytes "cf,81")
6024 * getting encoded as "cf,CTLESC,81"
6025 * and here, converted to "cf,\,81" -
6026 * which does not go well with some flavors
6027 * of fnmatch() in unicode locales
6028 * (for example, glibc <= 2.22).
6030 * Lets add "\" only on the chars which need it.
6031 * Testcases for less obvious chars are shown.
6036 || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */
6037 || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */
6038 || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */
6039 || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */
6040 /* Some libc support [^negate], that's why "^" also needs love */
6041 || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */
6046 } else if (*p == '\\' && !inquotes) {
6047 /* naked back slash */
6048 protect_against_glob = 0;
6051 #if BASH_PATTERN_SUBST
6052 else if (*p == '/' && slash) {
6053 /* stop handling globbing and mark location of slash */
6054 globbing = slash = 0;
6058 protect_against_glob = globbing;
6063 if (flag & RMESCAPE_GROW) {
6065 STADJUST(q - r + 1, expdest);
6069 #define pmatch(a, b) !fnmatch((a), (b), 0)
6072 * Prepare a pattern for a expmeta (internal glob(3)) call.
6074 * Returns an stalloced string.
6077 preglob(const char *pattern, int flag)
6079 return rmescapes((char *)pattern, flag | RMESCAPE_GLOB);
6083 * Put a string on the stack.
6086 memtodest(const char *p, size_t len, int syntax, int quotes)
6093 q = makestrspace((quotes & QUOTES_ESC) ? len * 2 : len, expdest);
6096 unsigned char c = *p++;
6098 if (quotes & QUOTES_ESC) {
6099 int n = SIT(c, syntax);
6101 || (((quotes & EXP_FULL) || syntax != BASESYNTAX)
6108 } else if (!(quotes & QUOTES_KEEPNUL))
6117 strtodest(const char *p, int syntax, int quotes)
6119 size_t len = strlen(p);
6120 memtodest(p, len, syntax, quotes);
6125 * Record the fact that we have to scan this region of the
6126 * string for IFS characters.
6129 recordregion(int start, int end, int nulonly)
6131 struct ifsregion *ifsp;
6133 if (ifslastp == NULL) {
6137 ifsp = ckzalloc(sizeof(*ifsp));
6138 /*ifsp->next = NULL; - ckzalloc did it */
6139 ifslastp->next = ifsp;
6143 ifslastp->begoff = start;
6144 ifslastp->endoff = end;
6145 ifslastp->nulonly = nulonly;
6149 removerecordregions(int endoff)
6151 if (ifslastp == NULL)
6154 if (ifsfirst.endoff > endoff) {
6155 while (ifsfirst.next) {
6156 struct ifsregion *ifsp;
6158 ifsp = ifsfirst.next->next;
6159 free(ifsfirst.next);
6160 ifsfirst.next = ifsp;
6163 if (ifsfirst.begoff > endoff) {
6166 ifslastp = &ifsfirst;
6167 ifsfirst.endoff = endoff;
6172 ifslastp = &ifsfirst;
6173 while (ifslastp->next && ifslastp->next->begoff < endoff)
6174 ifslastp = ifslastp->next;
6175 while (ifslastp->next) {
6176 struct ifsregion *ifsp;
6178 ifsp = ifslastp->next->next;
6179 free(ifslastp->next);
6180 ifslastp->next = ifsp;
6183 if (ifslastp->endoff > endoff)
6184 ifslastp->endoff = endoff;
6188 exptilde(char *startp, char *p, int flags)
6194 int quotes = flags & QUOTES_ESC;
6198 while ((c = *++p) != '\0') {
6205 if (flags & EXP_VARTILDE)
6215 if (*name == '\0') {
6216 home = lookupvar("HOME");
6218 pw = getpwnam(name);
6223 if (!home || !*home)
6226 strtodest(home, SQSYNTAX, quotes);
6234 * Execute a command inside back quotes. If it's a builtin command, we
6235 * want to save its output in a block obtained from malloc. Otherwise
6236 * we fork off a subprocess and get the output of the command via a pipe.
6237 * Should be called with interrupts off.
6239 struct backcmd { /* result of evalbackcmd */
6240 int fd; /* file descriptor to read from */
6241 int nleft; /* number of chars in buffer */
6242 char *buf; /* buffer */
6243 struct job *jp; /* job structure for command */
6246 /* These forward decls are needed to use "eval" code for backticks handling: */
6247 /* flags in argument to evaltree */
6248 #define EV_EXIT 01 /* exit after evaluating tree */
6249 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
6250 static int evaltree(union node *, int);
6252 /* An evaltree() which is known to never return.
6253 * Used to use an alias:
6254 * static int evaltreenr(union node *, int) __attribute__((alias("evaltree"),__noreturn__));
6255 * but clang was reported to "transfer" noreturn-ness to evaltree() as well.
6257 static ALWAYS_INLINE NORETURN void
6258 evaltreenr(union node *n, int flags)
6261 bb_unreachable(abort());
6265 static void FAST_FUNC
6266 evalbackcmd(union node *n, struct backcmd *result)
6280 ash_msg_and_raise_error("pipe call failed");
6281 jp = makejob(/*n,*/ 1);
6282 if (forkshell(jp, n, FORK_NOJOB) == 0) {
6288 dup2_or_raise(pip[1], 1);
6291 /* TODO: eflag clearing makes the following not abort:
6292 * ash -c 'set -e; z=$(false;echo foo); echo $z'
6293 * which is what bash does (unless it is in POSIX mode).
6294 * dash deleted "eflag = 0" line in the commit
6295 * Date: Mon, 28 Jun 2010 17:11:58 +1000
6296 * [EVAL] Don't clear eflag in evalbackcmd
6297 * For now, preserve bash-like behavior, it seems to be somewhat more useful:
6301 evaltreenr(n, EV_EXIT);
6306 result->fd = pip[0];
6310 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
6311 result->fd, result->buf, result->nleft, result->jp));
6315 * Expand stuff in backwards quotes.
6318 expbackq(union node *cmd, int flag)
6326 int syntax = flag & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
6327 struct stackmark smark;
6330 startloc = expdest - (char *)stackblock();
6331 pushstackmark(&smark, startloc);
6332 evalbackcmd(cmd, &in);
6333 popstackmark(&smark);
6340 memtodest(p, i, syntax, flag & QUOTES_ESC);
6344 i = nonblock_immune_read(in.fd, buf, sizeof(buf));
6345 TRACE(("expbackq: read returns %d\n", i));
6354 back_exitstatus = waitforjob(in.jp);
6358 /* Eat all trailing newlines */
6360 for (; dest > (char *)stackblock() && dest[-1] == '\n';)
6364 if (!(flag & EXP_QUOTED))
6365 recordregion(startloc, dest - (char *)stackblock(), 0);
6366 TRACE(("evalbackq: size:%d:'%.*s'\n",
6367 (int)((dest - (char *)stackblock()) - startloc),
6368 (int)((dest - (char *)stackblock()) - startloc),
6369 stackblock() + startloc));
6372 #if ENABLE_FEATURE_SH_MATH
6374 * Expand arithmetic expression. Backup to start of expression,
6375 * evaluate, place result in (backed up) result, adjust string position.
6387 * This routine is slightly over-complicated for
6388 * efficiency. Next we scan backwards looking for the
6389 * start of arithmetic.
6391 start = stackblock();
6398 while ((unsigned char)*p != CTLARI) {
6402 ash_msg_and_raise_error("missing CTLARI (shouldn't happen)");
6407 esc = esclen(start, p);
6417 removerecordregions(begoff);
6421 if (flag & QUOTES_ESC)
6422 rmescapes(p + 1, 0);
6424 len = cvtnum(ash_arith(p + 1));
6426 if (!(flag & EXP_QUOTED))
6427 recordregion(begoff, begoff + len, 0);
6431 /* argstr needs it */
6432 static char *evalvar(char *p, int flags);
6435 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
6436 * characters to allow for further processing. Otherwise treat
6437 * $@ like $* since no splitting will be performed.
6440 argstr(char *p, int flags)
6442 static const char spclchars[] ALIGN1 = {
6450 #if ENABLE_FEATURE_SH_MATH
6455 const char *reject = spclchars;
6456 int breakall = (flags & (EXP_WORD | EXP_QUOTED)) == EXP_WORD;
6461 if (!(flags & EXP_VARTILDE)) {
6463 } else if (flags & EXP_VARTILDE2) {
6468 if (flags & EXP_TILDE) {
6471 flags &= ~EXP_TILDE;
6475 p = exptilde(p, q, flags);
6478 startloc = expdest - (char *)stackblock();
6482 length += strcspn(p + length, reject);
6486 IF_FEATURE_SH_MATH(|| c == CTLENDARI)
6488 /* c == '=' || c == ':' || c == CTLENDARI */
6494 expdest = stack_nputstr(p, length, expdest);
6495 newloc = expdest - (char *)stackblock();
6496 if (breakall && !inquotes && newloc > startloc) {
6497 recordregion(startloc, newloc, 0);
6508 if (flags & EXP_VARTILDE2) {
6512 flags |= EXP_VARTILDE2;
6517 * sort of a hack - expand tildes in variable
6518 * assignments (after the first '=' and after ':'s).
6527 case CTLENDVAR: /* ??? */
6530 inquotes ^= EXP_QUOTED;
6531 /* "$@" syntax adherence hack */
6532 if (inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) {
6533 p = evalvar(p + 1, flags | inquotes) + 1;
6537 if (flags & QUOTES_ESC) {
6548 * Quoted parameter expansion pattern: remove quote
6549 * unless inside inner quotes or we have a literal
6552 if (((flags | inquotes) & (EXP_QPAT | EXP_QUOTED)) ==
6553 EXP_QPAT && *p != '\\')
6558 TRACE(("argstr: evalvar('%s')\n", p));
6559 p = evalvar(p, flags | inquotes);
6560 TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock()));
6563 expbackq(argbackq->n, flags | inquotes);
6564 argbackq = argbackq->next;
6566 #if ENABLE_FEATURE_SH_MATH
6569 expari(flags | inquotes);
6578 scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM,
6579 char *pattern, int quotes, int zero)
6588 const char *s = loc2;
6595 match = pmatch(pattern, s);
6600 if (quotes && (unsigned char)*loc == CTLESC)
6609 scanright(char *startp, char *rmesc, char *rmescend,
6610 char *pattern, int quotes, int match_at_start)
6612 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6613 int try2optimize = match_at_start;
6619 /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}":
6620 * startp="escaped_value_of_v" rmesc="raw_value_of_v"
6621 * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1
6623 * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc,
6624 * and on each iteration they go back two/one char until they reach the beginning.
6625 * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc.
6627 /* TODO: document in what other circumstances we are called. */
6629 for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) {
6632 const char *s = loc2;
6633 if (match_at_start) {
6637 match = pmatch(pattern, s);
6638 //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match);
6642 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6644 /* Maybe we can optimize this:
6645 * if pattern ends with unescaped *, we can avoid checking
6646 * shorter strings: if "foo*" doesn't match "raw_value_of_v",
6647 * it won't match truncated "raw_value_of_" strings too.
6649 unsigned plen = strlen(pattern);
6650 /* Does it end with "*"? */
6651 if (plen != 0 && pattern[--plen] == '*') {
6652 /* "xxxx*" is not escaped */
6653 /* "xxx\*" is escaped */
6654 /* "xx\\*" is not escaped */
6655 /* "x\\\*" is escaped */
6657 while (plen != 0 && pattern[--plen] == '\\')
6660 break; /* ends with unescaped "*" */
6668 esc = esclen(startp, loc);
6679 static void varunset(const char *, const char *, const char *, int) NORETURN;
6681 varunset(const char *end, const char *var, const char *umsg, int varflags)
6687 msg = "parameter not set";
6689 if ((unsigned char)*end == CTLENDVAR) {
6690 if (varflags & VSNUL)
6696 ash_msg_and_raise_error("%.*s: %s%s", (int)(end - var - 1), var, msg, tail);
6700 subevalvar(char *p, char *varname, int strloc, int subtype,
6701 int startloc, int varflags, int flag)
6703 struct nodelist *saveargbackq = argbackq;
6704 int quotes = flag & QUOTES_ESC;
6707 char *rmesc, *rmescend;
6709 int amount, resetloc;
6710 IF_BASH_PATTERN_SUBST(int workloc;)
6711 IF_BASH_PATTERN_SUBST(char *repl = NULL;)
6713 char *(*scan)(char*, char*, char*, char*, int, int);
6715 //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)",
6716 // p, varname, strloc, subtype, startloc, varflags, quotes);
6718 argstr(p, EXP_TILDE | (subtype != VSASSIGN && subtype != VSQUESTION ?
6719 (flag & (EXP_QUOTED | EXP_QPAT) ? EXP_QPAT : EXP_CASE) : 0)
6721 STPUTC('\0', expdest);
6722 argbackq = saveargbackq;
6723 startp = (char *)stackblock() + startloc;
6727 setvar0(varname, startp);
6728 amount = startp - expdest;
6729 STADJUST(amount, expdest);
6733 varunset(p, varname, startp, varflags);
6738 int pos, len, orig_len;
6741 loc = str = stackblock() + strloc;
6743 # if !ENABLE_FEATURE_SH_MATH
6744 # define ash_arith number
6746 /* Read POS in ${var:POS:LEN} */
6747 colon = strchr(loc, ':');
6748 if (colon) *colon = '\0';
6749 pos = ash_arith(loc);
6750 if (colon) *colon = ':';
6752 /* Read LEN in ${var:POS:LEN} */
6753 len = str - startp - 1;
6754 /* *loc != '\0', guaranteed by parser */
6758 /* Adjust the length by the number of escapes */
6759 for (ptr = startp; ptr < (str - 1); ptr++) {
6760 if ((unsigned char)*ptr == CTLESC) {
6767 if (*loc++ == ':') {
6769 len = ash_arith(loc);
6771 /* Skip POS in ${var:POS:LEN} */
6773 while (*loc && *loc != ':') {
6776 if (*loc++ == ':') {
6777 len = ash_arith(loc);
6783 /* ${VAR:$((-n)):l} starts n chars from the end */
6784 pos = orig_len + pos;
6786 if ((unsigned)pos >= orig_len) {
6787 /* apart from obvious ${VAR:999999:l},
6788 * covers ${VAR:$((-9999999)):l} - result is ""
6795 /* ${VAR:N:-M} sets LEN to strlen()-M */
6796 len = (orig_len - pos) + len;
6798 if ((unsigned)len > (orig_len - pos))
6799 len = orig_len - pos;
6801 for (str = startp; pos; str++, pos--) {
6802 if (quotes && (unsigned char)*str == CTLESC)
6805 for (loc = startp; len; len--) {
6806 if (quotes && (unsigned char)*str == CTLESC)
6811 amount = loc - expdest;
6812 STADJUST(amount, expdest);
6815 #endif /* BASH_SUBSTR */
6818 resetloc = expdest - (char *)stackblock();
6820 #if BASH_PATTERN_SUBST
6821 /* We'll comeback here if we grow the stack while handling
6822 * a VSREPLACE or VSREPLACEALL, since our pointers into the
6823 * stack will need rebasing, and we'll need to remove our work
6829 amount = expdest - ((char *)stackblock() + resetloc);
6830 STADJUST(-amount, expdest);
6831 startp = (char *)stackblock() + startloc;
6834 rmescend = (char *)stackblock() + strloc;
6836 rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW);
6837 if (rmesc != startp) {
6839 startp = (char *)stackblock() + startloc;
6843 str = (char *)stackblock() + strloc;
6845 * Example: v='a\bc'; echo ${v/\\b/_\\_\z_}
6846 * The result is a_\_z_c (not a\_\_z_c)!
6848 * The search pattern and replace string treat backslashes differently!
6849 * RMESCAPE_SLASH causes preglob to work differently on the pattern
6850 * and string. It's only used on the first call.
6852 preglob(str, IF_BASH_PATTERN_SUBST(
6853 (subtype == VSREPLACE || subtype == VSREPLACEALL) && !repl ?
6854 RMESCAPE_SLASH : ) 0);
6856 #if BASH_PATTERN_SUBST
6857 workloc = expdest - (char *)stackblock();
6858 if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
6863 repl = strchr(str, CTLESC);
6869 //bb_error_msg("str:'%s' repl:'%s'", str, repl);
6871 /* If there's no pattern to match, return the expansion unmolested */
6880 loc = scanright(idx, rmesc, rmescend, str, quotes, 1);
6881 //bb_error_msg("scanright('%s'):'%s'", str, loc);
6883 /* No match, advance */
6884 char *restart_detect = stackblock();
6886 STPUTC(*idx, expdest);
6887 if (quotes && (unsigned char)*idx == CTLESC) {
6890 STPUTC(*idx, expdest);
6892 if (stackblock() != restart_detect)
6897 /* continue; - prone to quadratic behavior, smarter code: */
6900 if (str[0] == '*') {
6901 /* Pattern is "*foo". If "*foo" does not match "long_string",
6902 * it would never match "ong_string" etc, no point in trying.
6909 if (subtype == VSREPLACEALL) {
6911 if (quotes && (unsigned char)*idx == CTLESC)
6920 //bb_error_msg("repl:'%s'", repl);
6921 for (loc = (char*)repl; *loc; loc++) {
6922 char *restart_detect = stackblock();
6923 if (quotes && *loc == '\\') {
6924 STPUTC(CTLESC, expdest);
6927 STPUTC(*loc, expdest);
6928 if (stackblock() != restart_detect)
6933 if (subtype == VSREPLACE) {
6934 //bb_error_msg("tail:'%s', quotes:%x", idx, quotes);
6936 char *restart_detect = stackblock();
6937 STPUTC(*idx, expdest);
6938 if (stackblock() != restart_detect)
6947 /* We've put the replaced text into a buffer at workloc, now
6948 * move it to the right place and adjust the stack.
6950 STPUTC('\0', expdest);
6951 startp = (char *)stackblock() + startloc;
6952 memmove(startp, (char *)stackblock() + workloc, len + 1);
6953 //bb_error_msg("startp:'%s'", startp);
6954 amount = expdest - (startp + len);
6955 STADJUST(-amount, expdest);
6958 #endif /* BASH_PATTERN_SUBST */
6960 subtype -= VSTRIMRIGHT;
6962 if (subtype < 0 || subtype > 7)
6965 /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */
6966 zero = subtype >> 1;
6967 /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */
6968 scan = (subtype & 1) ^ zero ? scanleft : scanright;
6970 loc = scan(startp, rmesc, rmescend, str, quotes, zero);
6973 memmove(startp, loc, str - loc);
6974 loc = startp + (str - loc) - 1;
6977 amount = loc - expdest;
6978 STADJUST(amount, expdest);
6984 * Add the value of a specialized variable to the stack string.
6985 * name parameter (examples):
6986 * ash -c 'echo $1' name:'1='
6987 * ash -c 'echo $qwe' name:'qwe='
6988 * ash -c 'echo $$' name:'$='
6989 * ash -c 'echo ${$}' name:'$='
6990 * ash -c 'echo ${$##q}' name:'$=q'
6991 * ash -c 'echo ${#$}' name:'$='
6992 * note: examples with bad shell syntax:
6993 * ash -c 'echo ${#$1}' name:'$=1'
6994 * ash -c 'echo ${#1#}' name:'1=#'
6996 static NOINLINE ssize_t
6997 varvalue(char *name, int varflags, int flags, int *quotedp)
7004 int quoted = *quotedp;
7005 int subtype = varflags & VSTYPE;
7006 int discard = subtype == VSPLUS || subtype == VSLENGTH;
7007 int quotes = (discard ? 0 : (flags & QUOTES_ESC)) | QUOTES_KEEPNUL;
7010 sep = (flags & EXP_FULL) << CHAR_BIT;
7011 syntax = quoted ? DQSYNTAX : BASESYNTAX;
7021 num = shellparam.nparam;
7029 goto check_1char_name;
7031 expdest = makestrspace(NOPTS, expdest);
7032 for (i = NOPTS - 1; i >= 0; i--) {
7034 USTPUTC(optletters(i), expdest);
7040 /* handles cases similar to ${#$1} */
7041 if (name[2] != '\0')
7042 raise_error_syntax("bad substitution");
7055 sep |= ifsset() ? ifsval()[0] : ' ';
7062 while ((p = *ap++) != NULL) {
7063 len += strtodest(p, syntax, quotes);
7067 memtodest(&sepc, 1, syntax, quotes);
7082 num = atoi(name); /* number(name) fails on ${N#str} etc */
7083 if (num < 0 || num > shellparam.nparam)
7085 p = num ? shellparam.p[num - 1] : arg0;
7088 /* NB: name has form "VAR=..." */
7089 p = lookupvar(name);
7094 len = strtodest(p, syntax, quotes);
7095 #if ENABLE_UNICODE_SUPPORT
7096 if (subtype == VSLENGTH && len > 0) {
7097 reinit_unicode_for_ash();
7098 if (unicode_status == UNICODE_ON) {
7099 STADJUST(-len, expdest);
7101 len = unicode_strlen(p);
7109 STADJUST(-len, expdest);
7114 * Expand a variable, and return a pointer to the next character in the
7118 evalvar(char *p, int flag)
7129 varflags = (unsigned char) *p++;
7130 subtype = varflags & VSTYPE;
7133 raise_error_syntax("bad substitution");
7135 quoted = flag & EXP_QUOTED;
7137 easy = (!quoted || (*var == '@' && shellparam.nparam));
7138 startloc = expdest - (char *)stackblock();
7139 p = strchr(p, '=') + 1; //TODO: use var_end(p)?
7142 varlen = varvalue(var, varflags, flag, "ed);
7143 if (varflags & VSNUL)
7146 if (subtype == VSPLUS) {
7147 varlen = -1 - varlen;
7151 if (subtype == VSMINUS) {
7156 flag | EXP_TILDE | EXP_WORD
7163 if (subtype == VSASSIGN || subtype == VSQUESTION) {
7167 subevalvar(p, var, 0, subtype, startloc, varflags,
7168 flag & ~QUOTES_ESC);
7171 * Remove any recorded regions beyond
7174 removerecordregions(startloc);
7178 if (varlen < 0 && uflag)
7179 varunset(p, var, 0, 0);
7181 if (subtype == VSLENGTH) {
7182 cvtnum(varlen > 0 ? varlen : 0);
7186 if (subtype == VSNORMAL) {
7190 recordregion(startloc, expdest - (char *)stackblock(), quoted);
7199 case VSTRIMRIGHTMAX:
7203 #if BASH_PATTERN_SUBST
7215 * Terminate the string and start recording the pattern
7218 STPUTC('\0', expdest);
7219 patloc = expdest - (char *)stackblock();
7220 if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype,
7221 startloc, varflags, flag)) {
7222 int amount = expdest - (
7223 (char *)stackblock() + patloc - 1
7225 STADJUST(-amount, expdest);
7227 /* Remove any recorded regions beyond start of variable */
7228 removerecordregions(startloc);
7233 if (subtype != VSNORMAL) { /* skip to end of alternative */
7236 unsigned char c = *p++;
7239 else if (c == CTLBACKQ) {
7241 argbackq = argbackq->next;
7242 } else if (c == CTLVAR) {
7243 if ((*p++ & VSTYPE) != VSNORMAL)
7245 } else if (c == CTLENDVAR) {
7255 * Add a file name to the list.
7258 addfname(const char *name)
7262 sp = stzalloc(sizeof(*sp));
7263 sp->text = sstrdup(name);
7265 exparg.lastp = &sp->next;
7268 /* Avoid glob() (and thus, stat() et al) for words like "echo" */
7270 hasmeta(const char *p)
7272 static const char chars[] ALIGN1 = {
7273 '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0
7277 p = strpbrk(p, chars);
7280 switch ((unsigned char) *p) {
7284 if (*p == CTLQUOTEMARK)
7288 if (*p == '\0') /* huh? */
7299 if (!strchr(p + 1, ']')) {
7300 /* It's not a properly closed [] pattern,
7301 * but other metas may follow. Continue checking.
7302 * my[file* _is_ globbed by bash
7303 * and matches filenames like "my[file1".
7319 /* If we want to use glob() from libc... */
7320 #if !ENABLE_ASH_INTERNAL_GLOB
7322 /* Add the result of glob() to the list */
7324 addglob(const glob_t *pglob)
7326 char **p = pglob->gl_pathv;
7333 expandmeta(struct strlist *str /*, int flag*/)
7335 /* TODO - EXP_REDIR */
7345 if (!hasmeta(str->text))
7349 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7350 // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match
7351 // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?)
7353 // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping:
7354 // if you pass it "file\?", it returns "file\?", not "file?", if no match.
7355 // Which means you need to unescape the string, right? Not so fast:
7356 // if there _is_ a file named "file\?" (with backslash), it is returned
7357 // as "file\?" too (whichever pattern you used to find it, say, "file*").
7358 // You DON'T KNOW by looking at the result whether you need to unescape it.
7360 // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?",
7361 // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file.
7362 // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match).
7363 // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly.
7364 // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob);
7365 // i = glob(p, GLOB_NOMAGIC, NULL, &pglob);
7366 i = glob(p, 0, NULL, &pglob);
7367 //bb_error_msg("glob('%s'):%d '%s'...", p, i, pglob.gl_pathv ? pglob.gl_pathv[0] : "-");
7372 #if 0 // glibc 2.24.90 bug? Patterns like "*/file", when match, don't set GLOB_MAGCHAR
7373 /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */
7374 if (!(pglob.gl_flags & GLOB_MAGCHAR))
7386 *exparg.lastp = str;
7387 rmescapes(str->text, 0);
7388 exparg.lastp = &str->next;
7390 default: /* GLOB_NOSPACE */
7393 ash_msg_and_raise_error(bb_msg_memory_exhausted);
7400 /* ENABLE_ASH_INTERNAL_GLOB: Homegrown globbing code. (dash also has both, uses homegrown one.) */
7403 * Do metacharacter (i.e. *, ?, [...]) expansion.
7406 expmeta(char *expdir, char *enddir, char *name)
7422 for (p = name; esc = 0, *p; p += esc + 1) {
7423 if (*p == '*' || *p == '?')
7425 else if (*p == '[') {
7432 if (*q == '/' || *q == '\0')
7442 if (p[esc] == '/') {
7445 start = p + esc + 1;
7449 if (metaflag == 0) { /* we've reached the end of the file name */
7450 if (enddir != expdir)
7458 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
7469 } while (p < start);
7471 if (enddir == expdir) {
7473 } else if (enddir == expdir + 1 && *expdir == '/') {
7482 if (enddir != expdir)
7484 if (*endname == 0) {
7497 while (!pending_int && (dp = readdir(dirp)) != NULL) {
7498 if (dp->d_name[0] == '.' && !matchdot)
7500 if (pmatch(start, dp->d_name)) {
7502 strcpy(enddir, dp->d_name);
7505 for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';)
7508 expmeta(expdir, p, endname);
7514 endname[-esc - 1] = esc ? '\\' : '/';
7517 static struct strlist *
7518 msort(struct strlist *list, int len)
7520 struct strlist *p, *q = NULL;
7521 struct strlist **lpp;
7529 for (n = half; --n >= 0;) {
7533 q->next = NULL; /* terminate first half of list */
7534 q = msort(list, half); /* sort first half of list */
7535 p = msort(p, len - half); /* sort second half */
7538 #if ENABLE_LOCALE_SUPPORT
7539 if (strcoll(p->text, q->text) < 0)
7541 if (strcmp(p->text, q->text) < 0)
7565 * Sort the results of file name expansion. It calculates the number of
7566 * strings to sort and then calls msort (short for merge sort) to do the
7569 static struct strlist *
7570 expsort(struct strlist *str)
7576 for (sp = str; sp; sp = sp->next)
7578 return msort(str, len);
7582 expandmeta(struct strlist *str /*, int flag*/)
7584 /* TODO - EXP_REDIR */
7588 struct strlist **savelastp;
7594 if (!hasmeta(str->text))
7596 savelastp = exparg.lastp;
7599 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7601 int i = strlen(str->text);
7602 //BUGGY estimation of how long expanded name can be
7603 expdir = ckmalloc(i < 2048 ? 2048 : i+1);
7605 expmeta(expdir, expdir, p);
7610 if (exparg.lastp == savelastp) {
7615 *exparg.lastp = str;
7616 rmescapes(str->text, 0);
7617 exparg.lastp = &str->next;
7619 *exparg.lastp = NULL;
7620 *savelastp = sp = expsort(*savelastp);
7621 while (sp->next != NULL)
7623 exparg.lastp = &sp->next;
7628 #endif /* ENABLE_ASH_INTERNAL_GLOB */
7631 * Perform variable substitution and command substitution on an argument,
7632 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
7633 * perform splitting and file name expansion. When arglist is NULL, perform
7634 * here document expansion.
7637 expandarg(union node *arg, struct arglist *arglist, int flag)
7642 argbackq = arg->narg.backquote;
7643 STARTSTACKSTR(expdest);
7644 TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag));
7645 argstr(arg->narg.text, flag);
7646 p = _STPUTC('\0', expdest);
7648 if (arglist == NULL) {
7649 /* here document expanded */
7652 p = grabstackstr(p);
7653 TRACE(("expandarg: p:'%s'\n", p));
7654 exparg.lastp = &exparg.list;
7658 if (flag & EXP_FULL) {
7659 ifsbreakup(p, &exparg);
7660 *exparg.lastp = NULL;
7661 exparg.lastp = &exparg.list;
7662 expandmeta(exparg.list /*, flag*/);
7664 sp = stzalloc(sizeof(*sp));
7667 exparg.lastp = &sp->next;
7669 *exparg.lastp = NULL;
7671 *arglist->lastp = exparg.list;
7672 arglist->lastp = exparg.lastp;
7680 * Expand shell variables and backquotes inside a here document.
7683 expandhere(union node *arg, int fd)
7685 expandarg(arg, (struct arglist *)NULL, EXP_QUOTED);
7686 full_write(fd, stackblock(), expdest - (char *)stackblock());
7690 * Returns true if the pattern matches the string.
7693 patmatch(char *pattern, const char *string)
7695 char *p = preglob(pattern, 0);
7696 //bb_error_msg("fnmatch(pattern:'%s',str:'%s')", p, string);
7697 return pmatch(p, string);
7701 * See if a pattern matches in a case statement.
7704 casematch(union node *pattern, char *val)
7706 struct stackmark smark;
7709 setstackmark(&smark);
7710 argbackq = pattern->narg.backquote;
7711 STARTSTACKSTR(expdest);
7712 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
7713 STACKSTRNUL(expdest);
7715 result = patmatch(stackblock(), val);
7716 popstackmark(&smark);
7721 /* ============ find_command */
7725 int (*builtin)(int, char **) FAST_FUNC;
7726 /* unsigned flags; */
7728 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
7729 /* "regular" builtins always take precedence over commands,
7730 * regardless of PATH=....%builtin... position */
7731 #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2)
7732 #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4)
7735 smallint cmdtype; /* CMDxxx */
7738 /* index >= 0 for commands without path (slashes) */
7739 /* (TODO: what exactly does the value mean? PATH position?) */
7740 /* index == -1 for commands with slashes */
7741 /* index == (-2 - applet_no) for NOFORK applets */
7742 const struct builtincmd *cmd;
7743 struct funcnode *func;
7746 /* values of cmdtype */
7747 #define CMDUNKNOWN -1 /* no entry in table for command */
7748 #define CMDNORMAL 0 /* command is an executable program */
7749 #define CMDFUNCTION 1 /* command is a shell function */
7750 #define CMDBUILTIN 2 /* command is a shell builtin */
7752 /* action to find_command() */
7753 #define DO_ERR 0x01 /* prints errors */
7754 #define DO_ABS 0x02 /* checks absolute paths */
7755 #define DO_NOFUNC 0x04 /* don't return shell functions, for command */
7756 #define DO_ALTPATH 0x08 /* using alternate path */
7757 #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */
7759 static void find_command(char *, struct cmdentry *, int, const char *);
7762 /* ============ Hashing commands */
7765 * When commands are first encountered, they are entered in a hash table.
7766 * This ensures that a full path search will not have to be done for them
7767 * on each invocation.
7769 * We should investigate converting to a linear search, even though that
7770 * would make the command name "hash" a misnomer.
7774 struct tblentry *next; /* next entry in hash chain */
7775 union param param; /* definition of builtin function */
7776 smallint cmdtype; /* CMDxxx */
7777 char rehash; /* if set, cd done since entry created */
7778 char cmdname[1]; /* name of command */
7781 static struct tblentry **cmdtable;
7782 #define INIT_G_cmdtable() do { \
7783 cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \
7786 static int builtinloc = -1; /* index in path of %builtin, or -1 */
7790 tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, char **envp)
7792 #if ENABLE_FEATURE_SH_STANDALONE
7793 if (applet_no >= 0) {
7794 if (APPLET_IS_NOEXEC(applet_no)) {
7798 popredir(/*drop:*/ 1);
7799 run_applet_no_and_exit(applet_no, cmd, argv);
7801 /* re-exec ourselves with the new arguments */
7802 execve(bb_busybox_exec_path, argv, envp);
7803 /* If they called chroot or otherwise made the binary no longer
7804 * executable, fall through */
7811 execve(cmd, argv, envp);
7812 } while (errno == EINTR);
7814 execve(cmd, argv, envp);
7816 if (cmd != bb_busybox_exec_path && errno == ENOEXEC) {
7817 /* Run "cmd" as a shell script:
7818 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
7819 * "If the execve() function fails with ENOEXEC, the shell
7820 * shall execute a command equivalent to having a shell invoked
7821 * with the command name as its first operand,
7822 * with any remaining arguments passed to the new shell"
7824 * That is, do not use $SHELL, user's shell, or /bin/sh;
7825 * just call ourselves.
7827 * Note that bash reads ~80 chars of the file, and if it sees
7828 * a zero byte before it sees newline, it doesn't try to
7829 * interpret it, but fails with "cannot execute binary file"
7830 * message and exit code 126. For one, this prevents attempts
7831 * to interpret foreign ELF binaries as shell scripts.
7833 argv[0] = (char*) cmd;
7834 cmd = bb_busybox_exec_path;
7835 /* NB: this is only possible because all callers of shellexec()
7836 * ensure that the argv[-1] slot exists!
7839 argv[0] = (char*) "ash";
7845 * Exec a program. Never returns. If you change this routine, you may
7846 * have to change the find_command routine as well.
7847 * argv[-1] must exist and be writable! See tryexec() for why.
7849 static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN;
7850 static void shellexec(char *prog, char **argv, const char *path, int idx)
7856 int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */
7858 envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL);
7859 if (strchr(prog, '/') != NULL
7860 #if ENABLE_FEATURE_SH_STANDALONE
7861 || (applet_no = find_applet_by_name(prog)) >= 0
7864 tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp);
7865 if (applet_no >= 0) {
7866 /* We tried execing ourself, but it didn't work.
7867 * Maybe /proc/self/exe doesn't exist?
7876 while ((cmdname = path_advance(&path, prog)) != NULL) {
7877 if (--idx < 0 && pathopt == NULL) {
7878 tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
7879 if (errno != ENOENT && errno != ENOTDIR)
7886 /* Map to POSIX errors */
7898 exitstatus = exerrno;
7899 TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n",
7900 prog, e, suppress_int));
7901 ash_msg_and_raise(EXEXIT, "%s: %s", prog, errmsg(e, "not found"));
7906 printentry(struct tblentry *cmdp)
7912 idx = cmdp->param.index;
7915 name = path_advance(&path, cmdp->cmdname);
7917 } while (--idx >= 0);
7918 out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr));
7922 * Clear out command entries. The argument specifies the first entry in
7923 * PATH which has changed.
7926 clearcmdentry(int firstchange)
7928 struct tblentry **tblp;
7929 struct tblentry **pp;
7930 struct tblentry *cmdp;
7933 for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) {
7935 while ((cmdp = *pp) != NULL) {
7936 if ((cmdp->cmdtype == CMDNORMAL &&
7937 cmdp->param.index >= firstchange)
7938 || (cmdp->cmdtype == CMDBUILTIN &&
7939 builtinloc >= firstchange)
7952 * Locate a command in the command hash table. If "add" is nonzero,
7953 * add the command to the table if it is not already present. The
7954 * variable "lastcmdentry" is set to point to the address of the link
7955 * pointing to the entry, so that delete_cmd_entry can delete the
7958 * Interrupts must be off if called with add != 0.
7960 static struct tblentry **lastcmdentry;
7962 static struct tblentry *
7963 cmdlookup(const char *name, int add)
7965 unsigned int hashval;
7967 struct tblentry *cmdp;
7968 struct tblentry **pp;
7971 hashval = (unsigned char)*p << 4;
7973 hashval += (unsigned char)*p++;
7975 pp = &cmdtable[hashval % CMDTABLESIZE];
7976 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
7977 if (strcmp(cmdp->cmdname, name) == 0)
7981 if (add && cmdp == NULL) {
7982 cmdp = *pp = ckzalloc(sizeof(struct tblentry)
7984 /* + 1 - already done because
7985 * tblentry::cmdname is char[1] */);
7986 /*cmdp->next = NULL; - ckzalloc did it */
7987 cmdp->cmdtype = CMDUNKNOWN;
7988 strcpy(cmdp->cmdname, name);
7995 * Delete the command entry returned on the last lookup.
7998 delete_cmd_entry(void)
8000 struct tblentry *cmdp;
8003 cmdp = *lastcmdentry;
8004 *lastcmdentry = cmdp->next;
8005 if (cmdp->cmdtype == CMDFUNCTION)
8006 freefunc(cmdp->param.func);
8012 * Add a new command entry, replacing any existing command entry for
8013 * the same name - except special builtins.
8016 addcmdentry(char *name, struct cmdentry *entry)
8018 struct tblentry *cmdp;
8020 cmdp = cmdlookup(name, 1);
8021 if (cmdp->cmdtype == CMDFUNCTION) {
8022 freefunc(cmdp->param.func);
8024 cmdp->cmdtype = entry->cmdtype;
8025 cmdp->param = entry->u;
8029 static int FAST_FUNC
8030 hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8032 struct tblentry **pp;
8033 struct tblentry *cmdp;
8035 struct cmdentry entry;
8038 if (nextopt("r") != '\0') {
8043 if (*argptr == NULL) {
8044 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8045 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8046 if (cmdp->cmdtype == CMDNORMAL)
8054 while ((name = *argptr) != NULL) {
8055 cmdp = cmdlookup(name, 0);
8057 && (cmdp->cmdtype == CMDNORMAL
8058 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
8062 find_command(name, &entry, DO_ERR, pathval());
8063 if (entry.cmdtype == CMDUNKNOWN)
8071 * Called when a cd is done. Marks all commands so the next time they
8072 * are executed they will be rehashed.
8077 struct tblentry **pp;
8078 struct tblentry *cmdp;
8080 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8081 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8082 if (cmdp->cmdtype == CMDNORMAL
8083 || (cmdp->cmdtype == CMDBUILTIN
8084 && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
8094 * Fix command hash table when PATH changed.
8095 * Called before PATH is changed. The argument is the new value of PATH;
8096 * pathval() still returns the old value at this point.
8097 * Called with interrupts off.
8099 static void FAST_FUNC
8100 changepath(const char *new)
8108 firstchange = 9999; /* assume no change */
8114 if ((*old == '\0' && *new == ':')
8115 || (*old == ':' && *new == '\0')
8119 old = new; /* ignore subsequent differences */
8123 if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin"))
8130 if (builtinloc < 0 && idx_bltin >= 0)
8131 builtinloc = idx_bltin; /* zap builtins */
8132 if (builtinloc >= 0 && idx_bltin < 0)
8134 clearcmdentry(firstchange);
8135 builtinloc = idx_bltin;
8171 typedef smallint token_id_t;
8173 /* Nth bit indicates if token marks the end of a list */
8176 /* 0 */ | (1u << TEOF)
8177 /* 1 */ | (0u << TNL)
8178 /* 2 */ | (0u << TREDIR)
8179 /* 3 */ | (0u << TWORD)
8180 /* 4 */ | (0u << TSEMI)
8181 /* 5 */ | (0u << TBACKGND)
8182 /* 6 */ | (0u << TAND)
8183 /* 7 */ | (0u << TOR)
8184 /* 8 */ | (0u << TPIPE)
8185 /* 9 */ | (0u << TLP)
8186 /* 10 */ | (1u << TRP)
8187 /* 11 */ | (1u << TENDCASE)
8188 /* 12 */ | (1u << TENDBQUOTE)
8189 /* 13 */ | (0u << TNOT)
8190 /* 14 */ | (0u << TCASE)
8191 /* 15 */ | (1u << TDO)
8192 /* 16 */ | (1u << TDONE)
8193 /* 17 */ | (1u << TELIF)
8194 /* 18 */ | (1u << TELSE)
8195 /* 19 */ | (1u << TESAC)
8196 /* 20 */ | (1u << TFI)
8197 /* 21 */ | (0u << TFOR)
8199 /* 22 */ | (0u << TFUNCTION)
8201 /* 23 */ | (0u << TIF)
8202 /* 24 */ | (0u << TIN)
8203 /* 25 */ | (1u << TTHEN)
8204 /* 26 */ | (0u << TUNTIL)
8205 /* 27 */ | (0u << TWHILE)
8206 /* 28 */ | (0u << TBEGIN)
8207 /* 29 */ | (1u << TEND)
8208 , /* thus far 29 bits used */
8211 static const char *const tokname_array[] = {
8225 #define KWDOFFSET 13
8226 /* the following are keywords */
8248 /* Wrapper around strcmp for qsort/bsearch/... */
8250 pstrcmp(const void *a, const void *b)
8252 return strcmp((char*)a, *(char**)b);
8255 static const char *const *
8256 findkwd(const char *s)
8258 return bsearch(s, tokname_array + KWDOFFSET,
8259 ARRAY_SIZE(tokname_array) - KWDOFFSET,
8260 sizeof(tokname_array[0]), pstrcmp);
8264 * Locate and print what a word is...
8267 describe_command(char *command, const char *path, int describe_command_verbose)
8269 struct cmdentry entry;
8270 #if ENABLE_ASH_ALIAS
8271 const struct alias *ap;
8274 path = path ? path : pathval();
8276 if (describe_command_verbose) {
8280 /* First look at the keywords */
8281 if (findkwd(command)) {
8282 out1str(describe_command_verbose ? " is a shell keyword" : command);
8286 #if ENABLE_ASH_ALIAS
8287 /* Then look at the aliases */
8288 ap = lookupalias(command, 0);
8290 if (!describe_command_verbose) {
8295 out1fmt(" is an alias for %s", ap->val);
8300 find_command(command, &entry, DO_ABS, path);
8302 switch (entry.cmdtype) {
8304 int j = entry.u.index;
8310 p = path_advance(&path, command);
8314 if (describe_command_verbose) {
8315 out1fmt(" is %s", p);
8323 if (describe_command_verbose) {
8324 out1str(" is a shell function");
8331 if (describe_command_verbose) {
8332 out1fmt(" is a %sshell builtin",
8333 IS_BUILTIN_SPECIAL(entry.u.cmd) ?
8334 "special " : nullstr
8342 if (describe_command_verbose) {
8343 out1str(": not found\n");
8352 static int FAST_FUNC
8353 typecmd(int argc UNUSED_PARAM, char **argv)
8359 /* type -p ... ? (we don't bother checking for 'p') */
8360 if (argv[1] && argv[1][0] == '-') {
8365 err |= describe_command(argv[i++], NULL, verbose);
8370 #if ENABLE_ASH_CMDCMD
8371 /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */
8373 parse_command_args(char **argv, const char **path)
8386 if (c == '-' && !*cp) {
8394 *path = bb_default_path;
8397 /* run 'typecmd' for other options */
8406 static int FAST_FUNC
8407 commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8415 const char *path = NULL;
8417 /* "command [-p] PROG ARGS" (that is, without -V or -v)
8418 * never reaches this function.
8421 while ((c = nextopt("pvV")) != '\0')
8423 verify |= VERIFY_VERBOSE;
8425 /*verify |= VERIFY_BRIEF*/;
8431 path = bb_default_path;
8433 /* Mimic bash: just "command -v" doesn't complain, it's a nop */
8435 if (/*verify && */ cmd)
8436 return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/);
8443 /*static int funcblocksize; // size of structures in function */
8444 /*static int funcstringsize; // size of strings in node */
8445 static void *funcblock; /* block to allocate function from */
8446 static char *funcstring_end; /* end of block to allocate strings from */
8448 static const uint8_t nodesize[N_NUMBER] ALIGN1 = {
8449 [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)),
8450 [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)),
8451 [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)),
8452 [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)),
8453 [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)),
8454 [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)),
8455 [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)),
8456 [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)),
8457 [NIF ] = SHELL_ALIGN(sizeof(struct nif)),
8458 [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)),
8459 [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)),
8460 [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)),
8461 [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)),
8462 [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)),
8463 [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)),
8464 [NARG ] = SHELL_ALIGN(sizeof(struct narg)),
8465 [NTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8466 #if BASH_REDIR_OUTPUT
8467 [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)),
8469 [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)),
8470 [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)),
8471 [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8472 [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)),
8473 [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8474 [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8475 [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8476 [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8477 [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)),
8480 static int calcsize(int funcblocksize, union node *n);
8483 sizenodelist(int funcblocksize, struct nodelist *lp)
8486 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
8487 funcblocksize = calcsize(funcblocksize, lp->n);
8490 return funcblocksize;
8494 calcsize(int funcblocksize, union node *n)
8497 return funcblocksize;
8498 funcblocksize += nodesize[n->type];
8501 funcblocksize = calcsize(funcblocksize, n->ncmd.redirect);
8502 funcblocksize = calcsize(funcblocksize, n->ncmd.args);
8503 funcblocksize = calcsize(funcblocksize, n->ncmd.assign);
8506 funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist);
8511 funcblocksize = calcsize(funcblocksize, n->nredir.redirect);
8512 funcblocksize = calcsize(funcblocksize, n->nredir.n);
8519 funcblocksize = calcsize(funcblocksize, n->nbinary.ch2);
8520 funcblocksize = calcsize(funcblocksize, n->nbinary.ch1);
8523 funcblocksize = calcsize(funcblocksize, n->nif.elsepart);
8524 funcblocksize = calcsize(funcblocksize, n->nif.ifpart);
8525 funcblocksize = calcsize(funcblocksize, n->nif.test);
8528 funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */
8529 funcblocksize = calcsize(funcblocksize, n->nfor.body);
8530 funcblocksize = calcsize(funcblocksize, n->nfor.args);
8533 funcblocksize = calcsize(funcblocksize, n->ncase.cases);
8534 funcblocksize = calcsize(funcblocksize, n->ncase.expr);
8537 funcblocksize = calcsize(funcblocksize, n->nclist.body);
8538 funcblocksize = calcsize(funcblocksize, n->nclist.pattern);
8539 funcblocksize = calcsize(funcblocksize, n->nclist.next);
8543 funcblocksize = sizenodelist(funcblocksize, n->narg.backquote);
8544 funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */
8545 funcblocksize = calcsize(funcblocksize, n->narg.next);
8548 #if BASH_REDIR_OUTPUT
8555 funcblocksize = calcsize(funcblocksize, n->nfile.fname);
8556 funcblocksize = calcsize(funcblocksize, n->nfile.next);
8560 funcblocksize = calcsize(funcblocksize, n->ndup.vname);
8561 funcblocksize = calcsize(funcblocksize, n->ndup.next);
8565 funcblocksize = calcsize(funcblocksize, n->nhere.doc);
8566 funcblocksize = calcsize(funcblocksize, n->nhere.next);
8569 funcblocksize = calcsize(funcblocksize, n->nnot.com);
8572 return funcblocksize;
8576 nodeckstrdup(char *s)
8578 funcstring_end -= SHELL_ALIGN(strlen(s) + 1);
8579 return strcpy(funcstring_end, s);
8582 static union node *copynode(union node *);
8584 static struct nodelist *
8585 copynodelist(struct nodelist *lp)
8587 struct nodelist *start;
8588 struct nodelist **lpp;
8593 funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist));
8594 (*lpp)->n = copynode(lp->n);
8596 lpp = &(*lpp)->next;
8603 copynode(union node *n)
8610 funcblock = (char *) funcblock + nodesize[n->type];
8614 new->ncmd.redirect = copynode(n->ncmd.redirect);
8615 new->ncmd.args = copynode(n->ncmd.args);
8616 new->ncmd.assign = copynode(n->ncmd.assign);
8619 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
8620 new->npipe.pipe_backgnd = n->npipe.pipe_backgnd;
8625 new->nredir.redirect = copynode(n->nredir.redirect);
8626 new->nredir.n = copynode(n->nredir.n);
8633 new->nbinary.ch2 = copynode(n->nbinary.ch2);
8634 new->nbinary.ch1 = copynode(n->nbinary.ch1);
8637 new->nif.elsepart = copynode(n->nif.elsepart);
8638 new->nif.ifpart = copynode(n->nif.ifpart);
8639 new->nif.test = copynode(n->nif.test);
8642 new->nfor.var = nodeckstrdup(n->nfor.var);
8643 new->nfor.body = copynode(n->nfor.body);
8644 new->nfor.args = copynode(n->nfor.args);
8647 new->ncase.cases = copynode(n->ncase.cases);
8648 new->ncase.expr = copynode(n->ncase.expr);
8651 new->nclist.body = copynode(n->nclist.body);
8652 new->nclist.pattern = copynode(n->nclist.pattern);
8653 new->nclist.next = copynode(n->nclist.next);
8657 new->narg.backquote = copynodelist(n->narg.backquote);
8658 new->narg.text = nodeckstrdup(n->narg.text);
8659 new->narg.next = copynode(n->narg.next);
8662 #if BASH_REDIR_OUTPUT
8669 new->nfile.fname = copynode(n->nfile.fname);
8670 new->nfile.fd = n->nfile.fd;
8671 new->nfile.next = copynode(n->nfile.next);
8675 new->ndup.vname = copynode(n->ndup.vname);
8676 new->ndup.dupfd = n->ndup.dupfd;
8677 new->ndup.fd = n->ndup.fd;
8678 new->ndup.next = copynode(n->ndup.next);
8682 new->nhere.doc = copynode(n->nhere.doc);
8683 new->nhere.fd = n->nhere.fd;
8684 new->nhere.next = copynode(n->nhere.next);
8687 new->nnot.com = copynode(n->nnot.com);
8690 new->type = n->type;
8695 * Make a copy of a parse tree.
8697 static struct funcnode *
8698 copyfunc(union node *n)
8703 /*funcstringsize = 0;*/
8704 blocksize = offsetof(struct funcnode, n) + calcsize(0, n);
8705 f = ckzalloc(blocksize /* + funcstringsize */);
8706 funcblock = (char *) f + offsetof(struct funcnode, n);
8707 funcstring_end = (char *) f + blocksize;
8709 /* f->count = 0; - ckzalloc did it */
8714 * Define a shell function.
8717 defun(union node *func)
8719 struct cmdentry entry;
8722 entry.cmdtype = CMDFUNCTION;
8723 entry.u.func = copyfunc(func);
8724 addcmdentry(func->narg.text, &entry);
8728 /* Reasons for skipping commands (see comment on breakcmd routine) */
8729 #define SKIPBREAK (1 << 0)
8730 #define SKIPCONT (1 << 1)
8731 #define SKIPFUNC (1 << 2)
8732 static smallint evalskip; /* set to SKIPxxx if we are skipping commands */
8733 static int skipcount; /* number of levels to skip */
8734 static int funcnest; /* depth of function calls */
8735 static int loopnest; /* current loop nesting level */
8737 /* Forward decl way out to parsing code - dotrap needs it */
8738 static int evalstring(char *s, int flags);
8740 /* Called to execute a trap.
8741 * Single callsite - at the end of evaltree().
8742 * If we return non-zero, evaltree raises EXEXIT exception.
8744 * Perhaps we should avoid entering new trap handlers
8745 * while we are executing a trap handler. [is it a TODO?]
8752 uint8_t last_status;
8757 last_status = exitstatus;
8761 TRACE(("dotrap entered\n"));
8762 for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) {
8774 /* non-trapped SIGINT is handled separately by raise_interrupt,
8775 * don't upset it by resetting gotsig[SIGINT-1] */
8776 if (sig == SIGINT && !p)
8779 TRACE(("sig %d is active, will run handler '%s'\n", sig, p));
8785 exitstatus = last_status;
8786 TRACE(("dotrap returns\n"));
8789 /* forward declarations - evaluation is fairly recursive business... */
8790 static int evalloop(union node *, int);
8791 static int evalfor(union node *, int);
8792 static int evalcase(union node *, int);
8793 static int evalsubshell(union node *, int);
8794 static void expredir(union node *);
8795 static int evalpipe(union node *, int);
8796 static int evalcommand(union node *, int);
8797 static int evalbltin(const struct builtincmd *, int, char **, int);
8798 static void prehash(union node *);
8801 * Evaluate a parse tree. The value is left in the global variable
8805 evaltree(union node *n, int flags)
8808 int (*evalfn)(union node *, int);
8812 TRACE(("evaltree(NULL) called\n"));
8815 TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags));
8822 out1fmt("Node type = %d\n", n->type);
8827 status = !evaltree(n->nnot.com, EV_TESTED);
8830 expredir(n->nredir.redirect);
8831 pushredir(n->nredir.redirect);
8832 status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
8834 status = evaltree(n->nredir.n, flags & EV_TESTED);
8836 if (n->nredir.redirect)
8837 popredir(/*drop:*/ 0);
8840 evalfn = evalcommand;
8842 if (eflag && !(flags & EV_TESTED))
8854 evalfn = evalsubshell;
8867 #error NAND + 1 != NOR
8869 #if NOR + 1 != NSEMI
8870 #error NOR + 1 != NSEMI
8872 unsigned is_or = n->type - NAND;
8875 (flags | ((is_or >> 1) - 1)) & EV_TESTED
8877 if ((!status) == is_or || evalskip)
8883 status = evalfn(n, flags);
8887 status = evaltree(n->nif.test, EV_TESTED);
8894 if (n->nif.elsepart) {
8895 n = n->nif.elsepart;
8902 /* Not necessary. To test it:
8903 * "false; f() { qwerty; }; echo $?" should print 0.
8907 exitstatus = status;
8911 /* Order of checks below is important:
8912 * signal handlers trigger before exit caused by "set -e".
8916 if (checkexit & status)
8917 raise_exception(EXEXIT);
8918 if (flags & EV_EXIT)
8919 raise_exception(EXEXIT);
8921 TRACE(("leaving evaltree (no interrupts)\n"));
8928 int skip = evalskip;
8935 if (--skipcount <= 0) {
8946 evalloop(union node *n, int flags)
8957 i = evaltree(n->nbinary.ch1, EV_TESTED);
8959 if (skip == SKIPFUNC)
8963 if (n->type != NWHILE)
8967 status = evaltree(n->nbinary.ch2, flags);
8969 } while (!(skip & ~SKIPCONT));
8976 evalfor(union node *n, int flags)
8978 struct arglist arglist;
8981 struct stackmark smark;
8984 setstackmark(&smark);
8985 arglist.list = NULL;
8986 arglist.lastp = &arglist.list;
8987 for (argp = n->nfor.args; argp; argp = argp->narg.next) {
8988 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
8990 *arglist.lastp = NULL;
8994 for (sp = arglist.list; sp; sp = sp->next) {
8995 setvar0(n->nfor.var, sp->text);
8996 status = evaltree(n->nfor.body, flags);
8997 if (skiploop() & ~SKIPCONT)
9001 popstackmark(&smark);
9007 evalcase(union node *n, int flags)
9011 struct arglist arglist;
9012 struct stackmark smark;
9015 setstackmark(&smark);
9016 arglist.list = NULL;
9017 arglist.lastp = &arglist.list;
9018 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
9019 for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
9020 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
9021 if (casematch(patp, arglist.list->text)) {
9022 /* Ensure body is non-empty as otherwise
9023 * EV_EXIT may prevent us from setting the
9026 if (evalskip == 0 && cp->nclist.body) {
9027 status = evaltree(cp->nclist.body, flags);
9034 popstackmark(&smark);
9040 * Kick off a subshell to evaluate a tree.
9043 evalsubshell(union node *n, int flags)
9046 int backgnd = (n->type == NBACKGND); /* FORK_BG(1) if yes, else FORK_FG(0) */
9049 expredir(n->nredir.redirect);
9050 if (!backgnd && (flags & EV_EXIT) && !may_have_traps)
9053 if (backgnd == FORK_FG)
9055 jp = makejob(/*n,*/ 1);
9056 if (forkshell(jp, n, backgnd) == 0) {
9061 flags &= ~EV_TESTED;
9063 redirect(n->nredir.redirect, 0);
9064 evaltreenr(n->nredir.n, flags);
9069 if (backgnd == FORK_FG)
9070 status = waitforjob(jp);
9076 * Compute the names of the files in a redirection list.
9078 static void fixredir(union node *, const char *, int);
9080 expredir(union node *n)
9084 for (redir = n; redir; redir = redir->nfile.next) {
9088 fn.lastp = &fn.list;
9089 switch (redir->type) {
9093 #if BASH_REDIR_OUTPUT
9098 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
9099 TRACE(("expredir expanded to '%s'\n", fn.list->text));
9100 #if BASH_REDIR_OUTPUT
9104 // By the design of stack allocator, the loop of this kind:
9105 // while true; do while true; do break; done </dev/null; done
9106 // will look like a memory leak: ash plans to free expfname's
9107 // of "/dev/null" as soon as it finishes running the loop
9108 // (in this case, never).
9109 // This "fix" is wrong:
9110 if (redir->nfile.expfname)
9111 stunalloc(redir->nfile.expfname);
9112 // It results in corrupted state of stacked allocations.
9114 redir->nfile.expfname = fn.list->text;
9117 case NTOFD: /* >& */
9118 if (redir->ndup.vname) {
9119 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
9120 if (fn.list == NULL)
9121 ash_msg_and_raise_error("redir error");
9122 #if BASH_REDIR_OUTPUT
9123 //FIXME: we used expandarg with different args!
9124 if (!isdigit_str9(fn.list->text)) {
9125 /* >&file, not >&fd */
9126 if (redir->nfile.fd != 1) /* 123>&file - BAD */
9127 ash_msg_and_raise_error("redir error");
9129 goto store_expfname;
9132 fixredir(redir, fn.list->text, 1);
9140 * Evaluate a pipeline. All the processes in the pipeline are children
9141 * of the process creating the pipeline. (This differs from some versions
9142 * of the shell, which make the last process in a pipeline the parent
9146 evalpipe(union node *n, int flags)
9149 struct nodelist *lp;
9155 TRACE(("evalpipe(0x%lx) called\n", (long)n));
9157 for (lp = n->npipe.cmdlist; lp; lp = lp->next)
9161 if (n->npipe.pipe_backgnd == 0)
9163 jp = makejob(/*n,*/ pipelen);
9165 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
9169 if (pipe(pip) < 0) {
9171 ash_msg_and_raise_error("pipe call failed");
9174 if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) {
9188 evaltreenr(lp->n, flags);
9195 /* Don't want to trigger debugging */
9199 if (n->npipe.pipe_backgnd == 0) {
9200 status = waitforjob(jp);
9201 TRACE(("evalpipe: job done exit status %d\n", status));
9209 * Controls whether the shell is interactive or not.
9212 setinteractive(int on)
9214 static smallint is_interactive;
9216 if (++on == is_interactive)
9218 is_interactive = on;
9222 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
9223 if (is_interactive > 1) {
9224 /* Looks like they want an interactive shell */
9225 static smallint did_banner;
9228 /* note: ash and hush share this string */
9229 out1fmt("\n\n%s %s\n"
9230 IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n")
9233 "built-in shell (ash)"
9247 setinteractive(iflag);
9249 #if ENABLE_FEATURE_EDITING_VI
9251 line_input_state->flags |= VI_MODE;
9253 line_input_state->flags &= ~VI_MODE;
9255 viflag = 0; /* forcibly keep the option off */
9259 struct localvar_list {
9260 struct localvar_list *next;
9261 struct localvar *lv;
9264 static struct localvar_list *localvar_stack;
9267 * Called after a function returns.
9268 * Interrupts must be off.
9271 poplocalvars(int keep)
9273 struct localvar_list *ll;
9274 struct localvar *lvp, *next;
9278 ll = localvar_stack;
9279 localvar_stack = ll->next;
9284 while ((lvp = next) != NULL) {
9287 TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-"));
9289 int bits = VSTRFIXED;
9291 if (lvp->flags != VUNSET) {
9292 if (vp->var_text == lvp->text)
9294 else if (!(lvp->flags & (VTEXTFIXED|VSTACK)))
9295 free((char*)lvp->text);
9299 vp->flags |= (lvp->flags & bits);
9302 (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
9303 unsetvar(vp->var_text);
9304 } else if (vp == NULL) { /* $- saved */
9305 memcpy(optlist, lvp->text, sizeof(optlist));
9306 free((char*)lvp->text);
9308 } else if (lvp->flags == VUNSET) {
9309 vp->flags &= ~(VSTRFIXED|VREADONLY);
9310 unsetvar(vp->var_text);
9313 vp->var_func(var_end(lvp->text));
9314 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
9315 free((char*)vp->var_text);
9316 vp->flags = lvp->flags;
9317 vp->var_text = lvp->text;
9325 * Create a new localvar environment.
9327 static struct localvar_list *
9330 struct localvar_list *ll;
9333 ll = ckzalloc(sizeof(*ll));
9334 /*ll->lv = NULL; - zalloc did it */
9335 ll->next = localvar_stack;
9336 localvar_stack = ll;
9343 unwindlocalvars(struct localvar_list *stop)
9345 while (localvar_stack != stop)
9350 evalfun(struct funcnode *func, int argc, char **argv, int flags)
9352 volatile struct shparam saveparam;
9353 struct jmploc *volatile savehandler;
9354 struct jmploc jmploc;
9357 saveparam = shellparam;
9358 savehandler = exception_handler;
9359 e = setjmp(jmploc.loc);
9364 exception_handler = &jmploc;
9365 shellparam.malloced = 0;
9369 shellparam.nparam = argc - 1;
9370 shellparam.p = argv + 1;
9371 #if ENABLE_ASH_GETOPTS
9372 shellparam.optind = 1;
9373 shellparam.optoff = -1;
9376 evaltree(func->n.narg.next, flags & EV_TESTED);
9382 freeparam(&shellparam);
9383 shellparam = saveparam;
9384 exception_handler = savehandler;
9386 evalskip &= ~SKIPFUNC;
9391 * Make a variable a local variable. When a variable is made local, it's
9392 * value and flags are saved in a localvar structure. The saved values
9393 * will be restored when the shell function returns. We handle the name
9394 * "-" as a special case: it makes changes to "set +-options" local
9395 * (options will be restored on return from the function).
9400 struct localvar *lvp;
9403 char *eq = strchr(name, '=');
9406 /* Cater for duplicate "local". Examples:
9407 * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x
9408 * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x
9410 lvp = localvar_stack->lv;
9412 if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) {
9416 * it's a duplicate "local VAR" declaration, do nothing
9423 lvp = ckzalloc(sizeof(*lvp));
9424 if (LONE_DASH(name)) {
9426 p = ckmalloc(sizeof(optlist));
9427 lvp->text = memcpy(p, optlist, sizeof(optlist));
9430 vpp = hashvar(name);
9431 vp = *findvar(vpp, name);
9433 /* variable did not exist yet */
9435 vp = setvareq(name, VSTRFIXED);
9437 vp = setvar(name, NULL, VSTRFIXED);
9438 lvp->flags = VUNSET;
9440 lvp->text = vp->var_text;
9441 lvp->flags = vp->flags;
9442 /* make sure neither "struct var" nor string gets freed
9443 * during (un)setting:
9445 vp->flags |= VSTRFIXED|VTEXTFIXED;
9449 /* "local VAR" unsets VAR: */
9450 setvar0(name, NULL);
9454 lvp->next = localvar_stack->lv;
9455 localvar_stack->lv = lvp;
9461 * The "local" command.
9463 static int FAST_FUNC
9464 localcmd(int argc UNUSED_PARAM, char **argv)
9468 if (!localvar_stack)
9469 ash_msg_and_raise_error("not in a function");
9472 while ((name = *argv++) != NULL) {
9478 static int FAST_FUNC
9479 falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9484 static int FAST_FUNC
9485 truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9490 static int FAST_FUNC
9491 execcmd(int argc UNUSED_PARAM, char **argv)
9494 while (nextopt("a:") != '\0')
9495 /* nextopt() sets optionarg to "-a ARGV0" */;
9501 iflag = 0; /* exit on error */
9504 /* We should set up signals for "exec CMD"
9505 * the same way as for "CMD" without "exec".
9506 * But optschanged->setinteractive->setsignal
9507 * still thought we are a root shell. Therefore, for example,
9508 * SIGQUIT is still set to IGN. Fix it:
9512 /*setsignal(SIGTERM); - unnecessary because of iflag=0 */
9513 /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */
9514 /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */
9518 argv[0] = optionarg;
9519 shellexec(prog, argv, pathval(), 0);
9526 * The return command.
9528 static int FAST_FUNC
9529 returncmd(int argc UNUSED_PARAM, char **argv)
9532 * If called outside a function, do what ksh does;
9533 * skip the rest of the file.
9535 evalskip = SKIPFUNC;
9536 return argv[1] ? number(argv[1]) : exitstatus;
9539 /* Forward declarations for builtintab[] */
9540 static int breakcmd(int, char **) FAST_FUNC;
9541 static int dotcmd(int, char **) FAST_FUNC;
9542 static int evalcmd(int, char **, int) FAST_FUNC;
9543 static int exitcmd(int, char **) FAST_FUNC;
9544 static int exportcmd(int, char **) FAST_FUNC;
9545 #if ENABLE_ASH_GETOPTS
9546 static int getoptscmd(int, char **) FAST_FUNC;
9549 static int helpcmd(int, char **) FAST_FUNC;
9552 static int historycmd(int, char **) FAST_FUNC;
9554 #if ENABLE_FEATURE_SH_MATH
9555 static int letcmd(int, char **) FAST_FUNC;
9557 static int readcmd(int, char **) FAST_FUNC;
9558 static int setcmd(int, char **) FAST_FUNC;
9559 static int shiftcmd(int, char **) FAST_FUNC;
9560 static int timescmd(int, char **) FAST_FUNC;
9561 static int trapcmd(int, char **) FAST_FUNC;
9562 static int umaskcmd(int, char **) FAST_FUNC;
9563 static int unsetcmd(int, char **) FAST_FUNC;
9564 static int ulimitcmd(int, char **) FAST_FUNC;
9566 #define BUILTIN_NOSPEC "0"
9567 #define BUILTIN_SPECIAL "1"
9568 #define BUILTIN_REGULAR "2"
9569 #define BUILTIN_SPEC_REG "3"
9570 #define BUILTIN_ASSIGN "4"
9571 #define BUILTIN_SPEC_ASSG "5"
9572 #define BUILTIN_REG_ASSG "6"
9573 #define BUILTIN_SPEC_REG_ASSG "7"
9575 /* Stubs for calling non-FAST_FUNC's */
9577 static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); }
9579 #if ENABLE_ASH_PRINTF
9580 static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); }
9582 #if ENABLE_ASH_TEST || BASH_TEST2
9583 static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); }
9586 /* Keep these in proper order since it is searched via bsearch() */
9587 static const struct builtincmd builtintab[] = {
9588 { BUILTIN_SPEC_REG "." , dotcmd },
9589 { BUILTIN_SPEC_REG ":" , truecmd },
9591 { BUILTIN_REGULAR "[" , testcmd },
9594 { BUILTIN_REGULAR "[[" , testcmd },
9596 #if ENABLE_ASH_ALIAS
9597 { BUILTIN_REG_ASSG "alias" , aliascmd },
9600 { BUILTIN_REGULAR "bg" , fg_bgcmd },
9602 { BUILTIN_SPEC_REG "break" , breakcmd },
9603 { BUILTIN_REGULAR "cd" , cdcmd },
9604 { BUILTIN_NOSPEC "chdir" , cdcmd },
9605 #if ENABLE_ASH_CMDCMD
9606 { BUILTIN_REGULAR "command" , commandcmd },
9608 { BUILTIN_SPEC_REG "continue", breakcmd },
9610 { BUILTIN_REGULAR "echo" , echocmd },
9612 { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/
9613 { BUILTIN_SPEC_REG "exec" , execcmd },
9614 { BUILTIN_SPEC_REG "exit" , exitcmd },
9615 { BUILTIN_SPEC_REG_ASSG "export" , exportcmd },
9616 { BUILTIN_REGULAR "false" , falsecmd },
9618 { BUILTIN_REGULAR "fg" , fg_bgcmd },
9620 #if ENABLE_ASH_GETOPTS
9621 { BUILTIN_REGULAR "getopts" , getoptscmd },
9623 { BUILTIN_NOSPEC "hash" , hashcmd },
9625 { BUILTIN_NOSPEC "help" , helpcmd },
9628 { BUILTIN_NOSPEC "history" , historycmd },
9631 { BUILTIN_REGULAR "jobs" , jobscmd },
9632 { BUILTIN_REGULAR "kill" , killcmd },
9634 #if ENABLE_FEATURE_SH_MATH
9635 { BUILTIN_NOSPEC "let" , letcmd },
9637 { BUILTIN_SPEC_REG_ASSG "local" , localcmd },
9638 #if ENABLE_ASH_PRINTF
9639 { BUILTIN_REGULAR "printf" , printfcmd },
9641 { BUILTIN_NOSPEC "pwd" , pwdcmd },
9642 { BUILTIN_REGULAR "read" , readcmd },
9643 { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
9644 { BUILTIN_SPEC_REG "return" , returncmd },
9645 { BUILTIN_SPEC_REG "set" , setcmd },
9646 { BUILTIN_SPEC_REG "shift" , shiftcmd },
9648 { BUILTIN_SPEC_REG "source" , dotcmd },
9651 { BUILTIN_REGULAR "test" , testcmd },
9653 { BUILTIN_SPEC_REG "times" , timescmd },
9654 { BUILTIN_SPEC_REG "trap" , trapcmd },
9655 { BUILTIN_REGULAR "true" , truecmd },
9656 { BUILTIN_NOSPEC "type" , typecmd },
9657 { BUILTIN_NOSPEC "ulimit" , ulimitcmd },
9658 { BUILTIN_REGULAR "umask" , umaskcmd },
9659 #if ENABLE_ASH_ALIAS
9660 { BUILTIN_REGULAR "unalias" , unaliascmd },
9662 { BUILTIN_SPEC_REG "unset" , unsetcmd },
9663 { BUILTIN_REGULAR "wait" , waitcmd },
9666 /* Should match the above table! */
9667 #define COMMANDCMD (builtintab + \
9669 /* [ */ 1 * ENABLE_ASH_TEST + \
9670 /* [[ */ 1 * BASH_TEST2 + \
9671 /* alias */ 1 * ENABLE_ASH_ALIAS + \
9672 /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \
9673 /* break cd cddir */ 3)
9674 #define EVALCMD (COMMANDCMD + \
9675 /* command */ 1 * ENABLE_ASH_CMDCMD + \
9676 /* continue */ 1 + \
9677 /* echo */ 1 * ENABLE_ASH_ECHO + \
9679 #define EXECCMD (EVALCMD + \
9683 * Search the table of builtin commands.
9686 pstrcmp1(const void *a, const void *b)
9688 return strcmp((char*)a, *(char**)b + 1);
9690 static struct builtincmd *
9691 find_builtin(const char *name)
9693 struct builtincmd *bp;
9696 name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]),
9703 * Execute a simple command.
9706 isassignment(const char *p)
9708 const char *q = endofname(p);
9713 static int FAST_FUNC
9714 bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9716 /* Preserve exitstatus of a previous possible redirection
9717 * as POSIX mandates */
9718 return back_exitstatus;
9721 evalcommand(union node *cmd, int flags)
9723 static const struct builtincmd null_bltin = {
9724 "\0\0", bltincmd /* why three NULs? */
9726 struct localvar_list *localvar_stop;
9727 struct redirtab *redir_stop;
9728 struct stackmark smark;
9730 struct arglist arglist;
9731 struct arglist varlist;
9734 const struct strlist *sp;
9735 struct cmdentry cmdentry;
9742 smallint cmd_is_exec;
9744 /* First expand the arguments. */
9745 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
9746 setstackmark(&smark);
9747 localvar_stop = pushlocalvars();
9748 back_exitstatus = 0;
9750 cmdentry.cmdtype = CMDBUILTIN;
9751 cmdentry.u.cmd = &null_bltin;
9752 varlist.lastp = &varlist.list;
9753 *varlist.lastp = NULL;
9754 arglist.lastp = &arglist.list;
9755 *arglist.lastp = NULL;
9758 if (cmd->ncmd.args) {
9759 struct builtincmd *bcmd;
9760 smallint pseudovarflag;
9762 bcmd = find_builtin(cmd->ncmd.args->narg.text);
9763 pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd);
9765 for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) {
9766 struct strlist **spp;
9768 spp = arglist.lastp;
9769 if (pseudovarflag && isassignment(argp->narg.text))
9770 expandarg(argp, &arglist, EXP_VARTILDE);
9772 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
9774 for (sp = *spp; sp; sp = sp->next)
9779 /* Reserve one extra spot at the front for shellexec. */
9780 nargv = stalloc(sizeof(char *) * (argc + 2));
9782 for (sp = arglist.list; sp; sp = sp->next) {
9783 TRACE(("evalcommand arg: %s\n", sp->text));
9784 *nargv++ = sp->text;
9789 if (iflag && funcnest == 0 && argc > 0)
9790 lastarg = nargv[-1];
9792 expredir(cmd->ncmd.redirect);
9793 redir_stop = pushredir(cmd->ncmd.redirect);
9795 status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2);
9797 path = vpath.var_text;
9798 for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
9799 struct strlist **spp;
9802 spp = varlist.lastp;
9803 expandarg(argp, &varlist, EXP_VARTILDE);
9805 mklocal((*spp)->text);
9808 * Modify the command lookup path, if a PATH= assignment
9812 if (varcmp(p, path) == 0)
9816 /* Print the command if xflag is set. */
9818 const char *pfx = "";
9820 fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX));
9824 char *varval = sp->text;
9825 char *eq = strchrnul(varval, '=');
9828 fdprintf(preverrout_fd, "%s%.*s%s",
9830 (int)(eq - varval), varval,
9831 maybe_single_quote(eq)
9839 fdprintf(preverrout_fd, "%s%s",
9841 /* always quote if matches reserved word: */
9843 ? single_quote(sp->text)
9844 : maybe_single_quote(sp->text)
9849 safe_write(preverrout_fd, "\n", 1);
9855 /* Now locate the command. */
9857 int cmd_flag = DO_ERR;
9858 #if ENABLE_ASH_CMDCMD
9859 const char *oldpath = path + 5;
9863 find_command(argv[0], &cmdentry, cmd_flag, path);
9864 if (cmdentry.cmdtype == CMDUNKNOWN) {
9865 flush_stdout_stderr();
9870 /* implement bltin and command here */
9871 if (cmdentry.cmdtype != CMDBUILTIN)
9874 spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd);
9875 if (cmdentry.u.cmd == EXECCMD)
9877 #if ENABLE_ASH_CMDCMD
9878 if (cmdentry.u.cmd == COMMANDCMD) {
9880 nargv = parse_command_args(argv, &path);
9883 /* It's "command [-p] PROG ARGS" (that is, no -Vv).
9884 * nargv => "PROG". path is updated if -p.
9886 argc -= nargv - argv;
9888 cmd_flag |= DO_NOFUNC;
9897 exitstatus = status;
9899 /* We have a redirection error. */
9901 raise_exception(EXERROR);
9906 /* Execute the command. */
9907 switch (cmdentry.cmdtype) {
9910 #if ENABLE_FEATURE_SH_NOFORK
9911 /* (1) BUG: if variables are set, we need to fork, or save/restore them
9912 * around run_nofork_applet() call.
9913 * (2) Should this check also be done in forkshell()?
9914 * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...)
9916 /* find_command() encodes applet_no as (-2 - applet_no) */
9917 int applet_no = (- cmdentry.u.index - 2);
9918 if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
9919 listsetvar(varlist.list, VEXPORT|VSTACK);
9920 /* run <applet>_main() */
9921 status = run_nofork_applet(applet_no, argv);
9925 /* Can we avoid forking off? For example, very last command
9926 * in a script or a subshell does not need forking,
9927 * we can just exec it.
9929 if (!(flags & EV_EXIT) || may_have_traps) {
9930 /* No, forking off a child is necessary */
9933 jp = makejob(/*cmd,*/ 1);
9934 if (forkshell(jp, cmd, FORK_FG) != 0) {
9936 status = waitforjob(jp);
9938 TRACE(("forked child exited with %d\n", status));
9943 /* fall through to exec'ing external program */
9945 listsetvar(varlist.list, VEXPORT|VSTACK);
9946 shellexec(argv[0], argv, path, cmdentry.u.index);
9950 if (spclbltin > 0 || argc == 0) {
9952 if (cmd_is_exec && argc > 1)
9953 listsetvar(varlist.list, VEXPORT);
9956 /* Tight loop with builtins only:
9957 * "while kill -0 $child; do true; done"
9958 * will never exit even if $child died, unless we do this
9959 * to reap the zombie and make kill detect that it's gone: */
9960 dowait(DOWAIT_NONBLOCK, NULL);
9962 if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
9963 if (exception_type == EXERROR && spclbltin <= 0) {
9968 longjmp(exception_handler->loc, 1);
9974 /* See above for the rationale */
9975 dowait(DOWAIT_NONBLOCK, NULL);
9976 if (evalfun(cmdentry.u.func, argc, argv, flags))
9979 status = exitstatus;
9984 if (cmd->ncmd.redirect)
9985 popredir(/*drop:*/ cmd_is_exec);
9986 unwindredir(redir_stop);
9987 unwindlocalvars(localvar_stop);
9989 /* dsl: I think this is intended to be used to support
9990 * '_' in 'vi' command mode during line editing...
9991 * However I implemented that within libedit itself.
9993 setvar0("_", lastarg);
9995 popstackmark(&smark);
10001 evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags)
10003 char *volatile savecmdname;
10004 struct jmploc *volatile savehandler;
10005 struct jmploc jmploc;
10009 savecmdname = commandname;
10010 savehandler = exception_handler;
10011 i = setjmp(jmploc.loc);
10014 exception_handler = &jmploc;
10015 commandname = argv[0];
10017 optptr = NULL; /* initialize nextopt */
10018 if (cmd == EVALCMD)
10019 status = evalcmd(argc, argv, flags);
10021 status = (*cmd->builtin)(argc, argv);
10022 flush_stdout_stderr();
10023 status |= ferror(stdout);
10024 exitstatus = status;
10027 commandname = savecmdname;
10028 exception_handler = savehandler;
10034 goodname(const char *p)
10036 return endofname(p)[0] == '\0';
10041 * Search for a command. This is called before we fork so that the
10042 * location of the command will be available in the parent as well as
10043 * the child. The check for "goodname" is an overly conservative
10044 * check that the name will not be subject to expansion.
10047 prehash(union node *n)
10049 struct cmdentry entry;
10051 if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text))
10052 find_command(n->ncmd.args->narg.text, &entry, 0, pathval());
10056 /* ============ Builtin commands
10058 * Builtin commands whose functions are closely tied to evaluation
10059 * are implemented here.
10063 * Handle break and continue commands. Break, continue, and return are
10064 * all handled by setting the evalskip flag. The evaluation routines
10065 * above all check this flag, and if it is set they start skipping
10066 * commands rather than executing them. The variable skipcount is
10067 * the number of loops to break/continue, or the number of function
10068 * levels to return. (The latter is always 1.) It should probably
10069 * be an error to break out of more loops than exist, but it isn't
10070 * in the standard shell so we don't make it one here.
10072 static int FAST_FUNC
10073 breakcmd(int argc UNUSED_PARAM, char **argv)
10075 int n = argv[1] ? number(argv[1]) : 1;
10078 ash_msg_and_raise_error(msg_illnum, argv[1]);
10082 evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK;
10090 * This implements the input routines used by the parser.
10094 INPUT_PUSH_FILE = 1,
10095 INPUT_NOFILE_OK = 2,
10098 static smallint checkkwd;
10099 /* values of checkkwd variable */
10100 #define CHKALIAS 0x1
10103 #define CHKEOFMARK 0x8
10106 * Push a string back onto the input at this current parsefile level.
10107 * We handle aliases this way.
10109 #if !ENABLE_ASH_ALIAS
10110 #define pushstring(s, ap) pushstring(s)
10113 pushstring(char *s, struct alias *ap)
10115 struct strpush *sp;
10120 if (g_parsefile->strpush) {
10121 sp = ckzalloc(sizeof(*sp));
10122 sp->prev = g_parsefile->strpush;
10124 sp = &(g_parsefile->basestrpush);
10126 g_parsefile->strpush = sp;
10127 sp->prev_string = g_parsefile->next_to_pgetc;
10128 sp->prev_left_in_line = g_parsefile->left_in_line;
10129 sp->unget = g_parsefile->unget;
10130 memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc));
10131 #if ENABLE_ASH_ALIAS
10134 ap->flag |= ALIASINUSE;
10138 g_parsefile->next_to_pgetc = s;
10139 g_parsefile->left_in_line = len;
10140 g_parsefile->unget = 0;
10147 struct strpush *sp = g_parsefile->strpush;
10150 #if ENABLE_ASH_ALIAS
10152 if (g_parsefile->next_to_pgetc[-1] == ' '
10153 || g_parsefile->next_to_pgetc[-1] == '\t'
10155 checkkwd |= CHKALIAS;
10157 if (sp->string != sp->ap->val) {
10160 sp->ap->flag &= ~ALIASINUSE;
10161 if (sp->ap->flag & ALIASDEAD) {
10162 unalias(sp->ap->name);
10166 g_parsefile->next_to_pgetc = sp->prev_string;
10167 g_parsefile->left_in_line = sp->prev_left_in_line;
10168 g_parsefile->unget = sp->unget;
10169 memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc));
10170 g_parsefile->strpush = sp->prev;
10171 if (sp != &(g_parsefile->basestrpush))
10180 char *buf = g_parsefile->buf;
10182 g_parsefile->next_to_pgetc = buf;
10183 #if ENABLE_FEATURE_EDITING
10185 if (!iflag || g_parsefile->pf_fd != STDIN_FILENO)
10186 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10189 # if ENABLE_ASH_IDLE_TIMEOUT
10191 const char *tmout_var = lookupvar("TMOUT");
10193 timeout = atoi(tmout_var) * 1000;
10199 # if ENABLE_FEATURE_TAB_COMPLETION
10200 line_input_state->path_lookup = pathval();
10202 reinit_unicode_for_ash();
10203 nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ, timeout);
10205 /* ^C pressed, "convert" to SIGINT */
10206 write(STDOUT_FILENO, "^C", 2);
10207 if (trap[SIGINT]) {
10213 exitstatus = 128 + SIGINT;
10219 /* Ctrl+D pressed */
10222 # if ENABLE_ASH_IDLE_TIMEOUT
10223 else if (errno == EAGAIN && timeout > 0) {
10224 puts("\007timed out waiting for input: auto-logout");
10231 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10234 #if 0 /* disabled: nonblock_immune_read() handles this problem */
10236 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
10237 int flags = fcntl(0, F_GETFL);
10238 if (flags >= 0 && (flags & O_NONBLOCK)) {
10239 flags &= ~O_NONBLOCK;
10240 if (fcntl(0, F_SETFL, flags) >= 0) {
10241 out2str("sh: turning off NDELAY mode\n");
10252 * Refill the input buffer and return the next input character:
10254 * 1) If a string was pushed back on the input, pop it;
10255 * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM)
10256 * or we are reading from a string so we can't refill the buffer,
10258 * 3) If there is more stuff in this buffer, use it else call read to fill it.
10259 * 4) Process input up to the next newline, deleting nul characters.
10261 //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__)
10262 #define pgetc_debug(...) ((void)0)
10263 static int pgetc(void);
10270 if (g_parsefile->strpush) {
10271 #if ENABLE_ASH_ALIAS
10272 if (g_parsefile->left_in_line == -1
10273 && g_parsefile->strpush->ap
10274 && g_parsefile->next_to_pgetc[-1] != ' '
10275 && g_parsefile->next_to_pgetc[-1] != '\t'
10277 pgetc_debug("preadbuffer PEOA");
10284 /* on both branches above g_parsefile->left_in_line < 0.
10285 * "pgetc" needs refilling.
10288 /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read",
10289 * pungetc() may increment it a few times.
10290 * Assuming it won't increment it to less than -90.
10292 if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) {
10293 pgetc_debug("preadbuffer PEOF1");
10294 /* even in failure keep left_in_line and next_to_pgetc
10295 * in lock step, for correct multi-layer pungetc.
10296 * left_in_line was decremented before preadbuffer(),
10297 * must inc next_to_pgetc: */
10298 g_parsefile->next_to_pgetc++;
10302 more = g_parsefile->left_in_buffer;
10304 flush_stdout_stderr();
10308 /* don't try reading again */
10309 g_parsefile->left_in_line = -99;
10310 pgetc_debug("preadbuffer PEOF2");
10311 g_parsefile->next_to_pgetc++;
10316 /* Find out where's the end of line.
10317 * Set g_parsefile->left_in_line
10318 * and g_parsefile->left_in_buffer acordingly.
10319 * NUL chars are deleted.
10321 q = g_parsefile->next_to_pgetc;
10329 memmove(q, q + 1, more);
10333 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10339 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10340 if (g_parsefile->left_in_line < 0)
10345 g_parsefile->left_in_buffer = more;
10350 out2str(g_parsefile->next_to_pgetc);
10354 pgetc_debug("preadbuffer at %d:%p'%s'",
10355 g_parsefile->left_in_line,
10356 g_parsefile->next_to_pgetc,
10357 g_parsefile->next_to_pgetc);
10358 return (unsigned char)*g_parsefile->next_to_pgetc++;
10364 g_parsefile->linno++;
10365 setprompt_if(doprompt, 2);
10370 g_parsefile->linno++;
10371 needprompt = doprompt;
10379 pgetc_debug("pgetc at %d:%p'%s'",
10380 g_parsefile->left_in_line,
10381 g_parsefile->next_to_pgetc,
10382 g_parsefile->next_to_pgetc);
10383 if (g_parsefile->unget)
10384 return g_parsefile->lastc[--g_parsefile->unget];
10386 if (--g_parsefile->left_in_line >= 0)
10387 c = (unsigned char)*g_parsefile->next_to_pgetc++;
10391 g_parsefile->lastc[1] = g_parsefile->lastc[0];
10392 g_parsefile->lastc[0] = c;
10397 #if ENABLE_ASH_ALIAS
10399 pgetc_without_PEOA(void)
10403 pgetc_debug("pgetc at %d:%p'%s'",
10404 g_parsefile->left_in_line,
10405 g_parsefile->next_to_pgetc,
10406 g_parsefile->next_to_pgetc);
10408 } while (c == PEOA);
10412 # define pgetc_without_PEOA() pgetc()
10416 * Undo a call to pgetc. Only two characters may be pushed back.
10417 * PEOF may be pushed back.
10422 g_parsefile->unget++;
10425 /* This one eats backslash+newline */
10431 while ((c = pgetc()) == '\\') {
10432 if (pgetc() != '\n') {
10444 * To handle the "." command, a stack of input files is used. Pushfile
10445 * adds a new entry to the stack and popfile restores the previous level.
10450 struct parsefile *pf;
10452 pf = ckzalloc(sizeof(*pf));
10453 pf->prev = g_parsefile;
10455 /*pf->strpush = NULL; - ckzalloc did it */
10456 /*pf->basestrpush.prev = NULL;*/
10464 struct parsefile *pf = g_parsefile;
10470 if (pf->pf_fd >= 0)
10473 while (pf->strpush)
10475 g_parsefile = pf->prev;
10481 * Return to top level.
10486 while (g_parsefile != &basepf)
10491 * Close the file(s) that the shell is reading commands from. Called
10492 * after a fork is done.
10498 if (g_parsefile->pf_fd > 0) {
10499 close(g_parsefile->pf_fd);
10500 g_parsefile->pf_fd = 0;
10505 * Like setinputfile, but takes an open file descriptor. Call this with
10509 setinputfd(int fd, int push)
10513 g_parsefile->buf = NULL;
10515 g_parsefile->pf_fd = fd;
10516 if (g_parsefile->buf == NULL)
10517 g_parsefile->buf = ckmalloc(IBUFSIZ);
10518 g_parsefile->left_in_buffer = 0;
10519 g_parsefile->left_in_line = 0;
10520 g_parsefile->linno = 1;
10524 * Set the input to take input from a file. If push is set, push the
10525 * old input onto the stack first.
10528 setinputfile(const char *fname, int flags)
10533 fd = open(fname, O_RDONLY);
10535 if (flags & INPUT_NOFILE_OK)
10538 ash_msg_and_raise_error("can't open '%s'", fname);
10543 close_on_exec_on(fd);
10544 setinputfd(fd, flags & INPUT_PUSH_FILE);
10551 * Like setinputfile, but takes input from a string.
10554 setinputstring(char *string)
10558 g_parsefile->next_to_pgetc = string;
10559 g_parsefile->left_in_line = strlen(string);
10560 g_parsefile->buf = NULL;
10561 g_parsefile->linno = 1;
10567 * Routines to check for mail.
10570 #if ENABLE_ASH_MAIL
10572 /* Hash of mtimes of mailboxes */
10573 static unsigned mailtime_hash;
10574 /* Set if MAIL or MAILPATH is changed. */
10575 static smallint mail_var_path_changed;
10578 * Print appropriate message(s) if mail has arrived.
10579 * If mail_var_path_changed is set,
10580 * then the value of MAIL has mail_var_path_changed,
10581 * so we just update the values.
10590 struct stackmark smark;
10593 setstackmark(&smark);
10594 mpath = mpathset() ? mpathval() : mailval();
10597 p = path_advance(&mpath, nullstr);
10602 for (q = p; *q; q++)
10608 q[-1] = '\0'; /* delete trailing '/' */
10609 if (stat(p, &statb) < 0) {
10612 /* Very simplistic "hash": just a sum of all mtimes */
10613 new_hash += (unsigned)statb.st_mtime;
10615 if (!mail_var_path_changed && mailtime_hash != new_hash) {
10616 if (mailtime_hash != 0)
10617 out2str("you have mail\n");
10618 mailtime_hash = new_hash;
10620 mail_var_path_changed = 0;
10621 popstackmark(&smark);
10624 static void FAST_FUNC
10625 changemail(const char *val UNUSED_PARAM)
10627 mail_var_path_changed = 1;
10630 #endif /* ASH_MAIL */
10633 /* ============ ??? */
10636 * Set the shell parameters.
10639 setparam(char **argv)
10645 for (nparam = 0; argv[nparam]; nparam++)
10647 ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap));
10649 *ap++ = ckstrdup(*argv++);
10652 freeparam(&shellparam);
10653 shellparam.malloced = 1;
10654 shellparam.nparam = nparam;
10655 shellparam.p = newparam;
10656 #if ENABLE_ASH_GETOPTS
10657 shellparam.optind = 1;
10658 shellparam.optoff = -1;
10663 * Process shell options. The global variable argptr contains a pointer
10664 * to the argument list; we advance it past the options.
10666 * SUSv3 section 2.8.1 "Consequences of Shell Errors" says:
10667 * For a non-interactive shell, an error condition encountered
10668 * by a special built-in ... shall cause the shell to write a diagnostic message
10669 * to standard error and exit as shown in the following table:
10670 * Error Special Built-In
10672 * Utility syntax error (option or operand error) Shall exit
10674 * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142)
10675 * we see that bash does not do that (set "finishes" with error code 1 instead,
10676 * and shell continues), and people rely on this behavior!
10678 * set -o barfoo 2>/dev/null
10681 * Oh well. Let's mimic that.
10684 plus_minus_o(char *name, int val)
10689 for (i = 0; i < NOPTS; i++) {
10690 if (strcmp(name, optnames(i)) == 0) {
10695 ash_msg("illegal option %co %s", val ? '-' : '+', name);
10698 for (i = 0; i < NOPTS; i++) {
10700 out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off");
10702 out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i));
10708 setoption(int flag, int val)
10712 for (i = 0; i < NOPTS; i++) {
10713 if (optletters(i) == flag) {
10718 ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag);
10722 options(int cmdline, int *login_sh)
10730 while ((p = *argptr) != NULL) {
10732 if (c != '-' && c != '+')
10735 val = 0; /* val = 0 if c == '+' */
10738 if (p[0] == '\0' || LONE_DASH(p)) {
10740 /* "-" means turn off -x and -v */
10743 /* "--" means reset params */
10744 else if (*argptr == NULL)
10747 break; /* "-" or "--" terminates options */
10750 /* first char was + or - */
10751 while ((c = *p++) != '\0') {
10752 /* bash 3.2 indeed handles -c CMD and +c CMD the same */
10753 if (c == 'c' && cmdline) {
10754 minusc = p; /* command is after shell args */
10755 } else if (c == 'o') {
10756 if (plus_minus_o(*argptr, val)) {
10757 /* it already printed err message */
10758 return 1; /* error */
10762 } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
10765 /* bash does not accept +-login, we also won't */
10766 } else if (cmdline && val && (c == '-')) { /* long options */
10767 if (strcmp(p, "login") == 0) {
10781 * The shift builtin command.
10783 static int FAST_FUNC
10784 shiftcmd(int argc UNUSED_PARAM, char **argv)
10791 n = number(argv[1]);
10792 if (n > shellparam.nparam)
10793 n = 0; /* bash compat, was = shellparam.nparam; */
10795 shellparam.nparam -= n;
10796 for (ap1 = shellparam.p; --n >= 0; ap1++) {
10797 if (shellparam.malloced)
10800 ap2 = shellparam.p;
10801 while ((*ap2++ = *ap1++) != NULL)
10803 #if ENABLE_ASH_GETOPTS
10804 shellparam.optind = 1;
10805 shellparam.optoff = -1;
10812 * POSIX requires that 'set' (but not export or readonly) output the
10813 * variables in lexicographic order - by the locale's collating order (sigh).
10814 * Maybe we could keep them in an ordered balanced binary tree
10815 * instead of hashed lists.
10816 * For now just roll 'em through qsort for printing...
10819 showvars(const char *sep_prefix, int on, int off)
10822 char **ep, **epend;
10824 ep = listvars(on, off, &epend);
10825 qsort(ep, epend - ep, sizeof(char *), vpcmp);
10827 sep = *sep_prefix ? " " : sep_prefix;
10829 for (; ep < epend; ep++) {
10833 p = strchrnul(*ep, '=');
10836 q = single_quote(++p);
10837 out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q);
10843 * The set command builtin.
10845 static int FAST_FUNC
10846 setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
10851 return showvars(nullstr, 0, VUNSET);
10854 retval = options(/*cmdline:*/ 0, NULL);
10855 if (retval == 0) { /* if no parse error... */
10857 if (*argptr != NULL) {
10865 #if ENABLE_ASH_RANDOM_SUPPORT
10866 static void FAST_FUNC
10867 change_random(const char *value)
10871 if (value == NULL) {
10872 /* "get", generate */
10873 t = next_random(&random_gen);
10874 /* set without recursion */
10875 setvar(vrandom.var_text, utoa(t), VNOFUNC);
10876 vrandom.flags &= ~VNOFUNC;
10879 t = strtoul(value, NULL, 10);
10880 INIT_RANDOM_T(&random_gen, (t ? t : 1), t);
10885 #if ENABLE_ASH_GETOPTS
10887 getopts(char *optstr, char *optvar, char **optfirst)
10894 int ind = shellparam.optind;
10895 int off = shellparam.optoff;
10899 shellparam.optind = -1;
10900 optnext = optfirst + ind - 1;
10902 if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off)
10905 p = optnext[-1] + off;
10906 if (p == NULL || *p == '\0') {
10907 /* Current word is done, advance */
10909 if (p == NULL || *p != '-' || *++p == '\0') {
10916 if (LONE_DASH(p)) /* check for "--" */
10921 for (q = optstr; *q != c;) {
10923 if (optstr[0] == ':') {
10925 /*sbuf[1] = '\0'; - already is */
10926 setvar0("OPTARG", sbuf);
10928 fprintf(stderr, "Illegal option -%c\n", c);
10929 unsetvar("OPTARG");
10939 if (*p == '\0' && (p = *optnext) == NULL) {
10940 if (optstr[0] == ':') {
10942 /*sbuf[1] = '\0'; - already is */
10943 setvar0("OPTARG", sbuf);
10946 fprintf(stderr, "No arg for -%c option\n", c);
10947 unsetvar("OPTARG");
10955 setvar0("OPTARG", p);
10958 setvar0("OPTARG", nullstr);
10960 ind = optnext - optfirst + 1;
10961 setvar("OPTIND", itoa(ind), VNOFUNC);
10963 /*sbuf[1] = '\0'; - already is */
10964 setvar0(optvar, sbuf);
10966 shellparam.optoff = p ? p - *(optnext - 1) : -1;
10967 shellparam.optind = ind;
10973 * The getopts builtin. Shellparam.optnext points to the next argument
10974 * to be processed. Shellparam.optptr points to the next character to
10975 * be processed in the current argument. If shellparam.optnext is NULL,
10976 * then it's the first time getopts has been called.
10978 static int FAST_FUNC
10979 getoptscmd(int argc, char **argv)
10984 ash_msg_and_raise_error("usage: getopts optstring var [arg]");
10986 optbase = shellparam.p;
10987 if ((unsigned)shellparam.optind > shellparam.nparam + 1) {
10988 shellparam.optind = 1;
10989 shellparam.optoff = -1;
10992 optbase = &argv[3];
10993 if ((unsigned)shellparam.optind > argc - 2) {
10994 shellparam.optind = 1;
10995 shellparam.optoff = -1;
10999 return getopts(argv[1], argv[2], optbase);
11001 #endif /* ASH_GETOPTS */
11004 /* ============ Shell parser */
11007 struct heredoc *next; /* next here document in list */
11008 union node *here; /* redirection node */
11009 char *eofmark; /* string indicating end of input */
11010 smallint striptabs; /* if set, strip leading tabs */
11013 static smallint tokpushback; /* last token pushed back */
11014 static smallint quoteflag; /* set if (part of) last token was quoted */
11015 static token_id_t lasttoken; /* last token read (integer id Txxx) */
11016 static struct heredoc *heredoclist; /* list of here documents to read */
11017 static char *wordtext; /* text of last word returned by readtoken */
11018 static struct nodelist *backquotelist;
11019 static union node *redirnode;
11020 static struct heredoc *heredoc;
11022 static const char *
11023 tokname(char *buf, int tok)
11026 return tokname_array[tok];
11027 sprintf(buf, "\"%s\"", tokname_array[tok]);
11031 /* raise_error_unexpected_syntax:
11032 * Called when an unexpected token is read during the parse. The argument
11033 * is the token that is expected, or -1 if more than one type of token can
11034 * occur at this point.
11036 static void raise_error_unexpected_syntax(int) NORETURN;
11038 raise_error_unexpected_syntax(int token)
11044 l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken));
11046 sprintf(msg + l, " (expecting %s)", tokname(buf, token));
11047 raise_error_syntax(msg);
11051 /* parsing is heavily cross-recursive, need these forward decls */
11052 static union node *andor(void);
11053 static union node *pipeline(void);
11054 static union node *parse_command(void);
11055 static void parseheredoc(void);
11056 static int peektoken(void);
11057 static int readtoken(void);
11059 static union node *
11062 union node *n1, *n2, *n3;
11067 switch (peektoken()) {
11075 if (!n1 && (nlflag & 1))
11081 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11082 if (nlflag == 2 && ((1 << peektoken()) & tokendlist))
11088 if (tok == TBACKGND) {
11089 if (n2->type == NPIPE) {
11090 n2->npipe.pipe_backgnd = 1;
11092 if (n2->type != NREDIR) {
11093 n3 = stzalloc(sizeof(struct nredir));
11095 /*n3->nredir.redirect = NULL; - stzalloc did it */
11098 n2->type = NBACKGND;
11104 n3 = stzalloc(sizeof(struct nbinary));
11106 n3->nbinary.ch1 = n1;
11107 n3->nbinary.ch2 = n2;
11120 raise_error_unexpected_syntax(-1);
11127 static union node *
11130 union node *n1, *n2, *n3;
11138 } else if (t == TOR) {
11144 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11146 n3 = stzalloc(sizeof(struct nbinary));
11148 n3->nbinary.ch1 = n1;
11149 n3->nbinary.ch2 = n2;
11154 static union node *
11157 union node *n1, *n2, *pipenode;
11158 struct nodelist *lp, *prev;
11162 TRACE(("pipeline: entered\n"));
11163 if (readtoken() == TNOT) {
11165 checkkwd = CHKKWD | CHKALIAS;
11168 n1 = parse_command();
11169 if (readtoken() == TPIPE) {
11170 pipenode = stzalloc(sizeof(struct npipe));
11171 pipenode->type = NPIPE;
11172 /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */
11173 lp = stzalloc(sizeof(struct nodelist));
11174 pipenode->npipe.cmdlist = lp;
11178 lp = stzalloc(sizeof(struct nodelist));
11179 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11180 lp->n = parse_command();
11182 } while (readtoken() == TPIPE);
11188 n2 = stzalloc(sizeof(struct nnot));
11196 static union node *
11201 n = stzalloc(sizeof(struct narg));
11203 /*n->narg.next = NULL; - stzalloc did it */
11204 n->narg.text = wordtext;
11205 n->narg.backquote = backquotelist;
11210 fixredir(union node *n, const char *text, int err)
11214 TRACE(("Fix redir %s %d\n", text, err));
11216 n->ndup.vname = NULL;
11218 fd = bb_strtou(text, NULL, 10);
11219 if (!errno && fd >= 0)
11220 n->ndup.dupfd = fd;
11221 else if (LONE_DASH(text))
11222 n->ndup.dupfd = -1;
11225 raise_error_syntax("bad fd number");
11226 n->ndup.vname = makename();
11233 union node *n = redirnode;
11235 if (n->type == NHERE)
11236 checkkwd = CHKEOFMARK;
11237 if (readtoken() != TWORD)
11238 raise_error_unexpected_syntax(-1);
11239 if (n->type == NHERE) {
11240 struct heredoc *here = heredoc;
11243 if (quoteflag == 0)
11245 TRACE(("Here document %d\n", n->type));
11246 rmescapes(wordtext, 0);
11247 here->eofmark = wordtext;
11249 if (heredoclist == NULL)
11250 heredoclist = here;
11252 for (p = heredoclist; p->next; p = p->next)
11256 } else if (n->type == NTOFD || n->type == NFROMFD) {
11257 fixredir(n, wordtext, 0);
11259 n->nfile.fname = makename();
11263 static union node *
11266 union node *args, **app;
11267 union node *n = NULL;
11268 union node *vars, **vpp;
11269 union node **rpp, *redir;
11272 smallint double_brackets_flag = 0;
11274 IF_BASH_FUNCTION(smallint function_flag = 0;)
11283 savecheckkwd = CHKALIAS;
11286 checkkwd = savecheckkwd;
11291 if (peektoken() != TWORD)
11292 raise_error_unexpected_syntax(TWORD);
11297 case TAND: /* "&&" */
11298 case TOR: /* "||" */
11299 if (!double_brackets_flag) {
11303 wordtext = (char *) (t == TAND ? "-a" : "-o");
11306 n = stzalloc(sizeof(struct narg));
11308 /*n->narg.next = NULL; - stzalloc did it */
11309 n->narg.text = wordtext;
11311 if (strcmp("[[", wordtext) == 0)
11312 double_brackets_flag = 1;
11313 else if (strcmp("]]", wordtext) == 0)
11314 double_brackets_flag = 0;
11316 n->narg.backquote = backquotelist;
11317 if (savecheckkwd && isassignment(wordtext)) {
11319 vpp = &n->narg.next;
11322 app = &n->narg.next;
11326 if (function_flag) {
11327 checkkwd = CHKNL | CHKKWD;
11328 switch (peektoken()) {
11340 if (strcmp("[[", wordtext) == 0)
11344 raise_error_unexpected_syntax(-1);
11350 *rpp = n = redirnode;
11351 rpp = &n->nfile.next;
11352 parsefname(); /* read name of redirection file */
11355 IF_BASH_FUNCTION(do_func:)
11356 if (args && app == &args->narg.next
11359 struct builtincmd *bcmd;
11362 /* We have a function */
11363 if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP)
11364 raise_error_unexpected_syntax(TRP);
11365 name = n->narg.text;
11366 if (!goodname(name)
11367 || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd))
11369 raise_error_syntax("bad function name");
11372 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11373 n->narg.next = parse_command();
11376 IF_BASH_FUNCTION(function_flag = 0;)
11387 n = stzalloc(sizeof(struct ncmd));
11389 n->ncmd.args = args;
11390 n->ncmd.assign = vars;
11391 n->ncmd.redirect = redir;
11395 static union node *
11396 parse_command(void)
11398 union node *n1, *n2;
11399 union node *ap, **app;
11400 union node *cp, **cpp;
11401 union node *redir, **rpp;
11408 switch (readtoken()) {
11410 raise_error_unexpected_syntax(-1);
11413 n1 = stzalloc(sizeof(struct nif));
11415 n1->nif.test = list(0);
11416 if (readtoken() != TTHEN)
11417 raise_error_unexpected_syntax(TTHEN);
11418 n1->nif.ifpart = list(0);
11420 while (readtoken() == TELIF) {
11421 n2->nif.elsepart = stzalloc(sizeof(struct nif));
11422 n2 = n2->nif.elsepart;
11424 n2->nif.test = list(0);
11425 if (readtoken() != TTHEN)
11426 raise_error_unexpected_syntax(TTHEN);
11427 n2->nif.ifpart = list(0);
11429 if (lasttoken == TELSE)
11430 n2->nif.elsepart = list(0);
11432 n2->nif.elsepart = NULL;
11440 n1 = stzalloc(sizeof(struct nbinary));
11441 n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
11442 n1->nbinary.ch1 = list(0);
11445 TRACE(("expecting DO got '%s' %s\n", tokname_array[got],
11446 got == TWORD ? wordtext : ""));
11447 raise_error_unexpected_syntax(TDO);
11449 n1->nbinary.ch2 = list(0);
11454 if (readtoken() != TWORD || quoteflag || !goodname(wordtext))
11455 raise_error_syntax("bad for loop variable");
11456 n1 = stzalloc(sizeof(struct nfor));
11458 n1->nfor.var = wordtext;
11459 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11460 if (readtoken() == TIN) {
11462 while (readtoken() == TWORD) {
11463 n2 = stzalloc(sizeof(struct narg));
11465 /*n2->narg.next = NULL; - stzalloc did it */
11466 n2->narg.text = wordtext;
11467 n2->narg.backquote = backquotelist;
11469 app = &n2->narg.next;
11472 n1->nfor.args = ap;
11473 if (lasttoken != TNL && lasttoken != TSEMI)
11474 raise_error_unexpected_syntax(-1);
11476 n2 = stzalloc(sizeof(struct narg));
11478 /*n2->narg.next = NULL; - stzalloc did it */
11479 n2->narg.text = (char *)dolatstr;
11480 /*n2->narg.backquote = NULL;*/
11481 n1->nfor.args = n2;
11483 * Newline or semicolon here is optional (but note
11484 * that the original Bourne shell only allowed NL).
11486 if (lasttoken != TSEMI)
11489 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11490 if (readtoken() != TDO)
11491 raise_error_unexpected_syntax(TDO);
11492 n1->nfor.body = list(0);
11496 n1 = stzalloc(sizeof(struct ncase));
11498 if (readtoken() != TWORD)
11499 raise_error_unexpected_syntax(TWORD);
11500 n1->ncase.expr = n2 = stzalloc(sizeof(struct narg));
11502 /*n2->narg.next = NULL; - stzalloc did it */
11503 n2->narg.text = wordtext;
11504 n2->narg.backquote = backquotelist;
11505 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11506 if (readtoken() != TIN)
11507 raise_error_unexpected_syntax(TIN);
11508 cpp = &n1->ncase.cases;
11510 checkkwd = CHKNL | CHKKWD;
11512 while (t != TESAC) {
11513 if (lasttoken == TLP)
11515 *cpp = cp = stzalloc(sizeof(struct nclist));
11517 app = &cp->nclist.pattern;
11519 *app = ap = stzalloc(sizeof(struct narg));
11521 /*ap->narg.next = NULL; - stzalloc did it */
11522 ap->narg.text = wordtext;
11523 ap->narg.backquote = backquotelist;
11524 if (readtoken() != TPIPE)
11526 app = &ap->narg.next;
11529 //ap->narg.next = NULL;
11530 if (lasttoken != TRP)
11531 raise_error_unexpected_syntax(TRP);
11532 cp->nclist.body = list(2);
11534 cpp = &cp->nclist.next;
11536 checkkwd = CHKNL | CHKKWD;
11540 raise_error_unexpected_syntax(TENDCASE);
11547 n1 = stzalloc(sizeof(struct nredir));
11548 n1->type = NSUBSHELL;
11549 n1->nredir.n = list(0);
11550 /*n1->nredir.redirect = NULL; - stzalloc did it */
11557 IF_BASH_FUNCTION(case TFUNCTION:)
11561 return simplecmd();
11564 if (readtoken() != t)
11565 raise_error_unexpected_syntax(t);
11568 /* Now check for redirection which may follow command */
11569 checkkwd = CHKKWD | CHKALIAS;
11571 while (readtoken() == TREDIR) {
11572 *rpp = n2 = redirnode;
11573 rpp = &n2->nfile.next;
11579 if (n1->type != NSUBSHELL) {
11580 n2 = stzalloc(sizeof(struct nredir));
11585 n1->nredir.redirect = redir;
11590 #if BASH_DOLLAR_SQUOTE
11592 decode_dollar_squote(void)
11594 static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
11600 p = strchr(C_escapes, c);
11605 if ((unsigned char)(c - '0') <= 7) { /* \ooo */
11609 } while ((unsigned char)(c - '0') <= 7 && --cnt);
11611 } else if (c == 'x') { /* \xHH */
11615 } while (isxdigit(c) && --cnt);
11617 if (cnt == 3) { /* \x but next char is "bad" */
11621 } else { /* simple seq like \\ or \t */
11626 c = bb_process_escape_sequence((void*)&p);
11627 } else { /* unrecognized "\z": print both chars unless ' or " */
11628 if (c != '\'' && c != '"') {
11630 c |= 0x100; /* "please encode \, then me" */
11637 /* Used by expandstr to get here-doc like behaviour. */
11638 #define FAKEEOFMARK ((char*)(uintptr_t)1)
11640 static ALWAYS_INLINE int
11641 realeofmark(const char *eofmark)
11643 return eofmark && eofmark != FAKEEOFMARK;
11647 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
11648 * is not NULL, read a here document. In the latter case, eofmark is the
11649 * word which marks the end of the document and striptabs is true if
11650 * leading tabs should be stripped from the document. The argument c
11651 * is the first character of the input token or document.
11653 * Because C does not have internal subroutines, I have simulated them
11654 * using goto's to implement the subroutine linkage. The following macros
11655 * will run code that appears at the end of readtoken1.
11657 #define CHECKEND() {goto checkend; checkend_return:;}
11658 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
11659 #define PARSESUB() {goto parsesub; parsesub_return:;}
11660 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
11661 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
11662 #define PARSEARITH() {goto parsearith; parsearith_return:;}
11664 readtoken1(int c, int syntax, char *eofmark, int striptabs)
11666 /* NB: syntax parameter fits into smallint */
11667 /* c parameter is an unsigned char or PEOF or PEOA */
11670 struct nodelist *bqlist;
11674 IF_FEATURE_SH_MATH(smallint prevsyntax;) /* syntax before arithmetic */
11675 smallint pssyntax; /* we are expanding a prompt string */
11676 int varnest; /* levels of variables expansion */
11677 IF_FEATURE_SH_MATH(int arinest;) /* levels of arithmetic expansion */
11678 IF_FEATURE_SH_MATH(int parenlevel;) /* levels of parens in arithmetic */
11679 int dqvarnest; /* levels of variables expansion within double quotes */
11681 IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;)
11683 startlinno = g_parsefile->linno;
11686 IF_FEATURE_SH_MATH(prevsyntax = 0;)
11687 #if ENABLE_ASH_EXPAND_PRMT
11688 pssyntax = (syntax == PSSYNTAX);
11692 pssyntax = 0; /* constant */
11694 dblquote = (syntax == DQSYNTAX);
11696 IF_FEATURE_SH_MATH(arinest = 0;)
11697 IF_FEATURE_SH_MATH(parenlevel = 0;)
11700 STARTSTACKSTR(out);
11702 /* For each line, until end of word */
11703 CHECKEND(); /* set c to PEOF if at end of here document */
11704 for (;;) { /* until end of line or end of word */
11705 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
11706 switch (SIT(c, syntax)) {
11707 case CNL: /* '\n' */
11708 if (syntax == BASESYNTAX)
11709 goto endword; /* exit outer loop */
11713 goto loop; /* continue outer loop */
11718 #if BASH_DOLLAR_SQUOTE
11719 if (c == '\\' && bash_dollar_squote) {
11720 c = decode_dollar_squote();
11722 /* skip $'\000', $'\x00' (like bash) */
11726 /* Unknown escape. Encode as '\z' */
11727 c = (unsigned char)c;
11728 if (eofmark == NULL || dblquote)
11729 USTPUTC(CTLESC, out);
11730 USTPUTC('\\', out);
11734 if (eofmark == NULL || dblquote)
11735 USTPUTC(CTLESC, out);
11738 case CBACK: /* backslash */
11739 c = pgetc_without_PEOA();
11741 USTPUTC(CTLESC, out);
11742 USTPUTC('\\', out);
11744 } else if (c == '\n') {
11747 if (pssyntax && c == '$') {
11748 USTPUTC(CTLESC, out);
11749 USTPUTC('\\', out);
11751 /* Backslash is retained if we are in "str" and next char isn't special */
11756 && (c != '"' || eofmark != NULL)
11758 USTPUTC('\\', out);
11760 USTPUTC(CTLESC, out);
11768 if (eofmark == NULL) {
11769 USTPUTC(CTLQUOTEMARK, out);
11777 IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;)
11778 if (eofmark != NULL && varnest == 0) {
11781 if (dqvarnest == 0) {
11782 syntax = BASESYNTAX;
11789 case CVAR: /* '$' */
11790 PARSESUB(); /* parse substitution */
11792 case CENDVAR: /* '}' */
11795 if (dqvarnest > 0) {
11802 #if ENABLE_FEATURE_SH_MATH
11803 case CLP: /* '(' in arithmetic */
11807 case CRP: /* ')' in arithmetic */
11808 if (parenlevel > 0) {
11811 if (pgetc_eatbnl() == ')') {
11813 if (--arinest == 0) {
11814 syntax = prevsyntax;
11818 * unbalanced parens
11819 * (don't 2nd guess - no error)
11827 case CBQUOTE: /* '`' */
11831 goto endword; /* exit outer loop */
11835 if (varnest == 0) {
11836 #if BASH_REDIR_OUTPUT
11838 //Can't call pgetc_eatbnl() here, this requires three-deep pungetc()
11839 if (pgetc() == '>')
11840 c = 0x100 + '>'; /* flag &> */
11844 goto endword; /* exit outer loop */
11846 IF_ASH_ALIAS(if (c != PEOA))
11853 #if ENABLE_FEATURE_SH_MATH
11854 if (syntax == ARISYNTAX)
11855 raise_error_syntax("missing '))'");
11857 if (syntax != BASESYNTAX && eofmark == NULL)
11858 raise_error_syntax("unterminated quoted string");
11859 if (varnest != 0) {
11860 startlinno = g_parsefile->linno;
11862 raise_error_syntax("missing '}'");
11864 USTPUTC('\0', out);
11865 len = out - (char *)stackblock();
11866 out = stackblock();
11867 if (eofmark == NULL) {
11868 if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>'))
11871 if (isdigit_str9(out)) {
11872 PARSEREDIR(); /* passed as params: out, c */
11873 lasttoken = TREDIR;
11876 /* else: non-number X seen, interpret it
11877 * as "NNNX>file" = "NNNX >file" */
11881 quoteflag = quotef;
11882 backquotelist = bqlist;
11883 grabstackblock(len);
11887 /* end of readtoken routine */
11890 * Check to see whether we are at the end of the here document. When this
11891 * is called, c is set to the first character of the next input line. If
11892 * we are at the end of the here document, this routine sets the c to PEOF.
11895 if (realeofmark(eofmark)) {
11899 #if ENABLE_ASH_ALIAS
11901 c = pgetc_without_PEOA();
11904 while (c == '\t') {
11905 c = pgetc_without_PEOA();
11909 markloc = out - (char *)stackblock();
11910 for (p = eofmark; STPUTC(c, out), *p; p++) {
11914 c = pgetc_without_PEOA();
11917 if (c == '\n' || c == PEOF) {
11919 g_parsefile->linno++;
11920 needprompt = doprompt;
11925 p = (char *)stackblock() + markloc + 1;
11926 len_here = out - p;
11929 len_here -= (c >= PEOF);
11935 str = alloca(len_here + 1);
11936 *(char *)mempcpy(str, p, len_here) = '\0';
11938 pushstring(str, NULL);
11943 STADJUST((char *)stackblock() + markloc - out, out);
11945 goto checkend_return;
11949 * Parse a redirection operator. The variable "out" points to a string
11950 * specifying the fd to be redirected. The variable "c" contains the
11951 * first character of the redirection operator.
11954 /* out is already checked to be a valid number or "" */
11955 int fd = (*out == '\0' ? -1 : atoi(out));
11958 np = stzalloc(sizeof(struct nfile));
11963 np->type = NAPPEND;
11965 np->type = NCLOBBER;
11968 /* it also can be NTO2 (>&file), but we can't figure it out yet */
11974 #if BASH_REDIR_OUTPUT
11975 else if (c == 0x100 + '>') { /* this flags &> redirection */
11977 pgetc(); /* this is '>', no need to check */
11981 else { /* c == '<' */
11982 /*np->nfile.fd = 0; - stzalloc did it */
11986 if (sizeof(struct nfile) != sizeof(struct nhere)) {
11987 np = stzalloc(sizeof(struct nhere));
11988 /*np->nfile.fd = 0; - stzalloc did it */
11991 heredoc = stzalloc(sizeof(struct heredoc));
11992 heredoc->here = np;
11995 heredoc->striptabs = 1;
11997 /*heredoc->striptabs = 0; - stzalloc did it */
12003 np->type = NFROMFD;
12007 np->type = NFROMTO;
12019 goto parseredir_return;
12023 * Parse a substitution. At this point, we have read the dollar sign
12024 * and nothing else.
12027 /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
12028 * (assuming ascii char codes, as the original implementation did) */
12029 #define is_special(c) \
12030 (((unsigned)(c) - 33 < 32) \
12031 && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1))
12033 unsigned char subtype;
12036 c = pgetc_eatbnl();
12037 if ((checkkwd & CHKEOFMARK)
12038 || c > 255 /* PEOA or PEOF */
12039 || (c != '(' && c != '{' && !is_name(c) && !is_special(c))
12041 #if BASH_DOLLAR_SQUOTE
12042 if (syntax != DQSYNTAX && c == '\'')
12043 bash_dollar_squote = 1;
12048 } else if (c == '(') {
12049 /* $(command) or $((arith)) */
12050 if (pgetc_eatbnl() == '(') {
12051 #if ENABLE_FEATURE_SH_MATH
12054 raise_error_syntax("support for $((arith)) is disabled");
12061 /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */
12062 USTPUTC(CTLVAR, out);
12063 typeloc = out - (char *)stackblock();
12065 subtype = VSNORMAL;
12067 c = pgetc_eatbnl();
12072 /* $[{[#]]NAME[}] */
12075 c = pgetc_eatbnl();
12076 } while (is_in_name(c));
12077 } else if (isdigit(c)) {
12078 /* $[{[#]]NUM[}] */
12081 c = pgetc_eatbnl();
12082 } while (isdigit(c));
12083 } else if (is_special(c)) {
12084 /* $[{[#]]<specialchar>[}] */
12087 c = pgetc_eatbnl();
12088 if (!subtype && cc == '#') {
12089 subtype = VSLENGTH;
12090 if (c == '_' || isalnum(c))
12093 c = pgetc_eatbnl();
12094 if (cc == '}' || c != '}') {
12105 if (c != '}' && subtype == VSLENGTH) {
12106 /* ${#VAR didn't end with } */
12110 if (subtype == 0) {
12111 static const char types[] ALIGN1 = "}-+?=";
12112 /* ${VAR...} but not $VAR or ${#VAR} */
12113 /* c == first char after VAR */
12116 c = pgetc_eatbnl();
12118 /* This check is only needed to not misinterpret
12119 * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD}
12122 if (!strchr(types, c)) {
12123 subtype = VSSUBSTR;
12125 break; /* "goto badsub" is bigger (!) */
12131 const char *p = strchr(types, c);
12134 subtype |= p - types + VSNORMAL;
12140 subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT);
12141 c = pgetc_eatbnl();
12147 #if BASH_PATTERN_SUBST
12149 /* ${v/[/]pattern/repl} */
12150 //TODO: encode pattern and repl separately.
12151 // Currently ${v/$var_with_slash/repl} is horribly broken
12152 subtype = VSREPLACE;
12153 c = pgetc_eatbnl();
12156 subtype++; /* VSREPLACEALL */
12164 ((unsigned char *)stackblock())[typeloc] = subtype;
12165 if (subtype != VSNORMAL) {
12172 goto parsesub_return;
12176 * Called to parse command substitutions. Newstyle is set if the command
12177 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12178 * list of commands (passed by reference), and savelen is the number of
12179 * characters on the top of the stack which must be preserved.
12182 struct nodelist **nlpp;
12186 smallint saveprompt = 0;
12189 savelen = out - (char *)stackblock();
12192 * FIXME: this can allocate very large block on stack and SEGV.
12194 * echo "..<100kbytes>..`true` $(true) `true` ..."
12195 * allocates 100kb for every command subst. With about
12196 * a hundred command substitutions stack overflows.
12197 * With larger prepended string, SEGV happens sooner.
12199 str = alloca(savelen);
12200 memcpy(str, stackblock(), savelen);
12204 /* We must read until the closing backquote, giving special
12205 * treatment to some slashes, and then push the string and
12206 * reread it as input, interpreting it normally.
12212 STARTSTACKSTR(pout);
12216 setprompt_if(needprompt, 2);
12227 * If eating a newline, avoid putting
12228 * the newline into the new character
12229 * stream (via the STPUTC after the
12234 if (pc != '\\' && pc != '`' && pc != '$'
12235 && (!dblquote || pc != '"')
12237 STPUTC('\\', pout);
12239 if (pc <= 255 /* not PEOA or PEOF */) {
12245 IF_ASH_ALIAS(case PEOA:)
12246 startlinno = g_parsefile->linno;
12247 raise_error_syntax("EOF in backquote substitution");
12259 STPUTC('\0', pout);
12260 psavelen = pout - (char *)stackblock();
12261 if (psavelen > 0) {
12262 pstr = grabstackstr(pout);
12263 setinputstring(pstr);
12268 nlpp = &(*nlpp)->next;
12269 *nlpp = stzalloc(sizeof(**nlpp));
12270 /* (*nlpp)->next = NULL; - stzalloc did it */
12273 saveprompt = doprompt;
12280 doprompt = saveprompt;
12281 else if (readtoken() != TRP)
12282 raise_error_unexpected_syntax(TRP);
12287 * Start reading from old file again, ignoring any pushed back
12288 * tokens left from the backquote parsing
12293 while (stackblocksize() <= savelen)
12295 STARTSTACKSTR(out);
12297 memcpy(out, str, savelen);
12298 STADJUST(savelen, out);
12300 USTPUTC(CTLBACKQ, out);
12302 goto parsebackq_oldreturn;
12303 goto parsebackq_newreturn;
12306 #if ENABLE_FEATURE_SH_MATH
12308 * Parse an arithmetic expansion (indicate start of one and set state)
12311 if (++arinest == 1) {
12312 prevsyntax = syntax;
12313 syntax = ARISYNTAX;
12315 USTPUTC(CTLARI, out);
12316 goto parsearith_return;
12319 } /* end of readtoken */
12322 * Read the next input token.
12323 * If the token is a word, we set backquotelist to the list of cmds in
12324 * backquotes. We set quoteflag to true if any part of the word was
12326 * If the token is TREDIR, then we set redirnode to a structure containing
12328 * In all cases, the variable startlinno is set to the number of the line
12329 * on which the token starts.
12331 * [Change comment: here documents and internal procedures]
12332 * [Readtoken shouldn't have any arguments. Perhaps we should make the
12333 * word parsing code into a separate routine. In this case, readtoken
12334 * doesn't need to have any internal procedures, but parseword does.
12335 * We could also make parseoperator in essence the main routine, and
12336 * have parseword (readtoken1?) handle both words and redirection.]
12338 #define NEW_xxreadtoken
12339 #ifdef NEW_xxreadtoken
12340 /* singles must be first! */
12341 static const char xxreadtoken_chars[7] ALIGN1 = {
12342 '\n', '(', ')', /* singles */
12343 '&', '|', ';', /* doubles */
12347 #define xxreadtoken_singles 3
12348 #define xxreadtoken_doubles 3
12350 static const char xxreadtoken_tokens[] ALIGN1 = {
12351 TNL, TLP, TRP, /* only single occurrence allowed */
12352 TBACKGND, TPIPE, TSEMI, /* if single occurrence */
12353 TEOF, /* corresponds to trailing nul */
12354 TAND, TOR, TENDCASE /* if double occurrence */
12366 setprompt_if(needprompt, 2);
12367 startlinno = g_parsefile->linno;
12368 for (;;) { /* until token or start of word found */
12370 if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA))
12374 while ((c = pgetc()) != '\n' && c != PEOF)
12377 } else if (c == '\\') {
12378 if (pgetc() != '\n') {
12380 break; /* return readtoken1(...) */
12386 p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
12392 p = strchr(xxreadtoken_chars, c);
12394 break; /* return readtoken1(...) */
12396 if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) {
12398 if (cc == c) { /* double occurrence? */
12399 p += xxreadtoken_doubles + 1;
12402 #if BASH_REDIR_OUTPUT
12403 if (c == '&' && cc == '>') /* &> */
12404 break; /* return readtoken1(...) */
12409 lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
12414 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
12416 #else /* old xxreadtoken */
12417 #define RETURN(token) return lasttoken = token
12427 setprompt_if(needprompt, 2);
12428 startlinno = g_parsefile->linno;
12429 for (;;) { /* until token or start of word found */
12432 case ' ': case '\t':
12433 IF_ASH_ALIAS(case PEOA:)
12436 while ((c = pgetc()) != '\n' && c != PEOF)
12441 if (pgetc() == '\n') {
12453 if (pgetc() == '&')
12458 if (pgetc() == '|')
12463 if (pgetc() == ';')
12476 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
12479 #endif /* old xxreadtoken */
12485 int kwd = checkkwd;
12487 smallint alreadyseen = tokpushback;
12490 #if ENABLE_ASH_ALIAS
12506 if (t != TWORD || quoteflag) {
12511 * check for keywords
12513 if (kwd & CHKKWD) {
12514 const char *const *pp;
12516 pp = findkwd(wordtext);
12518 lasttoken = t = pp - tokname_array;
12519 TRACE(("keyword '%s' recognized\n", tokname_array[t]));
12524 if (checkkwd & CHKALIAS) {
12525 #if ENABLE_ASH_ALIAS
12527 ap = lookupalias(wordtext, 1);
12530 pushstring(ap->val, ap);
12540 TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
12542 TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
12558 * Read and parse a command. Returns NODE_EOF on end of file.
12559 * (NULL is a valid parse tree indicating a blank line.)
12561 static union node *
12562 parsecmd(int interact)
12567 doprompt = interact;
12568 setprompt_if(doprompt, doprompt);
12574 * Input any here documents.
12579 struct heredoc *here;
12582 here = heredoclist;
12583 heredoclist = NULL;
12586 setprompt_if(needprompt, 2);
12587 readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX,
12588 here->eofmark, here->striptabs);
12589 n = stzalloc(sizeof(struct narg));
12590 n->narg.type = NARG;
12591 /*n->narg.next = NULL; - stzalloc did it */
12592 n->narg.text = wordtext;
12593 n->narg.backquote = backquotelist;
12594 here->here->nhere.doc = n;
12600 static const char *
12601 expandstr(const char *ps, int syntax_type)
12606 /* XXX Fix (char *) cast. */
12607 setinputstring((char *)ps);
12609 saveprompt = doprompt;
12611 readtoken1(pgetc(), syntax_type, FAKEEOFMARK, 0);
12612 doprompt = saveprompt;
12616 n.narg.type = NARG;
12617 n.narg.next = NULL;
12618 n.narg.text = wordtext;
12619 n.narg.backquote = backquotelist;
12621 expandarg(&n, NULL, EXP_QUOTED);
12622 return stackblock();
12628 return tokpushback && lasttoken == TEOF;
12632 * Execute a command or commands contained in a string.
12635 evalstring(char *s, int flags)
12637 struct jmploc *volatile savehandler;
12638 struct jmploc jmploc;
12642 struct stackmark smark;
12647 setstackmark(&smark);
12650 /* On exception inside execution loop, we must popfile().
12651 * Try interactively:
12653 * command eval "a=b" # throws "is read only" error
12654 * "command BLTIN" is not supposed to abort (even in non-interactive use).
12655 * But if we skip popfile(), we hit EOF in eval's string, and exit.
12657 savehandler = exception_handler;
12658 ex = setjmp(jmploc.loc);
12661 exception_handler = &jmploc;
12663 while ((n = parsecmd(0)) != NODE_EOF) {
12666 i = evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT));
12669 popstackmark(&smark);
12674 popstackmark(&smark);
12678 exception_handler = savehandler;
12680 longjmp(exception_handler->loc, ex);
12686 * The eval command.
12688 static int FAST_FUNC
12689 evalcmd(int argc UNUSED_PARAM, char **argv, int flags)
12698 STARTSTACKSTR(concat);
12700 concat = stack_putstr(p, concat);
12704 STPUTC(' ', concat);
12706 STPUTC('\0', concat);
12707 p = grabstackstr(concat);
12709 return evalstring(p, flags & EV_TESTED);
12715 * Read and execute commands.
12716 * "Top" is nonzero for the top level command loop;
12717 * it turns on prompting if the shell is interactive.
12723 struct stackmark smark;
12728 TRACE(("cmdloop(%d) called\n", top));
12732 setstackmark(&smark);
12735 showjobs(SHOW_CHANGED|SHOW_STDERR);
12738 if (iflag && top) {
12742 n = parsecmd(inter);
12744 if (DEBUG > 2 && debug && (n != NODE_EOF))
12747 if (n == NODE_EOF) {
12748 if (!top || numeof >= 50)
12750 if (!stoppedjobs()) {
12753 out2str("\nUse \"exit\" to leave shell.\n");
12756 } else if (nflag == 0) {
12759 /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */
12762 i = evaltree(n, 0);
12766 popstackmark(&smark);
12770 evalskip &= ~SKIPFUNC;
12778 * Take commands from a file. To be compatible we should do a path
12779 * search for the file, which is necessary to find sub-commands.
12782 find_dot_file(char *name)
12785 const char *path = pathval();
12788 /* don't try this for absolute or relative paths */
12789 if (strchr(name, '/'))
12792 while ((fullname = path_advance(&path, name)) != NULL) {
12793 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
12795 * Don't bother freeing here, since it will
12796 * be freed by the caller.
12800 if (fullname != name)
12801 stunalloc(fullname);
12804 /* not found in the PATH */
12805 ash_msg_and_raise_error("%s: not found", name);
12809 static int FAST_FUNC
12810 dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM)
12812 /* "false; . empty_file; echo $?" should print 0, not 1: */
12816 char *args_need_save;
12817 volatile struct shparam saveparam;
12820 // struct strlist *sp;
12821 // for (sp = cmdenviron; sp; sp = sp->next)
12822 // setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
12824 nextopt(nullstr); /* handle possible "--" */
12828 /* bash says: "bash: .: filename argument required" */
12829 return 2; /* bash compat */
12832 /* This aborts if file isn't found, which is POSIXly correct.
12833 * bash returns exitcode 1 instead.
12835 fullname = find_dot_file(argv[0]);
12837 args_need_save = argv[0];
12838 if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */
12840 saveparam = shellparam;
12841 shellparam.malloced = 0;
12845 shellparam.nparam = argc;
12846 shellparam.p = argv;
12849 /* This aborts if file can't be opened, which is POSIXly correct.
12850 * bash returns exitcode 1 instead.
12852 setinputfile(fullname, INPUT_PUSH_FILE);
12853 commandname = fullname;
12854 status = cmdloop(0);
12857 if (args_need_save) {
12858 freeparam(&shellparam);
12859 shellparam = saveparam;
12865 static int FAST_FUNC
12866 exitcmd(int argc UNUSED_PARAM, char **argv)
12871 exitstatus = number(argv[1]);
12872 raise_exception(EXEXIT);
12877 * Read a file containing shell functions.
12880 readcmdfile(char *name)
12882 setinputfile(name, INPUT_PUSH_FILE);
12888 /* ============ find_command inplementation */
12891 * Resolve a command name. If you change this routine, you may have to
12892 * change the shellexec routine as well.
12895 find_command(char *name, struct cmdentry *entry, int act, const char *path)
12897 struct tblentry *cmdp;
12904 struct builtincmd *bcmd;
12906 /* If name contains a slash, don't use PATH or hash table */
12907 if (strchr(name, '/') != NULL) {
12908 entry->u.index = -1;
12909 if (act & DO_ABS) {
12910 while (stat(name, &statb) < 0) {
12912 if (errno == EINTR)
12915 entry->cmdtype = CMDUNKNOWN;
12919 entry->cmdtype = CMDNORMAL;
12923 /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */
12925 updatetbl = (path == pathval());
12928 if (strstr(path, "%builtin") != NULL)
12929 act |= DO_ALTBLTIN;
12932 /* If name is in the table, check answer will be ok */
12933 cmdp = cmdlookup(name, 0);
12934 if (cmdp != NULL) {
12937 switch (cmdp->cmdtype) {
12955 } else if (cmdp->rehash == 0)
12956 /* if not invalidated by cd, we're done */
12960 /* If %builtin not in path, check for builtin next */
12961 bcmd = find_builtin(name);
12963 if (IS_BUILTIN_REGULAR(bcmd))
12964 goto builtin_success;
12965 if (act & DO_ALTPATH) {
12966 if (!(act & DO_ALTBLTIN))
12967 goto builtin_success;
12968 } else if (builtinloc <= 0) {
12969 goto builtin_success;
12973 #if ENABLE_FEATURE_SH_STANDALONE
12975 int applet_no = find_applet_by_name(name);
12976 if (applet_no >= 0) {
12977 entry->cmdtype = CMDNORMAL;
12978 entry->u.index = -2 - applet_no;
12984 /* We have to search path. */
12985 prev = -1; /* where to start */
12986 if (cmdp && cmdp->rehash) { /* doing a rehash */
12987 if (cmdp->cmdtype == CMDBUILTIN)
12990 prev = cmdp->param.index;
12996 while ((fullname = path_advance(&path, name)) != NULL) {
12997 stunalloc(fullname);
12998 /* NB: code below will still use fullname
12999 * despite it being "unallocated" */
13002 if (prefix(pathopt, "builtin")) {
13004 goto builtin_success;
13007 if ((act & DO_NOFUNC)
13008 || !prefix(pathopt, "func")
13009 ) { /* ignore unimplemented options */
13013 /* if rehash, don't redo absolute path names */
13014 if (fullname[0] == '/' && idx <= prev) {
13017 TRACE(("searchexec \"%s\": no change\n", name));
13020 while (stat(fullname, &statb) < 0) {
13022 if (errno == EINTR)
13025 if (errno != ENOENT && errno != ENOTDIR)
13029 e = EACCES; /* if we fail, this will be the error */
13030 if (!S_ISREG(statb.st_mode))
13032 if (pathopt) { /* this is a %func directory */
13033 stalloc(strlen(fullname) + 1);
13034 /* NB: stalloc will return space pointed by fullname
13035 * (because we don't have any intervening allocations
13036 * between stunalloc above and this stalloc) */
13037 readcmdfile(fullname);
13038 cmdp = cmdlookup(name, 0);
13039 if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION)
13040 ash_msg_and_raise_error("%s not defined in %s", name, fullname);
13041 stunalloc(fullname);
13044 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
13046 entry->cmdtype = CMDNORMAL;
13047 entry->u.index = idx;
13051 cmdp = cmdlookup(name, 1);
13052 cmdp->cmdtype = CMDNORMAL;
13053 cmdp->param.index = idx;
13058 /* We failed. If there was an entry for this command, delete it */
13059 if (cmdp && updatetbl)
13060 delete_cmd_entry();
13062 ash_msg("%s: %s", name, errmsg(e, "not found"));
13063 entry->cmdtype = CMDUNKNOWN;
13068 entry->cmdtype = CMDBUILTIN;
13069 entry->u.cmd = bcmd;
13073 cmdp = cmdlookup(name, 1);
13074 cmdp->cmdtype = CMDBUILTIN;
13075 cmdp->param.cmd = bcmd;
13079 entry->cmdtype = cmdp->cmdtype;
13080 entry->u = cmdp->param;
13085 * The trap builtin.
13087 static int FAST_FUNC
13088 trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13092 int signo, exitcode;
13097 for (signo = 0; signo < NSIG; signo++) {
13098 char *tr = trap_ptr[signo];
13100 /* note: bash adds "SIG", but only if invoked
13101 * as "bash". If called as "sh", or if set -o posix,
13102 * then it prints short signal names.
13103 * We are printing short names: */
13104 out1fmt("trap -- %s %s\n",
13106 get_signame(signo));
13107 /* trap_ptr != trap only if we are in special-cased `trap` code.
13108 * In this case, we will exit very soon, no need to free(). */
13109 /* if (trap_ptr != trap && tp[0]) */
13114 if (trap_ptr != trap) {
13122 /* Why the second check?
13123 * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..."
13124 * In this case, NUM is signal no, not an action.
13127 if (ap[1] && !is_number(ap[0]))
13132 signo = get_signum(*ap);
13134 /* Mimic bash message exactly */
13135 ash_msg("%s: invalid signal specification", *ap);
13141 if (LONE_DASH(action))
13144 if (action[0]) /* not NULL and not "" and not "-" */
13145 may_have_traps = 1;
13146 action = ckstrdup(action);
13150 trap[signo] = action;
13161 /* ============ Builtins */
13163 #if ENABLE_ASH_HELP
13164 static int FAST_FUNC
13165 helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13171 "Built-in commands:\n"
13172 "------------------\n");
13173 for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
13174 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '),
13175 builtintab[i].name + 1);
13181 # if ENABLE_FEATURE_SH_STANDALONE
13183 const char *a = applet_names;
13185 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a);
13190 while (*a++ != '\0')
13195 newline_and_flush(stdout);
13196 return EXIT_SUCCESS;
13201 static int FAST_FUNC
13202 historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13204 show_history(line_input_state);
13205 return EXIT_SUCCESS;
13210 * The export and readonly commands.
13212 static int FAST_FUNC
13213 exportcmd(int argc UNUSED_PARAM, char **argv)
13223 /* "readonly" in bash accepts, but ignores -n.
13224 * We do the same: it saves a conditional in nextopt's param.
13227 while ((opt = nextopt("np")) != '\0') {
13229 flag_off = VEXPORT;
13232 if (argv[0][0] == 'r') {
13234 flag_off = 0; /* readonly ignores -n */
13236 flag_off = ~flag_off;
13238 /*if (opt_p_not_specified) - bash doesn't check this. Try "export -p NAME" */
13244 p = strchr(name, '=');
13248 vp = *findvar(hashvar(name), name);
13250 vp->flags = ((vp->flags | flag) & flag_off);
13254 setvar(name, p, (flag & flag_off));
13255 } while ((name = *++aptr) != NULL);
13260 /* No arguments. Show the list of exported or readonly vars.
13263 showvars(argv[0], flag, 0);
13268 * Delete a function if it exists.
13271 unsetfunc(const char *name)
13273 struct tblentry *cmdp;
13275 cmdp = cmdlookup(name, 0);
13276 if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION)
13277 delete_cmd_entry();
13281 * The unset builtin command. We unset the function before we unset the
13282 * variable to allow a function to be unset when there is a readonly variable
13283 * with the same name.
13285 static int FAST_FUNC
13286 unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13292 while ((i = nextopt("vf")) != 0) {
13296 for (ap = argptr; *ap; ap++) {
13307 static const unsigned char timescmd_str[] ALIGN1 = {
13308 ' ', offsetof(struct tms, tms_utime),
13309 '\n', offsetof(struct tms, tms_stime),
13310 ' ', offsetof(struct tms, tms_cutime),
13311 '\n', offsetof(struct tms, tms_cstime),
13314 static int FAST_FUNC
13315 timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13317 unsigned long clk_tck, s, t;
13318 const unsigned char *p;
13321 clk_tck = bb_clk_tck();
13326 t = *(clock_t *)(((char *) &buf) + p[1]);
13329 out1fmt("%lum%lu.%03lus%c",
13331 (t * 1000) / clk_tck,
13339 #if ENABLE_FEATURE_SH_MATH
13341 * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell.
13342 * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
13344 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
13346 static int FAST_FUNC
13347 letcmd(int argc UNUSED_PARAM, char **argv)
13353 ash_msg_and_raise_error("expression expected");
13355 i = ash_arith(*argv);
13363 * The read builtin. Options:
13364 * -r Do not interpret '\' specially
13365 * -s Turn off echo (tty only)
13366 * -n NCHARS Read NCHARS max
13367 * -p PROMPT Display PROMPT on stderr (if input is from tty)
13368 * -t SECONDS Timeout after SECONDS (tty or pipe only)
13369 * -u FD Read from given FD instead of fd 0
13370 * This uses unbuffered input, which may be avoidable in some cases.
13371 * TODO: bash also has:
13372 * -a ARRAY Read into array[0],[1],etc
13373 * -d DELIM End on DELIM char, not newline
13374 * -e Use line editing (tty only)
13376 static int FAST_FUNC
13377 readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13379 char *opt_n = NULL;
13380 char *opt_p = NULL;
13381 char *opt_t = NULL;
13382 char *opt_u = NULL;
13383 int read_flags = 0;
13387 while ((i = nextopt("p:u:rt:n:s")) != '\0') {
13396 read_flags |= BUILTIN_READ_SILENT;
13402 read_flags |= BUILTIN_READ_RAW;
13412 /* "read -s" needs to save/restore termios, can't allow ^C
13413 * to jump out of it.
13417 r = shell_builtin_read(setvar0,
13419 bltinlookup("IFS"), /* can be NULL */
13428 if ((uintptr_t)r == 1 && errno == EINTR) {
13429 /* to get SIGCHLD: sleep 1 & read x; echo $x */
13430 if (pending_sig == 0)
13434 if ((uintptr_t)r > 1)
13435 ash_msg_and_raise_error(r);
13437 return (uintptr_t)r;
13440 static int FAST_FUNC
13441 umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13443 static const char permuser[3] ALIGN1 = "ogu";
13446 int symbolic_mode = 0;
13448 while (nextopt("S") != '\0') {
13457 if (*argptr == NULL) {
13458 if (symbolic_mode) {
13459 char buf[sizeof(",u=rwx,g=rwx,o=rwx")];
13466 *p++ = permuser[i];
13468 /* mask is 0..0uuugggooo. i=2 selects uuu bits */
13469 if (!(mask & 0400)) *p++ = 'r';
13470 if (!(mask & 0200)) *p++ = 'w';
13471 if (!(mask & 0100)) *p++ = 'x';
13479 out1fmt("%04o\n", mask);
13482 char *modestr = *argptr;
13483 /* numeric umasks are taken as-is */
13484 /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
13485 if (!isdigit(modestr[0]))
13487 mask = bb_parse_mode(modestr, mask);
13488 if ((unsigned)mask > 0777) {
13489 ash_msg_and_raise_error("illegal mode: %s", modestr);
13491 if (!isdigit(modestr[0]))
13498 static int FAST_FUNC
13499 ulimitcmd(int argc UNUSED_PARAM, char **argv)
13501 return shell_builtin_ulimit(argv);
13504 /* ============ main() and helpers */
13507 * Called to exit the shell.
13516 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
13517 save_history(line_input_state);
13519 status = exitstatus;
13520 TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
13521 if (setjmp(loc.loc)) {
13522 if (exception_type == EXEXIT)
13523 status = exitstatus;
13526 exception_handler = &loc;
13532 /*free(p); - we'll exit soon */
13535 /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}".
13536 * our setjobctl(0) does not panic if tcsetpgrp fails inside it.
13539 flush_stdout_stderr();
13547 /* we will never free this */
13548 basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ);
13550 sigmode[SIGCHLD - 1] = S_DFL;
13551 setsignal(SIGCHLD);
13553 /* bash re-enables SIGHUP which is SIG_IGNed on entry.
13554 * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$"
13556 signal(SIGHUP, SIG_DFL);
13561 struct stat st1, st2;
13564 for (envp = environ; envp && *envp; envp++) {
13565 p = endofname(*envp);
13566 if (p != *envp && *p == '=') {
13567 setvareq(*envp, VEXPORT|VTEXTFIXED);
13571 setvareq((char*)defoptindvar, VTEXTFIXED);
13573 setvar0("PPID", utoa(getppid()));
13575 p = lookupvar("SHLVL");
13576 setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT);
13578 #if BASH_HOSTNAME_VAR
13579 if (!lookupvar("HOSTNAME")) {
13580 struct utsname uts;
13582 setvar0("HOSTNAME", uts.nodename);
13585 p = lookupvar("PWD");
13587 if (p[0] != '/' || stat(p, &st1) || stat(".", &st2)
13588 || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino
13598 //usage:#define ash_trivial_usage
13599 //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]"
13600 //usage:#define ash_full_usage "\n\n"
13601 //usage: "Unix shell interpreter"
13604 * Process the shell command line arguments.
13607 procargs(char **argv)
13610 const char *xminusc;
13615 login_sh = xargv[0] && xargv[0][0] == '-';
13617 /* if (xargv[0]) - mmm, this is always true! */
13619 for (i = 0; i < NOPTS; i++)
13622 if (options(/*cmdline:*/ 1, &login_sh)) {
13623 /* it already printed err message */
13624 raise_exception(EXERROR);
13628 if (*xargv == NULL) {
13630 ash_msg_and_raise_error(bb_msg_requires_arg, "-c");
13633 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
13637 for (i = 0; i < NOPTS; i++)
13638 if (optlist[i] == 2)
13643 /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */
13648 } else if (!sflag) {
13649 setinputfile(*xargv, 0);
13652 commandname = arg0;
13655 shellparam.p = xargv;
13656 #if ENABLE_ASH_GETOPTS
13657 shellparam.optind = 1;
13658 shellparam.optoff = -1;
13660 /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */
13662 shellparam.nparam++;
13671 * Read /etc/profile, ~/.profile, $ENV.
13674 read_profile(const char *name)
13676 name = expandstr(name, DQSYNTAX);
13677 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
13684 * This routine is called when an error or an interrupt occurs in an
13685 * interactive shell and control is returned to the main command loop.
13686 * (In dash, this function is auto-generated by build machinery).
13695 /* from expand.c: */
13698 /* from input.c: */
13699 g_parsefile->left_in_buffer = 0;
13700 g_parsefile->left_in_line = 0; /* clear input buffer */
13703 /* from redir.c: */
13707 unwindlocalvars(NULL);
13711 static short profile_buf[16384];
13712 extern int etext();
13716 * Main routine. We initialize things, parse the arguments, execute
13717 * profiles if we're a login shell, and then call cmdloop to execute
13718 * commands. The setjmp call sets up the location to jump to when an
13719 * exception occurs. When an exception occurs the variable "state"
13720 * is used to figure out how far we had gotten.
13722 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
13723 int ash_main(int argc UNUSED_PARAM, char **argv)
13725 volatile smallint state;
13726 struct jmploc jmploc;
13727 struct stackmark smark;
13730 /* Initialize global data */
13734 #if ENABLE_ASH_ALIAS
13740 monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
13743 #if ENABLE_FEATURE_EDITING
13744 line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
13747 if (setjmp(jmploc.loc)) {
13753 e = exception_type;
13755 if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) {
13759 newline_and_flush(stderr);
13762 popstackmark(&smark);
13763 FORCE_INT_ON; /* enable interrupts */
13772 exception_handler = &jmploc;
13773 rootpid = getpid();
13776 setstackmark(&smark);
13777 login_sh = procargs(argv);
13779 TRACE(("Shell args: "));
13780 trace_puts_args(argv);
13787 read_profile("/etc/profile");
13790 hp = lookupvar("HOME");
13792 read_profile("$HOME/.profile");
13798 getuid() == geteuid() && getgid() == getegid() &&
13802 const char *shinit = lookupvar("ENV");
13803 if (shinit != NULL && *shinit != '\0')
13804 read_profile(shinit);
13806 popstackmark(&smark);
13810 /* evalstring pushes parsefile stack.
13811 * Ensure we don't falsely claim that 0 (stdin)
13812 * is one of stacked source fds.
13813 * Testcase: ash -c 'exec 1>&0' must not complain. */
13814 // if (!sflag) g_parsefile->pf_fd = -1;
13815 // ^^ not necessary since now we special-case fd 0
13816 // in save_fd_on_redirect()
13817 evalstring(minusc, sflag ? 0 : EV_EXIT);
13820 if (sflag || minusc == NULL) {
13821 #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
13823 const char *hp = lookupvar("HISTFILE");
13825 hp = lookupvar("HOME");
13828 hp = concat_path_file(hp, ".ash_history");
13829 setvar0("HISTFILE", hp);
13832 hp = lookupvar("HISTFILE");
13836 line_input_state->hist_file = hp;
13837 # if ENABLE_FEATURE_SH_HISTFILESIZE
13838 hp = lookupvar("HISTFILESIZE");
13839 line_input_state->max_history = size_from_HISTFILESIZE(hp);
13843 state4: /* XXX ??? - why isn't this before the "if" statement */
13851 extern void _mcleanup(void);
13855 TRACE(("End of main reached\n"));
13862 * Copyright (c) 1989, 1991, 1993, 1994
13863 * The Regents of the University of California. All rights reserved.
13865 * This code is derived from software contributed to Berkeley by
13866 * Kenneth Almquist.
13868 * Redistribution and use in source and binary forms, with or without
13869 * modification, are permitted provided that the following conditions
13871 * 1. Redistributions of source code must retain the above copyright
13872 * notice, this list of conditions and the following disclaimer.
13873 * 2. Redistributions in binary form must reproduce the above copyright
13874 * notice, this list of conditions and the following disclaimer in the
13875 * documentation and/or other materials provided with the distribution.
13876 * 3. Neither the name of the University nor the names of its contributors
13877 * may be used to endorse or promote products derived from this software
13878 * without specific prior written permission.
13880 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
13881 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13882 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13883 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
13884 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13885 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
13886 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13887 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13888 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13889 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF