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 #if ENABLE_ASH_RANDOM_SUPPORT
221 # define CLEAR_RANDOM_T(rnd) ((void)0)
224 #include "NUM_APPLETS.h"
226 /* STANDALONE does not make sense, and won't compile */
227 # undef CONFIG_FEATURE_SH_STANDALONE
228 # undef ENABLE_FEATURE_SH_STANDALONE
229 # undef IF_FEATURE_SH_STANDALONE
230 # undef IF_NOT_FEATURE_SH_STANDALONE
231 # define ENABLE_FEATURE_SH_STANDALONE 0
232 # define IF_FEATURE_SH_STANDALONE(...)
233 # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
237 # define PIPE_BUF 4096 /* amount of buffering in a pipe */
241 # error "Do not even bother, ash will not run on NOMMU machine"
245 /* ============ Hash table sizes. Configurable. */
249 #define CMDTABLESIZE 31 /* should be prime */
252 /* ============ Shell options */
254 static const char *const optletters_optnames[] = {
278 #define optletters(n) optletters_optnames[n][0]
279 #define optnames(n) (optletters_optnames[n] + 1)
281 enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
284 /* ============ Misc data */
286 #define msg_illnum "Illegal number: %s"
289 * We enclose jmp_buf in a structure so that we can declare pointers to
290 * jump locations. The global variable handler contains the location to
291 * jump to when an exception occurs, and the global variable exception_type
292 * contains a code identifying the exception. To implement nested
293 * exception handlers, the user should save the value of handler on entry
294 * to an inner scope, set handler to point to a jmploc structure for the
295 * inner scope, and restore handler on exit from the scope.
301 struct globals_misc {
302 uint8_t exitstatus; /* exit status of last command */
303 uint8_t back_exitstatus;/* exit status of backquoted command */
304 smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */
305 int rootpid; /* pid of main shell */
306 /* shell level: 0 for the main shell, 1 for its children, and so on */
308 #define rootshell (!shlvl)
309 char *minusc; /* argument to -c option */
311 char *curdir; // = nullstr; /* current working directory */
312 char *physdir; // = nullstr; /* physical working directory */
314 char *arg0; /* value of $0 */
316 struct jmploc *exception_handler;
318 volatile int suppress_int; /* counter */
319 volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */
320 volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */
321 volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */
322 smallint exception_type; /* kind of exception (0..5) */
324 #define EXINT 0 /* SIGINT received */
325 #define EXERROR 1 /* a generic error */
326 #define EXEXIT 4 /* exit the shell */
329 char nullstr[1]; /* zero length string */
332 #define eflag optlist[0]
333 #define fflag optlist[1]
334 #define Iflag optlist[2]
335 #define iflag optlist[3]
336 #define mflag optlist[4]
337 #define nflag optlist[5]
338 #define sflag optlist[6]
339 #define xflag optlist[7]
340 #define vflag optlist[8]
341 #define Cflag optlist[9]
342 #define aflag optlist[10]
343 #define bflag optlist[11]
344 #define uflag optlist[12]
345 #define viflag optlist[13]
347 # define pipefail optlist[14]
352 # define nolog optlist[14 + BASH_PIPEFAIL]
353 # define debug optlist[15 + BASH_PIPEFAIL]
356 /* trap handler commands */
358 * Sigmode records the current value of the signal handlers for the various
359 * modes. A value of zero means that the current handler is not known.
360 * S_HARD_IGN indicates that the signal was ignored on entry to the shell.
362 char sigmode[NSIG - 1];
363 #define S_DFL 1 /* default signal handling (SIG_DFL) */
364 #define S_CATCH 2 /* signal is caught */
365 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
366 #define S_HARD_IGN 4 /* signal is ignored permanently */
368 /* indicates specified signal received */
369 uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */
370 uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */
372 char **trap_ptr; /* used only by "trap hack" */
374 /* Rarely referenced stuff */
375 #if ENABLE_ASH_RANDOM_SUPPORT
378 pid_t backgndpid; /* pid of last background process */
380 extern struct globals_misc *const ash_ptr_to_globals_misc;
381 #define G_misc (*ash_ptr_to_globals_misc)
382 #define exitstatus (G_misc.exitstatus )
383 #define back_exitstatus (G_misc.back_exitstatus )
384 #define job_warning (G_misc.job_warning)
385 #define rootpid (G_misc.rootpid )
386 #define shlvl (G_misc.shlvl )
387 #define minusc (G_misc.minusc )
388 #define curdir (G_misc.curdir )
389 #define physdir (G_misc.physdir )
390 #define arg0 (G_misc.arg0 )
391 #define exception_handler (G_misc.exception_handler)
392 #define exception_type (G_misc.exception_type )
393 #define suppress_int (G_misc.suppress_int )
394 #define pending_int (G_misc.pending_int )
395 #define got_sigchld (G_misc.got_sigchld )
396 #define pending_sig (G_misc.pending_sig )
397 #define isloginsh (G_misc.isloginsh )
398 #define nullstr (G_misc.nullstr )
399 #define optlist (G_misc.optlist )
400 #define sigmode (G_misc.sigmode )
401 #define gotsig (G_misc.gotsig )
402 #define may_have_traps (G_misc.may_have_traps )
403 #define trap (G_misc.trap )
404 #define trap_ptr (G_misc.trap_ptr )
405 #define random_gen (G_misc.random_gen )
406 #define backgndpid (G_misc.backgndpid )
407 #define INIT_G_misc() do { \
408 (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
416 /* ============ DEBUG */
418 static void trace_printf(const char *fmt, ...);
419 static void trace_vprintf(const char *fmt, va_list va);
420 # define TRACE(param) trace_printf param
421 # define TRACEV(param) trace_vprintf param
422 # define close(fd) do { \
424 if (close(dfd) < 0) \
425 bb_error_msg("bug on %d: closing %d(0x%x)", \
426 __LINE__, dfd, dfd); \
429 # define TRACE(param)
430 # define TRACEV(param)
434 /* ============ Utility functions */
435 #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
436 #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
439 isdigit_str9(const char *str)
441 int maxlen = 9 + 1; /* max 9 digits: 999999999 */
442 while (--maxlen && isdigit(*str))
444 return (*str == '\0');
448 var_end(const char *var)
457 /* ============ Interrupts / exceptions */
459 static void exitshell(void) NORETURN;
462 * These macros allow the user to suspend the handling of interrupt signals
463 * over a period of time. This is similar to SIGHOLD or to sigblock, but
464 * much more efficient and portable. (But hacking the kernel is so much
465 * more fun than worrying about efficiency and portability. :-))
468 # define INT_OFF do { \
469 TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \
474 # define INT_OFF do { \
481 * Called to raise an exception. Since C doesn't include exceptions, we
482 * just do a longjmp to the exception handler. The type of exception is
483 * stored in the global variable "exception_type".
485 static void raise_exception(int) NORETURN;
487 raise_exception(int e)
490 if (exception_handler == NULL)
495 longjmp(exception_handler->loc, 1);
498 #define raise_exception(e) do { \
499 TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \
500 raise_exception(e); \
505 * Called when a SIGINT is received. (If the user specifies
506 * that SIGINT is to be trapped or ignored using the trap builtin, then
507 * this routine is not called.) Suppressint is nonzero when interrupts
508 * are held using the INT_OFF macro. (The test for iflag is just
509 * defensive programming.)
511 static void raise_interrupt(void) NORETURN;
513 raise_interrupt(void)
516 /* Signal is not automatically unmasked after it is raised,
517 * do it ourself - unmask all signals */
518 sigprocmask_allsigs(SIG_UNBLOCK);
519 /* pending_sig = 0; - now done in signal_handler() */
521 if (!(rootshell && iflag)) {
522 /* Kill ourself with SIGINT */
523 signal(SIGINT, SIG_DFL);
526 /* bash: ^C even on empty command line sets $? */
527 exitstatus = SIGINT + 128;
528 raise_exception(EXINT);
532 #define raise_interrupt() do { \
533 TRACE(("raising interrupt on line %d\n", __LINE__)); \
538 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
542 if (--suppress_int == 0 && pending_int) {
547 # define INT_ON do { \
548 TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \
552 # define INT_ON int_on()
554 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
562 #define FORCE_INT_ON force_int_on()
564 #define SAVE_INT(v) ((v) = suppress_int)
566 #define RESTORE_INT(v) do { \
568 suppress_int = (v); \
569 if (suppress_int == 0 && pending_int) \
574 /* ============ Stdout/stderr output */
577 outstr(const char *p, FILE *file)
585 flush_stdout_stderr(void)
592 /* Was called outcslow(c,FILE*), but c was always '\n' */
594 newline_and_flush(FILE *dest)
602 static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2)));
604 out1fmt(const char *fmt, ...)
611 r = vprintf(fmt, ap);
617 static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4)));
619 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
626 ret = vsnprintf(outbuf, length, fmt, ap);
633 out1str(const char *p)
639 out2str(const char *p)
642 flush_stdout_stderr();
646 /* ============ Parser structures */
648 /* control characters in argument strings */
649 #define CTL_FIRST CTLESC
650 #define CTLESC ((unsigned char)'\201') /* escape next character */
651 #define CTLVAR ((unsigned char)'\202') /* variable defn */
652 #define CTLENDVAR ((unsigned char)'\203')
653 #define CTLBACKQ ((unsigned char)'\204')
654 #define CTLARI ((unsigned char)'\206') /* arithmetic expression */
655 #define CTLENDARI ((unsigned char)'\207')
656 #define CTLQUOTEMARK ((unsigned char)'\210')
657 #define CTL_LAST CTLQUOTEMARK
659 /* variable substitution byte (follows CTLVAR) */
660 #define VSTYPE 0x0f /* type of variable substitution */
661 #define VSNUL 0x10 /* colon--treat the empty string as unset */
663 /* values of VSTYPE field */
664 #define VSNORMAL 0x1 /* normal variable: $var or ${var} */
665 #define VSMINUS 0x2 /* ${var-text} */
666 #define VSPLUS 0x3 /* ${var+text} */
667 #define VSQUESTION 0x4 /* ${var?message} */
668 #define VSASSIGN 0x5 /* ${var=text} */
669 #define VSTRIMRIGHT 0x6 /* ${var%pattern} */
670 #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */
671 #define VSTRIMLEFT 0x8 /* ${var#pattern} */
672 #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */
673 #define VSLENGTH 0xa /* ${#var} */
675 #define VSSUBSTR 0xc /* ${var:position:length} */
677 #if BASH_PATTERN_SUBST
678 #define VSREPLACE 0xd /* ${var/pattern/replacement} */
679 #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */
682 static const char dolatstr[] ALIGN1 = {
683 CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0'
685 #define DOLATSTRLEN 6
704 #if BASH_REDIR_OUTPUT
721 smallint type; /* Nxxxx */
724 union node *redirect;
729 smallint pipe_backgnd;
730 struct nodelist *cmdlist;
736 union node *redirect;
749 union node *elsepart;
776 struct nodelist *backquote;
779 /* nfile and ndup layout must match!
780 * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight
781 * that it is actually NTO2 (>&file), and change its type.
798 char *_unused_expfname;
817 struct nredir nredir;
818 struct nbinary nbinary;
822 struct nclist nclist;
831 * NODE_EOF is returned by parsecmd when it encounters an end of file.
832 * It must be distinct from NULL.
834 #define NODE_EOF ((union node *) -1L)
837 struct nodelist *next;
850 freefunc(struct funcnode *f)
852 if (f && --f->count < 0)
857 /* ============ Debugging output */
861 static FILE *tracefile;
864 trace_printf(const char *fmt, ...)
871 fprintf(tracefile, "%u ", (int) time(NULL));
873 fprintf(tracefile, "[%u] ", (int) getpid());
875 fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int);
877 vfprintf(tracefile, fmt, va);
882 trace_vprintf(const char *fmt, va_list va)
886 vfprintf(tracefile, fmt, va);
887 fprintf(tracefile, "\n");
891 trace_puts(const char *s)
899 trace_puts_quoted(char *s)
906 putc('"', tracefile);
907 for (p = s; *p; p++) {
908 switch ((unsigned char)*p) {
909 case '\n': c = 'n'; goto backslash;
910 case '\t': c = 't'; goto backslash;
911 case '\r': c = 'r'; goto backslash;
912 case '\"': c = '\"'; goto backslash;
913 case '\\': c = '\\'; goto backslash;
914 case CTLESC: c = 'e'; goto backslash;
915 case CTLVAR: c = 'v'; goto backslash;
916 case CTLBACKQ: c = 'q'; goto backslash;
918 putc('\\', tracefile);
922 if (*p >= ' ' && *p <= '~')
925 putc('\\', tracefile);
926 putc((*p >> 6) & 03, tracefile);
927 putc((*p >> 3) & 07, tracefile);
928 putc(*p & 07, tracefile);
933 putc('"', tracefile);
937 trace_puts_args(char **ap)
944 trace_puts_quoted(*ap);
946 putc('\n', tracefile);
949 putc(' ', tracefile);
964 /* leave open because libedit might be using it */
967 strcpy(s, "./trace");
969 if (!freopen(s, "a", tracefile)) {
970 fprintf(stderr, "Can't re-open %s\n", s);
975 tracefile = fopen(s, "a");
976 if (tracefile == NULL) {
977 fprintf(stderr, "Can't open %s\n", s);
983 flags = fcntl(fileno(tracefile), F_GETFL);
985 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
987 setlinebuf(tracefile);
988 fputs("\nTracing started.\n", tracefile);
992 indent(int amount, char *pfx, FILE *fp)
996 for (i = 0; i < amount; i++) {
997 if (pfx && i == amount - 1)
1003 /* little circular references here... */
1004 static void shtree(union node *n, int ind, char *pfx, FILE *fp);
1007 sharg(union node *arg, FILE *fp)
1010 struct nodelist *bqlist;
1011 unsigned char subtype;
1013 if (arg->type != NARG) {
1014 out1fmt("<node type %d>\n", arg->type);
1017 bqlist = arg->narg.backquote;
1018 for (p = arg->narg.text; *p; p++) {
1019 switch ((unsigned char)*p) {
1028 if (subtype == VSLENGTH)
1036 if (subtype & VSNUL)
1039 switch (subtype & VSTYPE) {
1065 case VSTRIMRIGHTMAX:
1072 out1fmt("<subtype %d>", subtype);
1081 shtree(bqlist->n, -1, NULL, fp);
1092 shcmd(union node *cmd, FILE *fp)
1100 for (np = cmd->ncmd.args; np; np = np->narg.next) {
1106 for (np = cmd->ncmd.redirect; np; np = np->nfile.next) {
1110 switch (np->nfile.type) {
1111 case NTO: s = ">>"+1; dftfd = 1; break;
1112 case NCLOBBER: s = ">|"; dftfd = 1; break;
1113 case NAPPEND: s = ">>"; dftfd = 1; break;
1114 #if BASH_REDIR_OUTPUT
1117 case NTOFD: s = ">&"; dftfd = 1; break;
1118 case NFROM: s = "<"; break;
1119 case NFROMFD: s = "<&"; break;
1120 case NFROMTO: s = "<>"; break;
1121 default: s = "*error*"; break;
1123 if (np->nfile.fd != dftfd)
1124 fprintf(fp, "%d", np->nfile.fd);
1126 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1127 fprintf(fp, "%d", np->ndup.dupfd);
1129 sharg(np->nfile.fname, fp);
1136 shtree(union node *n, int ind, char *pfx, FILE *fp)
1138 struct nodelist *lp;
1144 indent(ind, pfx, fp);
1146 if (n == NODE_EOF) {
1161 shtree(n->nbinary.ch1, ind, NULL, fp);
1164 shtree(n->nbinary.ch2, ind, NULL, fp);
1172 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
1173 shtree(lp->n, 0, NULL, fp);
1177 if (n->npipe.pipe_backgnd)
1183 fprintf(fp, "<node type %d>", n->type);
1191 showtree(union node *n)
1193 trace_puts("showtree called\n");
1194 shtree(n, 1, NULL, stderr);
1200 /* ============ Parser data */
1203 * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up.
1206 struct strlist *next;
1213 struct strpush *prev; /* preceding string on stack */
1215 int prev_left_in_line;
1216 #if ENABLE_ASH_ALIAS
1217 struct alias *ap; /* if push was associated with an alias */
1219 char *string; /* remember the string since it may change */
1221 /* Remember last two characters for pungetc. */
1224 /* Number of outstanding calls to pungetc. */
1229 struct parsefile *prev; /* preceding file on stack */
1230 int linno; /* current line */
1231 int pf_fd; /* file descriptor (or -1 if string) */
1232 int left_in_line; /* number of chars left in this line */
1233 int left_in_buffer; /* number of chars left in this buffer past the line */
1234 char *next_to_pgetc; /* next char in buffer */
1235 char *buf; /* input buffer */
1236 struct strpush *strpush; /* for pushing strings at this level */
1237 struct strpush basestrpush; /* so pushing one is fast */
1239 /* Remember last two characters for pungetc. */
1242 /* Number of outstanding calls to pungetc. */
1246 static struct parsefile basepf; /* top level input file */
1247 static struct parsefile *g_parsefile = &basepf; /* current input file */
1248 static int startlinno; /* line # where last token started */
1249 static char *commandname; /* currently executing command */
1250 static struct strlist *cmdenviron; /* environment for builtin 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);
1307 static void raise_error_syntax(const char *) NORETURN;
1309 raise_error_syntax(const char *msg)
1311 ash_msg_and_raise_error("syntax error: %s", msg);
1315 static void ash_msg_and_raise(int, const char *, ...) NORETURN;
1317 ash_msg_and_raise(int cond, const char *msg, ...)
1322 ash_vmsg_and_raise(cond, msg, ap);
1328 * error/warning routines for external builtins
1331 ash_msg(const char *fmt, ...)
1341 * Return a string describing an error. The returned string may be a
1342 * pointer to a static buffer that will be overwritten on the next call.
1343 * Action describes the operation that got the error.
1346 errmsg(int e, const char *em)
1348 if (e == ENOENT || e == ENOTDIR) {
1355 /* ============ Memory allocation */
1358 /* I consider these wrappers nearly useless:
1359 * ok, they return you to nearest exception handler, but
1360 * how much memory do you leak in the process, making
1361 * memory starvation worse?
1364 ckrealloc(void * p, size_t nbytes)
1366 p = realloc(p, nbytes);
1368 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1373 ckmalloc(size_t nbytes)
1375 return ckrealloc(NULL, nbytes);
1379 ckzalloc(size_t nbytes)
1381 return memset(ckmalloc(nbytes), 0, nbytes);
1385 ckstrdup(const char *s)
1387 char *p = strdup(s);
1389 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1393 /* Using bbox equivalents. They exit if out of memory */
1394 # define ckrealloc xrealloc
1395 # define ckmalloc xmalloc
1396 # define ckzalloc xzalloc
1397 # define ckstrdup xstrdup
1401 * It appears that grabstackstr() will barf with such alignments
1402 * because stalloc() will return a string allocated in a new stackblock.
1404 #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE)
1406 /* Most machines require the value returned from malloc to be aligned
1407 * in some way. The following macro will get this right
1408 * on many machines. */
1409 SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,
1410 /* Minimum size of a block */
1411 MINSIZE = SHELL_ALIGN(504),
1414 struct stack_block {
1415 struct stack_block *prev;
1416 char space[MINSIZE];
1420 struct stack_block *stackp;
1426 struct globals_memstack {
1427 struct stack_block *g_stackp; // = &stackbase;
1428 char *g_stacknxt; // = stackbase.space;
1429 char *sstrend; // = stackbase.space + MINSIZE;
1430 size_t g_stacknleft; // = MINSIZE;
1431 struct stack_block stackbase;
1433 extern struct globals_memstack *const ash_ptr_to_globals_memstack;
1434 #define G_memstack (*ash_ptr_to_globals_memstack)
1435 #define g_stackp (G_memstack.g_stackp )
1436 #define g_stacknxt (G_memstack.g_stacknxt )
1437 #define sstrend (G_memstack.sstrend )
1438 #define g_stacknleft (G_memstack.g_stacknleft)
1439 #define stackbase (G_memstack.stackbase )
1440 #define INIT_G_memstack() do { \
1441 (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
1443 g_stackp = &stackbase; \
1444 g_stacknxt = stackbase.space; \
1445 g_stacknleft = MINSIZE; \
1446 sstrend = stackbase.space + MINSIZE; \
1450 #define stackblock() ((void *)g_stacknxt)
1451 #define stackblocksize() g_stacknleft
1454 * Parse trees for commands are allocated in lifo order, so we use a stack
1455 * to make this more efficient, and also to avoid all sorts of exception
1456 * handling code to handle interrupts in the middle of a parse.
1458 * The size 504 was chosen because the Ultrix malloc handles that size
1462 stalloc(size_t nbytes)
1467 aligned = SHELL_ALIGN(nbytes);
1468 if (aligned > g_stacknleft) {
1471 struct stack_block *sp;
1473 blocksize = aligned;
1474 if (blocksize < MINSIZE)
1475 blocksize = MINSIZE;
1476 len = sizeof(struct stack_block) - MINSIZE + blocksize;
1477 if (len < blocksize)
1478 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1481 sp->prev = g_stackp;
1482 g_stacknxt = sp->space;
1483 g_stacknleft = blocksize;
1484 sstrend = g_stacknxt + blocksize;
1489 g_stacknxt += aligned;
1490 g_stacknleft -= aligned;
1495 stzalloc(size_t nbytes)
1497 return memset(stalloc(nbytes), 0, nbytes);
1504 if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) {
1505 write(STDERR_FILENO, "stunalloc\n", 10);
1509 g_stacknleft += g_stacknxt - (char *)p;
1514 * Like strdup but works with the ash stack.
1517 sstrdup(const char *p)
1519 size_t len = strlen(p) + 1;
1520 return memcpy(stalloc(len), p, len);
1524 grabstackblock(size_t len)
1530 pushstackmark(struct stackmark *mark, size_t len)
1532 mark->stackp = g_stackp;
1533 mark->stacknxt = g_stacknxt;
1534 mark->stacknleft = g_stacknleft;
1535 grabstackblock(len);
1539 setstackmark(struct stackmark *mark)
1541 pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase);
1545 popstackmark(struct stackmark *mark)
1547 struct stack_block *sp;
1553 while (g_stackp != mark->stackp) {
1555 g_stackp = sp->prev;
1558 g_stacknxt = mark->stacknxt;
1559 g_stacknleft = mark->stacknleft;
1560 sstrend = mark->stacknxt + mark->stacknleft;
1565 * When the parser reads in a string, it wants to stick the string on the
1566 * stack and only adjust the stack pointer when it knows how big the
1567 * string is. Stackblock (defined in stack.h) returns a pointer to a block
1568 * of space on top of the stack and stackblocklen returns the length of
1569 * this block. Growstackblock will grow this space by at least one byte,
1570 * possibly moving it (like realloc). Grabstackblock actually allocates the
1571 * part of the block that has been used.
1574 growstackblock(void)
1578 newlen = g_stacknleft * 2;
1579 if (newlen < g_stacknleft)
1580 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1584 if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) {
1585 struct stack_block *sp;
1586 struct stack_block *prevstackp;
1591 prevstackp = sp->prev;
1592 grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
1593 sp = ckrealloc(sp, grosslen);
1594 sp->prev = prevstackp;
1596 g_stacknxt = sp->space;
1597 g_stacknleft = newlen;
1598 sstrend = sp->space + newlen;
1601 char *oldspace = g_stacknxt;
1602 size_t oldlen = g_stacknleft;
1603 char *p = stalloc(newlen);
1605 /* free the space we just allocated */
1606 g_stacknxt = memcpy(p, oldspace, oldlen);
1607 g_stacknleft += newlen;
1612 * The following routines are somewhat easier to use than the above.
1613 * The user declares a variable of type STACKSTR, which may be declared
1614 * to be a register. The macro STARTSTACKSTR initializes things. Then
1615 * the user uses the macro STPUTC to add characters to the string. In
1616 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
1617 * grown as necessary. When the user is done, she can just leave the
1618 * string there and refer to it using stackblock(). Or she can allocate
1619 * the space for it using grabstackstr(). If it is necessary to allow
1620 * someone else to use the stack temporarily and then continue to grow
1621 * the string, the user should use grabstack to allocate the space, and
1622 * then call ungrabstr(p) to return to the previous mode of operation.
1624 * USTPUTC is like STPUTC except that it doesn't check for overflow.
1625 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
1626 * is space for at least one character.
1631 size_t len = stackblocksize();
1633 return (char *)stackblock() + len;
1637 * Called from CHECKSTRSPACE.
1640 makestrspace(size_t newlen, char *p)
1642 size_t len = p - g_stacknxt;
1648 size = stackblocksize();
1650 if (nleft >= newlen)
1654 return (char *)stackblock() + len;
1658 stack_nputstr(const char *s, size_t n, char *p)
1660 p = makestrspace(n, p);
1661 p = (char *)mempcpy(p, s, n);
1666 stack_putstr(const char *s, char *p)
1668 return stack_nputstr(s, strlen(s), p);
1672 _STPUTC(int c, char *p)
1680 #define STARTSTACKSTR(p) ((p) = stackblock())
1681 #define STPUTC(c, p) ((p) = _STPUTC((c), (p)))
1682 #define CHECKSTRSPACE(n, p) do { \
1685 size_t m = sstrend - q; \
1687 (p) = makestrspace(l, q); \
1689 #define USTPUTC(c, p) (*(p)++ = (c))
1690 #define STACKSTRNUL(p) do { \
1691 if ((p) == sstrend) \
1692 (p) = growstackstr(); \
1695 #define STUNPUTC(p) (--(p))
1696 #define STTOPC(p) ((p)[-1])
1697 #define STADJUST(amount, p) ((p) += (amount))
1699 #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock())
1700 #define ungrabstackstr(s, p) stunalloc(s)
1701 #define stackstrend() ((void *)sstrend)
1704 /* ============ String helpers */
1707 * prefix -- see if pfx is a prefix of string.
1710 prefix(const char *string, const char *pfx)
1713 if (*pfx++ != *string++)
1716 return (char *) string;
1720 * Check for a valid number. This should be elsewhere.
1723 is_number(const char *p)
1728 } while (*++p != '\0');
1733 * Convert a string of digits to an integer, printing an error message on
1737 number(const char *s)
1740 ash_msg_and_raise_error(msg_illnum, s);
1745 * Produce a single quoted string suitable as input to the shell.
1746 * The return string is allocated on the stack.
1749 single_quote(const char *s)
1759 len = strchrnul(s, '\'') - s;
1761 q = p = makestrspace(len + 3, p);
1764 q = (char *)mempcpy(q, s, len) + len;
1773 do len++; while (*++s == '\'');
1775 q = p = makestrspace(len + 3, p);
1778 q = (char *)mempcpy(q, s - len, len);
1786 return stackblock();
1790 * Produce a possibly single quoted string suitable as input to the shell.
1791 * If 'conditional' is nonzero, quoting is only done if the string contains
1792 * non-shellsafe characters, or is identical to a shell keyword (reserved
1793 * word); if it is zero, quoting is always done.
1794 * If quoting was done, the return string is allocated on the stack,
1795 * otherwise a pointer to the original string is returned.
1798 maybe_single_quote(const char *s)
1803 /* Assuming ACSII */
1804 /* quote ctrl_chars space !"#$%&'()* */
1808 if (*p >= ';' && *p <= '?')
1817 /* quote {|}~ DEL and high bytes */
1820 /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */
1821 /* TODO: maybe avoid quoting % */
1827 return single_quote(s);
1831 /* ============ nextopt */
1833 static char **argptr; /* argument list for builtin commands */
1834 static char *optionarg; /* set by nextopt (like getopt) */
1835 static char *optptr; /* used by nextopt */
1838 * XXX - should get rid of. Have all builtins use getopt(3).
1839 * The library getopt must have the BSD extension static variable
1840 * "optreset", otherwise it can't be used within the shell safely.
1842 * Standard option processing (a la getopt) for builtin routines.
1843 * The only argument that is passed to nextopt is the option string;
1844 * the other arguments are unnecessary. It returns the character,
1845 * or '\0' on end of input.
1848 nextopt(const char *optstring)
1855 if (p == NULL || *p == '\0') {
1856 /* We ate entire "-param", take next one */
1862 if (*++p == '\0') /* just "-" ? */
1865 if (LONE_DASH(p)) /* "--" ? */
1867 /* p => next "-param" */
1869 /* p => some option char in the middle of a "-param" */
1871 for (q = optstring; *q != c;) {
1873 ash_msg_and_raise_error("illegal option -%c", c);
1881 ash_msg_and_raise_error("no arg for -%c option", c);
1891 /* ============ Shell variables */
1894 * The parsefile structure pointed to by the global variable parsefile
1895 * contains information about the current file being read.
1898 int nparam; /* # of positional parameters (without $0) */
1899 #if ENABLE_ASH_GETOPTS
1900 int optind; /* next parameter to be processed by getopts */
1901 int optoff; /* used by getopts */
1903 unsigned char malloced; /* if parameter list dynamically allocated */
1904 char **p; /* parameter list */
1908 * Free the list of positional parameters.
1911 freeparam(volatile struct shparam *param)
1913 if (param->malloced) {
1915 ap = ap1 = param->p;
1922 #if ENABLE_ASH_GETOPTS
1923 static void FAST_FUNC getoptsreset(const char *value);
1927 struct var *next; /* next entry in hash list */
1928 int flags; /* flags are defined above */
1929 const char *var_text; /* name=value */
1930 void (*var_func)(const char *) FAST_FUNC; /* function to be called when */
1931 /* the variable gets set/unset */
1935 struct localvar *next; /* next local variable in list */
1936 struct var *vp; /* the variable that was made local */
1937 int flags; /* saved flags */
1938 const char *text; /* saved text */
1942 #define VEXPORT 0x01 /* variable is exported */
1943 #define VREADONLY 0x02 /* variable cannot be modified */
1944 #define VSTRFIXED 0x04 /* variable struct is statically allocated */
1945 #define VTEXTFIXED 0x08 /* text is statically allocated */
1946 #define VSTACK 0x10 /* text is allocated on the stack */
1947 #define VUNSET 0x20 /* the variable is not set */
1948 #define VNOFUNC 0x40 /* don't call the callback function */
1949 #define VNOSET 0x80 /* do not set variable - just readonly test */
1950 #define VNOSAVE 0x100 /* when text is on the heap before setvareq */
1951 #if ENABLE_ASH_RANDOM_SUPPORT
1952 # define VDYNAMIC 0x200 /* dynamic variable */
1958 /* Need to be before varinit_data[] */
1959 #if ENABLE_LOCALE_SUPPORT
1960 static void FAST_FUNC
1961 change_lc_all(const char *value)
1963 if (value && *value != '\0')
1964 setlocale(LC_ALL, value);
1966 static void FAST_FUNC
1967 change_lc_ctype(const char *value)
1969 if (value && *value != '\0')
1970 setlocale(LC_CTYPE, value);
1974 static void chkmail(void);
1975 static void changemail(const char *var_value) FAST_FUNC;
1977 # define chkmail() ((void)0)
1979 static void changepath(const char *) FAST_FUNC;
1980 #if ENABLE_ASH_RANDOM_SUPPORT
1981 static void change_random(const char *) FAST_FUNC;
1984 static const struct {
1986 const char *var_text;
1987 void (*var_func)(const char *) FAST_FUNC;
1988 } varinit_data[] = {
1990 * Note: VEXPORT would not work correctly here for NOFORK applets:
1991 * some environment strings may be constant.
1993 { VSTRFIXED|VTEXTFIXED , defifsvar , NULL },
1995 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail },
1996 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail },
1998 { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath },
1999 { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL },
2000 { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL },
2001 { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL },
2002 #if ENABLE_ASH_GETOPTS
2003 { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset },
2005 #if ENABLE_ASH_RANDOM_SUPPORT
2006 { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random },
2008 #if ENABLE_LOCALE_SUPPORT
2009 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all },
2010 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype },
2012 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
2013 { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL },
2019 struct globals_var {
2020 struct shparam shellparam; /* $@ current positional parameters */
2021 struct redirtab *redirlist;
2022 int preverrout_fd; /* save fd2 before print debug if xflag is set. */
2023 struct var *vartab[VTABSIZE];
2024 struct var varinit[ARRAY_SIZE(varinit_data)];
2026 extern struct globals_var *const ash_ptr_to_globals_var;
2027 #define G_var (*ash_ptr_to_globals_var)
2028 #define shellparam (G_var.shellparam )
2029 //#define redirlist (G_var.redirlist )
2030 #define preverrout_fd (G_var.preverrout_fd)
2031 #define vartab (G_var.vartab )
2032 #define varinit (G_var.varinit )
2033 #define INIT_G_var() do { \
2035 (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
2037 for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
2038 varinit[i].flags = varinit_data[i].flags; \
2039 varinit[i].var_text = varinit_data[i].var_text; \
2040 varinit[i].var_func = varinit_data[i].var_func; \
2044 #define vifs varinit[0]
2046 # define vmail (&vifs)[1]
2047 # define vmpath (&vmail)[1]
2048 # define vpath (&vmpath)[1]
2050 # define vpath (&vifs)[1]
2052 #define vps1 (&vpath)[1]
2053 #define vps2 (&vps1)[1]
2054 #define vps4 (&vps2)[1]
2055 #if ENABLE_ASH_GETOPTS
2056 # define voptind (&vps4)[1]
2057 # if ENABLE_ASH_RANDOM_SUPPORT
2058 # define vrandom (&voptind)[1]
2061 # if ENABLE_ASH_RANDOM_SUPPORT
2062 # define vrandom (&vps4)[1]
2067 * The following macros access the values of the above variables.
2068 * They have to skip over the name. They return the null string
2069 * for unset variables.
2071 #define ifsval() (vifs.var_text + 4)
2072 #define ifsset() ((vifs.flags & VUNSET) == 0)
2074 # define mailval() (vmail.var_text + 5)
2075 # define mpathval() (vmpath.var_text + 9)
2076 # define mpathset() ((vmpath.flags & VUNSET) == 0)
2078 #define pathval() (vpath.var_text + 5)
2079 #define ps1val() (vps1.var_text + 4)
2080 #define ps2val() (vps2.var_text + 4)
2081 #define ps4val() (vps4.var_text + 4)
2082 #if ENABLE_ASH_GETOPTS
2083 # define optindval() (voptind.var_text + 7)
2086 #if ENABLE_ASH_GETOPTS
2087 static void FAST_FUNC
2088 getoptsreset(const char *value)
2090 shellparam.optind = number(value) ?: 1;
2091 shellparam.optoff = -1;
2096 * Compares two strings up to the first = or '\0'. The first
2097 * string must be terminated by '='; the second may be terminated by
2098 * either '=' or '\0'.
2101 varcmp(const char *p, const char *q)
2105 while ((c = *p) == (d = *q)) {
2106 if (c == '\0' || c == '=')
2120 * Find the appropriate entry in the hash table from the name.
2122 static struct var **
2123 hashvar(const char *p)
2127 hashval = ((unsigned char) *p) << 4;
2128 while (*p && *p != '=')
2129 hashval += (unsigned char) *p++;
2130 return &vartab[hashval % VTABSIZE];
2134 vpcmp(const void *a, const void *b)
2136 return varcmp(*(const char **)a, *(const char **)b);
2140 * This routine initializes the builtin variables.
2150 * PS1 depends on uid
2152 #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT
2153 vps1.var_text = "PS1=\\w \\$ ";
2156 vps1.var_text = "PS1=# ";
2159 end = vp + ARRAY_SIZE(varinit);
2161 vpp = hashvar(vp->var_text);
2164 } while (++vp < end);
2167 static struct var **
2168 findvar(struct var **vpp, const char *name)
2170 for (; *vpp; vpp = &(*vpp)->next) {
2171 if (varcmp((*vpp)->var_text, name) == 0) {
2179 * Find the value of a variable. Returns NULL if not set.
2181 static const char* FAST_FUNC
2182 lookupvar(const char *name)
2186 v = *findvar(hashvar(name), name);
2188 #if ENABLE_ASH_RANDOM_SUPPORT
2190 * Dynamic variables are implemented roughly the same way they are
2191 * in bash. Namely, they're "special" so long as they aren't unset.
2192 * As soon as they're unset, they're no longer dynamic, and dynamic
2193 * lookup will no longer happen at that point. -- PFM.
2195 if (v->flags & VDYNAMIC)
2198 if (!(v->flags & VUNSET))
2199 return var_end(v->var_text);
2204 #if ENABLE_UNICODE_SUPPORT
2206 reinit_unicode_for_ash(void)
2208 /* Unicode support should be activated even if LANG is set
2209 * _during_ shell execution, not only if it was set when
2210 * shell was started. Therefore, re-check LANG every time:
2212 if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
2213 || ENABLE_UNICODE_USING_LOCALE
2215 const char *s = lookupvar("LC_ALL");
2216 if (!s) s = lookupvar("LC_CTYPE");
2217 if (!s) s = lookupvar("LANG");
2222 # define reinit_unicode_for_ash() ((void)0)
2226 * Search the environment of a builtin command.
2229 bltinlookup(const char *name)
2233 for (sp = cmdenviron; sp; sp = sp->next) {
2234 if (varcmp(sp->text, name) == 0)
2235 return var_end(sp->text);
2237 return lookupvar(name);
2241 * Same as setvar except that the variable and value are passed in
2242 * the first argument as name=value. Since the first argument will
2243 * be actually stored in the table, it should not be a string that
2245 * Called with interrupts off.
2248 setvareq(char *s, int flags)
2250 struct var *vp, **vpp;
2253 flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
2254 vp = *findvar(vpp, s);
2256 if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) {
2259 if (flags & VNOSAVE)
2263 ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n);
2269 if (vp->var_func && !(flags & VNOFUNC))
2270 vp->var_func(var_end(s));
2272 if (!(vp->flags & (VTEXTFIXED|VSTACK)))
2273 free((char*)vp->var_text);
2275 flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET);
2277 /* variable s is not found */
2280 vp = ckzalloc(sizeof(*vp));
2282 /*vp->func = NULL; - ckzalloc did it */
2285 if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
2292 * Set the value of a variable. The flags argument is ored with the
2293 * flags of the variable. If val is NULL, the variable is unset.
2296 setvar(const char *name, const char *val, int flags)
2304 q = endofname(name);
2305 p = strchrnul(q, '=');
2307 if (!namelen || p != q)
2308 ash_msg_and_raise_error("%.*s: bad variable name", namelen, name);
2313 vallen = strlen(val);
2317 nameeq = ckmalloc(namelen + vallen + 2);
2318 p = mempcpy(nameeq, name, namelen);
2321 p = mempcpy(p, val, vallen);
2324 setvareq(nameeq, flags | VNOSAVE);
2328 static void FAST_FUNC
2329 setvar0(const char *name, const char *val)
2331 setvar(name, val, 0);
2335 * Unset the specified variable.
2338 unsetvar(const char *s)
2344 vpp = findvar(hashvar(s), s);
2348 int flags = vp->flags;
2351 if (flags & VREADONLY)
2353 #if ENABLE_ASH_RANDOM_SUPPORT
2354 vp->flags &= ~VDYNAMIC;
2358 if ((flags & VSTRFIXED) == 0) {
2360 if ((flags & (VTEXTFIXED|VSTACK)) == 0)
2361 free((char*)vp->var_text);
2367 vp->flags &= ~VEXPORT;
2377 * Process a linked list of variable assignments.
2380 listsetvar(struct strlist *list_set_var, int flags)
2382 struct strlist *lp = list_set_var;
2388 setvareq(lp->text, flags);
2395 * Generate a list of variables satisfying the given conditions.
2398 listvars(int on, int off, char ***end)
2409 for (vp = *vpp; vp; vp = vp->next) {
2410 if ((vp->flags & mask) == on) {
2411 if (ep == stackstrend())
2412 ep = growstackstr();
2413 *ep++ = (char*)vp->var_text;
2416 } while (++vpp < vartab + VTABSIZE);
2417 if (ep == stackstrend())
2418 ep = growstackstr();
2422 return grabstackstr(ep);
2426 /* ============ Path search helper
2428 * The variable path (passed by reference) should be set to the start
2429 * of the path before the first call; path_advance will update
2430 * this value as it proceeds. Successive calls to path_advance will return
2431 * the possible path expansions in sequence. If an option (indicated by
2432 * a percent sign) appears in the path entry then the global variable
2433 * pathopt will be set to point to it; otherwise pathopt will be set to
2436 static const char *pathopt; /* set by path_advance */
2439 path_advance(const char **path, const char *name)
2449 for (p = start; *p && *p != ':' && *p != '%'; p++)
2451 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
2452 while (stackblocksize() < len)
2456 q = mempcpy(q, start, p - start);
2463 while (*p && *p != ':')
2470 return stalloc(len);
2474 /* ============ Prompt */
2476 static smallint doprompt; /* if set, prompt the user */
2477 static smallint needprompt; /* true if interactive and at start of line */
2479 #if ENABLE_FEATURE_EDITING
2480 static line_input_t *line_input_state;
2481 static const char *cmdedit_prompt;
2483 putprompt(const char *s)
2485 if (ENABLE_ASH_EXPAND_PRMT) {
2486 free((char*)cmdedit_prompt);
2487 cmdedit_prompt = ckstrdup(s);
2494 putprompt(const char *s)
2500 /* expandstr() needs parsing machinery, so it is far away ahead... */
2501 static const char *expandstr(const char *ps);
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));
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 /* Values for syntax param */
2852 #define BASESYNTAX 0 /* not in quotes */
2853 #define DQSYNTAX 1 /* in double quotes */
2854 #define SQSYNTAX 2 /* in single quotes */
2855 #define ARISYNTAX 3 /* in arithmetic */
2856 #define PSSYNTAX 4 /* prompt. never passed to SIT() */
2858 #if USE_SIT_FUNCTION
2861 SIT(int c, int syntax)
2863 /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */
2864 static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~";
2866 * This causes '/' to be prepended with CTLESC in dquoted string,
2867 * making "./file"* treated incorrectly because we feed
2868 * ".\/file*" string to glob(), confusing it (see expandmeta func).
2869 * The "homegrown" glob implementation is okay with that,
2870 * but glibc one isn't. With '/' always treated as CWORD,
2873 # if ENABLE_ASH_ALIAS
2874 static const uint8_t syntax_index_table[] ALIGN1 = {
2875 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */
2876 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */
2877 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */
2881 static const uint8_t syntax_index_table[] ALIGN1 = {
2882 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */
2883 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */
2884 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */
2893 # if ENABLE_ASH_ALIAS
2899 /* Cast is purely for paranoia here,
2900 * just in case someone passed signed char to us */
2901 if ((unsigned char)c >= CTL_FIRST
2902 && (unsigned char)c <= CTL_LAST
2906 s = strchrnul(spec_symbls, c);
2909 indx = syntax_index_table[s - spec_symbls];
2911 return (S_I_T[indx] >> (syntax*4)) & 0xf;
2914 #else /* !USE_SIT_FUNCTION */
2916 static const uint8_t syntax_index_table[] ALIGN1 = {
2917 /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
2918 /* 0 */ CWORD_CWORD_CWORD_CWORD,
2919 /* 1 */ CWORD_CWORD_CWORD_CWORD,
2920 /* 2 */ CWORD_CWORD_CWORD_CWORD,
2921 /* 3 */ CWORD_CWORD_CWORD_CWORD,
2922 /* 4 */ CWORD_CWORD_CWORD_CWORD,
2923 /* 5 */ CWORD_CWORD_CWORD_CWORD,
2924 /* 6 */ CWORD_CWORD_CWORD_CWORD,
2925 /* 7 */ CWORD_CWORD_CWORD_CWORD,
2926 /* 8 */ CWORD_CWORD_CWORD_CWORD,
2927 /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD,
2928 /* 10 "\n" */ CNL_CNL_CNL_CNL,
2929 /* 11 */ CWORD_CWORD_CWORD_CWORD,
2930 /* 12 */ CWORD_CWORD_CWORD_CWORD,
2931 /* 13 */ CWORD_CWORD_CWORD_CWORD,
2932 /* 14 */ CWORD_CWORD_CWORD_CWORD,
2933 /* 15 */ CWORD_CWORD_CWORD_CWORD,
2934 /* 16 */ CWORD_CWORD_CWORD_CWORD,
2935 /* 17 */ CWORD_CWORD_CWORD_CWORD,
2936 /* 18 */ CWORD_CWORD_CWORD_CWORD,
2937 /* 19 */ CWORD_CWORD_CWORD_CWORD,
2938 /* 20 */ CWORD_CWORD_CWORD_CWORD,
2939 /* 21 */ CWORD_CWORD_CWORD_CWORD,
2940 /* 22 */ CWORD_CWORD_CWORD_CWORD,
2941 /* 23 */ CWORD_CWORD_CWORD_CWORD,
2942 /* 24 */ CWORD_CWORD_CWORD_CWORD,
2943 /* 25 */ CWORD_CWORD_CWORD_CWORD,
2944 /* 26 */ CWORD_CWORD_CWORD_CWORD,
2945 /* 27 */ CWORD_CWORD_CWORD_CWORD,
2946 /* 28 */ CWORD_CWORD_CWORD_CWORD,
2947 /* 29 */ CWORD_CWORD_CWORD_CWORD,
2948 /* 30 */ CWORD_CWORD_CWORD_CWORD,
2949 /* 31 */ CWORD_CWORD_CWORD_CWORD,
2950 /* 32 " " */ CSPCL_CWORD_CWORD_CWORD,
2951 /* 33 "!" */ CWORD_CCTL_CCTL_CWORD,
2952 /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD,
2953 /* 35 "#" */ CWORD_CWORD_CWORD_CWORD,
2954 /* 36 "$" */ CVAR_CVAR_CWORD_CVAR,
2955 /* 37 "%" */ CWORD_CWORD_CWORD_CWORD,
2956 /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD,
2957 /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD,
2958 /* 40 "(" */ CSPCL_CWORD_CWORD_CLP,
2959 /* 41 ")" */ CSPCL_CWORD_CWORD_CRP,
2960 /* 42 "*" */ CWORD_CCTL_CCTL_CWORD,
2961 /* 43 "+" */ CWORD_CWORD_CWORD_CWORD,
2962 /* 44 "," */ CWORD_CWORD_CWORD_CWORD,
2963 /* 45 "-" */ CWORD_CCTL_CCTL_CWORD,
2964 /* 46 "." */ CWORD_CWORD_CWORD_CWORD,
2965 /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */
2966 /* 47 "/" */ CWORD_CWORD_CWORD_CWORD,
2967 /* 48 "0" */ CWORD_CWORD_CWORD_CWORD,
2968 /* 49 "1" */ CWORD_CWORD_CWORD_CWORD,
2969 /* 50 "2" */ CWORD_CWORD_CWORD_CWORD,
2970 /* 51 "3" */ CWORD_CWORD_CWORD_CWORD,
2971 /* 52 "4" */ CWORD_CWORD_CWORD_CWORD,
2972 /* 53 "5" */ CWORD_CWORD_CWORD_CWORD,
2973 /* 54 "6" */ CWORD_CWORD_CWORD_CWORD,
2974 /* 55 "7" */ CWORD_CWORD_CWORD_CWORD,
2975 /* 56 "8" */ CWORD_CWORD_CWORD_CWORD,
2976 /* 57 "9" */ CWORD_CWORD_CWORD_CWORD,
2977 /* 58 ":" */ CWORD_CCTL_CCTL_CWORD,
2978 /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD,
2979 /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD,
2980 /* 61 "=" */ CWORD_CCTL_CCTL_CWORD,
2981 /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD,
2982 /* 63 "?" */ CWORD_CCTL_CCTL_CWORD,
2983 /* 64 "@" */ CWORD_CWORD_CWORD_CWORD,
2984 /* 65 "A" */ CWORD_CWORD_CWORD_CWORD,
2985 /* 66 "B" */ CWORD_CWORD_CWORD_CWORD,
2986 /* 67 "C" */ CWORD_CWORD_CWORD_CWORD,
2987 /* 68 "D" */ CWORD_CWORD_CWORD_CWORD,
2988 /* 69 "E" */ CWORD_CWORD_CWORD_CWORD,
2989 /* 70 "F" */ CWORD_CWORD_CWORD_CWORD,
2990 /* 71 "G" */ CWORD_CWORD_CWORD_CWORD,
2991 /* 72 "H" */ CWORD_CWORD_CWORD_CWORD,
2992 /* 73 "I" */ CWORD_CWORD_CWORD_CWORD,
2993 /* 74 "J" */ CWORD_CWORD_CWORD_CWORD,
2994 /* 75 "K" */ CWORD_CWORD_CWORD_CWORD,
2995 /* 76 "L" */ CWORD_CWORD_CWORD_CWORD,
2996 /* 77 "M" */ CWORD_CWORD_CWORD_CWORD,
2997 /* 78 "N" */ CWORD_CWORD_CWORD_CWORD,
2998 /* 79 "O" */ CWORD_CWORD_CWORD_CWORD,
2999 /* 80 "P" */ CWORD_CWORD_CWORD_CWORD,
3000 /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD,
3001 /* 82 "R" */ CWORD_CWORD_CWORD_CWORD,
3002 /* 83 "S" */ CWORD_CWORD_CWORD_CWORD,
3003 /* 84 "T" */ CWORD_CWORD_CWORD_CWORD,
3004 /* 85 "U" */ CWORD_CWORD_CWORD_CWORD,
3005 /* 86 "V" */ CWORD_CWORD_CWORD_CWORD,
3006 /* 87 "W" */ CWORD_CWORD_CWORD_CWORD,
3007 /* 88 "X" */ CWORD_CWORD_CWORD_CWORD,
3008 /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD,
3009 /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD,
3010 /* 91 "[" */ CWORD_CCTL_CCTL_CWORD,
3011 /* 92 "\" */ CBACK_CBACK_CCTL_CBACK,
3012 /* 93 "]" */ CWORD_CCTL_CCTL_CWORD,
3013 /* 94 "^" */ CWORD_CWORD_CWORD_CWORD,
3014 /* 95 "_" */ CWORD_CWORD_CWORD_CWORD,
3015 /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE,
3016 /* 97 "a" */ CWORD_CWORD_CWORD_CWORD,
3017 /* 98 "b" */ CWORD_CWORD_CWORD_CWORD,
3018 /* 99 "c" */ CWORD_CWORD_CWORD_CWORD,
3019 /* 100 "d" */ CWORD_CWORD_CWORD_CWORD,
3020 /* 101 "e" */ CWORD_CWORD_CWORD_CWORD,
3021 /* 102 "f" */ CWORD_CWORD_CWORD_CWORD,
3022 /* 103 "g" */ CWORD_CWORD_CWORD_CWORD,
3023 /* 104 "h" */ CWORD_CWORD_CWORD_CWORD,
3024 /* 105 "i" */ CWORD_CWORD_CWORD_CWORD,
3025 /* 106 "j" */ CWORD_CWORD_CWORD_CWORD,
3026 /* 107 "k" */ CWORD_CWORD_CWORD_CWORD,
3027 /* 108 "l" */ CWORD_CWORD_CWORD_CWORD,
3028 /* 109 "m" */ CWORD_CWORD_CWORD_CWORD,
3029 /* 110 "n" */ CWORD_CWORD_CWORD_CWORD,
3030 /* 111 "o" */ CWORD_CWORD_CWORD_CWORD,
3031 /* 112 "p" */ CWORD_CWORD_CWORD_CWORD,
3032 /* 113 "q" */ CWORD_CWORD_CWORD_CWORD,
3033 /* 114 "r" */ CWORD_CWORD_CWORD_CWORD,
3034 /* 115 "s" */ CWORD_CWORD_CWORD_CWORD,
3035 /* 116 "t" */ CWORD_CWORD_CWORD_CWORD,
3036 /* 117 "u" */ CWORD_CWORD_CWORD_CWORD,
3037 /* 118 "v" */ CWORD_CWORD_CWORD_CWORD,
3038 /* 119 "w" */ CWORD_CWORD_CWORD_CWORD,
3039 /* 120 "x" */ CWORD_CWORD_CWORD_CWORD,
3040 /* 121 "y" */ CWORD_CWORD_CWORD_CWORD,
3041 /* 122 "z" */ CWORD_CWORD_CWORD_CWORD,
3042 /* 123 "{" */ CWORD_CWORD_CWORD_CWORD,
3043 /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD,
3044 /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR,
3045 /* 126 "~" */ CWORD_CCTL_CCTL_CWORD,
3046 /* 127 del */ CWORD_CWORD_CWORD_CWORD,
3047 /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD,
3048 /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL,
3049 /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL,
3050 /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL,
3051 /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL,
3052 /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL,
3053 /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL,
3054 /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL,
3055 /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL,
3056 /* 137 */ CWORD_CWORD_CWORD_CWORD,
3057 /* 138 */ CWORD_CWORD_CWORD_CWORD,
3058 /* 139 */ CWORD_CWORD_CWORD_CWORD,
3059 /* 140 */ CWORD_CWORD_CWORD_CWORD,
3060 /* 141 */ CWORD_CWORD_CWORD_CWORD,
3061 /* 142 */ CWORD_CWORD_CWORD_CWORD,
3062 /* 143 */ CWORD_CWORD_CWORD_CWORD,
3063 /* 144 */ CWORD_CWORD_CWORD_CWORD,
3064 /* 145 */ CWORD_CWORD_CWORD_CWORD,
3065 /* 146 */ CWORD_CWORD_CWORD_CWORD,
3066 /* 147 */ CWORD_CWORD_CWORD_CWORD,
3067 /* 148 */ CWORD_CWORD_CWORD_CWORD,
3068 /* 149 */ CWORD_CWORD_CWORD_CWORD,
3069 /* 150 */ CWORD_CWORD_CWORD_CWORD,
3070 /* 151 */ CWORD_CWORD_CWORD_CWORD,
3071 /* 152 */ CWORD_CWORD_CWORD_CWORD,
3072 /* 153 */ CWORD_CWORD_CWORD_CWORD,
3073 /* 154 */ CWORD_CWORD_CWORD_CWORD,
3074 /* 155 */ CWORD_CWORD_CWORD_CWORD,
3075 /* 156 */ CWORD_CWORD_CWORD_CWORD,
3076 /* 157 */ CWORD_CWORD_CWORD_CWORD,
3077 /* 158 */ CWORD_CWORD_CWORD_CWORD,
3078 /* 159 */ CWORD_CWORD_CWORD_CWORD,
3079 /* 160 */ CWORD_CWORD_CWORD_CWORD,
3080 /* 161 */ CWORD_CWORD_CWORD_CWORD,
3081 /* 162 */ CWORD_CWORD_CWORD_CWORD,
3082 /* 163 */ CWORD_CWORD_CWORD_CWORD,
3083 /* 164 */ CWORD_CWORD_CWORD_CWORD,
3084 /* 165 */ CWORD_CWORD_CWORD_CWORD,
3085 /* 166 */ CWORD_CWORD_CWORD_CWORD,
3086 /* 167 */ CWORD_CWORD_CWORD_CWORD,
3087 /* 168 */ CWORD_CWORD_CWORD_CWORD,
3088 /* 169 */ CWORD_CWORD_CWORD_CWORD,
3089 /* 170 */ CWORD_CWORD_CWORD_CWORD,
3090 /* 171 */ CWORD_CWORD_CWORD_CWORD,
3091 /* 172 */ CWORD_CWORD_CWORD_CWORD,
3092 /* 173 */ CWORD_CWORD_CWORD_CWORD,
3093 /* 174 */ CWORD_CWORD_CWORD_CWORD,
3094 /* 175 */ CWORD_CWORD_CWORD_CWORD,
3095 /* 176 */ CWORD_CWORD_CWORD_CWORD,
3096 /* 177 */ CWORD_CWORD_CWORD_CWORD,
3097 /* 178 */ CWORD_CWORD_CWORD_CWORD,
3098 /* 179 */ CWORD_CWORD_CWORD_CWORD,
3099 /* 180 */ CWORD_CWORD_CWORD_CWORD,
3100 /* 181 */ CWORD_CWORD_CWORD_CWORD,
3101 /* 182 */ CWORD_CWORD_CWORD_CWORD,
3102 /* 183 */ CWORD_CWORD_CWORD_CWORD,
3103 /* 184 */ CWORD_CWORD_CWORD_CWORD,
3104 /* 185 */ CWORD_CWORD_CWORD_CWORD,
3105 /* 186 */ CWORD_CWORD_CWORD_CWORD,
3106 /* 187 */ CWORD_CWORD_CWORD_CWORD,
3107 /* 188 */ CWORD_CWORD_CWORD_CWORD,
3108 /* 189 */ CWORD_CWORD_CWORD_CWORD,
3109 /* 190 */ CWORD_CWORD_CWORD_CWORD,
3110 /* 191 */ CWORD_CWORD_CWORD_CWORD,
3111 /* 192 */ CWORD_CWORD_CWORD_CWORD,
3112 /* 193 */ CWORD_CWORD_CWORD_CWORD,
3113 /* 194 */ CWORD_CWORD_CWORD_CWORD,
3114 /* 195 */ CWORD_CWORD_CWORD_CWORD,
3115 /* 196 */ CWORD_CWORD_CWORD_CWORD,
3116 /* 197 */ CWORD_CWORD_CWORD_CWORD,
3117 /* 198 */ CWORD_CWORD_CWORD_CWORD,
3118 /* 199 */ CWORD_CWORD_CWORD_CWORD,
3119 /* 200 */ CWORD_CWORD_CWORD_CWORD,
3120 /* 201 */ CWORD_CWORD_CWORD_CWORD,
3121 /* 202 */ CWORD_CWORD_CWORD_CWORD,
3122 /* 203 */ CWORD_CWORD_CWORD_CWORD,
3123 /* 204 */ CWORD_CWORD_CWORD_CWORD,
3124 /* 205 */ CWORD_CWORD_CWORD_CWORD,
3125 /* 206 */ CWORD_CWORD_CWORD_CWORD,
3126 /* 207 */ CWORD_CWORD_CWORD_CWORD,
3127 /* 208 */ CWORD_CWORD_CWORD_CWORD,
3128 /* 209 */ CWORD_CWORD_CWORD_CWORD,
3129 /* 210 */ CWORD_CWORD_CWORD_CWORD,
3130 /* 211 */ CWORD_CWORD_CWORD_CWORD,
3131 /* 212 */ CWORD_CWORD_CWORD_CWORD,
3132 /* 213 */ CWORD_CWORD_CWORD_CWORD,
3133 /* 214 */ CWORD_CWORD_CWORD_CWORD,
3134 /* 215 */ CWORD_CWORD_CWORD_CWORD,
3135 /* 216 */ CWORD_CWORD_CWORD_CWORD,
3136 /* 217 */ CWORD_CWORD_CWORD_CWORD,
3137 /* 218 */ CWORD_CWORD_CWORD_CWORD,
3138 /* 219 */ CWORD_CWORD_CWORD_CWORD,
3139 /* 220 */ CWORD_CWORD_CWORD_CWORD,
3140 /* 221 */ CWORD_CWORD_CWORD_CWORD,
3141 /* 222 */ CWORD_CWORD_CWORD_CWORD,
3142 /* 223 */ CWORD_CWORD_CWORD_CWORD,
3143 /* 224 */ CWORD_CWORD_CWORD_CWORD,
3144 /* 225 */ CWORD_CWORD_CWORD_CWORD,
3145 /* 226 */ CWORD_CWORD_CWORD_CWORD,
3146 /* 227 */ CWORD_CWORD_CWORD_CWORD,
3147 /* 228 */ CWORD_CWORD_CWORD_CWORD,
3148 /* 229 */ CWORD_CWORD_CWORD_CWORD,
3149 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3150 /* 231 */ CWORD_CWORD_CWORD_CWORD,
3151 /* 232 */ CWORD_CWORD_CWORD_CWORD,
3152 /* 233 */ CWORD_CWORD_CWORD_CWORD,
3153 /* 234 */ CWORD_CWORD_CWORD_CWORD,
3154 /* 235 */ CWORD_CWORD_CWORD_CWORD,
3155 /* 236 */ CWORD_CWORD_CWORD_CWORD,
3156 /* 237 */ CWORD_CWORD_CWORD_CWORD,
3157 /* 238 */ CWORD_CWORD_CWORD_CWORD,
3158 /* 239 */ CWORD_CWORD_CWORD_CWORD,
3159 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3160 /* 241 */ CWORD_CWORD_CWORD_CWORD,
3161 /* 242 */ CWORD_CWORD_CWORD_CWORD,
3162 /* 243 */ CWORD_CWORD_CWORD_CWORD,
3163 /* 244 */ CWORD_CWORD_CWORD_CWORD,
3164 /* 245 */ CWORD_CWORD_CWORD_CWORD,
3165 /* 246 */ CWORD_CWORD_CWORD_CWORD,
3166 /* 247 */ CWORD_CWORD_CWORD_CWORD,
3167 /* 248 */ CWORD_CWORD_CWORD_CWORD,
3168 /* 249 */ CWORD_CWORD_CWORD_CWORD,
3169 /* 250 */ CWORD_CWORD_CWORD_CWORD,
3170 /* 251 */ CWORD_CWORD_CWORD_CWORD,
3171 /* 252 */ CWORD_CWORD_CWORD_CWORD,
3172 /* 253 */ CWORD_CWORD_CWORD_CWORD,
3173 /* 254 */ CWORD_CWORD_CWORD_CWORD,
3174 /* 255 */ CWORD_CWORD_CWORD_CWORD,
3175 /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
3176 # if ENABLE_ASH_ALIAS
3177 /* PEOA */ CSPCL_CIGN_CIGN_CIGN,
3182 # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf)
3183 #else /* debug version, caught one signed char bug */
3184 # define SIT(c, syntax) \
3186 if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \
3187 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3188 if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \
3189 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3190 ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \
3194 #endif /* !USE_SIT_FUNCTION */
3197 /* ============ Alias handling */
3199 #if ENABLE_ASH_ALIAS
3201 #define ALIASINUSE 1
3212 static struct alias **atab; // [ATABSIZE];
3213 #define INIT_G_alias() do { \
3214 atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
3218 static struct alias **
3219 __lookupalias(const char *name)
3221 unsigned int hashval;
3228 ch = (unsigned char)*p;
3232 ch = (unsigned char)*++p;
3234 app = &atab[hashval % ATABSIZE];
3236 for (; *app; app = &(*app)->next) {
3237 if (strcmp(name, (*app)->name) == 0) {
3245 static struct alias *
3246 lookupalias(const char *name, int check)
3248 struct alias *ap = *__lookupalias(name);
3250 if (check && ap && (ap->flag & ALIASINUSE))
3255 static struct alias *
3256 freealias(struct alias *ap)
3260 if (ap->flag & ALIASINUSE) {
3261 ap->flag |= ALIASDEAD;
3273 setalias(const char *name, const char *val)
3275 struct alias *ap, **app;
3277 app = __lookupalias(name);
3281 if (!(ap->flag & ALIASINUSE)) {
3284 ap->val = ckstrdup(val);
3285 ap->flag &= ~ALIASDEAD;
3288 ap = ckzalloc(sizeof(struct alias));
3289 ap->name = ckstrdup(name);
3290 ap->val = ckstrdup(val);
3291 /*ap->flag = 0; - ckzalloc did it */
3292 /*ap->next = NULL;*/
3299 unalias(const char *name)
3303 app = __lookupalias(name);
3307 *app = freealias(*app);
3318 struct alias *ap, **app;
3322 for (i = 0; i < ATABSIZE; i++) {
3324 for (ap = *app; ap; ap = *app) {
3325 *app = freealias(*app);
3335 printalias(const struct alias *ap)
3337 out1fmt("%s=%s\n", ap->name, single_quote(ap->val));
3341 * TODO - sort output
3343 static int FAST_FUNC
3344 aliascmd(int argc UNUSED_PARAM, char **argv)
3353 for (i = 0; i < ATABSIZE; i++) {
3354 for (ap = atab[i]; ap; ap = ap->next) {
3360 while ((n = *++argv) != NULL) {
3361 v = strchr(n+1, '=');
3362 if (v == NULL) { /* n+1: funny ksh stuff */
3363 ap = *__lookupalias(n);
3365 fprintf(stderr, "%s: %s not found\n", "alias", n);
3378 static int FAST_FUNC
3379 unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
3383 while (nextopt("a") != '\0') {
3387 for (i = 0; *argptr; argptr++) {
3388 if (unalias(*argptr)) {
3389 fprintf(stderr, "%s: %s not found\n", "unalias", *argptr);
3397 #endif /* ASH_ALIAS */
3400 /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */
3403 #define FORK_NOJOB 2
3405 /* mode flags for showjob(s) */
3406 #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */
3407 #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */
3408 #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */
3409 #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */
3412 * A job structure contains information about a job. A job is either a
3413 * single process or a set of processes contained in a pipeline. In the
3414 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
3418 pid_t ps_pid; /* process id */
3419 int ps_status; /* last process status from wait() */
3420 char *ps_cmd; /* text of command being run */
3424 struct procstat ps0; /* status of process */
3425 struct procstat *ps; /* status or processes when more than one */
3427 int stopstatus; /* status of a stopped job */
3429 unsigned nprocs; /* number of processes */
3431 #define JOBRUNNING 0 /* at least one proc running */
3432 #define JOBSTOPPED 1 /* all procs are stopped */
3433 #define JOBDONE 2 /* all procs are completed */
3437 sigint: 1, /* job was killed by SIGINT */
3438 jobctl: 1, /* job running under job control */
3440 waited: 1, /* true if this entry has been waited for */
3441 used: 1, /* true if this entry is in used */
3442 changed: 1; /* true if status has changed */
3443 struct job *prev_job; /* previous job */
3446 static struct job *makejob(/*union node *,*/ int);
3447 static int forkshell(struct job *, union node *, int);
3448 static int waitforjob(struct job *);
3451 enum { doing_jobctl = 0 };
3452 #define setjobctl(on) do {} while (0)
3454 static smallint doing_jobctl; //references:8
3455 static void setjobctl(int);
3462 ignoresig(int signo)
3464 /* Avoid unnecessary system calls. Is it already SIG_IGNed? */
3465 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
3466 /* No, need to do it */
3467 signal(signo, SIG_IGN);
3469 sigmode[signo - 1] = S_HARD_IGN;
3473 * Only one usage site - in setsignal()
3476 signal_handler(int signo)
3478 if (signo == SIGCHLD) {
3484 gotsig[signo - 1] = 1;
3485 pending_sig = signo;
3487 if (signo == SIGINT && !trap[SIGINT]) {
3488 if (!suppress_int) {
3490 raise_interrupt(); /* does not return */
3497 * Set the signal handler for the specified signal. The routine figures
3498 * out what it should be set to.
3501 setsignal(int signo)
3504 char cur_act, new_act;
3505 struct sigaction act;
3509 if (t != NULL) { /* trap for this sig is set */
3511 if (t[0] == '\0') /* trap is "": ignore this sig */
3515 if (rootshell && new_act == S_DFL) {
3518 if (iflag || minusc || sflag == 0)
3527 * "In all cases, bash ignores SIGQUIT. Non-builtin
3528 * commands run by bash have signal handlers
3529 * set to the values inherited by the shell
3530 * from its parent". */
3546 //TODO: if !rootshell, we reset SIGQUIT to DFL,
3547 //whereas we have to restore it to what shell got on entry
3548 //from the parent. See comment above
3550 if (signo == SIGCHLD)
3553 t = &sigmode[signo - 1];
3556 /* current setting is not yet known */
3557 if (sigaction(signo, NULL, &act)) {
3558 /* pretend it worked; maybe we should give a warning,
3559 * but other shells don't. We don't alter sigmode,
3560 * so we retry every time.
3561 * btw, in Linux it never fails. --vda */
3564 if (act.sa_handler == SIG_IGN) {
3565 cur_act = S_HARD_IGN;
3567 && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)
3569 cur_act = S_IGN; /* don't hard ignore these */
3573 if (cur_act == S_HARD_IGN || cur_act == new_act)
3576 act.sa_handler = SIG_DFL;
3579 act.sa_handler = signal_handler;
3582 act.sa_handler = SIG_IGN;
3586 /* flags and mask matter only if !DFL and !IGN, but we do it
3587 * for all cases for more deterministic behavior:
3590 sigfillset(&act.sa_mask);
3592 sigaction_set(signo, &act);
3597 /* mode flags for set_curjob */
3598 #define CUR_DELETE 2
3599 #define CUR_RUNNING 1
3600 #define CUR_STOPPED 0
3603 /* pgrp of shell on invocation */
3604 static int initialpgrp; //references:2
3605 static int ttyfd = -1; //5
3608 static struct job *jobtab; //5
3610 static unsigned njobs; //4
3612 static struct job *curjob; //lots
3613 /* number of presumed living untracked jobs */
3614 static int jobless; //4
3617 /* Bash has a feature: it restores termios after a successful wait for
3618 * a foreground job which had at least one stopped or sigkilled member.
3619 * The probable rationale is that SIGSTOP and SIGKILL can preclude task from
3620 * properly restoring tty state. Should we do this too?
3621 * A reproducer: ^Z an interactive python:
3624 * Python 2.7.12 (...)
3626 * { python leaves tty in -icanon -echo state. We do survive that... }
3627 * [1]+ Stopped python
3628 * { ...however, next program (python #2) does not survive it well: }
3630 * Python 2.7.12 (...)
3631 * >>> Traceback (most recent call last):
3632 * { above, I typed "qwerty<CR>", but -echo state is still in effect }
3633 * File "<stdin>", line 1, in <module>
3634 * NameError: name 'qwerty' is not defined
3636 * The implementation below is modeled on bash code and seems to work.
3637 * However, I'm not sure we should do this. For one: what if I'd fg
3638 * the stopped python instead? It'll be confused by "restored" tty state.
3640 static struct termios shell_tty_info;
3644 if (rootshell && ttyfd >= 0)
3645 tcgetattr(ttyfd, &shell_tty_info);
3650 /* if (rootshell) - caller ensures this */
3652 tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info);
3655 job_signal_status(struct job *jp)
3659 struct procstat *ps = jp->ps;
3660 for (i = 0; i < jp->nprocs; i++) {
3661 status = ps[i].ps_status;
3662 if (WIFSIGNALED(status) || WIFSTOPPED(status))
3668 restore_tty_if_stopped_or_signaled(struct job *jp)
3670 //TODO: check what happens if we come from waitforjob() in expbackq()
3672 int s = job_signal_status(jp);
3673 if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */
3678 # define get_tty_state() ((void)0)
3679 # define restore_tty_if_stopped_or_signaled(jp) ((void)0)
3683 set_curjob(struct job *jp, unsigned mode)
3686 struct job **jpp, **curp;
3688 /* first remove from list */
3689 jpp = curp = &curjob;
3694 jpp = &jp1->prev_job;
3696 *jpp = jp1->prev_job;
3698 /* Then re-insert in correct position */
3706 /* job being deleted */
3709 /* newly created job or backgrounded job,
3710 * put after all stopped jobs.
3715 if (!jp1 || jp1->state != JOBSTOPPED)
3718 jpp = &jp1->prev_job;
3724 /* newly stopped job - becomes curjob */
3725 jp->prev_job = *jpp;
3733 jobno(const struct job *jp)
3735 return jp - jobtab + 1;
3740 * Convert a job name to a job structure.
3743 #define getjob(name, getctl) getjob(name)
3746 getjob(const char *name, int getctl)
3750 const char *err_msg = "%s: no such job";
3754 char *(*match)(const char *, const char *);
3769 if (c == '+' || c == '%') {
3771 err_msg = "No current job";
3777 err_msg = "No previous job";
3787 if (num > 0 && num <= njobs) {
3788 jp = jobtab + num - 1;
3803 if (match(jp->ps[0].ps_cmd, p)) {
3807 err_msg = "%s: ambiguous";
3817 err_msg = "job %s not created under job control";
3818 if (getctl && jp->jobctl == 0)
3823 ash_msg_and_raise_error(err_msg, name);
3827 * Mark a job structure as unused.
3830 freejob(struct job *jp)
3832 struct procstat *ps;
3836 for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
3837 if (ps->ps_cmd != nullstr)
3840 if (jp->ps != &jp->ps0)
3843 set_curjob(jp, CUR_DELETE);
3849 xtcsetpgrp(int fd, pid_t pgrp)
3851 if (tcsetpgrp(fd, pgrp))
3852 ash_msg_and_raise_error("can't set tty process group (%m)");
3856 * Turn job control on and off.
3858 * Note: This code assumes that the third arg to ioctl is a character
3859 * pointer, which is true on Berkeley systems but not System V. Since
3860 * System V doesn't have job control yet, this isn't a problem now.
3862 * Called with interrupts off.
3870 if (on == doing_jobctl || rootshell == 0)
3874 ofd = fd = open(_PATH_TTY, O_RDWR);
3876 /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails.
3877 * That sometimes helps to acquire controlling tty.
3878 * Obviously, a workaround for bugs when someone
3879 * failed to provide a controlling tty to bash! :) */
3885 /* fd is a tty at this point */
3886 fd = fcntl(fd, F_DUPFD, 10);
3887 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */
3890 goto out; /* F_DUPFD failed */
3891 close_on_exec_on(fd);
3892 while (1) { /* while we are in the background */
3893 pgrp = tcgetpgrp(fd);
3896 ash_msg("can't access tty; job control turned off");
3900 if (pgrp == getpgrp())
3911 xtcsetpgrp(fd, pgrp);
3913 /* turning job control off */
3916 /* was xtcsetpgrp, but this can make exiting ash
3917 * loop forever if pty is already deleted */
3918 tcsetpgrp(fd, pgrp);
3932 static int FAST_FUNC
3933 killcmd(int argc, char **argv)
3935 if (argv[1] && strcmp(argv[1], "-l") != 0) {
3938 if (argv[i][0] == '%') {
3940 * "kill %N" - job kill
3941 * Converting to pgrp / pid kill
3947 jp = getjob(argv[i], 0);
3949 * In jobs started under job control, we signal
3950 * entire process group by kill -PGRP_ID.
3951 * This happens, f.e., in interactive shell.
3953 * Otherwise, we signal each child via
3954 * kill PID1 PID2 PID3.
3956 * sh -c 'sleep 1|sleep 1 & kill %1'
3957 * sh -c 'true|sleep 2 & sleep 1; kill %1'
3958 * sh -c 'true|sleep 1 & sleep 2; kill %1'
3960 n = jp->nprocs; /* can't be 0 (I hope) */
3963 dst = alloca(n * sizeof(int)*4);
3965 for (j = 0; j < n; j++) {
3966 struct procstat *ps = &jp->ps[j];
3967 /* Skip non-running and not-stopped members
3968 * (i.e. dead members) of the job
3970 if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status))
3973 * kill_main has matching code to expect
3974 * leading space. Needed to not confuse
3975 * negative pids with "kill -SIGNAL_NO" syntax
3977 dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid);
3981 } while (argv[++i]);
3983 return kill_main(argc, argv);
3987 showpipe(struct job *jp /*, FILE *out*/)
3989 struct procstat *ps;
3990 struct procstat *psend;
3992 psend = jp->ps + jp->nprocs;
3993 for (ps = jp->ps + 1; ps < psend; ps++)
3994 printf(" | %s", ps->ps_cmd);
3995 newline_and_flush(stdout);
3996 flush_stdout_stderr();
4001 restartjob(struct job *jp, int mode)
4003 struct procstat *ps;
4009 if (jp->state == JOBDONE)
4011 jp->state = JOBRUNNING;
4012 pgid = jp->ps[0].ps_pid;
4013 if (mode == FORK_FG) {
4015 xtcsetpgrp(ttyfd, pgid);
4017 killpg(pgid, SIGCONT);
4021 if (WIFSTOPPED(ps->ps_status)) {
4027 status = (mode == FORK_FG) ? waitforjob(jp) : 0;
4032 static int FAST_FUNC
4033 fg_bgcmd(int argc UNUSED_PARAM, char **argv)
4039 mode = (**argv == 'f') ? FORK_FG : FORK_BG;
4043 jp = getjob(*argv, 1);
4044 if (mode == FORK_BG) {
4045 set_curjob(jp, CUR_RUNNING);
4046 printf("[%d] ", jobno(jp));
4048 out1str(jp->ps[0].ps_cmd);
4049 showpipe(jp /*, stdout*/);
4050 retval = restartjob(jp, mode);
4051 } while (*argv && *++argv);
4057 sprint_status48(char *s, int status, int sigonly)
4063 if (!WIFEXITED(status)) {
4065 if (WIFSTOPPED(status))
4066 st = WSTOPSIG(status);
4069 st = WTERMSIG(status);
4071 if (st == SIGINT || st == SIGPIPE)
4074 if (WIFSTOPPED(status))
4079 //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata
4080 col = fmtstr(s, 32, strsignal(st));
4081 if (WCOREDUMP(status)) {
4082 strcpy(s + col, " (core dumped)");
4083 col += sizeof(" (core dumped)")-1;
4085 } else if (!sigonly) {
4086 st = WEXITSTATUS(status);
4087 col = fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st);
4094 wait_block_or_sig(int *status)
4101 /* Poll all children for changes in their state */
4103 /* if job control is active, accept stopped processes too */
4104 pid = waitpid(-1, status, doing_jobctl ? (WNOHANG|WUNTRACED) : WNOHANG);
4106 break; /* Error (e.g. EINTR, ECHILD) or pid */
4108 /* Children exist, but none are ready. Sleep until interesting signal */
4111 sigprocmask(SIG_SETMASK, &mask, &mask);
4112 while (!got_sigchld && !pending_sig)
4114 sigprocmask(SIG_SETMASK, &mask, NULL);
4115 #else /* unsafe: a signal can set pending_sig after check, but before pause() */
4116 while (!got_sigchld && !pending_sig)
4120 /* If it was SIGCHLD, poll children again */
4121 } while (got_sigchld);
4126 #define DOWAIT_NONBLOCK 0
4127 #define DOWAIT_BLOCK 1
4128 #define DOWAIT_BLOCK_OR_SIG 2
4131 dowait(int block, struct job *job)
4136 struct job *thisjob = NULL;
4138 TRACE(("dowait(0x%x) called\n", block));
4140 /* It's wrong to call waitpid() outside of INT_OFF region:
4141 * signal can arrive just after syscall return and handler can
4142 * longjmp away, losing stop/exit notification processing.
4143 * Thus, for "jobs" builtin, and for waiting for a fg job,
4144 * we call waitpid() (blocking or non-blocking) inside INT_OFF.
4146 * However, for "wait" builtin it is wrong to simply call waitpid()
4147 * in INT_OFF region: "wait" needs to wait for any running job
4148 * to change state, but should exit on any trap too.
4149 * In INT_OFF region, a signal just before syscall entry can set
4150 * pending_sig variables, but we can't check them, and we would
4151 * either enter a sleeping waitpid() (BUG), or need to busy-loop.
4153 * Because of this, we run inside INT_OFF, but use a special routine
4154 * which combines waitpid() and sigsuspend().
4155 * This is the reason why we need to have a handler for SIGCHLD:
4156 * SIG_DFL handler does not wake sigsuspend().
4159 if (block == DOWAIT_BLOCK_OR_SIG) {
4160 pid = wait_block_or_sig(&status);
4163 if (block == DOWAIT_NONBLOCK)
4164 wait_flags = WNOHANG;
4165 /* if job control is active, accept stopped processes too */
4167 wait_flags |= WUNTRACED;
4168 /* NB: _not_ safe_waitpid, we need to detect EINTR */
4169 pid = waitpid(-1, &status, wait_flags);
4171 TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
4172 pid, status, errno, strerror(errno)));
4177 for (jp = curjob; jp; jp = jp->prev_job) {
4179 struct procstat *ps;
4180 struct procstat *psend;
4181 if (jp->state == JOBDONE)
4185 psend = ps + jp->nprocs;
4187 if (ps->ps_pid == pid) {
4188 TRACE(("Job %d: changing status of proc %d "
4189 "from 0x%x to 0x%x\n",
4190 jobno(jp), pid, ps->ps_status, status));
4191 ps->ps_status = status;
4194 if (ps->ps_status == -1)
4195 jobstate = JOBRUNNING;
4197 if (jobstate == JOBRUNNING)
4199 if (WIFSTOPPED(ps->ps_status)) {
4200 jp->stopstatus = ps->ps_status;
4201 jobstate = JOBSTOPPED;
4204 } while (++ps < psend);
4208 /* Found the job where one of its processes changed its state.
4209 * Is there at least one live and running process in this job? */
4210 if (jobstate != JOBRUNNING) {
4211 /* No. All live processes in the job are stopped
4212 * (JOBSTOPPED) or there are no live processes (JOBDONE)
4214 thisjob->changed = 1;
4215 if (thisjob->state != jobstate) {
4216 TRACE(("Job %d: changing state from %d to %d\n",
4217 jobno(thisjob), thisjob->state, jobstate));
4218 thisjob->state = jobstate;
4220 if (jobstate == JOBSTOPPED)
4221 set_curjob(thisjob, CUR_STOPPED);
4227 /* The process wasn't found in job list */
4229 if (!WIFSTOPPED(status))
4235 if (thisjob && thisjob == job) {
4239 len = sprint_status48(s, status, 1);
4251 showjob(struct job *jp, int mode)
4253 struct procstat *ps;
4254 struct procstat *psend;
4257 char s[16 + 16 + 48];
4258 FILE *out = (mode & SHOW_STDERR ? stderr : stdout);
4262 if (mode & SHOW_ONLY_PGID) { /* jobs -p */
4263 /* just output process (group) id of pipeline */
4264 fprintf(out, "%d\n", ps->ps_pid);
4268 col = fmtstr(s, 16, "[%d] ", jobno(jp));
4273 else if (curjob && jp == curjob->prev_job)
4276 if (mode & SHOW_PIDS)
4277 col += fmtstr(s + col, 16, "%d ", ps->ps_pid);
4279 psend = ps + jp->nprocs;
4281 if (jp->state == JOBRUNNING) {
4282 strcpy(s + col, "Running");
4283 col += sizeof("Running") - 1;
4285 int status = psend[-1].ps_status;
4286 if (jp->state == JOBSTOPPED)
4287 status = jp->stopstatus;
4288 col += sprint_status48(s + col, status, 0);
4290 /* By now, "[JOBID]* [maybe PID] STATUS" is printed */
4292 /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line
4293 * or prints several "PID | <cmdN>" lines,
4294 * depending on SHOW_PIDS bit.
4295 * We do not print status of individual processes
4296 * between PID and <cmdN>. bash does it, but not very well:
4297 * first line shows overall job status, not process status,
4298 * making it impossible to know 1st process status.
4302 /* for each process */
4305 if (mode & SHOW_PIDS)
4306 col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1;
4308 fprintf(out, "%s%*c%s%s",
4310 33 - col >= 0 ? 33 - col : 0, ' ',
4311 ps == jp->ps ? "" : "| ",
4314 } while (++ps != psend);
4315 newline_and_flush(out);
4319 if (jp->state == JOBDONE) {
4320 TRACE(("showjob: freeing job %d\n", jobno(jp)));
4326 * Print a list of jobs. If "change" is nonzero, only print jobs whose
4327 * statuses have changed since the last call to showjobs.
4334 TRACE(("showjobs(0x%x) called\n", mode));
4336 /* Handle all finished jobs */
4337 while (dowait(DOWAIT_NONBLOCK, NULL) > 0)
4340 for (jp = curjob; jp; jp = jp->prev_job) {
4341 if (!(mode & SHOW_CHANGED) || jp->changed) {
4347 static int FAST_FUNC
4348 jobscmd(int argc UNUSED_PARAM, char **argv)
4353 while ((m = nextopt("lp")) != '\0') {
4357 mode |= SHOW_ONLY_PGID;
4363 showjob(getjob(*argv, 0), mode);
4373 /* Called only on finished or stopped jobs (no members are running) */
4375 getstatus(struct job *job)
4379 struct procstat *ps;
4381 /* Fetch last member's status */
4382 ps = job->ps + job->nprocs - 1;
4383 status = ps->ps_status;
4385 /* "set -o pipefail" mode: use last _nonzero_ status */
4386 while (status == 0 && --ps >= job->ps)
4387 status = ps->ps_status;
4390 retval = WEXITSTATUS(status);
4391 if (!WIFEXITED(status)) {
4393 retval = WSTOPSIG(status);
4394 if (!WIFSTOPPED(status))
4397 /* XXX: limits number of signals */
4398 retval = WTERMSIG(status);
4400 if (retval == SIGINT)
4406 TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n",
4407 jobno(job), job->nprocs, status, retval));
4411 static int FAST_FUNC
4412 waitcmd(int argc UNUSED_PARAM, char **argv)
4423 /* wait for all jobs */
4427 if (!jp) /* no running procs */
4429 if (jp->state == JOBRUNNING)
4435 * "When bash is waiting for an asynchronous command via
4436 * the wait builtin, the reception of a signal for which a trap
4437 * has been set will cause the wait builtin to return immediately
4438 * with an exit status greater than 128, immediately after which
4439 * the trap is executed."
4441 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4442 /* if child sends us a signal *and immediately exits*,
4443 * dowait() returns pid > 0. Check this case,
4444 * not "if (dowait() < 0)"!
4453 if (**argv != '%') {
4454 pid_t pid = number(*argv);
4459 if (job->ps[job->nprocs - 1].ps_pid == pid)
4461 job = job->prev_job;
4464 job = getjob(*argv, 0);
4466 /* loop until process terminated or stopped */
4467 while (job->state == JOBRUNNING) {
4468 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4473 retval = getstatus(job);
4480 retval = 128 + pending_sig;
4489 struct job *jp, *jq;
4491 len = njobs * sizeof(*jp);
4493 jp = ckrealloc(jq, len + 4 * sizeof(*jp));
4495 offset = (char *)jp - (char *)jq;
4497 /* Relocate pointers */
4500 jq = (struct job *)((char *)jq + l);
4504 #define joff(p) ((struct job *)((char *)(p) + l))
4505 #define jmove(p) (p) = (void *)((char *)(p) + offset)
4506 if (joff(jp)->ps == &jq->ps0)
4507 jmove(joff(jp)->ps);
4508 if (joff(jp)->prev_job)
4509 jmove(joff(jp)->prev_job);
4519 jp = (struct job *)((char *)jp + len);
4523 } while (--jq >= jp);
4528 * Return a new job structure.
4529 * Called with interrupts off.
4532 makejob(/*union node *node,*/ int nprocs)
4537 for (i = njobs, jp = jobtab; ; jp++) {
4544 if (jp->state != JOBDONE || !jp->waited)
4553 memset(jp, 0, sizeof(*jp));
4555 /* jp->jobctl is a bitfield.
4556 * "jp->jobctl |= doing_jobctl" likely to give awful code */
4560 jp->prev_job = curjob;
4565 jp->ps = ckmalloc(nprocs * sizeof(struct procstat));
4567 TRACE(("makejob(%d) returns %%%d\n", nprocs,
4574 * Return a string identifying a command (to be printed by the
4577 static char *cmdnextc;
4580 cmdputs(const char *s)
4582 static const char vstype[VSTYPE + 1][3] = {
4583 "", "}", "-", "+", "?", "=",
4584 "%", "%%", "#", "##"
4585 IF_BASH_SUBSTR(, ":")
4586 IF_BASH_PATTERN_SUBST(, "/", "//")
4589 const char *p, *str;
4593 unsigned char subtype = 0;
4597 nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
4599 while ((c = *p++) != '\0') {
4607 if ((subtype & VSTYPE) == VSLENGTH)
4613 str = "\"}" + !(quoted & 1);
4620 #if ENABLE_FEATURE_SH_MATH
4635 if ((subtype & VSTYPE) != VSNORMAL)
4637 str = vstype[subtype & VSTYPE];
4638 if (subtype & VSNUL)
4647 /* These can only happen inside quotes */
4660 while ((c = *str++) != '\0') {
4663 } /* while *p++ not NUL */
4666 USTPUTC('"', nextc);
4672 /* cmdtxt() and cmdlist() call each other */
4673 static void cmdtxt(union node *n);
4676 cmdlist(union node *np, int sep)
4678 for (; np; np = np->narg.next) {
4682 if (sep && np->narg.next)
4688 cmdtxt(union node *n)
4691 struct nodelist *lp;
4702 lp = n->npipe.cmdlist;
4720 cmdtxt(n->nbinary.ch1);
4736 cmdtxt(n->nif.test);
4738 if (n->nif.elsepart) {
4739 cmdtxt(n->nif.ifpart);
4741 n = n->nif.elsepart;
4759 cmdtxt(n->nbinary.ch1);
4769 cmdputs(n->nfor.var);
4771 cmdlist(n->nfor.args, 1);
4776 cmdputs(n->narg.text);
4780 cmdlist(n->ncmd.args, 1);
4781 cmdlist(n->ncmd.redirect, 0);
4794 cmdputs(n->ncase.expr->narg.text);
4796 for (np = n->ncase.cases; np; np = np->nclist.next) {
4797 cmdtxt(np->nclist.pattern);
4799 cmdtxt(np->nclist.body);
4813 #if BASH_REDIR_OUTPUT
4828 cmdputs(utoa(n->nfile.fd));
4830 if (n->type == NTOFD || n->type == NFROMFD) {
4831 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 -3 /* marks a slot of previously-closed fd */
5203 * Open a file in noclobber mode.
5204 * The code was copied from bash.
5207 noclobberopen(const char *fname)
5210 struct stat finfo, finfo2;
5213 * If the file exists and is a regular file, return an error
5216 r = stat(fname, &finfo);
5217 if (r == 0 && S_ISREG(finfo.st_mode)) {
5223 * If the file was not present (r != 0), make sure we open it
5224 * exclusively so that if it is created before we open it, our open
5225 * will fail. Make sure that we do not truncate an existing file.
5226 * Note that we don't turn on O_EXCL unless the stat failed -- if the
5227 * file was not a regular file, we leave O_EXCL off.
5230 return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666);
5231 fd = open(fname, O_WRONLY|O_CREAT, 0666);
5233 /* If the open failed, return the file descriptor right away. */
5238 * OK, the open succeeded, but the file may have been changed from a
5239 * non-regular file to a regular file between the stat and the open.
5240 * We are assuming that the O_EXCL open handles the case where FILENAME
5241 * did not exist and is symlinked to an existing file between the stat
5246 * If we can open it and fstat the file descriptor, and neither check
5247 * revealed that it was a regular file, and the file has not been
5248 * replaced, return the file descriptor.
5250 if (fstat(fd, &finfo2) == 0
5251 && !S_ISREG(finfo2.st_mode)
5252 && finfo.st_dev == finfo2.st_dev
5253 && finfo.st_ino == finfo2.st_ino
5258 /* The file has been replaced. badness. */
5265 * Handle here documents. Normally we fork off a process to write the
5266 * data to a pipe. If the document is short, we can stuff the data in
5267 * the pipe without forking.
5269 /* openhere needs this forward reference */
5270 static void expandhere(union node *arg, int fd);
5272 openhere(union node *redir)
5278 ash_msg_and_raise_error("pipe call failed");
5279 if (redir->type == NHERE) {
5280 len = strlen(redir->nhere.doc->narg.text);
5281 if (len <= PIPE_BUF) {
5282 full_write(pip[1], redir->nhere.doc->narg.text, len);
5286 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
5289 ignoresig(SIGINT); //signal(SIGINT, SIG_IGN);
5290 ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN);
5291 ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN);
5292 ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN);
5293 signal(SIGPIPE, SIG_DFL);
5294 if (redir->type == NHERE)
5295 full_write(pip[1], redir->nhere.doc->narg.text, len);
5297 expandhere(redir->nhere.doc, pip[1]);
5298 _exit(EXIT_SUCCESS);
5306 openredirect(union node *redir)
5311 switch (redir->nfile.type) {
5312 /* Can't happen, our single caller does this itself */
5318 return openhere(redir);
5321 /* For N[X]HERE, reading redir->nfile.expfname would touch beyond
5322 * allocated space. Do it only when we know it is safe.
5324 fname = redir->nfile.expfname;
5326 switch (redir->nfile.type) {
5332 f = open(fname, O_RDONLY);
5337 f = open(fname, O_RDWR|O_CREAT, 0666);
5342 #if BASH_REDIR_OUTPUT
5345 /* Take care of noclobber mode. */
5347 f = noclobberopen(fname);
5354 f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5359 f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666);
5367 ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory"));
5369 ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file"));
5373 * Copy a file descriptor to be >= 10. Throws exception on error.
5381 newfd = fcntl(from, F_DUPFD, 10);
5382 err = newfd < 0 ? errno : 0;
5385 ash_msg_and_raise_error("%d: %m", from);
5387 fcntl(newfd, F_SETFD, FD_CLOEXEC);
5393 dup2_or_raise(int from, int to)
5397 newfd = (from != to) ? dup2(from, to) : to;
5399 /* Happens when source fd is not open: try "echo >&99" */
5400 ash_msg_and_raise_error("%d: %m", from);
5405 /* Struct def and variable are moved down to the first usage site */
5410 struct redirtab *next;
5412 struct two_fd_t two_fd[];
5414 #define redirlist (G_var.redirlist)
5416 COPYFD_RESTORE = (int)~(INT_MAX),
5420 need_to_remember(struct redirtab *rp, int fd)
5424 if (!rp) /* remembering was not requested */
5427 for (i = 0; i < rp->pair_count; i++) {
5428 if (rp->two_fd[i].orig == fd) {
5429 /* already remembered */
5436 /* "hidden" fd is a fd used to read scripts, or a copy of such */
5438 is_hidden_fd(struct redirtab *rp, int fd)
5441 struct parsefile *pf;
5445 /* Check open scripts' fds */
5448 /* We skip pf_fd == 0 case because of the following case:
5449 * $ ash # running ash interactively
5451 * and in script.sh: "exec 9>&0".
5452 * Even though top-level pf_fd _is_ 0,
5453 * it's still ok to use it: "read" builtin uses it,
5454 * why should we cripple "exec" builtin?
5456 if (pf->pf_fd > 0 && fd == pf->pf_fd) {
5464 /* Check saved fds of redirects */
5465 fd |= COPYFD_RESTORE;
5466 for (i = 0; i < rp->pair_count; i++) {
5467 if (rp->two_fd[i].copy == fd) {
5475 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
5476 * old file descriptors are stashed away so that the redirection can be
5477 * undone by calling popredir.
5479 /* flags passed to redirect */
5480 #define REDIR_PUSH 01 /* save previous values of file descriptors */
5481 #define REDIR_SAVEFD2 03 /* set preverrout */
5483 redirect(union node *redir, int flags)
5485 struct redirtab *sv;
5490 int copied_fd2 = -1;
5499 if (flags & REDIR_PUSH) {
5500 union node *tmp = redir;
5503 #if BASH_REDIR_OUTPUT
5504 if (tmp->nfile.type == NTO2)
5507 tmp = tmp->nfile.next;
5509 sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0]));
5510 sv->next = redirlist;
5511 sv->pair_count = sv_pos;
5513 while (sv_pos > 0) {
5515 sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY;
5521 fd = redir->nfile.fd;
5522 if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) {
5523 right_fd = redir->ndup.dupfd;
5524 //bb_error_msg("doing %d > %d", fd, right_fd);
5525 /* redirect from/to same file descriptor? */
5528 /* "echo >&10" and 10 is a fd opened to a sh script? */
5529 if (is_hidden_fd(sv, right_fd)) {
5530 errno = EBADF; /* as if it is closed */
5531 ash_msg_and_raise_error("%d: %m", right_fd);
5535 newfd = openredirect(redir); /* always >= 0 */
5537 /* Descriptor wasn't open before redirect.
5538 * Mark it for close in the future */
5539 if (need_to_remember(sv, fd)) {
5540 goto remember_to_close;
5545 #if BASH_REDIR_OUTPUT
5548 if (need_to_remember(sv, fd)) {
5549 /* Copy old descriptor */
5550 /* Careful to not accidentally "save"
5551 * to the same fd as right side fd in N>&M */
5552 int minfd = right_fd < 10 ? 10 : right_fd + 1;
5553 #if defined(F_DUPFD_CLOEXEC)
5554 i = fcntl(fd, F_DUPFD_CLOEXEC, minfd);
5556 i = fcntl(fd, F_DUPFD, minfd);
5561 /* Strange error (e.g. "too many files" EMFILE?) */
5565 ash_msg_and_raise_error("%d: %m", fd);
5568 /* EBADF: it is not open - good, remember to close it */
5571 } else { /* fd is open, save its copy */
5572 #if !defined(F_DUPFD_CLOEXEC)
5573 fcntl(i, F_SETFD, FD_CLOEXEC);
5575 /* "exec fd>&-" should not close fds
5576 * which point to script file(s).
5577 * Force them to be restored afterwards */
5578 if (is_hidden_fd(sv, fd))
5579 i |= COPYFD_RESTORE;
5583 sv->two_fd[sv_pos].orig = fd;
5584 sv->two_fd[sv_pos].copy = i;
5588 /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */
5589 if (redir->ndup.dupfd < 0) { /* "fd>&-" */
5590 /* Don't want to trigger debugging */
5594 dup2_or_raise(redir->ndup.dupfd, fd);
5596 } else if (fd != newfd) { /* move newfd to fd */
5597 dup2_or_raise(newfd, fd);
5598 #if BASH_REDIR_OUTPUT
5599 if (!(redir->nfile.type == NTO2 && fd == 2))
5603 #if BASH_REDIR_OUTPUT
5604 if (redir->nfile.type == NTO2 && fd == 1) {
5605 /* We already redirected it to fd 1, now copy it to 2 */
5611 } while ((redir = redir->nfile.next) != NULL);
5614 if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0)
5615 preverrout_fd = copied_fd2;
5619 * Undo the effects of the last redirection.
5622 popredir(int drop, int restore)
5624 struct redirtab *rp;
5627 if (redirlist == NULL)
5631 for (i = 0; i < rp->pair_count; i++) {
5632 int fd = rp->two_fd[i].orig;
5633 int copy = rp->two_fd[i].copy;
5634 if (copy == CLOSED) {
5639 if (copy != EMPTY) {
5640 if (!drop || (restore && (copy & COPYFD_RESTORE))) {
5641 copy &= ~COPYFD_RESTORE;
5643 dup2_or_raise(copy, fd);
5645 close(copy & ~COPYFD_RESTORE);
5648 redirlist = rp->next;
5654 * Undo all redirections. Called on error or interrupt.
5658 redirectsafe(union node *redir, int flags)
5661 volatile int saveint;
5662 struct jmploc *volatile savehandler = exception_handler;
5663 struct jmploc jmploc;
5666 /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */
5667 err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2;
5669 exception_handler = &jmploc;
5670 redirect(redir, flags);
5672 exception_handler = savehandler;
5673 if (err && exception_type != EXERROR)
5674 longjmp(exception_handler->loc, 1);
5675 RESTORE_INT(saveint);
5680 /* ============ Routines to expand arguments to commands
5682 * We have to deal with backquotes, shell variables, and file metacharacters.
5685 #if ENABLE_FEATURE_SH_MATH
5687 ash_arith(const char *s)
5689 arith_state_t math_state;
5692 math_state.lookupvar = lookupvar;
5693 math_state.setvar = setvar0;
5694 //math_state.endofname = endofname;
5697 result = arith(&math_state, s);
5698 if (math_state.errmsg)
5699 ash_msg_and_raise_error(math_state.errmsg);
5709 #define EXP_FULL 0x1 /* perform word splitting & file globbing */
5710 #define EXP_TILDE 0x2 /* do normal tilde expansion */
5711 #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */
5712 #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */
5713 /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt"
5714 * POSIX says for this case:
5715 * Pathname expansion shall not be performed on the word by a
5716 * non-interactive shell; an interactive shell may perform it, but shall
5717 * do so only when the expansion would result in one word.
5718 * Currently, our code complies to the above rule by never globbing
5719 * redirection filenames.
5720 * Bash performs globbing, unless it is non-interactive and in POSIX mode.
5721 * (this means that on a typical Linux distro, bash almost always
5722 * performs globbing, and thus diverges from what we do).
5724 #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */
5725 #define EXP_QPAT 0x20 /* pattern in quoted parameter expansion */
5726 #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */
5727 #define EXP_WORD 0x80 /* expand word in parameter expansion */
5728 #define EXP_QUOTED 0x100 /* expand word in double quotes */
5732 #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */
5733 #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */
5734 #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */
5735 #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */
5736 #define RMESCAPE_SLASH 0x20 /* Stop globbing after slash */
5738 /* Add CTLESC when necessary. */
5739 #define QUOTES_ESC (EXP_FULL | EXP_CASE | EXP_QPAT | EXP_REDIR)
5740 /* Do not skip NUL characters. */
5741 #define QUOTES_KEEPNUL EXP_TILDE
5744 * Structure specifying which parts of the string should be searched
5745 * for IFS characters.
5748 struct ifsregion *next; /* next region in list */
5749 int begoff; /* offset of start of region */
5750 int endoff; /* offset of end of region */
5751 int nulonly; /* search for nul bytes only */
5755 struct strlist *list;
5756 struct strlist **lastp;
5759 /* output of current string */
5760 static char *expdest;
5761 /* list of back quote expressions */
5762 static struct nodelist *argbackq;
5763 /* first struct in list of ifs regions */
5764 static struct ifsregion ifsfirst;
5765 /* last struct in list */
5766 static struct ifsregion *ifslastp;
5767 /* holds expanded arg list */
5768 static struct arglist exparg;
5773 #if !ENABLE_FEATURE_SH_MATH
5774 /* cvtnum() is used even if math support is off (to prepare $? values and such) */
5775 typedef long arith_t;
5776 # define ARITH_FMT "%ld"
5783 expdest = makestrspace(sizeof(arith_t)*3 + 2, expdest);
5784 len = fmtstr(expdest, sizeof(arith_t)*3 + 2, ARITH_FMT, num);
5785 STADJUST(len, expdest);
5790 * Break the argument string into pieces based upon IFS and add the
5791 * strings to the argument list. The regions of the string to be
5792 * searched for IFS characters have been stored by recordregion.
5795 ifsbreakup(char *string, struct arglist *arglist)
5797 struct ifsregion *ifsp;
5802 const char *ifs, *realifs;
5807 if (ifslastp != NULL) {
5810 realifs = ifsset() ? ifsval() : defifs;
5813 p = string + ifsp->begoff;
5814 nulonly = ifsp->nulonly;
5815 ifs = nulonly ? nullstr : realifs;
5817 while (p < string + ifsp->endoff) {
5819 if ((unsigned char)*p == CTLESC)
5821 if (!strchr(ifs, *p)) {
5826 ifsspc = (strchr(defifs, *p) != NULL);
5827 /* Ignore IFS whitespace at start */
5828 if (q == start && ifsspc) {
5834 sp = stzalloc(sizeof(*sp));
5836 *arglist->lastp = sp;
5837 arglist->lastp = &sp->next;
5841 if (p >= string + ifsp->endoff) {
5845 if ((unsigned char)*p == CTLESC)
5847 if (strchr(ifs, *p) == NULL) {
5851 if (strchr(defifs, *p) == NULL) {
5866 } while (ifsp != NULL);
5875 sp = stzalloc(sizeof(*sp));
5877 *arglist->lastp = sp;
5878 arglist->lastp = &sp->next;
5884 struct ifsregion *p = ifsfirst.next;
5891 struct ifsregion *ifsp;
5896 ifsfirst.next = NULL;
5903 esclen(const char *start, const char *p)
5907 while (p > start && (unsigned char)*--p == CTLESC) {
5914 * Remove any CTLESC characters from a string.
5917 rmescapes(char *str, int flag)
5919 static const char qchars[] ALIGN1 = {
5920 IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' };
5924 unsigned protect_against_glob;
5926 IF_BASH_PATTERN_SUBST(unsigned slash = flag & RMESCAPE_SLASH;)
5928 p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash));
5934 if (flag & RMESCAPE_ALLOC) {
5935 size_t len = p - str;
5936 size_t fulllen = len + strlen(p) + 1;
5938 if (flag & RMESCAPE_GROW) {
5939 int strloc = str - (char *)stackblock();
5940 r = makestrspace(fulllen, expdest);
5941 /* p and str may be invalidated by makestrspace */
5942 str = (char *)stackblock() + strloc;
5944 } else if (flag & RMESCAPE_HEAP) {
5945 r = ckmalloc(fulllen);
5947 r = stalloc(fulllen);
5951 q = (char *)mempcpy(q, str, len);
5956 globbing = flag & RMESCAPE_GLOB;
5957 protect_against_glob = globbing;
5959 if ((unsigned char)*p == CTLQUOTEMARK) {
5960 // Note: both inquotes and protect_against_glob only affect whether
5961 // CTLESC,<ch> gets converted to <ch> or to \<ch>
5962 inquotes = ~inquotes;
5964 protect_against_glob = globbing;
5967 if ((unsigned char)*p == CTLESC) {
5971 ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)");
5973 if (protect_against_glob) {
5975 * We used to trust glob() and fnmatch() to eat
5976 * superfluous escapes (\z where z has no
5977 * special meaning anyway). But this causes
5978 * bugs such as string of one greek letter rho
5979 * (unicode-encoded as two bytes "cf,81")
5980 * getting encoded as "cf,CTLESC,81"
5981 * and here, converted to "cf,\,81" -
5982 * which does not go well with some flavors
5983 * of fnmatch() in unicode locales
5984 * (for example, glibc <= 2.22).
5986 * Lets add "\" only on the chars which need it.
5987 * Testcases for less obvious chars are shown.
5992 || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */
5993 || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */
5994 || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */
5995 || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */
5996 /* Some libc support [^negate], that's why "^" also needs love */
5997 || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */
6002 } else if (*p == '\\' && !inquotes) {
6003 /* naked back slash */
6004 protect_against_glob = 0;
6007 #if BASH_PATTERN_SUBST
6008 else if (*p == '/' && slash) {
6009 /* stop handling globbing and mark location of slash */
6010 globbing = slash = 0;
6014 protect_against_glob = globbing;
6019 if (flag & RMESCAPE_GROW) {
6021 STADJUST(q - r + 1, expdest);
6025 #define pmatch(a, b) !fnmatch((a), (b), 0)
6028 * Prepare a pattern for a expmeta (internal glob(3)) call.
6030 * Returns an stalloced string.
6033 preglob(const char *pattern, int flag)
6035 return rmescapes((char *)pattern, flag | RMESCAPE_GLOB);
6039 * Put a string on the stack.
6042 memtodest(const char *p, size_t len, int syntax, int quotes)
6049 q = makestrspace((quotes & QUOTES_ESC) ? len * 2 : len, expdest);
6052 unsigned char c = *p++;
6054 if (quotes & QUOTES_ESC) {
6055 int n = SIT(c, syntax);
6057 || (((quotes & EXP_FULL) || syntax != BASESYNTAX)
6064 } else if (!(quotes & QUOTES_KEEPNUL))
6073 strtodest(const char *p, int syntax, int quotes)
6075 size_t len = strlen(p);
6076 memtodest(p, len, syntax, quotes);
6081 * Record the fact that we have to scan this region of the
6082 * string for IFS characters.
6085 recordregion(int start, int end, int nulonly)
6087 struct ifsregion *ifsp;
6089 if (ifslastp == NULL) {
6093 ifsp = ckzalloc(sizeof(*ifsp));
6094 /*ifsp->next = NULL; - ckzalloc did it */
6095 ifslastp->next = ifsp;
6099 ifslastp->begoff = start;
6100 ifslastp->endoff = end;
6101 ifslastp->nulonly = nulonly;
6105 removerecordregions(int endoff)
6107 if (ifslastp == NULL)
6110 if (ifsfirst.endoff > endoff) {
6111 while (ifsfirst.next) {
6112 struct ifsregion *ifsp;
6114 ifsp = ifsfirst.next->next;
6115 free(ifsfirst.next);
6116 ifsfirst.next = ifsp;
6119 if (ifsfirst.begoff > endoff) {
6122 ifslastp = &ifsfirst;
6123 ifsfirst.endoff = endoff;
6128 ifslastp = &ifsfirst;
6129 while (ifslastp->next && ifslastp->next->begoff < endoff)
6130 ifslastp = ifslastp->next;
6131 while (ifslastp->next) {
6132 struct ifsregion *ifsp;
6134 ifsp = ifslastp->next->next;
6135 free(ifslastp->next);
6136 ifslastp->next = ifsp;
6139 if (ifslastp->endoff > endoff)
6140 ifslastp->endoff = endoff;
6144 exptilde(char *startp, char *p, int flags)
6150 int quotes = flags & QUOTES_ESC;
6154 while ((c = *++p) != '\0') {
6161 if (flags & EXP_VARTILDE)
6171 if (*name == '\0') {
6172 home = lookupvar("HOME");
6174 pw = getpwnam(name);
6179 if (!home || !*home)
6182 strtodest(home, SQSYNTAX, quotes);
6190 * Execute a command inside back quotes. If it's a builtin command, we
6191 * want to save its output in a block obtained from malloc. Otherwise
6192 * we fork off a subprocess and get the output of the command via a pipe.
6193 * Should be called with interrupts off.
6195 struct backcmd { /* result of evalbackcmd */
6196 int fd; /* file descriptor to read from */
6197 int nleft; /* number of chars in buffer */
6198 char *buf; /* buffer */
6199 struct job *jp; /* job structure for command */
6202 /* These forward decls are needed to use "eval" code for backticks handling: */
6203 #define EV_EXIT 01 /* exit after evaluating tree */
6204 static int evaltree(union node *, int);
6206 static void FAST_FUNC
6207 evalbackcmd(union node *n, struct backcmd *result)
6221 ash_msg_and_raise_error("pipe call failed");
6222 jp = makejob(/*n,*/ 1);
6223 if (forkshell(jp, n, FORK_NOJOB) == 0) {
6229 dup2_or_raise(pip[1], 1);
6232 /* TODO: eflag clearing makes the following not abort:
6233 * ash -c 'set -e; z=$(false;echo foo); echo $z'
6234 * which is what bash does (unless it is in POSIX mode).
6235 * dash deleted "eflag = 0" line in the commit
6236 * Date: Mon, 28 Jun 2010 17:11:58 +1000
6237 * [EVAL] Don't clear eflag in evalbackcmd
6238 * For now, preserve bash-like behavior, it seems to be somewhat more useful:
6242 evaltree(n, EV_EXIT); /* actually evaltreenr... */
6247 result->fd = pip[0];
6251 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
6252 result->fd, result->buf, result->nleft, result->jp));
6256 * Expand stuff in backwards quotes.
6259 expbackq(union node *cmd, int flag)
6267 int syntax = flag & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
6268 struct stackmark smark;
6271 startloc = expdest - (char *)stackblock();
6272 pushstackmark(&smark, startloc);
6273 evalbackcmd(cmd, &in);
6274 popstackmark(&smark);
6281 memtodest(p, i, syntax, flag & QUOTES_ESC);
6285 i = nonblock_immune_read(in.fd, buf, sizeof(buf));
6286 TRACE(("expbackq: read returns %d\n", i));
6295 back_exitstatus = waitforjob(in.jp);
6299 /* Eat all trailing newlines */
6301 for (; dest > (char *)stackblock() && dest[-1] == '\n';)
6305 if (!(flag & EXP_QUOTED))
6306 recordregion(startloc, dest - (char *)stackblock(), 0);
6307 TRACE(("evalbackq: size:%d:'%.*s'\n",
6308 (int)((dest - (char *)stackblock()) - startloc),
6309 (int)((dest - (char *)stackblock()) - startloc),
6310 stackblock() + startloc));
6313 #if ENABLE_FEATURE_SH_MATH
6315 * Expand arithmetic expression. Backup to start of expression,
6316 * evaluate, place result in (backed up) result, adjust string position.
6328 * This routine is slightly over-complicated for
6329 * efficiency. Next we scan backwards looking for the
6330 * start of arithmetic.
6332 start = stackblock();
6339 while ((unsigned char)*p != CTLARI) {
6343 ash_msg_and_raise_error("missing CTLARI (shouldn't happen)");
6348 esc = esclen(start, p);
6358 removerecordregions(begoff);
6362 if (flag & QUOTES_ESC)
6363 rmescapes(p + 1, 0);
6365 len = cvtnum(ash_arith(p + 1));
6367 if (!(flag & EXP_QUOTED))
6368 recordregion(begoff, begoff + len, 0);
6372 /* argstr needs it */
6373 static char *evalvar(char *p, int flags, struct strlist *var_str_list);
6376 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
6377 * characters to allow for further processing. Otherwise treat
6378 * $@ like $* since no splitting will be performed.
6380 * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence
6381 * over shell variables. Needed for "A=a B=$A; echo $B" case - we use it
6382 * for correct expansion of "B=$A" word.
6385 argstr(char *p, int flags, struct strlist *var_str_list)
6387 static const char spclchars[] ALIGN1 = {
6395 #if ENABLE_FEATURE_SH_MATH
6400 const char *reject = spclchars;
6401 int breakall = (flags & (EXP_WORD | EXP_QUOTED)) == EXP_WORD;
6406 if (!(flags & EXP_VARTILDE)) {
6408 } else if (flags & EXP_VARTILDE2) {
6413 if (flags & EXP_TILDE) {
6416 flags &= ~EXP_TILDE;
6420 p = exptilde(p, q, flags);
6423 startloc = expdest - (char *)stackblock();
6427 length += strcspn(p + length, reject);
6431 IF_FEATURE_SH_MATH(|| c == CTLENDARI)
6433 /* c == '=' || c == ':' || c == CTLENDARI */
6439 expdest = stack_nputstr(p, length, expdest);
6440 newloc = expdest - (char *)stackblock();
6441 if (breakall && !inquotes && newloc > startloc) {
6442 recordregion(startloc, newloc, 0);
6453 if (flags & EXP_VARTILDE2) {
6457 flags |= EXP_VARTILDE2;
6462 * sort of a hack - expand tildes in variable
6463 * assignments (after the first '=' and after ':'s).
6472 case CTLENDVAR: /* ??? */
6475 inquotes ^= EXP_QUOTED;
6476 /* "$@" syntax adherence hack */
6477 if (inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) {
6478 p = evalvar(p + 1, flags | inquotes, /* var_str_list: */ NULL) + 1;
6482 if (flags & QUOTES_ESC) {
6493 * Quoted parameter expansion pattern: remove quote
6494 * unless inside inner quotes or we have a literal
6497 if (((flags | inquotes) & (EXP_QPAT | EXP_QUOTED)) ==
6498 EXP_QPAT && *p != '\\')
6503 TRACE(("argstr: evalvar('%s')\n", p));
6504 p = evalvar(p, flags | inquotes, var_str_list);
6505 TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock()));
6508 expbackq(argbackq->n, flags | inquotes);
6509 argbackq = argbackq->next;
6511 #if ENABLE_FEATURE_SH_MATH
6514 expari(flags | inquotes);
6523 scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM,
6524 char *pattern, int quotes, int zero)
6533 const char *s = loc2;
6540 match = pmatch(pattern, s);
6545 if (quotes && (unsigned char)*loc == CTLESC)
6554 scanright(char *startp, char *rmesc, char *rmescend,
6555 char *pattern, int quotes, int match_at_start)
6557 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6558 int try2optimize = match_at_start;
6564 /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}":
6565 * startp="escaped_value_of_v" rmesc="raw_value_of_v"
6566 * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1
6568 * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc,
6569 * and on each iteration they go back two/one char until they reach the beginning.
6570 * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc.
6572 /* TODO: document in what other circumstances we are called. */
6574 for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) {
6577 const char *s = loc2;
6578 if (match_at_start) {
6582 match = pmatch(pattern, s);
6583 //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match);
6587 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6589 /* Maybe we can optimize this:
6590 * if pattern ends with unescaped *, we can avoid checking
6591 * shorter strings: if "foo*" doesn't match "raw_value_of_v",
6592 * it won't match truncated "raw_value_of_" strings too.
6594 unsigned plen = strlen(pattern);
6595 /* Does it end with "*"? */
6596 if (plen != 0 && pattern[--plen] == '*') {
6597 /* "xxxx*" is not escaped */
6598 /* "xxx\*" is escaped */
6599 /* "xx\\*" is not escaped */
6600 /* "x\\\*" is escaped */
6602 while (plen != 0 && pattern[--plen] == '\\')
6605 break; /* ends with unescaped "*" */
6613 esc = esclen(startp, loc);
6624 static void varunset(const char *, const char *, const char *, int) NORETURN;
6626 varunset(const char *end, const char *var, const char *umsg, int varflags)
6632 msg = "parameter not set";
6634 if ((unsigned char)*end == CTLENDVAR) {
6635 if (varflags & VSNUL)
6641 ash_msg_and_raise_error("%.*s: %s%s", (int)(end - var - 1), var, msg, tail);
6645 subevalvar(char *p, char *varname, int strloc, int subtype,
6646 int startloc, int varflags, int flag, struct strlist *var_str_list)
6648 struct nodelist *saveargbackq = argbackq;
6649 int quotes = flag & QUOTES_ESC;
6652 char *rmesc, *rmescend;
6654 int amount, resetloc;
6655 IF_BASH_PATTERN_SUBST(int workloc;)
6656 IF_BASH_PATTERN_SUBST(char *repl = NULL;)
6658 char *(*scan)(char*, char*, char*, char*, int, int);
6660 //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)",
6661 // p, varname, strloc, subtype, startloc, varflags, quotes);
6663 argstr(p, EXP_TILDE | (subtype != VSASSIGN && subtype != VSQUESTION ?
6664 (flag & (EXP_QUOTED | EXP_QPAT) ? EXP_QPAT : EXP_CASE) : 0),
6666 STPUTC('\0', expdest);
6667 argbackq = saveargbackq;
6668 startp = (char *)stackblock() + startloc;
6672 setvar0(varname, startp);
6673 amount = startp - expdest;
6674 STADJUST(amount, expdest);
6678 varunset(p, varname, startp, varflags);
6683 int pos, len, orig_len;
6686 loc = str = stackblock() + strloc;
6688 # if !ENABLE_FEATURE_SH_MATH
6689 # define ash_arith number
6691 /* Read POS in ${var:POS:LEN} */
6692 colon = strchr(loc, ':');
6693 if (colon) *colon = '\0';
6694 pos = ash_arith(loc);
6695 if (colon) *colon = ':';
6697 /* Read LEN in ${var:POS:LEN} */
6698 len = str - startp - 1;
6699 /* *loc != '\0', guaranteed by parser */
6703 /* Adjust the length by the number of escapes */
6704 for (ptr = startp; ptr < (str - 1); ptr++) {
6705 if ((unsigned char)*ptr == CTLESC) {
6712 if (*loc++ == ':') {
6714 len = ash_arith(loc);
6716 /* Skip POS in ${var:POS:LEN} */
6718 while (*loc && *loc != ':') {
6721 if (*loc++ == ':') {
6722 len = ash_arith(loc);
6728 /* ${VAR:$((-n)):l} starts n chars from the end */
6729 pos = orig_len + pos;
6731 if ((unsigned)pos >= orig_len) {
6732 /* apart from obvious ${VAR:999999:l},
6733 * covers ${VAR:$((-9999999)):l} - result is ""
6740 /* ${VAR:N:-M} sets LEN to strlen()-M */
6741 len = (orig_len - pos) + len;
6743 if ((unsigned)len > (orig_len - pos))
6744 len = orig_len - pos;
6746 for (str = startp; pos; str++, pos--) {
6747 if (quotes && (unsigned char)*str == CTLESC)
6750 for (loc = startp; len; len--) {
6751 if (quotes && (unsigned char)*str == CTLESC)
6756 amount = loc - expdest;
6757 STADJUST(amount, expdest);
6760 #endif /* BASH_SUBSTR */
6763 resetloc = expdest - (char *)stackblock();
6765 #if BASH_PATTERN_SUBST
6766 /* We'll comeback here if we grow the stack while handling
6767 * a VSREPLACE or VSREPLACEALL, since our pointers into the
6768 * stack will need rebasing, and we'll need to remove our work
6774 amount = expdest - ((char *)stackblock() + resetloc);
6775 STADJUST(-amount, expdest);
6776 startp = (char *)stackblock() + startloc;
6779 rmescend = (char *)stackblock() + strloc;
6781 rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW);
6782 if (rmesc != startp) {
6784 startp = (char *)stackblock() + startloc;
6788 str = (char *)stackblock() + strloc;
6790 * Example: v='a\bc'; echo ${v/\\b/_\\_\z_}
6791 * The result is a_\_z_c (not a\_\_z_c)!
6793 * The search pattern and replace string treat backslashes differently!
6794 * RMESCAPE_SLASH causes preglob to work differently on the pattern
6795 * and string. It's only used on the first call.
6797 preglob(str, IF_BASH_PATTERN_SUBST(
6798 (subtype == VSREPLACE || subtype == VSREPLACEALL) && !repl ?
6799 RMESCAPE_SLASH : ) 0);
6801 #if BASH_PATTERN_SUBST
6802 workloc = expdest - (char *)stackblock();
6803 if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
6808 repl = strchr(str, CTLESC);
6814 //bb_error_msg("str:'%s' repl:'%s'", str, repl);
6816 /* If there's no pattern to match, return the expansion unmolested */
6825 loc = scanright(idx, rmesc, rmescend, str, quotes, 1);
6826 //bb_error_msg("scanright('%s'):'%s'", str, loc);
6828 /* No match, advance */
6829 char *restart_detect = stackblock();
6831 STPUTC(*idx, expdest);
6832 if (quotes && (unsigned char)*idx == CTLESC) {
6835 STPUTC(*idx, expdest);
6837 if (stackblock() != restart_detect)
6842 /* continue; - prone to quadratic behavior, smarter code: */
6845 if (str[0] == '*') {
6846 /* Pattern is "*foo". If "*foo" does not match "long_string",
6847 * it would never match "ong_string" etc, no point in trying.
6854 if (subtype == VSREPLACEALL) {
6856 if (quotes && (unsigned char)*idx == CTLESC)
6865 //bb_error_msg("repl:'%s'", repl);
6866 for (loc = (char*)repl; *loc; loc++) {
6867 char *restart_detect = stackblock();
6868 if (quotes && *loc == '\\') {
6869 STPUTC(CTLESC, expdest);
6872 STPUTC(*loc, expdest);
6873 if (stackblock() != restart_detect)
6878 if (subtype == VSREPLACE) {
6879 //bb_error_msg("tail:'%s', quotes:%x", idx, quotes);
6881 char *restart_detect = stackblock();
6882 STPUTC(*idx, expdest);
6883 if (stackblock() != restart_detect)
6892 /* We've put the replaced text into a buffer at workloc, now
6893 * move it to the right place and adjust the stack.
6895 STPUTC('\0', expdest);
6896 startp = (char *)stackblock() + startloc;
6897 memmove(startp, (char *)stackblock() + workloc, len + 1);
6898 //bb_error_msg("startp:'%s'", startp);
6899 amount = expdest - (startp + len);
6900 STADJUST(-amount, expdest);
6903 #endif /* BASH_PATTERN_SUBST */
6905 subtype -= VSTRIMRIGHT;
6907 if (subtype < 0 || subtype > 7)
6910 /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */
6911 zero = subtype >> 1;
6912 /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */
6913 scan = (subtype & 1) ^ zero ? scanleft : scanright;
6915 loc = scan(startp, rmesc, rmescend, str, quotes, zero);
6918 memmove(startp, loc, str - loc);
6919 loc = startp + (str - loc) - 1;
6922 amount = loc - expdest;
6923 STADJUST(amount, expdest);
6929 * Add the value of a specialized variable to the stack string.
6930 * name parameter (examples):
6931 * ash -c 'echo $1' name:'1='
6932 * ash -c 'echo $qwe' name:'qwe='
6933 * ash -c 'echo $$' name:'$='
6934 * ash -c 'echo ${$}' name:'$='
6935 * ash -c 'echo ${$##q}' name:'$=q'
6936 * ash -c 'echo ${#$}' name:'$='
6937 * note: examples with bad shell syntax:
6938 * ash -c 'echo ${#$1}' name:'$=1'
6939 * ash -c 'echo ${#1#}' name:'1=#'
6941 static NOINLINE ssize_t
6942 varvalue(char *name, int varflags, int flags, struct strlist *var_str_list, int *quotedp)
6949 int quoted = *quotedp;
6950 int subtype = varflags & VSTYPE;
6951 int discard = subtype == VSPLUS || subtype == VSLENGTH;
6952 int quotes = (discard ? 0 : (flags & QUOTES_ESC)) | QUOTES_KEEPNUL;
6955 sep = (flags & EXP_FULL) << CHAR_BIT;
6956 syntax = quoted ? DQSYNTAX : BASESYNTAX;
6966 num = shellparam.nparam;
6974 goto check_1char_name;
6976 expdest = makestrspace(NOPTS, expdest);
6977 for (i = NOPTS - 1; i >= 0; i--) {
6979 USTPUTC(optletters(i), expdest);
6985 /* handles cases similar to ${#$1} */
6986 if (name[2] != '\0')
6987 raise_error_syntax("bad substitution");
7000 sep |= ifsset() ? ifsval()[0] : ' ';
7007 while ((p = *ap++) != NULL) {
7008 len += strtodest(p, syntax, quotes);
7012 memtodest(&sepc, 1, syntax, quotes);
7027 num = atoi(name); /* number(name) fails on ${N#str} etc */
7028 if (num < 0 || num > shellparam.nparam)
7030 p = num ? shellparam.p[num - 1] : arg0;
7033 /* NB: name has form "VAR=..." */
7035 /* "A=a B=$A" case: var_str_list is a list of "A=a" strings
7036 * which should be considered before we check variables. */
7038 unsigned name_len = (strchrnul(name, '=') - name) + 1;
7042 str = var_str_list->text;
7043 eq = strchr(str, '=');
7044 if (!eq) /* stop at first non-assignment */
7047 if (name_len == (unsigned)(eq - str)
7048 && strncmp(str, name, name_len) == 0
7051 /* goto value; - WRONG! */
7052 /* think "A=1 A=2 B=$A" */
7054 var_str_list = var_str_list->next;
7055 } while (var_str_list);
7059 p = lookupvar(name);
7064 len = strtodest(p, syntax, quotes);
7065 #if ENABLE_UNICODE_SUPPORT
7066 if (subtype == VSLENGTH && len > 0) {
7067 reinit_unicode_for_ash();
7068 if (unicode_status == UNICODE_ON) {
7069 STADJUST(-len, expdest);
7071 len = unicode_strlen(p);
7079 STADJUST(-len, expdest);
7084 * Expand a variable, and return a pointer to the next character in the
7088 evalvar(char *p, int flag, struct strlist *var_str_list)
7099 varflags = (unsigned char) *p++;
7100 subtype = varflags & VSTYPE;
7103 raise_error_syntax("bad substitution");
7105 quoted = flag & EXP_QUOTED;
7107 easy = (!quoted || (*var == '@' && shellparam.nparam));
7108 startloc = expdest - (char *)stackblock();
7109 p = strchr(p, '=') + 1; //TODO: use var_end(p)?
7112 varlen = varvalue(var, varflags, flag, var_str_list, "ed);
7113 if (varflags & VSNUL)
7116 if (subtype == VSPLUS) {
7117 varlen = -1 - varlen;
7121 if (subtype == VSMINUS) {
7126 flag | EXP_TILDE | EXP_WORD,
7134 if (subtype == VSASSIGN || subtype == VSQUESTION) {
7138 subevalvar(p, var, 0, subtype, startloc, varflags,
7139 flag & ~QUOTES_ESC, var_str_list);
7142 * Remove any recorded regions beyond
7145 removerecordregions(startloc);
7149 if (varlen < 0 && uflag)
7150 varunset(p, var, 0, 0);
7152 if (subtype == VSLENGTH) {
7153 cvtnum(varlen > 0 ? varlen : 0);
7157 if (subtype == VSNORMAL) {
7161 recordregion(startloc, expdest - (char *)stackblock(), quoted);
7170 case VSTRIMRIGHTMAX:
7174 #if BASH_PATTERN_SUBST
7186 * Terminate the string and start recording the pattern
7189 STPUTC('\0', expdest);
7190 patloc = expdest - (char *)stackblock();
7191 if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype,
7192 startloc, varflags, flag, var_str_list)) {
7193 int amount = expdest - (
7194 (char *)stackblock() + patloc - 1
7196 STADJUST(-amount, expdest);
7198 /* Remove any recorded regions beyond start of variable */
7199 removerecordregions(startloc);
7204 if (subtype != VSNORMAL) { /* skip to end of alternative */
7207 unsigned char c = *p++;
7210 else if (c == CTLBACKQ) {
7212 argbackq = argbackq->next;
7213 } else if (c == CTLVAR) {
7214 if ((*p++ & VSTYPE) != VSNORMAL)
7216 } else if (c == CTLENDVAR) {
7226 * Add a file name to the list.
7229 addfname(const char *name)
7233 sp = stzalloc(sizeof(*sp));
7234 sp->text = sstrdup(name);
7236 exparg.lastp = &sp->next;
7239 /* Avoid glob() (and thus, stat() et al) for words like "echo" */
7241 hasmeta(const char *p)
7243 static const char chars[] ALIGN1 = {
7244 '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0
7248 p = strpbrk(p, chars);
7251 switch ((unsigned char) *p) {
7255 if (*p == CTLQUOTEMARK)
7259 if (*p == '\0') /* huh? */
7270 if (!strchr(p + 1, ']')) {
7271 /* It's not a properly closed [] pattern,
7272 * but other metas may follow. Continue checking.
7273 * my[file* _is_ globbed by bash
7274 * and matches filenames like "my[file1".
7290 /* If we want to use glob() from libc... */
7291 #if !ENABLE_ASH_INTERNAL_GLOB
7293 /* Add the result of glob() to the list */
7295 addglob(const glob_t *pglob)
7297 char **p = pglob->gl_pathv;
7304 expandmeta(struct strlist *str /*, int flag*/)
7306 /* TODO - EXP_REDIR */
7316 if (!hasmeta(str->text))
7320 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7321 // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match
7322 // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?)
7324 // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping:
7325 // if you pass it "file\?", it returns "file\?", not "file?", if no match.
7326 // Which means you need to unescape the string, right? Not so fast:
7327 // if there _is_ a file named "file\?" (with backslash), it is returned
7328 // as "file\?" too (whichever pattern you used to find it, say, "file*").
7329 // You DON'T KNOW by looking at the result whether you need to unescape it.
7331 // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?",
7332 // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file.
7333 // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match).
7334 // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly.
7335 // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob);
7336 // i = glob(p, GLOB_NOMAGIC, NULL, &pglob);
7337 i = glob(p, 0, NULL, &pglob);
7338 //bb_error_msg("glob('%s'):%d '%s'...", p, i, pglob.gl_pathv ? pglob.gl_pathv[0] : "-");
7343 #if 0 // glibc 2.24.90 bug? Patterns like "*/file", when match, don't set GLOB_MAGCHAR
7344 /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */
7345 if (!(pglob.gl_flags & GLOB_MAGCHAR))
7357 *exparg.lastp = str;
7358 rmescapes(str->text, 0);
7359 exparg.lastp = &str->next;
7361 default: /* GLOB_NOSPACE */
7364 ash_msg_and_raise_error(bb_msg_memory_exhausted);
7371 /* ENABLE_ASH_INTERNAL_GLOB: Homegrown globbing code. (dash also has both, uses homegrown one.) */
7374 * Do metacharacter (i.e. *, ?, [...]) expansion.
7377 expmeta(char *expdir, char *enddir, char *name)
7393 for (p = name; esc = 0, *p; p += esc + 1) {
7394 if (*p == '*' || *p == '?')
7396 else if (*p == '[') {
7403 if (*q == '/' || *q == '\0')
7413 if (p[esc] == '/') {
7416 start = p + esc + 1;
7420 if (metaflag == 0) { /* we've reached the end of the file name */
7421 if (enddir != expdir)
7429 if (metaflag == 0 || lstat(expdir, &statb) >= 0)
7440 } while (p < start);
7442 if (enddir == expdir) {
7444 } else if (enddir == expdir + 1 && *expdir == '/') {
7453 if (enddir != expdir)
7455 if (*endname == 0) {
7468 while (!pending_int && (dp = readdir(dirp)) != NULL) {
7469 if (dp->d_name[0] == '.' && !matchdot)
7471 if (pmatch(start, dp->d_name)) {
7473 strcpy(enddir, dp->d_name);
7476 for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';)
7479 expmeta(expdir, p, endname);
7485 endname[-esc - 1] = esc ? '\\' : '/';
7488 static struct strlist *
7489 msort(struct strlist *list, int len)
7491 struct strlist *p, *q = NULL;
7492 struct strlist **lpp;
7500 for (n = half; --n >= 0;) {
7504 q->next = NULL; /* terminate first half of list */
7505 q = msort(list, half); /* sort first half of list */
7506 p = msort(p, len - half); /* sort second half */
7509 #if ENABLE_LOCALE_SUPPORT
7510 if (strcoll(p->text, q->text) < 0)
7512 if (strcmp(p->text, q->text) < 0)
7536 * Sort the results of file name expansion. It calculates the number of
7537 * strings to sort and then calls msort (short for merge sort) to do the
7540 static struct strlist *
7541 expsort(struct strlist *str)
7547 for (sp = str; sp; sp = sp->next)
7549 return msort(str, len);
7553 expandmeta(struct strlist *str /*, int flag*/)
7555 /* TODO - EXP_REDIR */
7559 struct strlist **savelastp;
7565 if (!hasmeta(str->text))
7567 savelastp = exparg.lastp;
7570 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7572 int i = strlen(str->text);
7573 //BUGGY estimation of how long expanded name can be
7574 expdir = ckmalloc(i < 2048 ? 2048 : i+1);
7576 expmeta(expdir, expdir, p);
7581 if (exparg.lastp == savelastp) {
7586 *exparg.lastp = str;
7587 rmescapes(str->text, 0);
7588 exparg.lastp = &str->next;
7590 *exparg.lastp = NULL;
7591 *savelastp = sp = expsort(*savelastp);
7592 while (sp->next != NULL)
7594 exparg.lastp = &sp->next;
7599 #endif /* ENABLE_ASH_INTERNAL_GLOB */
7602 * Perform variable substitution and command substitution on an argument,
7603 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
7604 * perform splitting and file name expansion. When arglist is NULL, perform
7605 * here document expansion.
7608 expandarg(union node *arg, struct arglist *arglist, int flag)
7613 argbackq = arg->narg.backquote;
7614 STARTSTACKSTR(expdest);
7615 TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag));
7616 argstr(arg->narg.text, flag,
7617 /* var_str_list: */ arglist ? arglist->list : NULL);
7618 p = _STPUTC('\0', expdest);
7620 if (arglist == NULL) {
7621 /* here document expanded */
7624 p = grabstackstr(p);
7625 TRACE(("expandarg: p:'%s'\n", p));
7626 exparg.lastp = &exparg.list;
7630 if (flag & EXP_FULL) {
7631 ifsbreakup(p, &exparg);
7632 *exparg.lastp = NULL;
7633 exparg.lastp = &exparg.list;
7634 expandmeta(exparg.list /*, flag*/);
7636 if (flag & EXP_REDIR) { /*XXX - for now, just remove escapes */
7638 TRACE(("expandarg: rmescapes:'%s'\n", p));
7640 sp = stzalloc(sizeof(*sp));
7643 exparg.lastp = &sp->next;
7645 *exparg.lastp = NULL;
7647 *arglist->lastp = exparg.list;
7648 arglist->lastp = exparg.lastp;
7656 * Expand shell variables and backquotes inside a here document.
7659 expandhere(union node *arg, int fd)
7661 expandarg(arg, (struct arglist *)NULL, EXP_QUOTED);
7662 full_write(fd, stackblock(), expdest - (char *)stackblock());
7666 * Returns true if the pattern matches the string.
7669 patmatch(char *pattern, const char *string)
7671 char *p = preglob(pattern, 0);
7672 //bb_error_msg("fnmatch(pattern:'%s',str:'%s')", p, string);
7673 return pmatch(p, string);
7677 * See if a pattern matches in a case statement.
7680 casematch(union node *pattern, char *val)
7682 struct stackmark smark;
7685 setstackmark(&smark);
7686 argbackq = pattern->narg.backquote;
7687 STARTSTACKSTR(expdest);
7688 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE,
7689 /* var_str_list: */ NULL);
7690 STACKSTRNUL(expdest);
7692 result = patmatch(stackblock(), val);
7693 popstackmark(&smark);
7698 /* ============ find_command */
7702 int (*builtin)(int, char **) FAST_FUNC;
7703 /* unsigned flags; */
7705 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
7706 /* "regular" builtins always take precedence over commands,
7707 * regardless of PATH=....%builtin... position */
7708 #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2)
7709 #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4)
7712 smallint cmdtype; /* CMDxxx */
7715 /* index >= 0 for commands without path (slashes) */
7716 /* (TODO: what exactly does the value mean? PATH position?) */
7717 /* index == -1 for commands with slashes */
7718 /* index == (-2 - applet_no) for NOFORK applets */
7719 const struct builtincmd *cmd;
7720 struct funcnode *func;
7723 /* values of cmdtype */
7724 #define CMDUNKNOWN -1 /* no entry in table for command */
7725 #define CMDNORMAL 0 /* command is an executable program */
7726 #define CMDFUNCTION 1 /* command is a shell function */
7727 #define CMDBUILTIN 2 /* command is a shell builtin */
7729 /* action to find_command() */
7730 #define DO_ERR 0x01 /* prints errors */
7731 #define DO_ABS 0x02 /* checks absolute paths */
7732 #define DO_NOFUNC 0x04 /* don't return shell functions, for command */
7733 #define DO_ALTPATH 0x08 /* using alternate path */
7734 #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */
7736 static void find_command(char *, struct cmdentry *, int, const char *);
7739 /* ============ Hashing commands */
7742 * When commands are first encountered, they are entered in a hash table.
7743 * This ensures that a full path search will not have to be done for them
7744 * on each invocation.
7746 * We should investigate converting to a linear search, even though that
7747 * would make the command name "hash" a misnomer.
7751 struct tblentry *next; /* next entry in hash chain */
7752 union param param; /* definition of builtin function */
7753 smallint cmdtype; /* CMDxxx */
7754 char rehash; /* if set, cd done since entry created */
7755 char cmdname[1]; /* name of command */
7758 static struct tblentry **cmdtable;
7759 #define INIT_G_cmdtable() do { \
7760 cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \
7763 static int builtinloc = -1; /* index in path of %builtin, or -1 */
7767 tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp)
7769 #if ENABLE_FEATURE_SH_STANDALONE
7770 if (applet_no >= 0) {
7771 if (APPLET_IS_NOEXEC(applet_no)) {
7775 run_applet_no_and_exit(applet_no, cmd, argv);
7777 /* re-exec ourselves with the new arguments */
7778 execve(bb_busybox_exec_path, argv, envp);
7779 /* If they called chroot or otherwise made the binary no longer
7780 * executable, fall through */
7787 execve(cmd, argv, envp);
7788 } while (errno == EINTR);
7790 execve(cmd, argv, envp);
7792 if (cmd != (char*) bb_busybox_exec_path && errno == ENOEXEC) {
7793 /* Run "cmd" as a shell script:
7794 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
7795 * "If the execve() function fails with ENOEXEC, the shell
7796 * shall execute a command equivalent to having a shell invoked
7797 * with the command name as its first operand,
7798 * with any remaining arguments passed to the new shell"
7800 * That is, do not use $SHELL, user's shell, or /bin/sh;
7801 * just call ourselves.
7803 * Note that bash reads ~80 chars of the file, and if it sees
7804 * a zero byte before it sees newline, it doesn't try to
7805 * interpret it, but fails with "cannot execute binary file"
7806 * message and exit code 126. For one, this prevents attempts
7807 * to interpret foreign ELF binaries as shell scripts.
7810 cmd = (char*) bb_busybox_exec_path;
7811 /* NB: this is only possible because all callers of shellexec()
7812 * ensure that the argv[-1] slot exists!
7815 argv[0] = (char*) "ash";
7821 * Exec a program. Never returns. If you change this routine, you may
7822 * have to change the find_command routine as well.
7823 * argv[-1] must exist and be writable! See tryexec() for why.
7825 static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN;
7826 static void shellexec(char *prog, char **argv, const char *path, int idx)
7832 int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */
7834 envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL);
7835 if (strchr(prog, '/') != NULL
7836 #if ENABLE_FEATURE_SH_STANDALONE
7837 || (applet_no = find_applet_by_name(prog)) >= 0
7840 tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp);
7841 if (applet_no >= 0) {
7842 /* We tried execing ourself, but it didn't work.
7843 * Maybe /proc/self/exe doesn't exist?
7852 while ((cmdname = path_advance(&path, prog)) != NULL) {
7853 if (--idx < 0 && pathopt == NULL) {
7854 tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
7855 if (errno != ENOENT && errno != ENOTDIR)
7862 /* Map to POSIX errors */
7874 exitstatus = exerrno;
7875 TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n",
7876 prog, e, suppress_int));
7877 ash_msg_and_raise(EXEXIT, "%s: %s", prog, errmsg(e, "not found"));
7882 printentry(struct tblentry *cmdp)
7888 idx = cmdp->param.index;
7891 name = path_advance(&path, cmdp->cmdname);
7893 } while (--idx >= 0);
7894 out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr));
7898 * Clear out command entries. The argument specifies the first entry in
7899 * PATH which has changed.
7902 clearcmdentry(int firstchange)
7904 struct tblentry **tblp;
7905 struct tblentry **pp;
7906 struct tblentry *cmdp;
7909 for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) {
7911 while ((cmdp = *pp) != NULL) {
7912 if ((cmdp->cmdtype == CMDNORMAL &&
7913 cmdp->param.index >= firstchange)
7914 || (cmdp->cmdtype == CMDBUILTIN &&
7915 builtinloc >= firstchange)
7928 * Locate a command in the command hash table. If "add" is nonzero,
7929 * add the command to the table if it is not already present. The
7930 * variable "lastcmdentry" is set to point to the address of the link
7931 * pointing to the entry, so that delete_cmd_entry can delete the
7934 * Interrupts must be off if called with add != 0.
7936 static struct tblentry **lastcmdentry;
7938 static struct tblentry *
7939 cmdlookup(const char *name, int add)
7941 unsigned int hashval;
7943 struct tblentry *cmdp;
7944 struct tblentry **pp;
7947 hashval = (unsigned char)*p << 4;
7949 hashval += (unsigned char)*p++;
7951 pp = &cmdtable[hashval % CMDTABLESIZE];
7952 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
7953 if (strcmp(cmdp->cmdname, name) == 0)
7957 if (add && cmdp == NULL) {
7958 cmdp = *pp = ckzalloc(sizeof(struct tblentry)
7960 /* + 1 - already done because
7961 * tblentry::cmdname is char[1] */);
7962 /*cmdp->next = NULL; - ckzalloc did it */
7963 cmdp->cmdtype = CMDUNKNOWN;
7964 strcpy(cmdp->cmdname, name);
7971 * Delete the command entry returned on the last lookup.
7974 delete_cmd_entry(void)
7976 struct tblentry *cmdp;
7979 cmdp = *lastcmdentry;
7980 *lastcmdentry = cmdp->next;
7981 if (cmdp->cmdtype == CMDFUNCTION)
7982 freefunc(cmdp->param.func);
7988 * Add a new command entry, replacing any existing command entry for
7989 * the same name - except special builtins.
7992 addcmdentry(char *name, struct cmdentry *entry)
7994 struct tblentry *cmdp;
7996 cmdp = cmdlookup(name, 1);
7997 if (cmdp->cmdtype == CMDFUNCTION) {
7998 freefunc(cmdp->param.func);
8000 cmdp->cmdtype = entry->cmdtype;
8001 cmdp->param = entry->u;
8005 static int FAST_FUNC
8006 hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8008 struct tblentry **pp;
8009 struct tblentry *cmdp;
8011 struct cmdentry entry;
8014 if (nextopt("r") != '\0') {
8019 if (*argptr == NULL) {
8020 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8021 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8022 if (cmdp->cmdtype == CMDNORMAL)
8030 while ((name = *argptr) != NULL) {
8031 cmdp = cmdlookup(name, 0);
8033 && (cmdp->cmdtype == CMDNORMAL
8034 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
8038 find_command(name, &entry, DO_ERR, pathval());
8039 if (entry.cmdtype == CMDUNKNOWN)
8047 * Called when a cd is done. Marks all commands so the next time they
8048 * are executed they will be rehashed.
8053 struct tblentry **pp;
8054 struct tblentry *cmdp;
8056 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8057 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8058 if (cmdp->cmdtype == CMDNORMAL
8059 || (cmdp->cmdtype == CMDBUILTIN
8060 && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
8070 * Fix command hash table when PATH changed.
8071 * Called before PATH is changed. The argument is the new value of PATH;
8072 * pathval() still returns the old value at this point.
8073 * Called with interrupts off.
8075 static void FAST_FUNC
8076 changepath(const char *new)
8084 firstchange = 9999; /* assume no change */
8090 if ((*old == '\0' && *new == ':')
8091 || (*old == ':' && *new == '\0')
8095 old = new; /* ignore subsequent differences */
8099 if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin"))
8106 if (builtinloc < 0 && idx_bltin >= 0)
8107 builtinloc = idx_bltin; /* zap builtins */
8108 if (builtinloc >= 0 && idx_bltin < 0)
8110 clearcmdentry(firstchange);
8111 builtinloc = idx_bltin;
8147 typedef smallint token_id_t;
8149 /* Nth bit indicates if token marks the end of a list */
8152 /* 0 */ | (1u << TEOF)
8153 /* 1 */ | (0u << TNL)
8154 /* 2 */ | (0u << TREDIR)
8155 /* 3 */ | (0u << TWORD)
8156 /* 4 */ | (0u << TSEMI)
8157 /* 5 */ | (0u << TBACKGND)
8158 /* 6 */ | (0u << TAND)
8159 /* 7 */ | (0u << TOR)
8160 /* 8 */ | (0u << TPIPE)
8161 /* 9 */ | (0u << TLP)
8162 /* 10 */ | (1u << TRP)
8163 /* 11 */ | (1u << TENDCASE)
8164 /* 12 */ | (1u << TENDBQUOTE)
8165 /* 13 */ | (0u << TNOT)
8166 /* 14 */ | (0u << TCASE)
8167 /* 15 */ | (1u << TDO)
8168 /* 16 */ | (1u << TDONE)
8169 /* 17 */ | (1u << TELIF)
8170 /* 18 */ | (1u << TELSE)
8171 /* 19 */ | (1u << TESAC)
8172 /* 20 */ | (1u << TFI)
8173 /* 21 */ | (0u << TFOR)
8175 /* 22 */ | (0u << TFUNCTION)
8177 /* 23 */ | (0u << TIF)
8178 /* 24 */ | (0u << TIN)
8179 /* 25 */ | (1u << TTHEN)
8180 /* 26 */ | (0u << TUNTIL)
8181 /* 27 */ | (0u << TWHILE)
8182 /* 28 */ | (0u << TBEGIN)
8183 /* 29 */ | (1u << TEND)
8184 , /* thus far 29 bits used */
8187 static const char *const tokname_array[] = {
8201 #define KWDOFFSET 13
8202 /* the following are keywords */
8224 /* Wrapper around strcmp for qsort/bsearch/... */
8226 pstrcmp(const void *a, const void *b)
8228 return strcmp((char*)a, *(char**)b);
8231 static const char *const *
8232 findkwd(const char *s)
8234 return bsearch(s, tokname_array + KWDOFFSET,
8235 ARRAY_SIZE(tokname_array) - KWDOFFSET,
8236 sizeof(tokname_array[0]), pstrcmp);
8240 * Locate and print what a word is...
8243 describe_command(char *command, const char *path, int describe_command_verbose)
8245 struct cmdentry entry;
8246 #if ENABLE_ASH_ALIAS
8247 const struct alias *ap;
8250 path = path ? path : pathval();
8252 if (describe_command_verbose) {
8256 /* First look at the keywords */
8257 if (findkwd(command)) {
8258 out1str(describe_command_verbose ? " is a shell keyword" : command);
8262 #if ENABLE_ASH_ALIAS
8263 /* Then look at the aliases */
8264 ap = lookupalias(command, 0);
8266 if (!describe_command_verbose) {
8271 out1fmt(" is an alias for %s", ap->val);
8276 find_command(command, &entry, DO_ABS, path);
8278 switch (entry.cmdtype) {
8280 int j = entry.u.index;
8286 p = path_advance(&path, command);
8290 if (describe_command_verbose) {
8291 out1fmt(" is %s", p);
8299 if (describe_command_verbose) {
8300 out1str(" is a shell function");
8307 if (describe_command_verbose) {
8308 out1fmt(" is a %sshell builtin",
8309 IS_BUILTIN_SPECIAL(entry.u.cmd) ?
8310 "special " : nullstr
8318 if (describe_command_verbose) {
8319 out1str(": not found\n");
8328 static int FAST_FUNC
8329 typecmd(int argc UNUSED_PARAM, char **argv)
8335 /* type -p ... ? (we don't bother checking for 'p') */
8336 if (argv[1] && argv[1][0] == '-') {
8341 err |= describe_command(argv[i++], NULL, verbose);
8346 #if ENABLE_ASH_CMDCMD
8347 /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */
8349 parse_command_args(char **argv, const char **path)
8362 if (c == '-' && !*cp) {
8370 *path = bb_default_path;
8373 /* run 'typecmd' for other options */
8382 static int FAST_FUNC
8383 commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8391 const char *path = NULL;
8393 /* "command [-p] PROG ARGS" (that is, without -V or -v)
8394 * never reaches this function.
8397 while ((c = nextopt("pvV")) != '\0')
8399 verify |= VERIFY_VERBOSE;
8401 /*verify |= VERIFY_BRIEF*/;
8407 path = bb_default_path;
8409 /* Mimic bash: just "command -v" doesn't complain, it's a nop */
8411 if (/*verify && */ cmd)
8412 return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/);
8419 /*static int funcblocksize; // size of structures in function */
8420 /*static int funcstringsize; // size of strings in node */
8421 static void *funcblock; /* block to allocate function from */
8422 static char *funcstring_end; /* end of block to allocate strings from */
8424 /* flags in argument to evaltree */
8425 #define EV_EXIT 01 /* exit after evaluating tree */
8426 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
8428 static const uint8_t nodesize[N_NUMBER] ALIGN1 = {
8429 [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)),
8430 [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)),
8431 [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)),
8432 [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)),
8433 [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)),
8434 [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)),
8435 [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)),
8436 [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)),
8437 [NIF ] = SHELL_ALIGN(sizeof(struct nif)),
8438 [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)),
8439 [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)),
8440 [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)),
8441 [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)),
8442 [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)),
8443 [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)),
8444 [NARG ] = SHELL_ALIGN(sizeof(struct narg)),
8445 [NTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8446 #if BASH_REDIR_OUTPUT
8447 [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)),
8449 [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)),
8450 [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)),
8451 [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8452 [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)),
8453 [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8454 [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8455 [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8456 [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8457 [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)),
8460 static int calcsize(int funcblocksize, union node *n);
8463 sizenodelist(int funcblocksize, struct nodelist *lp)
8466 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
8467 funcblocksize = calcsize(funcblocksize, lp->n);
8470 return funcblocksize;
8474 calcsize(int funcblocksize, union node *n)
8477 return funcblocksize;
8478 funcblocksize += nodesize[n->type];
8481 funcblocksize = calcsize(funcblocksize, n->ncmd.redirect);
8482 funcblocksize = calcsize(funcblocksize, n->ncmd.args);
8483 funcblocksize = calcsize(funcblocksize, n->ncmd.assign);
8486 funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist);
8491 funcblocksize = calcsize(funcblocksize, n->nredir.redirect);
8492 funcblocksize = calcsize(funcblocksize, n->nredir.n);
8499 funcblocksize = calcsize(funcblocksize, n->nbinary.ch2);
8500 funcblocksize = calcsize(funcblocksize, n->nbinary.ch1);
8503 funcblocksize = calcsize(funcblocksize, n->nif.elsepart);
8504 funcblocksize = calcsize(funcblocksize, n->nif.ifpart);
8505 funcblocksize = calcsize(funcblocksize, n->nif.test);
8508 funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */
8509 funcblocksize = calcsize(funcblocksize, n->nfor.body);
8510 funcblocksize = calcsize(funcblocksize, n->nfor.args);
8513 funcblocksize = calcsize(funcblocksize, n->ncase.cases);
8514 funcblocksize = calcsize(funcblocksize, n->ncase.expr);
8517 funcblocksize = calcsize(funcblocksize, n->nclist.body);
8518 funcblocksize = calcsize(funcblocksize, n->nclist.pattern);
8519 funcblocksize = calcsize(funcblocksize, n->nclist.next);
8523 funcblocksize = sizenodelist(funcblocksize, n->narg.backquote);
8524 funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */
8525 funcblocksize = calcsize(funcblocksize, n->narg.next);
8528 #if BASH_REDIR_OUTPUT
8535 funcblocksize = calcsize(funcblocksize, n->nfile.fname);
8536 funcblocksize = calcsize(funcblocksize, n->nfile.next);
8540 funcblocksize = calcsize(funcblocksize, n->ndup.vname);
8541 funcblocksize = calcsize(funcblocksize, n->ndup.next);
8545 funcblocksize = calcsize(funcblocksize, n->nhere.doc);
8546 funcblocksize = calcsize(funcblocksize, n->nhere.next);
8549 funcblocksize = calcsize(funcblocksize, n->nnot.com);
8552 return funcblocksize;
8556 nodeckstrdup(char *s)
8558 funcstring_end -= SHELL_ALIGN(strlen(s) + 1);
8559 return strcpy(funcstring_end, s);
8562 static union node *copynode(union node *);
8564 static struct nodelist *
8565 copynodelist(struct nodelist *lp)
8567 struct nodelist *start;
8568 struct nodelist **lpp;
8573 funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist));
8574 (*lpp)->n = copynode(lp->n);
8576 lpp = &(*lpp)->next;
8583 copynode(union node *n)
8590 funcblock = (char *) funcblock + nodesize[n->type];
8594 new->ncmd.redirect = copynode(n->ncmd.redirect);
8595 new->ncmd.args = copynode(n->ncmd.args);
8596 new->ncmd.assign = copynode(n->ncmd.assign);
8599 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
8600 new->npipe.pipe_backgnd = n->npipe.pipe_backgnd;
8605 new->nredir.redirect = copynode(n->nredir.redirect);
8606 new->nredir.n = copynode(n->nredir.n);
8613 new->nbinary.ch2 = copynode(n->nbinary.ch2);
8614 new->nbinary.ch1 = copynode(n->nbinary.ch1);
8617 new->nif.elsepart = copynode(n->nif.elsepart);
8618 new->nif.ifpart = copynode(n->nif.ifpart);
8619 new->nif.test = copynode(n->nif.test);
8622 new->nfor.var = nodeckstrdup(n->nfor.var);
8623 new->nfor.body = copynode(n->nfor.body);
8624 new->nfor.args = copynode(n->nfor.args);
8627 new->ncase.cases = copynode(n->ncase.cases);
8628 new->ncase.expr = copynode(n->ncase.expr);
8631 new->nclist.body = copynode(n->nclist.body);
8632 new->nclist.pattern = copynode(n->nclist.pattern);
8633 new->nclist.next = copynode(n->nclist.next);
8637 new->narg.backquote = copynodelist(n->narg.backquote);
8638 new->narg.text = nodeckstrdup(n->narg.text);
8639 new->narg.next = copynode(n->narg.next);
8642 #if BASH_REDIR_OUTPUT
8649 new->nfile.fname = copynode(n->nfile.fname);
8650 new->nfile.fd = n->nfile.fd;
8651 new->nfile.next = copynode(n->nfile.next);
8655 new->ndup.vname = copynode(n->ndup.vname);
8656 new->ndup.dupfd = n->ndup.dupfd;
8657 new->ndup.fd = n->ndup.fd;
8658 new->ndup.next = copynode(n->ndup.next);
8662 new->nhere.doc = copynode(n->nhere.doc);
8663 new->nhere.fd = n->nhere.fd;
8664 new->nhere.next = copynode(n->nhere.next);
8667 new->nnot.com = copynode(n->nnot.com);
8670 new->type = n->type;
8675 * Make a copy of a parse tree.
8677 static struct funcnode *
8678 copyfunc(union node *n)
8683 /*funcstringsize = 0;*/
8684 blocksize = offsetof(struct funcnode, n) + calcsize(0, n);
8685 f = ckzalloc(blocksize /* + funcstringsize */);
8686 funcblock = (char *) f + offsetof(struct funcnode, n);
8687 funcstring_end = (char *) f + blocksize;
8689 /* f->count = 0; - ckzalloc did it */
8694 * Define a shell function.
8697 defun(union node *func)
8699 struct cmdentry entry;
8702 entry.cmdtype = CMDFUNCTION;
8703 entry.u.func = copyfunc(func);
8704 addcmdentry(func->narg.text, &entry);
8708 /* Reasons for skipping commands (see comment on breakcmd routine) */
8709 #define SKIPBREAK (1 << 0)
8710 #define SKIPCONT (1 << 1)
8711 #define SKIPFUNC (1 << 2)
8712 static smallint evalskip; /* set to SKIPxxx if we are skipping commands */
8713 static int skipcount; /* number of levels to skip */
8714 static int funcnest; /* depth of function calls */
8715 static int loopnest; /* current loop nesting level */
8717 /* Forward decl way out to parsing code - dotrap needs it */
8718 static int evalstring(char *s, int flags);
8720 /* Called to execute a trap.
8721 * Single callsite - at the end of evaltree().
8722 * If we return non-zero, evaltree raises EXEXIT exception.
8724 * Perhaps we should avoid entering new trap handlers
8725 * while we are executing a trap handler. [is it a TODO?]
8732 uint8_t last_status;
8737 last_status = exitstatus;
8741 TRACE(("dotrap entered\n"));
8742 for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) {
8754 /* non-trapped SIGINT is handled separately by raise_interrupt,
8755 * don't upset it by resetting gotsig[SIGINT-1] */
8756 if (sig == SIGINT && !p)
8759 TRACE(("sig %d is active, will run handler '%s'\n", sig, p));
8765 exitstatus = last_status;
8766 TRACE(("dotrap returns\n"));
8769 /* forward declarations - evaluation is fairly recursive business... */
8770 static int evalloop(union node *, int);
8771 static int evalfor(union node *, int);
8772 static int evalcase(union node *, int);
8773 static int evalsubshell(union node *, int);
8774 static void expredir(union node *);
8775 static int evalpipe(union node *, int);
8776 static int evalcommand(union node *, int);
8777 static int evalbltin(const struct builtincmd *, int, char **, int);
8778 static void prehash(union node *);
8781 * Evaluate a parse tree. The value is left in the global variable
8785 evaltree(union node *n, int flags)
8788 int (*evalfn)(union node *, int);
8792 TRACE(("evaltree(NULL) called\n"));
8795 TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags));
8802 out1fmt("Node type = %d\n", n->type);
8807 status = !evaltree(n->nnot.com, EV_TESTED);
8810 expredir(n->nredir.redirect);
8811 status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
8813 status = evaltree(n->nredir.n, flags & EV_TESTED);
8815 if (n->nredir.redirect)
8816 popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */);
8819 evalfn = evalcommand;
8821 if (eflag && !(flags & EV_TESTED))
8833 evalfn = evalsubshell;
8846 #error NAND + 1 != NOR
8848 #if NOR + 1 != NSEMI
8849 #error NOR + 1 != NSEMI
8851 unsigned is_or = n->type - NAND;
8854 (flags | ((is_or >> 1) - 1)) & EV_TESTED
8856 if ((!status) == is_or || evalskip)
8862 status = evalfn(n, flags);
8866 status = evaltree(n->nif.test, EV_TESTED);
8873 if (n->nif.elsepart) {
8874 n = n->nif.elsepart;
8881 /* Not necessary. To test it:
8882 * "false; f() { qwerty; }; echo $?" should print 0.
8886 exitstatus = status;
8890 /* Order of checks below is important:
8891 * signal handlers trigger before exit caused by "set -e".
8895 if (checkexit & status)
8896 raise_exception(EXEXIT);
8897 if (flags & EV_EXIT)
8898 raise_exception(EXEXIT);
8900 TRACE(("leaving evaltree (no interrupts)\n"));
8904 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
8907 int evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__));
8912 int skip = evalskip;
8919 if (--skipcount <= 0) {
8930 evalloop(union node *n, int flags)
8941 i = evaltree(n->nbinary.ch1, EV_TESTED);
8943 if (skip == SKIPFUNC)
8947 if (n->type != NWHILE)
8951 status = evaltree(n->nbinary.ch2, flags);
8953 } while (!(skip & ~SKIPCONT));
8960 evalfor(union node *n, int flags)
8962 struct arglist arglist;
8965 struct stackmark smark;
8968 setstackmark(&smark);
8969 arglist.list = NULL;
8970 arglist.lastp = &arglist.list;
8971 for (argp = n->nfor.args; argp; argp = argp->narg.next) {
8972 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
8974 *arglist.lastp = NULL;
8978 for (sp = arglist.list; sp; sp = sp->next) {
8979 setvar0(n->nfor.var, sp->text);
8980 status = evaltree(n->nfor.body, flags);
8981 if (skiploop() & ~SKIPCONT)
8985 popstackmark(&smark);
8991 evalcase(union node *n, int flags)
8995 struct arglist arglist;
8996 struct stackmark smark;
8999 setstackmark(&smark);
9000 arglist.list = NULL;
9001 arglist.lastp = &arglist.list;
9002 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
9003 for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
9004 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
9005 if (casematch(patp, arglist.list->text)) {
9006 /* Ensure body is non-empty as otherwise
9007 * EV_EXIT may prevent us from setting the
9010 if (evalskip == 0 && cp->nclist.body) {
9011 status = evaltree(cp->nclist.body, flags);
9018 popstackmark(&smark);
9024 * Kick off a subshell to evaluate a tree.
9027 evalsubshell(union node *n, int flags)
9030 int backgnd = (n->type == NBACKGND); /* FORK_BG(1) if yes, else FORK_FG(0) */
9033 expredir(n->nredir.redirect);
9034 if (!backgnd && (flags & EV_EXIT) && !may_have_traps)
9037 if (backgnd == FORK_FG)
9039 jp = makejob(/*n,*/ 1);
9040 if (forkshell(jp, n, backgnd) == 0) {
9045 flags &= ~EV_TESTED;
9047 redirect(n->nredir.redirect, 0);
9048 evaltreenr(n->nredir.n, flags);
9053 if (backgnd == FORK_FG)
9054 status = waitforjob(jp);
9060 * Compute the names of the files in a redirection list.
9062 static void fixredir(union node *, const char *, int);
9064 expredir(union node *n)
9068 for (redir = n; redir; redir = redir->nfile.next) {
9072 fn.lastp = &fn.list;
9073 switch (redir->type) {
9077 #if BASH_REDIR_OUTPUT
9082 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
9083 TRACE(("expredir expanded to '%s'\n", fn.list->text));
9084 #if BASH_REDIR_OUTPUT
9088 // By the design of stack allocator, the loop of this kind:
9089 // while true; do while true; do break; done </dev/null; done
9090 // will look like a memory leak: ash plans to free expfname's
9091 // of "/dev/null" as soon as it finishes running the loop
9092 // (in this case, never).
9093 // This "fix" is wrong:
9094 if (redir->nfile.expfname)
9095 stunalloc(redir->nfile.expfname);
9096 // It results in corrupted state of stacked allocations.
9098 redir->nfile.expfname = fn.list->text;
9101 case NTOFD: /* >& */
9102 if (redir->ndup.vname) {
9103 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
9104 if (fn.list == NULL)
9105 ash_msg_and_raise_error("redir error");
9106 #if BASH_REDIR_OUTPUT
9107 //FIXME: we used expandarg with different args!
9108 if (!isdigit_str9(fn.list->text)) {
9109 /* >&file, not >&fd */
9110 if (redir->nfile.fd != 1) /* 123>&file - BAD */
9111 ash_msg_and_raise_error("redir error");
9113 goto store_expfname;
9116 fixredir(redir, fn.list->text, 1);
9124 * Evaluate a pipeline. All the processes in the pipeline are children
9125 * of the process creating the pipeline. (This differs from some versions
9126 * of the shell, which make the last process in a pipeline the parent
9130 evalpipe(union node *n, int flags)
9133 struct nodelist *lp;
9139 TRACE(("evalpipe(0x%lx) called\n", (long)n));
9141 for (lp = n->npipe.cmdlist; lp; lp = lp->next)
9145 if (n->npipe.pipe_backgnd == 0)
9147 jp = makejob(/*n,*/ pipelen);
9149 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
9153 if (pipe(pip) < 0) {
9155 ash_msg_and_raise_error("pipe call failed");
9158 if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) {
9172 evaltreenr(lp->n, flags);
9179 /* Don't want to trigger debugging */
9183 if (n->npipe.pipe_backgnd == 0) {
9184 status = waitforjob(jp);
9185 TRACE(("evalpipe: job done exit status %d\n", status));
9193 * Controls whether the shell is interactive or not.
9196 setinteractive(int on)
9198 static smallint is_interactive;
9200 if (++on == is_interactive)
9202 is_interactive = on;
9206 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
9207 if (is_interactive > 1) {
9208 /* Looks like they want an interactive shell */
9209 static smallint did_banner;
9212 /* note: ash and hush share this string */
9213 out1fmt("\n\n%s %s\n"
9214 IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n")
9217 "built-in shell (ash)"
9231 setinteractive(iflag);
9233 #if ENABLE_FEATURE_EDITING_VI
9235 line_input_state->flags |= VI_MODE;
9237 line_input_state->flags &= ~VI_MODE;
9239 viflag = 0; /* forcibly keep the option off */
9243 static struct localvar *localvars;
9246 * Called after a function returns.
9247 * Interrupts must be off.
9252 struct localvar *lvp;
9255 while ((lvp = localvars) != NULL) {
9256 localvars = lvp->next;
9258 TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-"));
9259 if (vp == NULL) { /* $- saved */
9260 memcpy(optlist, lvp->text, sizeof(optlist));
9261 free((char*)lvp->text);
9263 } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) {
9264 unsetvar(vp->var_text);
9267 vp->var_func(var_end(lvp->text));
9268 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
9269 free((char*)vp->var_text);
9270 vp->flags = lvp->flags;
9271 vp->var_text = lvp->text;
9278 evalfun(struct funcnode *func, int argc, char **argv, int flags)
9280 volatile struct shparam saveparam;
9281 struct localvar *volatile savelocalvars;
9282 struct jmploc *volatile savehandler;
9283 struct jmploc jmploc;
9286 saveparam = shellparam;
9287 savelocalvars = localvars;
9288 savehandler = exception_handler;
9289 e = setjmp(jmploc.loc);
9294 exception_handler = &jmploc;
9296 shellparam.malloced = 0;
9300 shellparam.nparam = argc - 1;
9301 shellparam.p = argv + 1;
9302 #if ENABLE_ASH_GETOPTS
9303 shellparam.optind = 1;
9304 shellparam.optoff = -1;
9306 evaltree(func->n.narg.next, flags & EV_TESTED);
9312 localvars = savelocalvars;
9313 freeparam(&shellparam);
9314 shellparam = saveparam;
9315 exception_handler = savehandler;
9317 evalskip &= ~SKIPFUNC;
9322 * Make a variable a local variable. When a variable is made local, it's
9323 * value and flags are saved in a localvar structure. The saved values
9324 * will be restored when the shell function returns. We handle the name
9325 * "-" as a special case: it makes changes to "set +-options" local
9326 * (options will be restored on return from the function).
9331 struct localvar *lvp;
9334 char *eq = strchr(name, '=');
9337 /* Cater for duplicate "local". Examples:
9338 * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x
9339 * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x
9343 if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) {
9347 * it's a duplicate "local VAR" declaration, do nothing
9354 lvp = ckzalloc(sizeof(*lvp));
9355 if (LONE_DASH(name)) {
9357 p = ckmalloc(sizeof(optlist));
9358 lvp->text = memcpy(p, optlist, sizeof(optlist));
9361 vpp = hashvar(name);
9362 vp = *findvar(vpp, name);
9364 /* variable did not exist yet */
9366 setvareq(name, VSTRFIXED);
9368 setvar(name, NULL, VSTRFIXED);
9369 vp = *vpp; /* the new variable */
9370 lvp->flags = VUNSET;
9372 lvp->text = vp->var_text;
9373 lvp->flags = vp->flags;
9374 /* make sure neither "struct var" nor string gets freed
9375 * during (un)setting:
9377 vp->flags |= VSTRFIXED|VTEXTFIXED;
9381 /* "local VAR" unsets VAR: */
9382 setvar0(name, NULL);
9386 lvp->next = localvars;
9393 * The "local" command.
9395 static int FAST_FUNC
9396 localcmd(int argc UNUSED_PARAM, char **argv)
9401 ash_msg_and_raise_error("not in a function");
9404 while ((name = *argv++) != NULL) {
9410 static int FAST_FUNC
9411 falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9416 static int FAST_FUNC
9417 truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9422 static int FAST_FUNC
9423 execcmd(int argc UNUSED_PARAM, char **argv)
9426 while (nextopt("a:") != '\0')
9427 /* nextopt() sets optionarg to "-a ARGV0" */;
9433 iflag = 0; /* exit on error */
9436 /* We should set up signals for "exec CMD"
9437 * the same way as for "CMD" without "exec".
9438 * But optschanged->setinteractive->setsignal
9439 * still thought we are a root shell. Therefore, for example,
9440 * SIGQUIT is still set to IGN. Fix it:
9444 /*setsignal(SIGTERM); - unnecessary because of iflag=0 */
9445 /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */
9446 /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */
9450 argv[0] = optionarg;
9451 shellexec(prog, argv, pathval(), 0);
9458 * The return command.
9460 static int FAST_FUNC
9461 returncmd(int argc UNUSED_PARAM, char **argv)
9464 * If called outside a function, do what ksh does;
9465 * skip the rest of the file.
9467 evalskip = SKIPFUNC;
9468 return argv[1] ? number(argv[1]) : exitstatus;
9471 /* Forward declarations for builtintab[] */
9472 static int breakcmd(int, char **) FAST_FUNC;
9473 static int dotcmd(int, char **) FAST_FUNC;
9474 static int evalcmd(int, char **, int) FAST_FUNC;
9475 static int exitcmd(int, char **) FAST_FUNC;
9476 static int exportcmd(int, char **) FAST_FUNC;
9477 #if ENABLE_ASH_GETOPTS
9478 static int getoptscmd(int, char **) FAST_FUNC;
9481 static int helpcmd(int, char **) FAST_FUNC;
9484 static int historycmd(int, char **) FAST_FUNC;
9486 #if ENABLE_FEATURE_SH_MATH
9487 static int letcmd(int, char **) FAST_FUNC;
9489 static int readcmd(int, char **) FAST_FUNC;
9490 static int setcmd(int, char **) FAST_FUNC;
9491 static int shiftcmd(int, char **) FAST_FUNC;
9492 static int timescmd(int, char **) FAST_FUNC;
9493 static int trapcmd(int, char **) FAST_FUNC;
9494 static int umaskcmd(int, char **) FAST_FUNC;
9495 static int unsetcmd(int, char **) FAST_FUNC;
9496 static int ulimitcmd(int, char **) FAST_FUNC;
9498 #define BUILTIN_NOSPEC "0"
9499 #define BUILTIN_SPECIAL "1"
9500 #define BUILTIN_REGULAR "2"
9501 #define BUILTIN_SPEC_REG "3"
9502 #define BUILTIN_ASSIGN "4"
9503 #define BUILTIN_SPEC_ASSG "5"
9504 #define BUILTIN_REG_ASSG "6"
9505 #define BUILTIN_SPEC_REG_ASSG "7"
9507 /* Stubs for calling non-FAST_FUNC's */
9509 static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); }
9511 #if ENABLE_ASH_PRINTF
9512 static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); }
9514 #if ENABLE_ASH_TEST || BASH_TEST2
9515 static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); }
9518 /* Keep these in proper order since it is searched via bsearch() */
9519 static const struct builtincmd builtintab[] = {
9520 { BUILTIN_SPEC_REG "." , dotcmd },
9521 { BUILTIN_SPEC_REG ":" , truecmd },
9523 { BUILTIN_REGULAR "[" , testcmd },
9526 { BUILTIN_REGULAR "[[" , testcmd },
9528 #if ENABLE_ASH_ALIAS
9529 { BUILTIN_REG_ASSG "alias" , aliascmd },
9532 { BUILTIN_REGULAR "bg" , fg_bgcmd },
9534 { BUILTIN_SPEC_REG "break" , breakcmd },
9535 { BUILTIN_REGULAR "cd" , cdcmd },
9536 { BUILTIN_NOSPEC "chdir" , cdcmd },
9537 #if ENABLE_ASH_CMDCMD
9538 { BUILTIN_REGULAR "command" , commandcmd },
9540 { BUILTIN_SPEC_REG "continue", breakcmd },
9542 { BUILTIN_REGULAR "echo" , echocmd },
9544 { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/
9545 { BUILTIN_SPEC_REG "exec" , execcmd },
9546 { BUILTIN_SPEC_REG "exit" , exitcmd },
9547 { BUILTIN_SPEC_REG_ASSG "export" , exportcmd },
9548 { BUILTIN_REGULAR "false" , falsecmd },
9550 { BUILTIN_REGULAR "fg" , fg_bgcmd },
9552 #if ENABLE_ASH_GETOPTS
9553 { BUILTIN_REGULAR "getopts" , getoptscmd },
9555 { BUILTIN_NOSPEC "hash" , hashcmd },
9557 { BUILTIN_NOSPEC "help" , helpcmd },
9560 { BUILTIN_NOSPEC "history" , historycmd },
9563 { BUILTIN_REGULAR "jobs" , jobscmd },
9564 { BUILTIN_REGULAR "kill" , killcmd },
9566 #if ENABLE_FEATURE_SH_MATH
9567 { BUILTIN_NOSPEC "let" , letcmd },
9569 { BUILTIN_ASSIGN "local" , localcmd },
9570 #if ENABLE_ASH_PRINTF
9571 { BUILTIN_REGULAR "printf" , printfcmd },
9573 { BUILTIN_NOSPEC "pwd" , pwdcmd },
9574 { BUILTIN_REGULAR "read" , readcmd },
9575 { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
9576 { BUILTIN_SPEC_REG "return" , returncmd },
9577 { BUILTIN_SPEC_REG "set" , setcmd },
9578 { BUILTIN_SPEC_REG "shift" , shiftcmd },
9580 { BUILTIN_SPEC_REG "source" , dotcmd },
9583 { BUILTIN_REGULAR "test" , testcmd },
9585 { BUILTIN_SPEC_REG "times" , timescmd },
9586 { BUILTIN_SPEC_REG "trap" , trapcmd },
9587 { BUILTIN_REGULAR "true" , truecmd },
9588 { BUILTIN_NOSPEC "type" , typecmd },
9589 { BUILTIN_NOSPEC "ulimit" , ulimitcmd },
9590 { BUILTIN_REGULAR "umask" , umaskcmd },
9591 #if ENABLE_ASH_ALIAS
9592 { BUILTIN_REGULAR "unalias" , unaliascmd },
9594 { BUILTIN_SPEC_REG "unset" , unsetcmd },
9595 { BUILTIN_REGULAR "wait" , waitcmd },
9598 /* Should match the above table! */
9599 #define COMMANDCMD (builtintab + \
9601 /* [ */ 1 * ENABLE_ASH_TEST + \
9602 /* [[ */ 1 * BASH_TEST2 + \
9603 /* alias */ 1 * ENABLE_ASH_ALIAS + \
9604 /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \
9605 /* break cd cddir */ 3)
9606 #define EVALCMD (COMMANDCMD + \
9607 /* command */ 1 * ENABLE_ASH_CMDCMD + \
9608 /* continue */ 1 + \
9609 /* echo */ 1 * ENABLE_ASH_ECHO + \
9611 #define EXECCMD (EVALCMD + \
9615 * Search the table of builtin commands.
9618 pstrcmp1(const void *a, const void *b)
9620 return strcmp((char*)a, *(char**)b + 1);
9622 static struct builtincmd *
9623 find_builtin(const char *name)
9625 struct builtincmd *bp;
9628 name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]),
9635 * Execute a simple command.
9638 isassignment(const char *p)
9640 const char *q = endofname(p);
9645 static int FAST_FUNC
9646 bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9648 /* Preserve exitstatus of a previous possible redirection
9649 * as POSIX mandates */
9650 return back_exitstatus;
9653 evalcommand(union node *cmd, int flags)
9655 static const struct builtincmd null_bltin = {
9656 "\0\0", bltincmd /* why three NULs? */
9658 struct stackmark smark;
9660 struct arglist arglist;
9661 struct arglist varlist;
9664 const struct strlist *sp;
9665 struct cmdentry cmdentry;
9672 struct builtincmd *bcmd;
9673 smallint cmd_is_exec;
9674 smallint pseudovarflag = 0;
9676 /* First expand the arguments. */
9677 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
9678 setstackmark(&smark);
9679 back_exitstatus = 0;
9681 cmdentry.cmdtype = CMDBUILTIN;
9682 cmdentry.u.cmd = &null_bltin;
9683 varlist.lastp = &varlist.list;
9684 *varlist.lastp = NULL;
9685 arglist.lastp = &arglist.list;
9686 *arglist.lastp = NULL;
9689 if (cmd->ncmd.args) {
9690 bcmd = find_builtin(cmd->ncmd.args->narg.text);
9691 pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd);
9694 for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) {
9695 struct strlist **spp;
9697 spp = arglist.lastp;
9698 if (pseudovarflag && isassignment(argp->narg.text))
9699 expandarg(argp, &arglist, EXP_VARTILDE);
9701 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
9703 for (sp = *spp; sp; sp = sp->next)
9707 /* Reserve one extra spot at the front for shellexec. */
9708 nargv = stalloc(sizeof(char *) * (argc + 2));
9710 for (sp = arglist.list; sp; sp = sp->next) {
9711 TRACE(("evalcommand arg: %s\n", sp->text));
9712 *nargv++ = sp->text;
9717 if (iflag && funcnest == 0 && argc > 0)
9718 lastarg = nargv[-1];
9721 expredir(cmd->ncmd.redirect);
9722 status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2);
9724 path = vpath.var_text;
9725 for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
9726 struct strlist **spp;
9729 spp = varlist.lastp;
9730 expandarg(argp, &varlist, EXP_VARTILDE);
9733 * Modify the command lookup path, if a PATH= assignment
9737 if (varcmp(p, path) == 0)
9741 /* Print the command if xflag is set. */
9743 const char *pfx = "";
9745 fdprintf(preverrout_fd, "%s", expandstr(ps4val()));
9749 char *varval = sp->text;
9750 char *eq = strchrnul(varval, '=');
9753 fdprintf(preverrout_fd, "%s%.*s%s",
9755 (int)(eq - varval), varval,
9756 maybe_single_quote(eq)
9764 fdprintf(preverrout_fd, "%s%s",
9766 /* always quote if matches reserved word: */
9768 ? single_quote(sp->text)
9769 : maybe_single_quote(sp->text)
9774 safe_write(preverrout_fd, "\n", 1);
9780 /* Now locate the command. */
9782 int cmd_flag = DO_ERR;
9783 #if ENABLE_ASH_CMDCMD
9784 const char *oldpath = path + 5;
9788 find_command(argv[0], &cmdentry, cmd_flag, path);
9789 if (cmdentry.cmdtype == CMDUNKNOWN) {
9790 flush_stdout_stderr();
9795 /* implement bltin and command here */
9796 if (cmdentry.cmdtype != CMDBUILTIN)
9799 spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd);
9800 if (cmdentry.u.cmd == EXECCMD)
9802 #if ENABLE_ASH_CMDCMD
9803 if (cmdentry.u.cmd == COMMANDCMD) {
9805 nargv = parse_command_args(argv, &path);
9808 /* It's "command [-p] PROG ARGS" (that is, no -Vv).
9809 * nargv => "PROG". path is updated if -p.
9811 argc -= nargv - argv;
9813 cmd_flag |= DO_NOFUNC;
9822 exitstatus = status;
9824 /* We have a redirection error. */
9826 raise_exception(EXERROR);
9831 /* Execute the command. */
9832 switch (cmdentry.cmdtype) {
9835 #if ENABLE_FEATURE_SH_NOFORK
9836 /* (1) BUG: if variables are set, we need to fork, or save/restore them
9837 * around run_nofork_applet() call.
9838 * (2) Should this check also be done in forkshell()?
9839 * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...)
9841 /* find_command() encodes applet_no as (-2 - applet_no) */
9842 int applet_no = (- cmdentry.u.index - 2);
9843 if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
9844 listsetvar(varlist.list, VEXPORT|VSTACK);
9845 /* run <applet>_main() */
9846 status = run_nofork_applet(applet_no, argv);
9850 /* Can we avoid forking off? For example, very last command
9851 * in a script or a subshell does not need forking,
9852 * we can just exec it.
9854 if (!(flags & EV_EXIT) || may_have_traps) {
9855 /* No, forking off a child is necessary */
9858 jp = makejob(/*cmd,*/ 1);
9859 if (forkshell(jp, cmd, FORK_FG) != 0) {
9861 status = waitforjob(jp);
9863 TRACE(("forked child exited with %d\n", status));
9868 /* fall through to exec'ing external program */
9870 listsetvar(varlist.list, VEXPORT|VSTACK);
9871 shellexec(argv[0], argv, path, cmdentry.u.index);
9875 cmdenviron = varlist.list;
9877 struct strlist *list = cmdenviron;
9879 if (spclbltin > 0 || argc == 0) {
9881 if (cmd_is_exec && argc > 1)
9884 listsetvar(list, i);
9886 /* Tight loop with builtins only:
9887 * "while kill -0 $child; do true; done"
9888 * will never exit even if $child died, unless we do this
9889 * to reap the zombie and make kill detect that it's gone: */
9890 dowait(DOWAIT_NONBLOCK, NULL);
9892 if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
9893 if (exception_type == EXERROR && spclbltin <= 0) {
9898 longjmp(exception_handler->loc, 1);
9903 listsetvar(varlist.list, 0);
9904 /* See above for the rationale */
9905 dowait(DOWAIT_NONBLOCK, NULL);
9906 if (evalfun(cmdentry.u.func, argc, argv, flags))
9909 status = exitstatus;
9914 if (cmd->ncmd.redirect)
9915 popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec);
9917 /* dsl: I think this is intended to be used to support
9918 * '_' in 'vi' command mode during line editing...
9919 * However I implemented that within libedit itself.
9921 setvar0("_", lastarg);
9923 popstackmark(&smark);
9929 evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags)
9931 char *volatile savecmdname;
9932 struct jmploc *volatile savehandler;
9933 struct jmploc jmploc;
9937 savecmdname = commandname;
9938 savehandler = exception_handler;
9939 i = setjmp(jmploc.loc);
9942 exception_handler = &jmploc;
9943 commandname = argv[0];
9945 optptr = NULL; /* initialize nextopt */
9947 status = evalcmd(argc, argv, flags);
9949 status = (*cmd->builtin)(argc, argv);
9950 flush_stdout_stderr();
9951 status |= ferror(stdout);
9952 exitstatus = status;
9955 commandname = savecmdname;
9956 exception_handler = savehandler;
9962 goodname(const char *p)
9964 return endofname(p)[0] == '\0';
9969 * Search for a command. This is called before we fork so that the
9970 * location of the command will be available in the parent as well as
9971 * the child. The check for "goodname" is an overly conservative
9972 * check that the name will not be subject to expansion.
9975 prehash(union node *n)
9977 struct cmdentry entry;
9979 if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text))
9980 find_command(n->ncmd.args->narg.text, &entry, 0, pathval());
9984 /* ============ Builtin commands
9986 * Builtin commands whose functions are closely tied to evaluation
9987 * are implemented here.
9991 * Handle break and continue commands. Break, continue, and return are
9992 * all handled by setting the evalskip flag. The evaluation routines
9993 * above all check this flag, and if it is set they start skipping
9994 * commands rather than executing them. The variable skipcount is
9995 * the number of loops to break/continue, or the number of function
9996 * levels to return. (The latter is always 1.) It should probably
9997 * be an error to break out of more loops than exist, but it isn't
9998 * in the standard shell so we don't make it one here.
10000 static int FAST_FUNC
10001 breakcmd(int argc UNUSED_PARAM, char **argv)
10003 int n = argv[1] ? number(argv[1]) : 1;
10006 ash_msg_and_raise_error(msg_illnum, argv[1]);
10010 evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK;
10018 * This implements the input routines used by the parser.
10022 INPUT_PUSH_FILE = 1,
10023 INPUT_NOFILE_OK = 2,
10026 static smallint checkkwd;
10027 /* values of checkkwd variable */
10028 #define CHKALIAS 0x1
10033 * Push a string back onto the input at this current parsefile level.
10034 * We handle aliases this way.
10036 #if !ENABLE_ASH_ALIAS
10037 #define pushstring(s, ap) pushstring(s)
10040 pushstring(char *s, struct alias *ap)
10042 struct strpush *sp;
10047 if (g_parsefile->strpush) {
10048 sp = ckzalloc(sizeof(*sp));
10049 sp->prev = g_parsefile->strpush;
10051 sp = &(g_parsefile->basestrpush);
10053 g_parsefile->strpush = sp;
10054 sp->prev_string = g_parsefile->next_to_pgetc;
10055 sp->prev_left_in_line = g_parsefile->left_in_line;
10056 sp->unget = g_parsefile->unget;
10057 memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc));
10058 #if ENABLE_ASH_ALIAS
10061 ap->flag |= ALIASINUSE;
10065 g_parsefile->next_to_pgetc = s;
10066 g_parsefile->left_in_line = len;
10067 g_parsefile->unget = 0;
10074 struct strpush *sp = g_parsefile->strpush;
10077 #if ENABLE_ASH_ALIAS
10079 if (g_parsefile->next_to_pgetc[-1] == ' '
10080 || g_parsefile->next_to_pgetc[-1] == '\t'
10082 checkkwd |= CHKALIAS;
10084 if (sp->string != sp->ap->val) {
10087 sp->ap->flag &= ~ALIASINUSE;
10088 if (sp->ap->flag & ALIASDEAD) {
10089 unalias(sp->ap->name);
10093 g_parsefile->next_to_pgetc = sp->prev_string;
10094 g_parsefile->left_in_line = sp->prev_left_in_line;
10095 g_parsefile->unget = sp->unget;
10096 memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc));
10097 g_parsefile->strpush = sp->prev;
10098 if (sp != &(g_parsefile->basestrpush))
10107 char *buf = g_parsefile->buf;
10109 g_parsefile->next_to_pgetc = buf;
10110 #if ENABLE_FEATURE_EDITING
10112 if (!iflag || g_parsefile->pf_fd != STDIN_FILENO)
10113 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10116 # if ENABLE_ASH_IDLE_TIMEOUT
10118 const char *tmout_var = lookupvar("TMOUT");
10120 timeout = atoi(tmout_var) * 1000;
10126 # if ENABLE_FEATURE_TAB_COMPLETION
10127 line_input_state->path_lookup = pathval();
10129 reinit_unicode_for_ash();
10130 nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ, timeout);
10132 /* ^C pressed, "convert" to SIGINT */
10133 write(STDOUT_FILENO, "^C", 2);
10134 if (trap[SIGINT]) {
10140 exitstatus = 128 + SIGINT;
10146 /* Ctrl+D pressed */
10149 # if ENABLE_ASH_IDLE_TIMEOUT
10150 else if (errno == EAGAIN && timeout > 0) {
10151 puts("\007timed out waiting for input: auto-logout");
10158 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10161 #if 0 /* disabled: nonblock_immune_read() handles this problem */
10163 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
10164 int flags = fcntl(0, F_GETFL);
10165 if (flags >= 0 && (flags & O_NONBLOCK)) {
10166 flags &= ~O_NONBLOCK;
10167 if (fcntl(0, F_SETFL, flags) >= 0) {
10168 out2str("sh: turning off NDELAY mode\n");
10179 * Refill the input buffer and return the next input character:
10181 * 1) If a string was pushed back on the input, pop it;
10182 * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM)
10183 * or we are reading from a string so we can't refill the buffer,
10185 * 3) If there is more stuff in this buffer, use it else call read to fill it.
10186 * 4) Process input up to the next newline, deleting nul characters.
10188 //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__)
10189 #define pgetc_debug(...) ((void)0)
10190 static int pgetc(void);
10197 if (g_parsefile->strpush) {
10198 #if ENABLE_ASH_ALIAS
10199 if (g_parsefile->left_in_line == -1
10200 && g_parsefile->strpush->ap
10201 && g_parsefile->next_to_pgetc[-1] != ' '
10202 && g_parsefile->next_to_pgetc[-1] != '\t'
10204 pgetc_debug("preadbuffer PEOA");
10211 /* on both branches above g_parsefile->left_in_line < 0.
10212 * "pgetc" needs refilling.
10215 /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read",
10216 * pungetc() may increment it a few times.
10217 * Assuming it won't increment it to less than -90.
10219 if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) {
10220 pgetc_debug("preadbuffer PEOF1");
10221 /* even in failure keep left_in_line and next_to_pgetc
10222 * in lock step, for correct multi-layer pungetc.
10223 * left_in_line was decremented before preadbuffer(),
10224 * must inc next_to_pgetc: */
10225 g_parsefile->next_to_pgetc++;
10229 more = g_parsefile->left_in_buffer;
10231 flush_stdout_stderr();
10235 /* don't try reading again */
10236 g_parsefile->left_in_line = -99;
10237 pgetc_debug("preadbuffer PEOF2");
10238 g_parsefile->next_to_pgetc++;
10243 /* Find out where's the end of line.
10244 * Set g_parsefile->left_in_line
10245 * and g_parsefile->left_in_buffer acordingly.
10246 * NUL chars are deleted.
10248 q = g_parsefile->next_to_pgetc;
10256 memmove(q, q + 1, more);
10260 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10266 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10267 if (g_parsefile->left_in_line < 0)
10272 g_parsefile->left_in_buffer = more;
10277 out2str(g_parsefile->next_to_pgetc);
10281 pgetc_debug("preadbuffer at %d:%p'%s'",
10282 g_parsefile->left_in_line,
10283 g_parsefile->next_to_pgetc,
10284 g_parsefile->next_to_pgetc);
10285 return (unsigned char)*g_parsefile->next_to_pgetc++;
10291 g_parsefile->linno++;
10292 setprompt_if(doprompt, 2);
10297 g_parsefile->linno++;
10298 needprompt = doprompt;
10306 pgetc_debug("pgetc at %d:%p'%s'",
10307 g_parsefile->left_in_line,
10308 g_parsefile->next_to_pgetc,
10309 g_parsefile->next_to_pgetc);
10310 if (g_parsefile->unget)
10311 return g_parsefile->lastc[--g_parsefile->unget];
10313 if (--g_parsefile->left_in_line >= 0)
10314 c = (unsigned char)*g_parsefile->next_to_pgetc++;
10318 g_parsefile->lastc[1] = g_parsefile->lastc[0];
10319 g_parsefile->lastc[0] = c;
10324 #if ENABLE_ASH_ALIAS
10326 pgetc_without_PEOA(void)
10330 pgetc_debug("pgetc at %d:%p'%s'",
10331 g_parsefile->left_in_line,
10332 g_parsefile->next_to_pgetc,
10333 g_parsefile->next_to_pgetc);
10335 } while (c == PEOA);
10339 # define pgetc_without_PEOA() pgetc()
10343 * Read a line from the script.
10346 pfgets(char *line, int len)
10352 while (--nleft > 0) {
10353 c = pgetc_without_PEOA();
10368 * Undo a call to pgetc. Only two characters may be pushed back.
10369 * PEOF may be pushed back.
10374 g_parsefile->unget++;
10377 /* This one eats backslash+newline */
10383 while ((c = pgetc()) == '\\') {
10384 if (pgetc() != '\n') {
10396 * To handle the "." command, a stack of input files is used. Pushfile
10397 * adds a new entry to the stack and popfile restores the previous level.
10402 struct parsefile *pf;
10404 pf = ckzalloc(sizeof(*pf));
10405 pf->prev = g_parsefile;
10407 /*pf->strpush = NULL; - ckzalloc did it */
10408 /*pf->basestrpush.prev = NULL;*/
10416 struct parsefile *pf = g_parsefile;
10422 if (pf->pf_fd >= 0)
10425 while (pf->strpush)
10427 g_parsefile = pf->prev;
10433 * Return to top level.
10438 while (g_parsefile != &basepf)
10443 * Close the file(s) that the shell is reading commands from. Called
10444 * after a fork is done.
10450 if (g_parsefile->pf_fd > 0) {
10451 close(g_parsefile->pf_fd);
10452 g_parsefile->pf_fd = 0;
10457 * Like setinputfile, but takes an open file descriptor. Call this with
10461 setinputfd(int fd, int push)
10465 g_parsefile->buf = NULL;
10467 g_parsefile->pf_fd = fd;
10468 if (g_parsefile->buf == NULL)
10469 g_parsefile->buf = ckmalloc(IBUFSIZ);
10470 g_parsefile->left_in_buffer = 0;
10471 g_parsefile->left_in_line = 0;
10472 g_parsefile->linno = 1;
10476 * Set the input to take input from a file. If push is set, push the
10477 * old input onto the stack first.
10480 setinputfile(const char *fname, int flags)
10485 fd = open(fname, O_RDONLY);
10487 if (flags & INPUT_NOFILE_OK)
10490 ash_msg_and_raise_error("can't open '%s'", fname);
10495 close_on_exec_on(fd);
10496 setinputfd(fd, flags & INPUT_PUSH_FILE);
10503 * Like setinputfile, but takes input from a string.
10506 setinputstring(char *string)
10510 g_parsefile->next_to_pgetc = string;
10511 g_parsefile->left_in_line = strlen(string);
10512 g_parsefile->buf = NULL;
10513 g_parsefile->linno = 1;
10519 * Routines to check for mail.
10522 #if ENABLE_ASH_MAIL
10524 /* Hash of mtimes of mailboxes */
10525 static unsigned mailtime_hash;
10526 /* Set if MAIL or MAILPATH is changed. */
10527 static smallint mail_var_path_changed;
10530 * Print appropriate message(s) if mail has arrived.
10531 * If mail_var_path_changed is set,
10532 * then the value of MAIL has mail_var_path_changed,
10533 * so we just update the values.
10542 struct stackmark smark;
10545 setstackmark(&smark);
10546 mpath = mpathset() ? mpathval() : mailval();
10549 p = path_advance(&mpath, nullstr);
10554 for (q = p; *q; q++)
10560 q[-1] = '\0'; /* delete trailing '/' */
10561 if (stat(p, &statb) < 0) {
10564 /* Very simplistic "hash": just a sum of all mtimes */
10565 new_hash += (unsigned)statb.st_mtime;
10567 if (!mail_var_path_changed && mailtime_hash != new_hash) {
10568 if (mailtime_hash != 0)
10569 out2str("you have mail\n");
10570 mailtime_hash = new_hash;
10572 mail_var_path_changed = 0;
10573 popstackmark(&smark);
10576 static void FAST_FUNC
10577 changemail(const char *val UNUSED_PARAM)
10579 mail_var_path_changed = 1;
10582 #endif /* ASH_MAIL */
10585 /* ============ ??? */
10588 * Set the shell parameters.
10591 setparam(char **argv)
10597 for (nparam = 0; argv[nparam]; nparam++)
10599 ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap));
10601 *ap++ = ckstrdup(*argv++);
10604 freeparam(&shellparam);
10605 shellparam.malloced = 1;
10606 shellparam.nparam = nparam;
10607 shellparam.p = newparam;
10608 #if ENABLE_ASH_GETOPTS
10609 shellparam.optind = 1;
10610 shellparam.optoff = -1;
10615 * Process shell options. The global variable argptr contains a pointer
10616 * to the argument list; we advance it past the options.
10618 * SUSv3 section 2.8.1 "Consequences of Shell Errors" says:
10619 * For a non-interactive shell, an error condition encountered
10620 * by a special built-in ... shall cause the shell to write a diagnostic message
10621 * to standard error and exit as shown in the following table:
10622 * Error Special Built-In
10624 * Utility syntax error (option or operand error) Shall exit
10626 * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142)
10627 * we see that bash does not do that (set "finishes" with error code 1 instead,
10628 * and shell continues), and people rely on this behavior!
10630 * set -o barfoo 2>/dev/null
10633 * Oh well. Let's mimic that.
10636 plus_minus_o(char *name, int val)
10641 for (i = 0; i < NOPTS; i++) {
10642 if (strcmp(name, optnames(i)) == 0) {
10647 ash_msg("illegal option %co %s", val ? '-' : '+', name);
10650 for (i = 0; i < NOPTS; i++) {
10652 out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off");
10654 out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i));
10660 setoption(int flag, int val)
10664 for (i = 0; i < NOPTS; i++) {
10665 if (optletters(i) == flag) {
10670 ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag);
10674 options(int cmdline)
10682 while ((p = *argptr) != NULL) {
10684 if (c != '-' && c != '+')
10687 val = 0; /* val = 0 if c == '+' */
10690 if (p[0] == '\0' || LONE_DASH(p)) {
10692 /* "-" means turn off -x and -v */
10695 /* "--" means reset params */
10696 else if (*argptr == NULL)
10699 break; /* "-" or "--" terminates options */
10702 /* first char was + or - */
10703 while ((c = *p++) != '\0') {
10704 /* bash 3.2 indeed handles -c CMD and +c CMD the same */
10705 if (c == 'c' && cmdline) {
10706 minusc = p; /* command is after shell args */
10707 } else if (c == 'o') {
10708 if (plus_minus_o(*argptr, val)) {
10709 /* it already printed err message */
10710 return 1; /* error */
10714 } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
10716 /* bash does not accept +-login, we also won't */
10717 } else if (cmdline && val && (c == '-')) { /* long options */
10718 if (strcmp(p, "login") == 0)
10730 * The shift builtin command.
10732 static int FAST_FUNC
10733 shiftcmd(int argc UNUSED_PARAM, char **argv)
10740 n = number(argv[1]);
10741 if (n > shellparam.nparam)
10742 n = 0; /* bash compat, was = shellparam.nparam; */
10744 shellparam.nparam -= n;
10745 for (ap1 = shellparam.p; --n >= 0; ap1++) {
10746 if (shellparam.malloced)
10749 ap2 = shellparam.p;
10750 while ((*ap2++ = *ap1++) != NULL)
10752 #if ENABLE_ASH_GETOPTS
10753 shellparam.optind = 1;
10754 shellparam.optoff = -1;
10761 * POSIX requires that 'set' (but not export or readonly) output the
10762 * variables in lexicographic order - by the locale's collating order (sigh).
10763 * Maybe we could keep them in an ordered balanced binary tree
10764 * instead of hashed lists.
10765 * For now just roll 'em through qsort for printing...
10768 showvars(const char *sep_prefix, int on, int off)
10771 char **ep, **epend;
10773 ep = listvars(on, off, &epend);
10774 qsort(ep, epend - ep, sizeof(char *), vpcmp);
10776 sep = *sep_prefix ? " " : sep_prefix;
10778 for (; ep < epend; ep++) {
10782 p = strchrnul(*ep, '=');
10785 q = single_quote(++p);
10786 out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q);
10792 * The set command builtin.
10794 static int FAST_FUNC
10795 setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
10800 return showvars(nullstr, 0, VUNSET);
10803 retval = options(/*cmdline:*/ 0);
10804 if (retval == 0) { /* if no parse error... */
10806 if (*argptr != NULL) {
10814 #if ENABLE_ASH_RANDOM_SUPPORT
10815 static void FAST_FUNC
10816 change_random(const char *value)
10820 if (value == NULL) {
10821 /* "get", generate */
10822 t = next_random(&random_gen);
10823 /* set without recursion */
10824 setvar(vrandom.var_text, utoa(t), VNOFUNC);
10825 vrandom.flags &= ~VNOFUNC;
10828 t = strtoul(value, NULL, 10);
10829 INIT_RANDOM_T(&random_gen, (t ? t : 1), t);
10834 #if ENABLE_ASH_GETOPTS
10836 getopts(char *optstr, char *optvar, char **optfirst)
10843 int ind = shellparam.optind;
10844 int off = shellparam.optoff;
10848 shellparam.optind = -1;
10849 optnext = optfirst + ind - 1;
10851 if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off)
10854 p = optnext[-1] + off;
10855 if (p == NULL || *p == '\0') {
10856 /* Current word is done, advance */
10858 if (p == NULL || *p != '-' || *++p == '\0') {
10865 if (LONE_DASH(p)) /* check for "--" */
10870 for (q = optstr; *q != c;) {
10872 if (optstr[0] == ':') {
10874 /*sbuf[1] = '\0'; - already is */
10875 setvar0("OPTARG", sbuf);
10877 fprintf(stderr, "Illegal option -%c\n", c);
10878 unsetvar("OPTARG");
10888 if (*p == '\0' && (p = *optnext) == NULL) {
10889 if (optstr[0] == ':') {
10891 /*sbuf[1] = '\0'; - already is */
10892 setvar0("OPTARG", sbuf);
10895 fprintf(stderr, "No arg for -%c option\n", c);
10896 unsetvar("OPTARG");
10904 setvar0("OPTARG", p);
10907 setvar0("OPTARG", nullstr);
10909 ind = optnext - optfirst + 1;
10910 setvar("OPTIND", itoa(ind), VNOFUNC);
10912 /*sbuf[1] = '\0'; - already is */
10913 setvar0(optvar, sbuf);
10915 shellparam.optoff = p ? p - *(optnext - 1) : -1;
10916 shellparam.optind = ind;
10922 * The getopts builtin. Shellparam.optnext points to the next argument
10923 * to be processed. Shellparam.optptr points to the next character to
10924 * be processed in the current argument. If shellparam.optnext is NULL,
10925 * then it's the first time getopts has been called.
10927 static int FAST_FUNC
10928 getoptscmd(int argc, char **argv)
10933 ash_msg_and_raise_error("usage: getopts optstring var [arg]");
10935 optbase = shellparam.p;
10936 if ((unsigned)shellparam.optind > shellparam.nparam + 1) {
10937 shellparam.optind = 1;
10938 shellparam.optoff = -1;
10941 optbase = &argv[3];
10942 if ((unsigned)shellparam.optind > argc - 2) {
10943 shellparam.optind = 1;
10944 shellparam.optoff = -1;
10948 return getopts(argv[1], argv[2], optbase);
10950 #endif /* ASH_GETOPTS */
10953 /* ============ Shell parser */
10956 struct heredoc *next; /* next here document in list */
10957 union node *here; /* redirection node */
10958 char *eofmark; /* string indicating end of input */
10959 smallint striptabs; /* if set, strip leading tabs */
10962 static smallint tokpushback; /* last token pushed back */
10963 static smallint quoteflag; /* set if (part of) last token was quoted */
10964 static token_id_t lasttoken; /* last token read (integer id Txxx) */
10965 static struct heredoc *heredoclist; /* list of here documents to read */
10966 static char *wordtext; /* text of last word returned by readtoken */
10967 static struct nodelist *backquotelist;
10968 static union node *redirnode;
10969 static struct heredoc *heredoc;
10971 static const char *
10972 tokname(char *buf, int tok)
10975 return tokname_array[tok];
10976 sprintf(buf, "\"%s\"", tokname_array[tok]);
10980 /* raise_error_unexpected_syntax:
10981 * Called when an unexpected token is read during the parse. The argument
10982 * is the token that is expected, or -1 if more than one type of token can
10983 * occur at this point.
10985 static void raise_error_unexpected_syntax(int) NORETURN;
10987 raise_error_unexpected_syntax(int token)
10993 l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken));
10995 sprintf(msg + l, " (expecting %s)", tokname(buf, token));
10996 raise_error_syntax(msg);
11000 #define EOFMARKLEN 79
11002 /* parsing is heavily cross-recursive, need these forward decls */
11003 static union node *andor(void);
11004 static union node *pipeline(void);
11005 static union node *parse_command(void);
11006 static void parseheredoc(void);
11007 static int peektoken(void);
11008 static int readtoken(void);
11010 static union node *
11013 union node *n1, *n2, *n3;
11018 switch (peektoken()) {
11026 if (!n1 && (nlflag & 1))
11032 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11033 if (nlflag == 2 && ((1 << peektoken()) & tokendlist))
11039 if (tok == TBACKGND) {
11040 if (n2->type == NPIPE) {
11041 n2->npipe.pipe_backgnd = 1;
11043 if (n2->type != NREDIR) {
11044 n3 = stzalloc(sizeof(struct nredir));
11046 /*n3->nredir.redirect = NULL; - stzalloc did it */
11049 n2->type = NBACKGND;
11055 n3 = stzalloc(sizeof(struct nbinary));
11057 n3->nbinary.ch1 = n1;
11058 n3->nbinary.ch2 = n2;
11071 raise_error_unexpected_syntax(-1);
11078 static union node *
11081 union node *n1, *n2, *n3;
11089 } else if (t == TOR) {
11095 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11097 n3 = stzalloc(sizeof(struct nbinary));
11099 n3->nbinary.ch1 = n1;
11100 n3->nbinary.ch2 = n2;
11105 static union node *
11108 union node *n1, *n2, *pipenode;
11109 struct nodelist *lp, *prev;
11113 TRACE(("pipeline: entered\n"));
11114 if (readtoken() == TNOT) {
11116 checkkwd = CHKKWD | CHKALIAS;
11119 n1 = parse_command();
11120 if (readtoken() == TPIPE) {
11121 pipenode = stzalloc(sizeof(struct npipe));
11122 pipenode->type = NPIPE;
11123 /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */
11124 lp = stzalloc(sizeof(struct nodelist));
11125 pipenode->npipe.cmdlist = lp;
11129 lp = stzalloc(sizeof(struct nodelist));
11130 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11131 lp->n = parse_command();
11133 } while (readtoken() == TPIPE);
11139 n2 = stzalloc(sizeof(struct nnot));
11147 static union node *
11152 n = stzalloc(sizeof(struct narg));
11154 /*n->narg.next = NULL; - stzalloc did it */
11155 n->narg.text = wordtext;
11156 n->narg.backquote = backquotelist;
11161 fixredir(union node *n, const char *text, int err)
11165 TRACE(("Fix redir %s %d\n", text, err));
11167 n->ndup.vname = NULL;
11169 fd = bb_strtou(text, NULL, 10);
11170 if (!errno && fd >= 0)
11171 n->ndup.dupfd = fd;
11172 else if (LONE_DASH(text))
11173 n->ndup.dupfd = -1;
11176 raise_error_syntax("bad fd number");
11177 n->ndup.vname = makename();
11182 * Returns true if the text contains nothing to expand (no dollar signs
11186 noexpand(const char *text)
11190 while ((c = *text++) != '\0') {
11191 if (c == CTLQUOTEMARK)
11195 else if (SIT(c, BASESYNTAX) == CCTL)
11204 union node *n = redirnode;
11206 if (readtoken() != TWORD)
11207 raise_error_unexpected_syntax(-1);
11208 if (n->type == NHERE) {
11209 struct heredoc *here = heredoc;
11213 if (quoteflag == 0)
11215 TRACE(("Here document %d\n", n->type));
11216 if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
11217 raise_error_syntax("illegal eof marker for << redirection");
11218 rmescapes(wordtext, 0);
11219 here->eofmark = wordtext;
11221 if (heredoclist == NULL)
11222 heredoclist = here;
11224 for (p = heredoclist; p->next; p = p->next)
11228 } else if (n->type == NTOFD || n->type == NFROMFD) {
11229 fixredir(n, wordtext, 0);
11231 n->nfile.fname = makename();
11235 static union node *
11238 union node *args, **app;
11239 union node *n = NULL;
11240 union node *vars, **vpp;
11241 union node **rpp, *redir;
11244 smallint double_brackets_flag = 0;
11246 IF_BASH_FUNCTION(smallint function_flag = 0;)
11255 savecheckkwd = CHKALIAS;
11258 checkkwd = savecheckkwd;
11263 if (peektoken() != TWORD)
11264 raise_error_unexpected_syntax(TWORD);
11269 case TAND: /* "&&" */
11270 case TOR: /* "||" */
11271 if (!double_brackets_flag) {
11275 wordtext = (char *) (t == TAND ? "-a" : "-o");
11278 n = stzalloc(sizeof(struct narg));
11280 /*n->narg.next = NULL; - stzalloc did it */
11281 n->narg.text = wordtext;
11283 if (strcmp("[[", wordtext) == 0)
11284 double_brackets_flag = 1;
11285 else if (strcmp("]]", wordtext) == 0)
11286 double_brackets_flag = 0;
11288 n->narg.backquote = backquotelist;
11289 if (savecheckkwd && isassignment(wordtext)) {
11291 vpp = &n->narg.next;
11294 app = &n->narg.next;
11298 if (function_flag) {
11299 checkkwd = CHKNL | CHKKWD;
11300 switch (peektoken()) {
11312 if (strcmp("[[", wordtext) == 0)
11316 raise_error_unexpected_syntax(-1);
11322 *rpp = n = redirnode;
11323 rpp = &n->nfile.next;
11324 parsefname(); /* read name of redirection file */
11327 IF_BASH_FUNCTION(do_func:)
11328 if (args && app == &args->narg.next
11331 struct builtincmd *bcmd;
11334 /* We have a function */
11335 if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP)
11336 raise_error_unexpected_syntax(TRP);
11337 name = n->narg.text;
11338 if (!goodname(name)
11339 || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd))
11341 raise_error_syntax("bad function name");
11344 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11345 n->narg.next = parse_command();
11348 IF_BASH_FUNCTION(function_flag = 0;)
11359 n = stzalloc(sizeof(struct ncmd));
11361 n->ncmd.args = args;
11362 n->ncmd.assign = vars;
11363 n->ncmd.redirect = redir;
11367 static union node *
11368 parse_command(void)
11370 union node *n1, *n2;
11371 union node *ap, **app;
11372 union node *cp, **cpp;
11373 union node *redir, **rpp;
11380 switch (readtoken()) {
11382 raise_error_unexpected_syntax(-1);
11385 n1 = stzalloc(sizeof(struct nif));
11387 n1->nif.test = list(0);
11388 if (readtoken() != TTHEN)
11389 raise_error_unexpected_syntax(TTHEN);
11390 n1->nif.ifpart = list(0);
11392 while (readtoken() == TELIF) {
11393 n2->nif.elsepart = stzalloc(sizeof(struct nif));
11394 n2 = n2->nif.elsepart;
11396 n2->nif.test = list(0);
11397 if (readtoken() != TTHEN)
11398 raise_error_unexpected_syntax(TTHEN);
11399 n2->nif.ifpart = list(0);
11401 if (lasttoken == TELSE)
11402 n2->nif.elsepart = list(0);
11404 n2->nif.elsepart = NULL;
11412 n1 = stzalloc(sizeof(struct nbinary));
11413 n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
11414 n1->nbinary.ch1 = list(0);
11417 TRACE(("expecting DO got '%s' %s\n", tokname_array[got],
11418 got == TWORD ? wordtext : ""));
11419 raise_error_unexpected_syntax(TDO);
11421 n1->nbinary.ch2 = list(0);
11426 if (readtoken() != TWORD || quoteflag || !goodname(wordtext))
11427 raise_error_syntax("bad for loop variable");
11428 n1 = stzalloc(sizeof(struct nfor));
11430 n1->nfor.var = wordtext;
11431 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11432 if (readtoken() == TIN) {
11434 while (readtoken() == TWORD) {
11435 n2 = stzalloc(sizeof(struct narg));
11437 /*n2->narg.next = NULL; - stzalloc did it */
11438 n2->narg.text = wordtext;
11439 n2->narg.backquote = backquotelist;
11441 app = &n2->narg.next;
11444 n1->nfor.args = ap;
11445 if (lasttoken != TNL && lasttoken != TSEMI)
11446 raise_error_unexpected_syntax(-1);
11448 n2 = stzalloc(sizeof(struct narg));
11450 /*n2->narg.next = NULL; - stzalloc did it */
11451 n2->narg.text = (char *)dolatstr;
11452 /*n2->narg.backquote = NULL;*/
11453 n1->nfor.args = n2;
11455 * Newline or semicolon here is optional (but note
11456 * that the original Bourne shell only allowed NL).
11458 if (lasttoken != TSEMI)
11461 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11462 if (readtoken() != TDO)
11463 raise_error_unexpected_syntax(TDO);
11464 n1->nfor.body = list(0);
11468 n1 = stzalloc(sizeof(struct ncase));
11470 if (readtoken() != TWORD)
11471 raise_error_unexpected_syntax(TWORD);
11472 n1->ncase.expr = n2 = stzalloc(sizeof(struct narg));
11474 /*n2->narg.next = NULL; - stzalloc did it */
11475 n2->narg.text = wordtext;
11476 n2->narg.backquote = backquotelist;
11477 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11478 if (readtoken() != TIN)
11479 raise_error_unexpected_syntax(TIN);
11480 cpp = &n1->ncase.cases;
11482 checkkwd = CHKNL | CHKKWD;
11484 while (t != TESAC) {
11485 if (lasttoken == TLP)
11487 *cpp = cp = stzalloc(sizeof(struct nclist));
11489 app = &cp->nclist.pattern;
11491 *app = ap = stzalloc(sizeof(struct narg));
11493 /*ap->narg.next = NULL; - stzalloc did it */
11494 ap->narg.text = wordtext;
11495 ap->narg.backquote = backquotelist;
11496 if (readtoken() != TPIPE)
11498 app = &ap->narg.next;
11501 //ap->narg.next = NULL;
11502 if (lasttoken != TRP)
11503 raise_error_unexpected_syntax(TRP);
11504 cp->nclist.body = list(2);
11506 cpp = &cp->nclist.next;
11508 checkkwd = CHKNL | CHKKWD;
11512 raise_error_unexpected_syntax(TENDCASE);
11519 n1 = stzalloc(sizeof(struct nredir));
11520 n1->type = NSUBSHELL;
11521 n1->nredir.n = list(0);
11522 /*n1->nredir.redirect = NULL; - stzalloc did it */
11529 IF_BASH_FUNCTION(case TFUNCTION:)
11533 return simplecmd();
11536 if (readtoken() != t)
11537 raise_error_unexpected_syntax(t);
11540 /* Now check for redirection which may follow command */
11541 checkkwd = CHKKWD | CHKALIAS;
11543 while (readtoken() == TREDIR) {
11544 *rpp = n2 = redirnode;
11545 rpp = &n2->nfile.next;
11551 if (n1->type != NSUBSHELL) {
11552 n2 = stzalloc(sizeof(struct nredir));
11557 n1->nredir.redirect = redir;
11562 #if BASH_DOLLAR_SQUOTE
11564 decode_dollar_squote(void)
11566 static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
11572 p = strchr(C_escapes, c);
11577 if ((unsigned char)(c - '0') <= 7) { /* \ooo */
11581 } while ((unsigned char)(c - '0') <= 7 && --cnt);
11583 } else if (c == 'x') { /* \xHH */
11587 } while (isxdigit(c) && --cnt);
11589 if (cnt == 3) { /* \x but next char is "bad" */
11593 } else { /* simple seq like \\ or \t */
11598 c = bb_process_escape_sequence((void*)&p);
11599 } else { /* unrecognized "\z": print both chars unless ' or " */
11600 if (c != '\'' && c != '"') {
11602 c |= 0x100; /* "please encode \, then me" */
11610 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
11611 * is not NULL, read a here document. In the latter case, eofmark is the
11612 * word which marks the end of the document and striptabs is true if
11613 * leading tabs should be stripped from the document. The argument c
11614 * is the first character of the input token or document.
11616 * Because C does not have internal subroutines, I have simulated them
11617 * using goto's to implement the subroutine linkage. The following macros
11618 * will run code that appears at the end of readtoken1.
11620 #define CHECKEND() {goto checkend; checkend_return:;}
11621 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
11622 #define PARSESUB() {goto parsesub; parsesub_return:;}
11623 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
11624 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
11625 #define PARSEARITH() {goto parsearith; parsearith_return:;}
11627 readtoken1(int c, int syntax, char *eofmark, int striptabs)
11629 /* NB: syntax parameter fits into smallint */
11630 /* c parameter is an unsigned char or PEOF or PEOA */
11633 char line[EOFMARKLEN + 1];
11634 struct nodelist *bqlist;
11638 IF_FEATURE_SH_MATH(smallint prevsyntax;) /* syntax before arithmetic */
11639 smallint pssyntax; /* we are expanding a prompt string */
11640 int varnest; /* levels of variables expansion */
11641 IF_FEATURE_SH_MATH(int arinest;) /* levels of arithmetic expansion */
11642 IF_FEATURE_SH_MATH(int parenlevel;) /* levels of parens in arithmetic */
11643 int dqvarnest; /* levels of variables expansion within double quotes */
11645 IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;)
11647 startlinno = g_parsefile->linno;
11650 IF_FEATURE_SH_MATH(prevsyntax = 0;)
11651 pssyntax = (syntax == PSSYNTAX);
11654 dblquote = (syntax == DQSYNTAX);
11656 IF_FEATURE_SH_MATH(arinest = 0;)
11657 IF_FEATURE_SH_MATH(parenlevel = 0;)
11660 STARTSTACKSTR(out);
11662 /* For each line, until end of word */
11663 CHECKEND(); /* set c to PEOF if at end of here document */
11664 for (;;) { /* until end of line or end of word */
11665 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
11666 switch (SIT(c, syntax)) {
11667 case CNL: /* '\n' */
11668 if (syntax == BASESYNTAX)
11669 goto endword; /* exit outer loop */
11673 goto loop; /* continue outer loop */
11678 #if BASH_DOLLAR_SQUOTE
11679 if (c == '\\' && bash_dollar_squote) {
11680 c = decode_dollar_squote();
11682 /* skip $'\000', $'\x00' (like bash) */
11686 /* Unknown escape. Encode as '\z' */
11687 c = (unsigned char)c;
11688 if (eofmark == NULL || dblquote)
11689 USTPUTC(CTLESC, out);
11690 USTPUTC('\\', out);
11694 if (eofmark == NULL || dblquote)
11695 USTPUTC(CTLESC, out);
11698 case CBACK: /* backslash */
11699 c = pgetc_without_PEOA();
11701 USTPUTC(CTLESC, out);
11702 USTPUTC('\\', out);
11704 } else if (c == '\n') {
11707 if (c == '$' && pssyntax) {
11708 USTPUTC(CTLESC, out);
11709 USTPUTC('\\', out);
11711 /* Backslash is retained if we are in "str" and next char isn't special */
11716 && (c != '"' || eofmark != NULL)
11718 USTPUTC('\\', out);
11720 USTPUTC(CTLESC, out);
11728 if (eofmark == NULL) {
11729 USTPUTC(CTLQUOTEMARK, out);
11737 IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;)
11738 if (eofmark != NULL && varnest == 0) {
11741 if (dqvarnest == 0) {
11742 syntax = BASESYNTAX;
11749 case CVAR: /* '$' */
11750 PARSESUB(); /* parse substitution */
11752 case CENDVAR: /* '}' */
11755 if (dqvarnest > 0) {
11762 #if ENABLE_FEATURE_SH_MATH
11763 case CLP: /* '(' in arithmetic */
11767 case CRP: /* ')' in arithmetic */
11768 if (parenlevel > 0) {
11771 if (pgetc_eatbnl() == ')') {
11773 if (--arinest == 0) {
11774 syntax = prevsyntax;
11778 * unbalanced parens
11779 * (don't 2nd guess - no error)
11787 case CBQUOTE: /* '`' */
11791 goto endword; /* exit outer loop */
11795 if (varnest == 0) {
11796 #if BASH_REDIR_OUTPUT
11798 //Can't call pgetc_eatbnl() here, this requires three-deep pungetc()
11799 if (pgetc() == '>')
11800 c = 0x100 + '>'; /* flag &> */
11804 goto endword; /* exit outer loop */
11806 IF_ASH_ALIAS(if (c != PEOA))
11813 #if ENABLE_FEATURE_SH_MATH
11814 if (syntax == ARISYNTAX)
11815 raise_error_syntax("missing '))'");
11817 if (syntax != BASESYNTAX && eofmark == NULL)
11818 raise_error_syntax("unterminated quoted string");
11819 if (varnest != 0) {
11820 startlinno = g_parsefile->linno;
11822 raise_error_syntax("missing '}'");
11824 USTPUTC('\0', out);
11825 len = out - (char *)stackblock();
11826 out = stackblock();
11827 if (eofmark == NULL) {
11828 if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>'))
11831 if (isdigit_str9(out)) {
11832 PARSEREDIR(); /* passed as params: out, c */
11833 lasttoken = TREDIR;
11836 /* else: non-number X seen, interpret it
11837 * as "NNNX>file" = "NNNX >file" */
11841 quoteflag = quotef;
11842 backquotelist = bqlist;
11843 grabstackblock(len);
11847 /* end of readtoken routine */
11850 * Check to see whether we are at the end of the here document. When this
11851 * is called, c is set to the first character of the next input line. If
11852 * we are at the end of the here document, this routine sets the c to PEOF.
11856 #if ENABLE_ASH_ALIAS
11858 c = pgetc_without_PEOA();
11861 while (c == '\t') {
11862 c = pgetc_without_PEOA();
11865 if (c == *eofmark) {
11866 if (pfgets(line, sizeof(line)) != NULL) {
11871 for (q = eofmark + 1;; p++, q++) {
11875 if (!*q || cc != *q)
11882 pushstring(line, NULL);
11887 goto checkend_return;
11891 * Parse a redirection operator. The variable "out" points to a string
11892 * specifying the fd to be redirected. The variable "c" contains the
11893 * first character of the redirection operator.
11896 /* out is already checked to be a valid number or "" */
11897 int fd = (*out == '\0' ? -1 : atoi(out));
11900 np = stzalloc(sizeof(struct nfile));
11905 np->type = NAPPEND;
11907 np->type = NCLOBBER;
11910 /* it also can be NTO2 (>&file), but we can't figure it out yet */
11916 #if BASH_REDIR_OUTPUT
11917 else if (c == 0x100 + '>') { /* this flags &> redirection */
11919 pgetc(); /* this is '>', no need to check */
11923 else { /* c == '<' */
11924 /*np->nfile.fd = 0; - stzalloc did it */
11928 if (sizeof(struct nfile) != sizeof(struct nhere)) {
11929 np = stzalloc(sizeof(struct nhere));
11930 /*np->nfile.fd = 0; - stzalloc did it */
11933 heredoc = stzalloc(sizeof(struct heredoc));
11934 heredoc->here = np;
11937 heredoc->striptabs = 1;
11939 /*heredoc->striptabs = 0; - stzalloc did it */
11945 np->type = NFROMFD;
11949 np->type = NFROMTO;
11961 goto parseredir_return;
11965 * Parse a substitution. At this point, we have read the dollar sign
11966 * and nothing else.
11969 /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
11970 * (assuming ascii char codes, as the original implementation did) */
11971 #define is_special(c) \
11972 (((unsigned)(c) - 33 < 32) \
11973 && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1))
11975 unsigned char subtype;
11978 c = pgetc_eatbnl();
11979 if (c > 255 /* PEOA or PEOF */
11980 || (c != '(' && c != '{' && !is_name(c) && !is_special(c))
11982 #if BASH_DOLLAR_SQUOTE
11983 if (syntax != DQSYNTAX && c == '\'')
11984 bash_dollar_squote = 1;
11989 } else if (c == '(') {
11990 /* $(command) or $((arith)) */
11991 if (pgetc_eatbnl() == '(') {
11992 #if ENABLE_FEATURE_SH_MATH
11995 raise_error_syntax("support for $((arith)) is disabled");
12002 /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */
12003 USTPUTC(CTLVAR, out);
12004 typeloc = out - (char *)stackblock();
12006 subtype = VSNORMAL;
12008 c = pgetc_eatbnl();
12013 /* $[{[#]]NAME[}] */
12016 c = pgetc_eatbnl();
12017 } while (is_in_name(c));
12018 } else if (isdigit(c)) {
12019 /* $[{[#]]NUM[}] */
12022 c = pgetc_eatbnl();
12023 } while (isdigit(c));
12024 } else if (is_special(c)) {
12025 /* $[{[#]]<specialchar>[}] */
12028 c = pgetc_eatbnl();
12029 if (!subtype && cc == '#') {
12030 subtype = VSLENGTH;
12031 if (c == '_' || isalnum(c))
12034 c = pgetc_eatbnl();
12035 if (cc == '}' || c != '}') {
12046 if (c != '}' && subtype == VSLENGTH) {
12047 /* ${#VAR didn't end with } */
12051 if (subtype == 0) {
12052 static const char types[] ALIGN1 = "}-+?=";
12053 /* ${VAR...} but not $VAR or ${#VAR} */
12054 /* c == first char after VAR */
12057 c = pgetc_eatbnl();
12059 /* This check is only needed to not misinterpret
12060 * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD}
12063 if (!strchr(types, c)) {
12064 subtype = VSSUBSTR;
12066 break; /* "goto badsub" is bigger (!) */
12072 const char *p = strchr(types, c);
12075 subtype |= p - types + VSNORMAL;
12081 subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT);
12082 c = pgetc_eatbnl();
12088 #if BASH_PATTERN_SUBST
12090 /* ${v/[/]pattern/repl} */
12091 //TODO: encode pattern and repl separately.
12092 // Currently ${v/$var_with_slash/repl} is horribly broken
12093 subtype = VSREPLACE;
12094 c = pgetc_eatbnl();
12097 subtype++; /* VSREPLACEALL */
12105 ((unsigned char *)stackblock())[typeloc] = subtype;
12106 if (subtype != VSNORMAL) {
12113 goto parsesub_return;
12117 * Called to parse command substitutions. Newstyle is set if the command
12118 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12119 * list of commands (passed by reference), and savelen is the number of
12120 * characters on the top of the stack which must be preserved.
12123 struct nodelist **nlpp;
12127 smallint saveprompt = 0;
12130 savelen = out - (char *)stackblock();
12133 * FIXME: this can allocate very large block on stack and SEGV.
12135 * echo "..<100kbytes>..`true` $(true) `true` ..."
12136 * allocates 100kb for every command subst. With about
12137 * a hundred command substitutions stack overflows.
12138 * With larger prepended string, SEGV happens sooner.
12140 str = alloca(savelen);
12141 memcpy(str, stackblock(), savelen);
12145 /* We must read until the closing backquote, giving special
12146 * treatment to some slashes, and then push the string and
12147 * reread it as input, interpreting it normally.
12153 STARTSTACKSTR(pout);
12157 setprompt_if(needprompt, 2);
12168 * If eating a newline, avoid putting
12169 * the newline into the new character
12170 * stream (via the STPUTC after the
12175 if (pc != '\\' && pc != '`' && pc != '$'
12176 && (!dblquote || pc != '"')
12178 STPUTC('\\', pout);
12180 if (pc <= 255 /* not PEOA or PEOF */) {
12186 IF_ASH_ALIAS(case PEOA:)
12187 startlinno = g_parsefile->linno;
12188 raise_error_syntax("EOF in backquote substitution");
12200 STPUTC('\0', pout);
12201 psavelen = pout - (char *)stackblock();
12202 if (psavelen > 0) {
12203 pstr = grabstackstr(pout);
12204 setinputstring(pstr);
12209 nlpp = &(*nlpp)->next;
12210 *nlpp = stzalloc(sizeof(**nlpp));
12211 /* (*nlpp)->next = NULL; - stzalloc did it */
12214 saveprompt = doprompt;
12221 doprompt = saveprompt;
12222 else if (readtoken() != TRP)
12223 raise_error_unexpected_syntax(TRP);
12228 * Start reading from old file again, ignoring any pushed back
12229 * tokens left from the backquote parsing
12234 while (stackblocksize() <= savelen)
12236 STARTSTACKSTR(out);
12238 memcpy(out, str, savelen);
12239 STADJUST(savelen, out);
12241 USTPUTC(CTLBACKQ, out);
12243 goto parsebackq_oldreturn;
12244 goto parsebackq_newreturn;
12247 #if ENABLE_FEATURE_SH_MATH
12249 * Parse an arithmetic expansion (indicate start of one and set state)
12252 if (++arinest == 1) {
12253 prevsyntax = syntax;
12254 syntax = ARISYNTAX;
12256 USTPUTC(CTLARI, out);
12257 goto parsearith_return;
12260 } /* end of readtoken */
12263 * Read the next input token.
12264 * If the token is a word, we set backquotelist to the list of cmds in
12265 * backquotes. We set quoteflag to true if any part of the word was
12267 * If the token is TREDIR, then we set redirnode to a structure containing
12269 * In all cases, the variable startlinno is set to the number of the line
12270 * on which the token starts.
12272 * [Change comment: here documents and internal procedures]
12273 * [Readtoken shouldn't have any arguments. Perhaps we should make the
12274 * word parsing code into a separate routine. In this case, readtoken
12275 * doesn't need to have any internal procedures, but parseword does.
12276 * We could also make parseoperator in essence the main routine, and
12277 * have parseword (readtoken1?) handle both words and redirection.]
12279 #define NEW_xxreadtoken
12280 #ifdef NEW_xxreadtoken
12281 /* singles must be first! */
12282 static const char xxreadtoken_chars[7] ALIGN1 = {
12283 '\n', '(', ')', /* singles */
12284 '&', '|', ';', /* doubles */
12288 #define xxreadtoken_singles 3
12289 #define xxreadtoken_doubles 3
12291 static const char xxreadtoken_tokens[] ALIGN1 = {
12292 TNL, TLP, TRP, /* only single occurrence allowed */
12293 TBACKGND, TPIPE, TSEMI, /* if single occurrence */
12294 TEOF, /* corresponds to trailing nul */
12295 TAND, TOR, TENDCASE /* if double occurrence */
12307 setprompt_if(needprompt, 2);
12308 startlinno = g_parsefile->linno;
12309 for (;;) { /* until token or start of word found */
12311 if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA))
12315 while ((c = pgetc()) != '\n' && c != PEOF)
12318 } else if (c == '\\') {
12319 if (pgetc() != '\n') {
12321 break; /* return readtoken1(...) */
12327 p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
12333 p = strchr(xxreadtoken_chars, c);
12335 break; /* return readtoken1(...) */
12337 if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) {
12339 if (cc == c) { /* double occurrence? */
12340 p += xxreadtoken_doubles + 1;
12343 #if BASH_REDIR_OUTPUT
12344 if (c == '&' && cc == '>') /* &> */
12345 break; /* return readtoken1(...) */
12350 lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
12355 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
12357 #else /* old xxreadtoken */
12358 #define RETURN(token) return lasttoken = token
12368 setprompt_if(needprompt, 2);
12369 startlinno = g_parsefile->linno;
12370 for (;;) { /* until token or start of word found */
12373 case ' ': case '\t':
12374 IF_ASH_ALIAS(case PEOA:)
12377 while ((c = pgetc()) != '\n' && c != PEOF)
12382 if (pgetc() == '\n') {
12394 if (pgetc() == '&')
12399 if (pgetc() == '|')
12404 if (pgetc() == ';')
12417 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
12420 #endif /* old xxreadtoken */
12426 int kwd = checkkwd;
12428 smallint alreadyseen = tokpushback;
12431 #if ENABLE_ASH_ALIAS
12447 if (t != TWORD || quoteflag) {
12452 * check for keywords
12454 if (kwd & CHKKWD) {
12455 const char *const *pp;
12457 pp = findkwd(wordtext);
12459 lasttoken = t = pp - tokname_array;
12460 TRACE(("keyword '%s' recognized\n", tokname_array[t]));
12465 if (checkkwd & CHKALIAS) {
12466 #if ENABLE_ASH_ALIAS
12468 ap = lookupalias(wordtext, 1);
12471 pushstring(ap->val, ap);
12481 TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
12483 TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
12499 * Read and parse a command. Returns NODE_EOF on end of file.
12500 * (NULL is a valid parse tree indicating a blank line.)
12502 static union node *
12503 parsecmd(int interact)
12508 doprompt = interact;
12509 setprompt_if(doprompt, doprompt);
12515 * Input any here documents.
12520 struct heredoc *here;
12523 here = heredoclist;
12524 heredoclist = NULL;
12527 setprompt_if(needprompt, 2);
12528 readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX,
12529 here->eofmark, here->striptabs);
12530 n = stzalloc(sizeof(struct narg));
12531 n->narg.type = NARG;
12532 /*n->narg.next = NULL; - stzalloc did it */
12533 n->narg.text = wordtext;
12534 n->narg.backquote = backquotelist;
12535 here->here->nhere.doc = n;
12542 * called by editline -- any expansions to the prompt should be added here.
12544 static const char *
12545 expandstr(const char *ps)
12550 /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value,
12551 * and token processing _can_ alter it (delete NULs etc). */
12552 setinputstring((char *)ps);
12554 saveprompt = doprompt;
12556 readtoken1(pgetc(), PSSYNTAX, nullstr, 0);
12557 doprompt = saveprompt;
12561 n.narg.type = NARG;
12562 n.narg.next = NULL;
12563 n.narg.text = wordtext;
12564 n.narg.backquote = backquotelist;
12566 expandarg(&n, NULL, EXP_QUOTED);
12567 return stackblock();
12571 * Execute a command or commands contained in a string.
12574 evalstring(char *s, int flags)
12576 struct jmploc *volatile savehandler;
12577 struct jmploc jmploc;
12581 struct stackmark smark;
12586 setstackmark(&smark);
12589 /* On exception inside execution loop, we must popfile().
12590 * Try interactively:
12592 * command eval "a=b" # throws "is read only" error
12593 * "command BLTIN" is not supposed to abort (even in non-interactive use).
12594 * But if we skip popfile(), we hit EOF in eval's string, and exit.
12596 savehandler = exception_handler;
12597 ex = setjmp(jmploc.loc);
12600 exception_handler = &jmploc;
12602 while ((n = parsecmd(0)) != NODE_EOF) {
12605 i = evaltree(n, flags);
12608 popstackmark(&smark);
12613 popstackmark(&smark);
12617 exception_handler = savehandler;
12619 longjmp(exception_handler->loc, ex);
12625 * The eval command.
12627 static int FAST_FUNC
12628 evalcmd(int argc UNUSED_PARAM, char **argv, int flags)
12637 STARTSTACKSTR(concat);
12639 concat = stack_putstr(p, concat);
12643 STPUTC(' ', concat);
12645 STPUTC('\0', concat);
12646 p = grabstackstr(concat);
12648 return evalstring(p, flags & EV_TESTED);
12654 * Read and execute commands.
12655 * "Top" is nonzero for the top level command loop;
12656 * it turns on prompting if the shell is interactive.
12662 struct stackmark smark;
12667 TRACE(("cmdloop(%d) called\n", top));
12671 setstackmark(&smark);
12674 showjobs(SHOW_CHANGED|SHOW_STDERR);
12677 if (iflag && top) {
12681 n = parsecmd(inter);
12683 if (DEBUG > 2 && debug && (n != NODE_EOF))
12686 if (n == NODE_EOF) {
12687 if (!top || numeof >= 50)
12689 if (!stoppedjobs()) {
12692 out2str("\nUse \"exit\" to leave shell.\n");
12695 } else if (nflag == 0) {
12698 /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */
12701 i = evaltree(n, 0);
12705 popstackmark(&smark);
12709 evalskip &= ~SKIPFUNC;
12717 * Take commands from a file. To be compatible we should do a path
12718 * search for the file, which is necessary to find sub-commands.
12721 find_dot_file(char *name)
12724 const char *path = pathval();
12727 /* don't try this for absolute or relative paths */
12728 if (strchr(name, '/'))
12731 while ((fullname = path_advance(&path, name)) != NULL) {
12732 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
12734 * Don't bother freeing here, since it will
12735 * be freed by the caller.
12739 if (fullname != name)
12740 stunalloc(fullname);
12743 /* not found in the PATH */
12744 ash_msg_and_raise_error("%s: not found", name);
12748 static int FAST_FUNC
12749 dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM)
12751 /* "false; . empty_file; echo $?" should print 0, not 1: */
12755 char *args_need_save;
12756 struct strlist *sp;
12757 volatile struct shparam saveparam;
12759 for (sp = cmdenviron; sp; sp = sp->next)
12760 setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
12762 nextopt(nullstr); /* handle possible "--" */
12766 /* bash says: "bash: .: filename argument required" */
12767 return 2; /* bash compat */
12770 /* This aborts if file isn't found, which is POSIXly correct.
12771 * bash returns exitcode 1 instead.
12773 fullname = find_dot_file(argv[0]);
12775 args_need_save = argv[0];
12776 if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */
12778 saveparam = shellparam;
12779 shellparam.malloced = 0;
12783 shellparam.nparam = argc;
12784 shellparam.p = argv;
12787 /* This aborts if file can't be opened, which is POSIXly correct.
12788 * bash returns exitcode 1 instead.
12790 setinputfile(fullname, INPUT_PUSH_FILE);
12791 commandname = fullname;
12792 status = cmdloop(0);
12795 if (args_need_save) {
12796 freeparam(&shellparam);
12797 shellparam = saveparam;
12803 static int FAST_FUNC
12804 exitcmd(int argc UNUSED_PARAM, char **argv)
12809 exitstatus = number(argv[1]);
12810 raise_exception(EXEXIT);
12815 * Read a file containing shell functions.
12818 readcmdfile(char *name)
12820 setinputfile(name, INPUT_PUSH_FILE);
12826 /* ============ find_command inplementation */
12829 * Resolve a command name. If you change this routine, you may have to
12830 * change the shellexec routine as well.
12833 find_command(char *name, struct cmdentry *entry, int act, const char *path)
12835 struct tblentry *cmdp;
12842 struct builtincmd *bcmd;
12844 /* If name contains a slash, don't use PATH or hash table */
12845 if (strchr(name, '/') != NULL) {
12846 entry->u.index = -1;
12847 if (act & DO_ABS) {
12848 while (stat(name, &statb) < 0) {
12850 if (errno == EINTR)
12853 entry->cmdtype = CMDUNKNOWN;
12857 entry->cmdtype = CMDNORMAL;
12861 /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */
12863 updatetbl = (path == pathval());
12866 if (strstr(path, "%builtin") != NULL)
12867 act |= DO_ALTBLTIN;
12870 /* If name is in the table, check answer will be ok */
12871 cmdp = cmdlookup(name, 0);
12872 if (cmdp != NULL) {
12875 switch (cmdp->cmdtype) {
12893 } else if (cmdp->rehash == 0)
12894 /* if not invalidated by cd, we're done */
12898 /* If %builtin not in path, check for builtin next */
12899 bcmd = find_builtin(name);
12901 if (IS_BUILTIN_REGULAR(bcmd))
12902 goto builtin_success;
12903 if (act & DO_ALTPATH) {
12904 if (!(act & DO_ALTBLTIN))
12905 goto builtin_success;
12906 } else if (builtinloc <= 0) {
12907 goto builtin_success;
12911 #if ENABLE_FEATURE_SH_STANDALONE
12913 int applet_no = find_applet_by_name(name);
12914 if (applet_no >= 0) {
12915 entry->cmdtype = CMDNORMAL;
12916 entry->u.index = -2 - applet_no;
12922 /* We have to search path. */
12923 prev = -1; /* where to start */
12924 if (cmdp && cmdp->rehash) { /* doing a rehash */
12925 if (cmdp->cmdtype == CMDBUILTIN)
12928 prev = cmdp->param.index;
12934 while ((fullname = path_advance(&path, name)) != NULL) {
12935 stunalloc(fullname);
12936 /* NB: code below will still use fullname
12937 * despite it being "unallocated" */
12940 if (prefix(pathopt, "builtin")) {
12942 goto builtin_success;
12945 if ((act & DO_NOFUNC)
12946 || !prefix(pathopt, "func")
12947 ) { /* ignore unimplemented options */
12951 /* if rehash, don't redo absolute path names */
12952 if (fullname[0] == '/' && idx <= prev) {
12955 TRACE(("searchexec \"%s\": no change\n", name));
12958 while (stat(fullname, &statb) < 0) {
12960 if (errno == EINTR)
12963 if (errno != ENOENT && errno != ENOTDIR)
12967 e = EACCES; /* if we fail, this will be the error */
12968 if (!S_ISREG(statb.st_mode))
12970 if (pathopt) { /* this is a %func directory */
12971 stalloc(strlen(fullname) + 1);
12972 /* NB: stalloc will return space pointed by fullname
12973 * (because we don't have any intervening allocations
12974 * between stunalloc above and this stalloc) */
12975 readcmdfile(fullname);
12976 cmdp = cmdlookup(name, 0);
12977 if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION)
12978 ash_msg_and_raise_error("%s not defined in %s", name, fullname);
12979 stunalloc(fullname);
12982 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
12984 entry->cmdtype = CMDNORMAL;
12985 entry->u.index = idx;
12989 cmdp = cmdlookup(name, 1);
12990 cmdp->cmdtype = CMDNORMAL;
12991 cmdp->param.index = idx;
12996 /* We failed. If there was an entry for this command, delete it */
12997 if (cmdp && updatetbl)
12998 delete_cmd_entry();
13000 ash_msg("%s: %s", name, errmsg(e, "not found"));
13001 entry->cmdtype = CMDUNKNOWN;
13006 entry->cmdtype = CMDBUILTIN;
13007 entry->u.cmd = bcmd;
13011 cmdp = cmdlookup(name, 1);
13012 cmdp->cmdtype = CMDBUILTIN;
13013 cmdp->param.cmd = bcmd;
13017 entry->cmdtype = cmdp->cmdtype;
13018 entry->u = cmdp->param;
13023 * The trap builtin.
13025 static int FAST_FUNC
13026 trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13030 int signo, exitcode;
13035 for (signo = 0; signo < NSIG; signo++) {
13036 char *tr = trap_ptr[signo];
13038 /* note: bash adds "SIG", but only if invoked
13039 * as "bash". If called as "sh", or if set -o posix,
13040 * then it prints short signal names.
13041 * We are printing short names: */
13042 out1fmt("trap -- %s %s\n",
13044 get_signame(signo));
13045 /* trap_ptr != trap only if we are in special-cased `trap` code.
13046 * In this case, we will exit very soon, no need to free(). */
13047 /* if (trap_ptr != trap && tp[0]) */
13052 if (trap_ptr != trap) {
13065 signo = get_signum(*ap);
13066 if (signo < 0 || signo >= NSIG) {
13067 /* Mimic bash message exactly */
13068 ash_msg("%s: invalid signal specification", *ap);
13074 if (LONE_DASH(action))
13077 if (action[0]) /* not NULL and not "" and not "-" */
13078 may_have_traps = 1;
13079 action = ckstrdup(action);
13083 trap[signo] = action;
13094 /* ============ Builtins */
13096 #if ENABLE_ASH_HELP
13097 static int FAST_FUNC
13098 helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13104 "Built-in commands:\n"
13105 "------------------\n");
13106 for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
13107 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '),
13108 builtintab[i].name + 1);
13114 # if ENABLE_FEATURE_SH_STANDALONE
13116 const char *a = applet_names;
13118 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a);
13123 while (*a++ != '\0')
13128 newline_and_flush(stdout);
13129 return EXIT_SUCCESS;
13134 static int FAST_FUNC
13135 historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13137 show_history(line_input_state);
13138 return EXIT_SUCCESS;
13143 * The export and readonly commands.
13145 static int FAST_FUNC
13146 exportcmd(int argc UNUSED_PARAM, char **argv)
13156 /* "readonly" in bash accepts, but ignores -n.
13157 * We do the same: it saves a conditional in nextopt's param.
13160 while ((opt = nextopt("np")) != '\0') {
13162 flag_off = VEXPORT;
13165 if (argv[0][0] == 'r') {
13167 flag_off = 0; /* readonly ignores -n */
13169 flag_off = ~flag_off;
13171 /*if (opt_p_not_specified) - bash doesn't check this. Try "export -p NAME" */
13177 p = strchr(name, '=');
13181 vp = *findvar(hashvar(name), name);
13183 vp->flags = ((vp->flags | flag) & flag_off);
13187 setvar(name, p, (flag & flag_off));
13188 } while ((name = *++aptr) != NULL);
13193 /* No arguments. Show the list of exported or readonly vars.
13196 showvars(argv[0], flag, 0);
13201 * Delete a function if it exists.
13204 unsetfunc(const char *name)
13206 struct tblentry *cmdp;
13208 cmdp = cmdlookup(name, 0);
13209 if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION)
13210 delete_cmd_entry();
13214 * The unset builtin command. We unset the function before we unset the
13215 * variable to allow a function to be unset when there is a readonly variable
13216 * with the same name.
13218 static int FAST_FUNC
13219 unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13226 while ((i = nextopt("vf")) != 0) {
13230 for (ap = argptr; *ap; ap++) {
13243 static const unsigned char timescmd_str[] ALIGN1 = {
13244 ' ', offsetof(struct tms, tms_utime),
13245 '\n', offsetof(struct tms, tms_stime),
13246 ' ', offsetof(struct tms, tms_cutime),
13247 '\n', offsetof(struct tms, tms_cstime),
13250 static int FAST_FUNC
13251 timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13253 unsigned long clk_tck, s, t;
13254 const unsigned char *p;
13257 clk_tck = bb_clk_tck();
13262 t = *(clock_t *)(((char *) &buf) + p[1]);
13265 out1fmt("%lum%lu.%03lus%c",
13267 (t * 1000) / clk_tck,
13275 #if ENABLE_FEATURE_SH_MATH
13277 * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell.
13278 * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
13280 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
13282 static int FAST_FUNC
13283 letcmd(int argc UNUSED_PARAM, char **argv)
13289 ash_msg_and_raise_error("expression expected");
13291 i = ash_arith(*argv);
13299 * The read builtin. Options:
13300 * -r Do not interpret '\' specially
13301 * -s Turn off echo (tty only)
13302 * -n NCHARS Read NCHARS max
13303 * -p PROMPT Display PROMPT on stderr (if input is from tty)
13304 * -t SECONDS Timeout after SECONDS (tty or pipe only)
13305 * -u FD Read from given FD instead of fd 0
13306 * This uses unbuffered input, which may be avoidable in some cases.
13307 * TODO: bash also has:
13308 * -a ARRAY Read into array[0],[1],etc
13309 * -d DELIM End on DELIM char, not newline
13310 * -e Use line editing (tty only)
13312 static int FAST_FUNC
13313 readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13315 char *opt_n = NULL;
13316 char *opt_p = NULL;
13317 char *opt_t = NULL;
13318 char *opt_u = NULL;
13319 int read_flags = 0;
13323 while ((i = nextopt("p:u:rt:n:s")) != '\0') {
13332 read_flags |= BUILTIN_READ_SILENT;
13338 read_flags |= BUILTIN_READ_RAW;
13348 /* "read -s" needs to save/restore termios, can't allow ^C
13349 * to jump out of it.
13353 r = shell_builtin_read(setvar0,
13355 bltinlookup("IFS"), /* can be NULL */
13364 if ((uintptr_t)r == 1 && errno == EINTR) {
13365 /* to get SIGCHLD: sleep 1 & read x; echo $x */
13366 if (pending_sig == 0)
13370 if ((uintptr_t)r > 1)
13371 ash_msg_and_raise_error(r);
13373 return (uintptr_t)r;
13376 static int FAST_FUNC
13377 umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13379 static const char permuser[3] ALIGN1 = "ogu";
13382 int symbolic_mode = 0;
13384 while (nextopt("S") != '\0') {
13393 if (*argptr == NULL) {
13394 if (symbolic_mode) {
13395 char buf[sizeof(",u=rwx,g=rwx,o=rwx")];
13402 *p++ = permuser[i];
13404 /* mask is 0..0uuugggooo. i=2 selects uuu bits */
13405 if (!(mask & 0400)) *p++ = 'r';
13406 if (!(mask & 0200)) *p++ = 'w';
13407 if (!(mask & 0100)) *p++ = 'x';
13415 out1fmt("%04o\n", mask);
13418 char *modestr = *argptr;
13419 /* numeric umasks are taken as-is */
13420 /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
13421 if (!isdigit(modestr[0]))
13423 mask = bb_parse_mode(modestr, mask);
13424 if ((unsigned)mask > 0777) {
13425 ash_msg_and_raise_error("illegal mode: %s", modestr);
13427 if (!isdigit(modestr[0]))
13434 static int FAST_FUNC
13435 ulimitcmd(int argc UNUSED_PARAM, char **argv)
13437 return shell_builtin_ulimit(argv);
13440 /* ============ main() and helpers */
13443 * Called to exit the shell.
13452 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
13453 save_history(line_input_state);
13455 status = exitstatus;
13456 TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
13457 if (setjmp(loc.loc)) {
13458 if (exception_type == EXEXIT)
13459 status = exitstatus;
13462 exception_handler = &loc;
13468 /*free(p); - we'll exit soon */
13471 /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}".
13472 * our setjobctl(0) does not panic if tcsetpgrp fails inside it.
13475 flush_stdout_stderr();
13483 /* we will never free this */
13484 basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ);
13486 sigmode[SIGCHLD - 1] = S_DFL;
13487 setsignal(SIGCHLD);
13489 /* bash re-enables SIGHUP which is SIG_IGNed on entry.
13490 * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$"
13492 signal(SIGHUP, SIG_DFL);
13497 struct stat st1, st2;
13500 for (envp = environ; envp && *envp; envp++) {
13501 p = endofname(*envp);
13502 if (p != *envp && *p == '=') {
13503 setvareq(*envp, VEXPORT|VTEXTFIXED);
13507 setvareq((char*)defoptindvar, VTEXTFIXED);
13509 setvar0("PPID", utoa(getppid()));
13511 p = lookupvar("SHLVL");
13512 setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT);
13514 #if BASH_HOSTNAME_VAR
13515 if (!lookupvar("HOSTNAME")) {
13516 struct utsname uts;
13518 setvar0("HOSTNAME", uts.nodename);
13521 p = lookupvar("PWD");
13523 if (p[0] != '/' || stat(p, &st1) || stat(".", &st2)
13524 || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino
13534 //usage:#define ash_trivial_usage
13535 //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]"
13536 //usage:#define ash_full_usage "\n\n"
13537 //usage: "Unix shell interpreter"
13540 * Process the shell command line arguments.
13543 procargs(char **argv)
13546 const char *xminusc;
13551 /* if (xargv[0]) - mmm, this is always true! */
13553 for (i = 0; i < NOPTS; i++)
13556 if (options(/*cmdline:*/ 1)) {
13557 /* it already printed err message */
13558 raise_exception(EXERROR);
13562 if (*xargv == NULL) {
13564 ash_msg_and_raise_error(bb_msg_requires_arg, "-c");
13567 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
13571 for (i = 0; i < NOPTS; i++)
13572 if (optlist[i] == 2)
13577 /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */
13582 } else if (!sflag) {
13583 setinputfile(*xargv, 0);
13586 commandname = arg0;
13589 shellparam.p = xargv;
13590 #if ENABLE_ASH_GETOPTS
13591 shellparam.optind = 1;
13592 shellparam.optoff = -1;
13594 /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */
13596 shellparam.nparam++;
13603 * Read /etc/profile, ~/.profile, $ENV.
13606 read_profile(const char *name)
13608 name = expandstr(name);
13609 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
13616 * This routine is called when an error or an interrupt occurs in an
13617 * interactive shell and control is returned to the main command loop.
13618 * (In dash, this function is auto-generated by build machinery).
13627 /* from expand.c: */
13630 /* from input.c: */
13631 g_parsefile->left_in_buffer = 0;
13632 g_parsefile->left_in_line = 0; /* clear input buffer */
13635 /* from redir.c: */
13637 popredir(/*drop:*/ 0, /*restore:*/ 0);
13641 static short profile_buf[16384];
13642 extern int etext();
13646 * Main routine. We initialize things, parse the arguments, execute
13647 * profiles if we're a login shell, and then call cmdloop to execute
13648 * commands. The setjmp call sets up the location to jump to when an
13649 * exception occurs. When an exception occurs the variable "state"
13650 * is used to figure out how far we had gotten.
13652 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
13653 int ash_main(int argc UNUSED_PARAM, char **argv)
13655 volatile smallint state;
13656 struct jmploc jmploc;
13657 struct stackmark smark;
13659 /* Initialize global data */
13663 #if ENABLE_ASH_ALIAS
13669 monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
13672 #if ENABLE_FEATURE_EDITING
13673 line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
13676 if (setjmp(jmploc.loc)) {
13682 e = exception_type;
13684 if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) {
13688 newline_and_flush(stderr);
13691 popstackmark(&smark);
13692 FORCE_INT_ON; /* enable interrupts */
13701 exception_handler = &jmploc;
13702 rootpid = getpid();
13705 setstackmark(&smark);
13708 TRACE(("Shell args: "));
13709 trace_puts_args(argv);
13712 if (argv[0] && argv[0][0] == '-')
13718 read_profile("/etc/profile");
13721 hp = lookupvar("HOME");
13723 read_profile("$HOME/.profile");
13729 getuid() == geteuid() && getgid() == getegid() &&
13733 const char *shinit = lookupvar("ENV");
13734 if (shinit != NULL && *shinit != '\0')
13735 read_profile(shinit);
13737 popstackmark(&smark);
13741 /* evalstring pushes parsefile stack.
13742 * Ensure we don't falsely claim that 0 (stdin)
13743 * is one of stacked source fds.
13744 * Testcase: ash -c 'exec 1>&0' must not complain. */
13745 // if (!sflag) g_parsefile->pf_fd = -1;
13746 // ^^ not necessary since now we special-case fd 0
13747 // in is_hidden_fd() to not be considered "hidden fd"
13748 evalstring(minusc, 0);
13751 if (sflag || minusc == NULL) {
13752 #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
13754 const char *hp = lookupvar("HISTFILE");
13756 hp = lookupvar("HOME");
13759 hp = concat_path_file(hp, ".ash_history");
13760 setvar0("HISTFILE", hp);
13763 hp = lookupvar("HISTFILE");
13767 line_input_state->hist_file = hp;
13768 # if ENABLE_FEATURE_SH_HISTFILESIZE
13769 hp = lookupvar("HISTFILESIZE");
13770 line_input_state->max_history = size_from_HISTFILESIZE(hp);
13774 state4: /* XXX ??? - why isn't this before the "if" statement */
13782 extern void _mcleanup(void);
13786 TRACE(("End of main reached\n"));
13793 * Copyright (c) 1989, 1991, 1993, 1994
13794 * The Regents of the University of California. All rights reserved.
13796 * This code is derived from software contributed to Berkeley by
13797 * Kenneth Almquist.
13799 * Redistribution and use in source and binary forms, with or without
13800 * modification, are permitted provided that the following conditions
13802 * 1. Redistributions of source code must retain the above copyright
13803 * notice, this list of conditions and the following disclaimer.
13804 * 2. Redistributions in binary form must reproduce the above copyright
13805 * notice, this list of conditions and the following disclaimer in the
13806 * documentation and/or other materials provided with the distribution.
13807 * 3. Neither the name of the University nor the names of its contributors
13808 * may be used to endorse or promote products derived from this software
13809 * without specific prior written permission.
13811 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
13812 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13813 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13814 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
13815 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13816 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
13817 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13818 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13819 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13820 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF