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 (78 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_BASH_SOURCE_CURDIR
55 //config: bool "'source' and '.' builtins search current directory after $PATH"
56 //config: default n # do not encourage non-standard behavior
57 //config: depends on ASH_BASH_COMPAT
59 //config: This is not compliant with standards. Avoid if possible.
61 //config:config ASH_BASH_NOT_FOUND_HOOK
62 //config: bool "command_not_found_handle hook support"
64 //config: depends on ASH_BASH_COMPAT
66 //config: Enable support for the 'command_not_found_handle' hook function,
67 //config: from GNU bash, which allows for alternative command not found
70 //config:config ASH_JOB_CONTROL
71 //config: bool "Job control"
73 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
75 //config:config ASH_ALIAS
76 //config: bool "Alias support"
78 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
80 //config:config ASH_RANDOM_SUPPORT
81 //config: bool "Pseudorandom generator and $RANDOM variable"
83 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
85 //config: Enable pseudorandom generator and dynamic variable "$RANDOM".
86 //config: Each read of "$RANDOM" will generate a new pseudorandom value.
87 //config: You can reset the generator by using a specified start value.
88 //config: After "unset RANDOM" the generator will switch off and this
89 //config: variable will no longer have special treatment.
91 //config:config ASH_EXPAND_PRMT
92 //config: bool "Expand prompt string"
94 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
96 //config: $PS# may contain volatile content, such as backquote commands.
97 //config: This option recreates the prompt string from the environment
98 //config: variable each time it is displayed.
100 //config:config ASH_IDLE_TIMEOUT
101 //config: bool "Idle timeout variable $TMOUT"
103 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
105 //config: Enable bash-like auto-logout after $TMOUT seconds of idle time.
107 //config:config ASH_MAIL
108 //config: bool "Check for new mail in interactive shell"
110 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
112 //config: Enable "check for new mail" function:
113 //config: if set, $MAIL file and $MAILPATH list of files
114 //config: are checked for mtime changes, and "you have mail"
115 //config: message is printed if change is detected.
117 //config:config ASH_ECHO
118 //config: bool "echo builtin"
120 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
122 //config:config ASH_PRINTF
123 //config: bool "printf builtin"
125 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
127 //config:config ASH_TEST
128 //config: bool "test builtin"
130 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
132 //config:config ASH_HELP
133 //config: bool "help builtin"
135 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
137 //config:config ASH_GETOPTS
138 //config: bool "getopts builtin"
140 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
142 //config:config ASH_CMDCMD
143 //config: bool "command builtin"
145 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
147 //config: Enable support for the 'command' builtin, which allows
148 //config: you to run the specified command or builtin,
149 //config: even when there is a function with the same name.
151 //config:endif # ash options
153 //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP))
154 // APPLET_ODDNAME:name main location suid_type help
155 //applet:IF_SH_IS_ASH( APPLET_ODDNAME(sh, ash, BB_DIR_BIN, BB_SUID_DROP, ash))
156 //applet:IF_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, BB_DIR_BIN, BB_SUID_DROP, ash))
158 //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o
159 //kbuild:lib-$(CONFIG_SH_IS_ASH) += ash.o ash_ptr_hack.o shell_common.o
160 //kbuild:lib-$(CONFIG_BASH_IS_ASH) += ash.o ash_ptr_hack.o shell_common.o
161 //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o
164 * DEBUG=1 to compile in debugging ('set -o debug' turns on)
165 * DEBUG=2 to compile in and turn on debugging.
166 * When debugging is on ("set -o debug" was executed, or DEBUG=2),
167 * debugging info is written to ./trace, quit signal generates core dump.
170 /* Tweak debug output verbosity here */
174 #define DEBUG_INTONOFF 0
178 #define JOBS ENABLE_ASH_JOB_CONTROL
181 #include <sys/times.h>
182 #include <sys/utsname.h> /* for setting $HOSTNAME */
183 #include "busybox.h" /* for applet_names */
184 #if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS
185 # include "embedded_scripts.h"
187 # define NUM_SCRIPTS 0
190 /* So far, all bash compat is controlled by one config option */
191 /* Separate defines document which part of code implements what */
192 /* function keyword */
193 #define BASH_FUNCTION ENABLE_ASH_BASH_COMPAT
194 #define IF_BASH_FUNCTION IF_ASH_BASH_COMPAT
196 #define BASH_REDIR_OUTPUT ENABLE_ASH_BASH_COMPAT
197 #define IF_BASH_REDIR_OUTPUT IF_ASH_BASH_COMPAT
199 #define BASH_DOLLAR_SQUOTE ENABLE_ASH_BASH_COMPAT
200 #define IF_BASH_DOLLAR_SQUOTE IF_ASH_BASH_COMPAT
201 #define BASH_PATTERN_SUBST ENABLE_ASH_BASH_COMPAT
202 #define IF_BASH_PATTERN_SUBST IF_ASH_BASH_COMPAT
203 #define BASH_SUBSTR ENABLE_ASH_BASH_COMPAT
204 #define IF_BASH_SUBSTR IF_ASH_BASH_COMPAT
205 /* BASH_TEST2: [[ EXPR ]]
206 * Status of [[ support:
207 * We replace && and || with -a and -o
209 * singleword+noglob expansion:
210 * v='a b'; [[ $v = 'a b' ]]; echo 0:$?
211 * [[ /bin/n* ]]; echo 0:$?
212 * -a/-o are not AND/OR ops! (they are just strings)
213 * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc)
214 * = is glob match operator, not equality operator: STR = GLOB
215 * (in GLOB, quoting is significant on char-by-char basis: a*cd"*")
217 * add =~ regex match operator: STR =~ REGEX
219 #define BASH_TEST2 (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST)
220 #define BASH_SOURCE ENABLE_ASH_BASH_COMPAT
221 #define BASH_PIPEFAIL ENABLE_ASH_BASH_COMPAT
222 #define BASH_HOSTNAME_VAR ENABLE_ASH_BASH_COMPAT
223 #define BASH_EPOCH_VARS ENABLE_ASH_BASH_COMPAT
224 #define BASH_SHLVL_VAR ENABLE_ASH_BASH_COMPAT
225 #define BASH_XTRACEFD ENABLE_ASH_BASH_COMPAT
226 #define BASH_READ_D ENABLE_ASH_BASH_COMPAT
227 #define IF_BASH_READ_D IF_ASH_BASH_COMPAT
228 #define BASH_WAIT_N ENABLE_ASH_BASH_COMPAT
230 #if defined(__ANDROID_API__) && __ANDROID_API__ <= 24
231 /* Bionic at least up to version 24 has no glob() */
232 # undef ENABLE_ASH_INTERNAL_GLOB
233 # define ENABLE_ASH_INTERNAL_GLOB 1
236 #if !ENABLE_ASH_INTERNAL_GLOB && defined(__UCLIBC__)
237 # error uClibc glob() is buggy, use ASH_INTERNAL_GLOB.
238 # error The bug is: for "$PWD"/<pattern> ash will escape e.g. dashes in "$PWD"
239 # error with backslash, even ones which do not need to be: "/a-b" -> "/a\-b"
240 # error glob() should unbackslash them and match. uClibc does not unbackslash,
241 # error fails to match dirname, subsequently not expanding <pattern> in it.
243 // if (glob("/etc/polkit\\-1", 0, NULL, &pglob)) - this returns 0 on uclibc, no bug
244 // if (glob("/etc/polkit\\-1/*", 0, NULL, &pglob)) printf("uclibc bug!\n");
247 #if !ENABLE_ASH_INTERNAL_GLOB
252 #include "shell_common.h"
253 #if ENABLE_FEATURE_SH_MATH
256 typedef long arith_t;
257 # define ARITH_FMT "%ld"
259 #if ENABLE_ASH_RANDOM_SUPPORT
262 # define CLEAR_RANDOM_T(rnd) ((void)0)
265 #include "NUM_APPLETS.h"
267 /* STANDALONE does not make sense, and won't compile */
268 # undef CONFIG_FEATURE_SH_STANDALONE
269 # undef ENABLE_FEATURE_SH_STANDALONE
270 # undef IF_FEATURE_SH_STANDALONE
271 # undef IF_NOT_FEATURE_SH_STANDALONE
272 # define ENABLE_FEATURE_SH_STANDALONE 0
273 # define IF_FEATURE_SH_STANDALONE(...)
274 # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
277 #ifndef F_DUPFD_CLOEXEC
278 # define F_DUPFD_CLOEXEC F_DUPFD
284 # define PIPE_BUF 4096 /* amount of buffering in a pipe */
288 # error "Do not even bother, ash will not run on NOMMU machine"
291 /* We use a trick to have more optimized code (fewer pointer reloads):
292 * ash.c: extern struct globals *const ash_ptr_to_globals;
293 * ash_ptr_hack.c: struct globals *ash_ptr_to_globals;
294 * This way, compiler in ash.c knows the pointer can not change.
296 * However, this may break on weird arches or toolchains. In this case,
297 * set "-DBB_GLOBAL_CONST=''" in CONFIG_EXTRA_CFLAGS to disable
300 #ifndef BB_GLOBAL_CONST
301 # define BB_GLOBAL_CONST const
305 /* ============ Hash table sizes. Configurable. */
309 #define CMDTABLESIZE 31 /* should be prime */
312 /* ============ Shell options */
314 static const char *const optletters_optnames[] = {
318 /* The below allowed this invocation:
319 * ash -c 'set -i; echo $-; sleep 5; echo $-'
320 * to be ^C-ed and get to interactive ash prompt.
321 * bash does not support such "set -i".
322 * In our code, this is denoted by empty long name:
327 /* Ditto: bash has no "set -s" */
345 //bash 4.4.23 also has these opts (with these defaults):
353 //interactive-comments on
360 #define optletters(n) optletters_optnames[n][0]
361 #define optnames(n) (optletters_optnames[n] + 1)
363 enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
366 /* ============ Misc data */
368 #define msg_illnum "Illegal number: %s"
371 * We enclose jmp_buf in a structure so that we can declare pointers to
372 * jump locations. The global variable handler contains the location to
373 * jump to when an exception occurs, and the global variable exception_type
374 * contains a code identifying the exception. To implement nested
375 * exception handlers, the user should save the value of handler on entry
376 * to an inner scope, set handler to point to a jmploc structure for the
377 * inner scope, and restore handler on exit from the scope.
383 struct globals_misc {
384 uint8_t exitstatus; /* exit status of last command */
385 uint8_t back_exitstatus;/* exit status of backquoted command */
386 smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */
387 int savestatus; /* exit status of last command outside traps */
388 int rootpid; /* pid of main shell */
389 /* shell level: 0 for the main shell, 1 for its children, and so on */
391 #define rootshell (!shlvl)
394 char *minusc; /* argument to -c option */
396 char *curdir; // = nullstr; /* current working directory */
397 char *physdir; // = nullstr; /* physical working directory */
399 char *arg0; /* value of $0 */
401 struct jmploc *exception_handler;
403 volatile int suppress_int; /* counter */
404 volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */
405 volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */
406 volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */
407 smallint exception_type; /* kind of exception: */
408 #define EXINT 0 /* SIGINT received */
409 #define EXERROR 1 /* a generic error */
410 #define EXEND 3 /* exit the shell */
411 #define EXEXIT 4 /* exit the shell via exitcmd */
413 char nullstr[1]; /* zero length string */
416 #define eflag optlist[0]
417 #define fflag optlist[1]
418 #define Iflag optlist[2]
419 #define iflag optlist[3]
420 #define mflag optlist[4]
421 #define nflag optlist[5]
422 #define sflag optlist[6]
423 #define cflag optlist[7]
424 #define xflag optlist[8]
425 #define vflag optlist[9]
426 #define Cflag optlist[10]
427 #define aflag optlist[11]
428 #define bflag optlist[12]
429 #define uflag optlist[13]
430 #define viflag optlist[14]
432 # define pipefail optlist[15]
437 # define nolog optlist[15 + BASH_PIPEFAIL]
438 # define debug optlist[16 + BASH_PIPEFAIL]
441 /* trap handler commands */
443 * Sigmode records the current value of the signal handlers for the various
444 * modes. A value of zero means that the current handler is not known.
445 * S_HARD_IGN indicates that the signal was ignored on entry to the shell.
447 char sigmode[NSIG - 1];
448 #define S_DFL 1 /* default signal handling (SIG_DFL) */
449 #define S_CATCH 2 /* signal is caught */
450 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
451 #define S_HARD_IGN 4 /* signal is ignored permanently (it was SIG_IGN on entry to shell) */
453 /* indicates specified signal received */
454 uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */
455 uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */
457 char **trap_ptr; /* used only by "trap hack" */
459 /* Rarely referenced stuff */
460 #if ENABLE_ASH_RANDOM_SUPPORT
463 pid_t backgndpid; /* pid of last background process */
465 extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc;
466 #define G_misc (*ash_ptr_to_globals_misc)
467 #define exitstatus (G_misc.exitstatus )
468 #define back_exitstatus (G_misc.back_exitstatus )
469 #define job_warning (G_misc.job_warning)
470 #define savestatus (G_misc.savestatus )
471 #define rootpid (G_misc.rootpid )
472 #define shlvl (G_misc.shlvl )
473 #define errlinno (G_misc.errlinno )
474 #define minusc (G_misc.minusc )
475 #define curdir (G_misc.curdir )
476 #define physdir (G_misc.physdir )
477 #define arg0 (G_misc.arg0 )
478 #define exception_handler (G_misc.exception_handler)
479 #define exception_type (G_misc.exception_type )
480 #define suppress_int (G_misc.suppress_int )
481 #define pending_int (G_misc.pending_int )
482 #define got_sigchld (G_misc.got_sigchld )
483 #define pending_sig (G_misc.pending_sig )
484 #define nullstr (G_misc.nullstr )
485 #define optlist (G_misc.optlist )
486 #define sigmode (G_misc.sigmode )
487 #define gotsig (G_misc.gotsig )
488 #define may_have_traps (G_misc.may_have_traps )
489 #define trap (G_misc.trap )
490 #define trap_ptr (G_misc.trap_ptr )
491 #define random_gen (G_misc.random_gen )
492 #define backgndpid (G_misc.backgndpid )
493 #define INIT_G_misc() do { \
494 (*(struct globals_misc**)not_const_pp(&ash_ptr_to_globals_misc)) = xzalloc(sizeof(G_misc)); \
503 /* ============ DEBUG */
505 static void trace_printf(const char *fmt, ...);
506 static void trace_vprintf(const char *fmt, va_list va);
507 # define TRACE(param) trace_printf param
508 # define TRACEV(param) trace_vprintf param
509 # define close(fd) do { \
511 if (close(dfd) < 0) \
512 bb_error_msg("bug on %d: closing %d(0x%x)", \
513 __LINE__, dfd, dfd); \
516 # define TRACE(param)
517 # define TRACEV(param)
521 /* ============ Utility functions */
522 #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
523 #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
526 isdigit_str9(const char *str)
528 int maxlen = 9 + 1; /* max 9 digits: 999999999 */
529 while (--maxlen && isdigit(*str))
531 return (*str == '\0');
535 var_end(const char *var)
544 /* ============ Interrupts / exceptions */
546 static void exitshell(void) NORETURN;
549 * These macros allow the user to suspend the handling of interrupt signals
550 * over a period of time. This is similar to SIGHOLD or to sigblock, but
551 * much more efficient and portable. (But hacking the kernel is so much
552 * more fun than worrying about efficiency and portability. :-))
555 # define INT_OFF do { \
556 TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \
561 # define INT_OFF do { \
568 * Called to raise an exception. Since C doesn't include exceptions, we
569 * just do a longjmp to the exception handler. The type of exception is
570 * stored in the global variable "exception_type".
572 static void raise_exception(int) NORETURN;
574 raise_exception(int e)
577 if (exception_handler == NULL)
582 longjmp(exception_handler->loc, 1);
585 #define raise_exception(e) do { \
586 TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \
587 raise_exception(e); \
592 * Called when a SIGINT is received. (If the user specifies
593 * that SIGINT is to be trapped or ignored using the trap builtin, then
594 * this routine is not called.) Suppressint is nonzero when interrupts
595 * are held using the INT_OFF macro. (The test for iflag is just
596 * defensive programming.)
598 static void raise_interrupt(void) NORETURN;
600 raise_interrupt(void)
603 /* Signal is not automatically unmasked after it is raised,
604 * do it ourself - unmask all signals */
605 sigprocmask_allsigs(SIG_UNBLOCK);
606 /* pending_sig = 0; - now done in signal_handler() */
608 if (!(rootshell && iflag)) {
609 /* Kill ourself with SIGINT */
610 signal(SIGINT, SIG_DFL);
613 /* bash: ^C even on empty command line sets $? */
614 exitstatus = SIGINT + 128;
615 raise_exception(EXINT);
619 #define raise_interrupt() do { \
620 TRACE(("raising interrupt on line %d\n", __LINE__)); \
625 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
629 if (--suppress_int == 0 && pending_int) {
634 # define INT_ON do { \
635 TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \
639 # define INT_ON int_on()
641 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
649 #define FORCE_INT_ON force_int_on()
651 #define SAVE_INT(v) ((v) = suppress_int)
653 #define RESTORE_INT(v) do { \
655 suppress_int = (v); \
656 if (suppress_int == 0 && pending_int) \
661 /* ============ Stdout/stderr output */
664 outstr(const char *p, FILE *file)
672 flush_stdout_stderr(void)
679 /* Was called outcslow(c,FILE*), but c was always '\n' */
681 newline_and_flush(FILE *dest)
689 static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2)));
691 out1fmt(const char *fmt, ...)
698 r = vprintf(fmt, ap);
704 static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4)));
706 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
713 ret = vsnprintf(outbuf, length, fmt, ap);
716 return ret > (int)length ? length : ret;
720 out1str(const char *p)
726 out2str(const char *p)
729 flush_stdout_stderr();
733 /* ============ Parser structures */
735 /* control characters in argument strings */
736 #define CTL_FIRST CTLESC
737 #define CTLESC ((unsigned char)'\201') /* escape next character */
738 #define CTLVAR ((unsigned char)'\202') /* variable defn */
739 #define CTLENDVAR ((unsigned char)'\203')
740 #define CTLBACKQ ((unsigned char)'\204')
741 #define CTLARI ((unsigned char)'\206') /* arithmetic expression */
742 #define CTLENDARI ((unsigned char)'\207')
743 #define CTLQUOTEMARK ((unsigned char)'\210')
744 #define CTL_LAST CTLQUOTEMARK
746 /* variable substitution byte (follows CTLVAR) */
747 #define VSTYPE 0x0f /* type of variable substitution */
748 #define VSNUL 0x10 /* colon--treat the empty string as unset */
750 /* values of VSTYPE field */
751 #define VSNORMAL 0x1 /* normal variable: $var or ${var} */
752 #define VSMINUS 0x2 /* ${var-text} */
753 #define VSPLUS 0x3 /* ${var+text} */
754 #define VSQUESTION 0x4 /* ${var?message} */
755 #define VSASSIGN 0x5 /* ${var=text} */
756 #define VSTRIMRIGHT 0x6 /* ${var%pattern} */
757 #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */
758 #define VSTRIMLEFT 0x8 /* ${var#pattern} */
759 #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */
760 #define VSLENGTH 0xa /* ${#var} */
762 #define VSSUBSTR 0xc /* ${var:position:length} */
764 #if BASH_PATTERN_SUBST
765 #define VSREPLACE 0xd /* ${var/pattern/replacement} */
766 #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */
769 static const char dolatstr[] ALIGN1 = {
770 CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0'
772 #define DOLATSTRLEN 6
791 #if BASH_REDIR_OUTPUT
808 smallint type; /* Nxxxx */
812 union node *redirect;
817 smallint pipe_backgnd;
818 struct nodelist *cmdlist;
825 union node *redirect;
838 union node *elsepart;
874 struct nodelist *backquote;
877 /* nfile and ndup layout must match!
878 * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight
879 * that it is actually NTO2 (>&file), and change its type.
896 char *_unused_expfname;
915 struct nredir nredir;
916 struct nbinary nbinary;
920 struct nclist nclist;
921 struct ndefun ndefun;
930 * NODE_EOF is returned by parsecmd when it encounters an end of file.
931 * It must be distinct from NULL.
933 #define NODE_EOF ((union node *) -1L)
936 struct nodelist *next;
949 freefunc(struct funcnode *f)
951 if (f && --f->count < 0)
956 /* ============ Debugging output */
960 static FILE *tracefile;
963 trace_printf(const char *fmt, ...)
970 fprintf(tracefile, "%u ", (int) time(NULL));
972 fprintf(tracefile, "[%u] ", (int) getpid());
974 fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int);
976 vfprintf(tracefile, fmt, va);
981 trace_vprintf(const char *fmt, va_list va)
985 vfprintf(tracefile, fmt, va);
986 fprintf(tracefile, "\n");
990 trace_puts(const char *s)
998 trace_puts_quoted(char *s)
1005 putc('"', tracefile);
1006 for (p = s; *p; p++) {
1007 switch ((unsigned char)*p) {
1008 case '\n': c = 'n'; goto backslash;
1009 case '\t': c = 't'; goto backslash;
1010 case '\r': c = 'r'; goto backslash;
1011 case '\"': c = '\"'; goto backslash;
1012 case '\\': c = '\\'; goto backslash;
1013 case CTLESC: c = 'e'; goto backslash;
1014 case CTLVAR: c = 'v'; goto backslash;
1015 case CTLBACKQ: c = 'q'; goto backslash;
1017 putc('\\', tracefile);
1021 if (*p >= ' ' && *p <= '~')
1022 putc(*p, tracefile);
1024 putc('\\', tracefile);
1025 putc((*p >> 6) & 03, tracefile);
1026 putc((*p >> 3) & 07, tracefile);
1027 putc(*p & 07, tracefile);
1032 putc('"', tracefile);
1036 trace_puts_args(char **ap)
1043 trace_puts_quoted(*ap);
1045 putc('\n', tracefile);
1048 putc(' ', tracefile);
1063 /* leave open because libedit might be using it */
1066 strcpy(s, "./trace");
1068 if (!freopen(s, "a", tracefile)) {
1069 fprintf(stderr, "Can't re-open %s\n", s);
1074 tracefile = fopen(s, "a");
1075 if (tracefile == NULL) {
1076 fprintf(stderr, "Can't open %s\n", s);
1082 flags = fcntl(fileno(tracefile), F_GETFL);
1084 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
1086 setlinebuf(tracefile);
1087 fputs("\nTracing started.\n", tracefile);
1091 indent(int amount, char *pfx, FILE *fp)
1095 for (i = 0; i < amount; i++) {
1096 if (pfx && i == amount - 1)
1102 /* little circular references here... */
1103 static void shtree(union node *n, int ind, char *pfx, FILE *fp);
1106 sharg(union node *arg, FILE *fp)
1109 struct nodelist *bqlist;
1110 unsigned char subtype;
1112 if (arg->type != NARG) {
1113 out1fmt("<node type %d>\n", arg->type);
1116 bqlist = arg->narg.backquote;
1117 for (p = arg->narg.text; *p; p++) {
1118 switch ((unsigned char)*p) {
1127 if (subtype == VSLENGTH)
1135 if (subtype & VSNUL)
1138 switch (subtype & VSTYPE) {
1164 case VSTRIMRIGHTMAX:
1171 out1fmt("<subtype %d>", subtype);
1180 shtree(bqlist->n, -1, NULL, fp);
1191 shcmd(union node *cmd, FILE *fp)
1199 for (np = cmd->ncmd.args; np; np = np->narg.next) {
1205 for (np = cmd->ncmd.redirect; np; np = np->nfile.next) {
1209 switch (np->nfile.type) {
1210 case NTO: s = ">>"+1; dftfd = 1; break;
1211 case NCLOBBER: s = ">|"; dftfd = 1; break;
1212 case NAPPEND: s = ">>"; dftfd = 1; break;
1213 #if BASH_REDIR_OUTPUT
1216 case NTOFD: s = ">&"; dftfd = 1; break;
1217 case NFROM: s = "<"; break;
1218 case NFROMFD: s = "<&"; break;
1219 case NFROMTO: s = "<>"; break;
1220 default: s = "*error*"; break;
1222 if (np->nfile.fd != dftfd)
1223 fprintf(fp, "%d", np->nfile.fd);
1225 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1226 fprintf(fp, "%d", np->ndup.dupfd);
1228 sharg(np->nfile.fname, fp);
1235 shtree(union node *n, int ind, char *pfx, FILE *fp)
1237 struct nodelist *lp;
1243 indent(ind, pfx, fp);
1245 if (n == NODE_EOF) {
1260 shtree(n->nbinary.ch1, ind, NULL, fp);
1263 shtree(n->nbinary.ch2, ind, NULL, fp);
1271 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
1272 shtree(lp->n, 0, NULL, fp);
1276 if (n->npipe.pipe_backgnd)
1282 fprintf(fp, "<node type %d>", n->type);
1290 showtree(union node *n)
1292 trace_puts("showtree called\n");
1293 shtree(n, 1, NULL, stderr);
1299 /* ============ Parser data */
1302 * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up.
1305 struct strlist *next;
1312 struct strpush *prev; /* preceding string on stack */
1314 int prev_left_in_line;
1315 #if ENABLE_ASH_ALIAS
1316 struct alias *ap; /* if push was associated with an alias */
1318 char *string; /* remember the string since it may change */
1320 /* Remember last two characters for pungetc. */
1323 /* Number of outstanding calls to pungetc. */
1328 * The parsefile structure pointed to by the global variable parsefile
1329 * contains information about the current file being read.
1332 struct parsefile *prev; /* preceding file on stack */
1333 int linno; /* current line */
1334 int pf_fd; /* file descriptor (or -1 if string) */
1335 int left_in_line; /* number of chars left in this line */
1336 int left_in_buffer; /* number of chars left in this buffer past the line */
1337 char *next_to_pgetc; /* next char in buffer */
1338 char *buf; /* input buffer */
1339 struct strpush *strpush; /* for pushing strings at this level */
1340 struct strpush basestrpush; /* so pushing one is fast */
1342 /* Remember last two characters for pungetc. */
1345 /* Number of outstanding calls to pungetc. */
1349 static struct parsefile basepf; /* top level input file */
1350 static struct parsefile *g_parsefile = &basepf; /* current input file */
1351 static char *commandname; /* currently executing command */
1354 /* ============ Message printing */
1357 ash_vmsg(const char *msg, va_list ap)
1359 fprintf(stderr, "%s: ", arg0);
1361 if (strcmp(arg0, commandname))
1362 fprintf(stderr, "%s: ", commandname);
1363 if (!iflag || g_parsefile->pf_fd > 0)
1364 fprintf(stderr, "line %d: ", errlinno);
1366 vfprintf(stderr, msg, ap);
1367 newline_and_flush(stderr);
1371 * Exverror is called to raise the error exception. If the second argument
1372 * is not NULL then error prints an error message using printf style
1373 * formatting. It then raises the error exception.
1375 static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN;
1377 ash_vmsg_and_raise(int cond, const char *msg, va_list ap)
1381 TRACE(("ash_vmsg_and_raise(%d):", cond));
1384 TRACE(("ash_vmsg_and_raise(%d):NULL\n", cond));
1389 flush_stdout_stderr();
1390 raise_exception(cond);
1394 static void ash_msg_and_raise_error(const char *, ...) NORETURN;
1396 ash_msg_and_raise_error(const char *msg, ...)
1403 ash_vmsg_and_raise(EXERROR, msg, ap);
1409 * 'fmt' must be a string literal.
1411 #define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": "STRERROR_FMT, ##__VA_ARGS__ STRERROR_ERRNO)
1413 static void raise_error_syntax(const char *) NORETURN;
1415 raise_error_syntax(const char *msg)
1417 errlinno = g_parsefile->linno;
1418 ash_msg_and_raise_error("syntax error: %s", msg);
1422 static void ash_msg_and_raise(int, const char *, ...) NORETURN;
1424 ash_msg_and_raise(int cond, const char *msg, ...)
1429 ash_vmsg_and_raise(cond, msg, ap);
1435 * error/warning routines for external builtins
1438 ash_msg(const char *fmt, ...)
1448 * Return a string describing an error. The returned string may be a
1449 * pointer to a static buffer that will be overwritten on the next call.
1450 * Action describes the operation that got the error.
1453 errmsg(int e, const char *em)
1455 if (e == ENOENT || e == ENOTDIR) {
1462 /* ============ Memory allocation */
1465 /* I consider these wrappers nearly useless:
1466 * ok, they return you to nearest exception handler, but
1467 * how much memory do you leak in the process, making
1468 * memory starvation worse?
1471 ckrealloc(void * p, size_t nbytes)
1473 p = realloc(p, nbytes);
1475 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1480 ckmalloc(size_t nbytes)
1482 return ckrealloc(NULL, nbytes);
1486 ckzalloc(size_t nbytes)
1488 return memset(ckmalloc(nbytes), 0, nbytes);
1492 ckstrdup(const char *s)
1494 char *p = strdup(s);
1496 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1500 /* Using bbox equivalents. They exit if out of memory */
1501 # define ckrealloc xrealloc
1502 # define ckmalloc xmalloc
1503 # define ckzalloc xzalloc
1504 # define ckstrdup xstrdup
1508 * It appears that grabstackstr() will barf with such alignments
1509 * because stalloc() will return a string allocated in a new stackblock.
1511 #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE)
1513 /* Most machines require the value returned from malloc to be aligned
1514 * in some way. The following macro will get this right
1515 * on many machines. */
1516 SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,
1517 /* Minimum size of a block */
1518 MINSIZE = SHELL_ALIGN(504),
1521 struct stack_block {
1522 struct stack_block *prev;
1523 char space[MINSIZE];
1527 struct stack_block *stackp;
1533 struct globals_memstack {
1534 struct stack_block *g_stackp; // = &stackbase;
1535 char *g_stacknxt; // = stackbase.space;
1536 char *sstrend; // = stackbase.space + MINSIZE;
1537 size_t g_stacknleft; // = MINSIZE;
1538 struct stack_block stackbase;
1540 extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack;
1541 #define G_memstack (*ash_ptr_to_globals_memstack)
1542 #define g_stackp (G_memstack.g_stackp )
1543 #define g_stacknxt (G_memstack.g_stacknxt )
1544 #define sstrend (G_memstack.sstrend )
1545 #define g_stacknleft (G_memstack.g_stacknleft)
1546 #define stackbase (G_memstack.stackbase )
1547 #define INIT_G_memstack() do { \
1548 (*(struct globals_memstack**)not_const_pp(&ash_ptr_to_globals_memstack)) = xzalloc(sizeof(G_memstack)); \
1550 g_stackp = &stackbase; \
1551 g_stacknxt = stackbase.space; \
1552 g_stacknleft = MINSIZE; \
1553 sstrend = stackbase.space + MINSIZE; \
1557 #define stackblock() ((void *)g_stacknxt)
1558 #define stackblocksize() g_stacknleft
1561 * Parse trees for commands are allocated in lifo order, so we use a stack
1562 * to make this more efficient, and also to avoid all sorts of exception
1563 * handling code to handle interrupts in the middle of a parse.
1565 * The size 504 was chosen because the Ultrix malloc handles that size
1569 stalloc(size_t nbytes)
1574 aligned = SHELL_ALIGN(nbytes);
1575 if (aligned > g_stacknleft) {
1578 struct stack_block *sp;
1580 blocksize = aligned;
1581 if (blocksize < MINSIZE)
1582 blocksize = MINSIZE;
1583 len = sizeof(struct stack_block) - MINSIZE + blocksize;
1584 if (len < blocksize)
1585 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1588 sp->prev = g_stackp;
1589 g_stacknxt = sp->space;
1590 g_stacknleft = blocksize;
1591 sstrend = g_stacknxt + blocksize;
1596 g_stacknxt += aligned;
1597 g_stacknleft -= aligned;
1602 stzalloc(size_t nbytes)
1604 return memset(stalloc(nbytes), 0, nbytes);
1611 if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) {
1612 write(STDERR_FILENO, "stunalloc\n", 10);
1616 g_stacknleft += g_stacknxt - (char *)p;
1621 * Like strdup but works with the ash stack.
1624 sstrdup(const char *p)
1626 size_t len = strlen(p) + 1;
1627 return memcpy(stalloc(len), p, len);
1630 static ALWAYS_INLINE void
1631 grabstackblock(size_t len)
1637 pushstackmark(struct stackmark *mark, size_t len)
1639 mark->stackp = g_stackp;
1640 mark->stacknxt = g_stacknxt;
1641 mark->stacknleft = g_stacknleft;
1642 grabstackblock(len);
1646 setstackmark(struct stackmark *mark)
1648 pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase);
1652 popstackmark(struct stackmark *mark)
1654 struct stack_block *sp;
1660 while (g_stackp != mark->stackp) {
1662 g_stackp = sp->prev;
1665 g_stacknxt = mark->stacknxt;
1666 g_stacknleft = mark->stacknleft;
1667 sstrend = mark->stacknxt + mark->stacknleft;
1672 * When the parser reads in a string, it wants to stick the string on the
1673 * stack and only adjust the stack pointer when it knows how big the
1674 * string is. Stackblock (defined in stack.h) returns a pointer to a block
1675 * of space on top of the stack and stackblocklen returns the length of
1676 * this block. Growstackblock will grow this space by at least one byte,
1677 * possibly moving it (like realloc). Grabstackblock actually allocates the
1678 * part of the block that has been used.
1681 growstackblock(size_t min)
1685 newlen = g_stacknleft * 2;
1686 if (newlen < g_stacknleft)
1687 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1688 min = SHELL_ALIGN(min | 128);
1692 if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) {
1693 struct stack_block *sp;
1694 struct stack_block *prevstackp;
1699 prevstackp = sp->prev;
1700 grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
1701 sp = ckrealloc(sp, grosslen);
1702 sp->prev = prevstackp;
1704 g_stacknxt = sp->space;
1705 g_stacknleft = newlen;
1706 sstrend = sp->space + newlen;
1709 char *oldspace = g_stacknxt;
1710 size_t oldlen = g_stacknleft;
1711 char *p = stalloc(newlen);
1713 /* free the space we just allocated */
1714 g_stacknxt = memcpy(p, oldspace, oldlen);
1715 g_stacknleft += newlen;
1720 * The following routines are somewhat easier to use than the above.
1721 * The user declares a variable of type STACKSTR, which may be declared
1722 * to be a register. The macro STARTSTACKSTR initializes things. Then
1723 * the user uses the macro STPUTC to add characters to the string. In
1724 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
1725 * grown as necessary. When the user is done, she can just leave the
1726 * string there and refer to it using stackblock(). Or she can allocate
1727 * the space for it using grabstackstr(). If it is necessary to allow
1728 * someone else to use the stack temporarily and then continue to grow
1729 * the string, the user should use grabstack to allocate the space, and
1730 * then call ungrabstr(p) to return to the previous mode of operation.
1732 * USTPUTC is like STPUTC except that it doesn't check for overflow.
1733 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
1734 * is space for at least one character.
1739 size_t len = stackblocksize();
1741 return (char *)stackblock() + len;
1745 growstackto(size_t len)
1747 if (stackblocksize() < len)
1748 growstackblock(len);
1749 return stackblock();
1753 * Called from CHECKSTRSPACE.
1756 makestrspace(size_t newlen, char *p)
1758 size_t len = p - g_stacknxt;
1760 return growstackto(len + newlen) + len;
1764 stack_nputstr(const char *s, size_t n, char *p)
1766 p = makestrspace(n, p);
1767 p = (char *)mempcpy(p, s, n);
1772 stack_putstr(const char *s, char *p)
1774 return stack_nputstr(s, strlen(s), p);
1778 _STPUTC(int c, char *p)
1786 #define STARTSTACKSTR(p) ((p) = stackblock())
1787 #define STPUTC(c, p) ((p) = _STPUTC((c), (p)))
1788 #define CHECKSTRSPACE(n, p) do { \
1791 size_t m = sstrend - q; \
1793 (p) = makestrspace(l, q); \
1795 #define USTPUTC(c, p) (*(p)++ = (c))
1796 #define STACKSTRNUL(p) do { \
1797 if ((p) == sstrend) \
1798 (p) = growstackstr(); \
1801 #define STUNPUTC(p) (--(p))
1802 #define STTOPC(p) ((p)[-1])
1803 #define STADJUST(amount, p) ((p) += (amount))
1805 #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock())
1806 #define ungrabstackstr(s, p) stunalloc(s)
1807 #define stackstrend() ((void *)sstrend)
1810 /* ============ String helpers */
1813 * prefix -- see if pfx is a prefix of string.
1816 prefix(const char *string, const char *pfx)
1819 if (*pfx++ != *string++)
1822 return (char *) string;
1826 * Check for a valid number. This should be elsewhere.
1829 is_number(const char *p)
1834 } while (*++p != '\0');
1839 * Convert a string of digits to an integer, printing an error message on
1843 number(const char *s)
1846 ash_msg_and_raise_error(msg_illnum, s);
1851 * Produce a single quoted string suitable as input to the shell.
1852 * The return string is allocated on the stack.
1855 single_quote(const char *s)
1865 len = strchrnul(s, '\'') - s;
1867 q = p = makestrspace(len + 3, p);
1870 q = (char *)mempcpy(q, s, len);
1879 do len++; while (*++s == '\'');
1881 q = p = makestrspace(len + 3, p);
1884 q = (char *)mempcpy(q, s - len, len);
1892 return stackblock();
1896 * Produce a possibly single quoted string suitable as input to the shell.
1897 * If quoting was done, the return string is allocated on the stack,
1898 * otherwise a pointer to the original string is returned.
1901 maybe_single_quote(const char *s)
1906 /* Assuming ACSII */
1907 /* quote ctrl_chars space !"#$%&'()* */
1911 if (*p >= ';' && *p <= '?')
1920 /* quote {|}~ DEL and high bytes */
1923 /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */
1924 /* TODO: maybe avoid quoting % */
1930 return single_quote(s);
1934 /* ============ nextopt */
1936 static char **argptr; /* argument list for builtin commands */
1937 static char *optionarg; /* set by nextopt (like getopt) */
1938 static char *optptr; /* used by nextopt */
1941 * XXX - should get rid of. Have all builtins use getopt(3).
1942 * The library getopt must have the BSD extension static variable
1943 * "optreset", otherwise it can't be used within the shell safely.
1945 * Standard option processing (a la getopt) for builtin routines.
1946 * The only argument that is passed to nextopt is the option string;
1947 * the other arguments are unnecessary. It returns the character,
1948 * or '\0' on end of input.
1951 nextopt(const char *optstring)
1958 if (p == NULL || *p == '\0') {
1959 /* We ate entire "-param", take next one */
1965 if (*++p == '\0') /* just "-" ? */
1968 if (LONE_DASH(p)) /* "--" ? */
1970 /* p => next "-param" */
1972 /* p => some option char in the middle of a "-param" */
1974 for (q = optstring; *q != c;) {
1976 ash_msg_and_raise_error("illegal option -%c", c);
1984 ash_msg_and_raise_error("no arg for -%c option", c);
1994 /* ============ Shell variables */
1997 int nparam; /* # of positional parameters (without $0) */
1998 #if ENABLE_ASH_GETOPTS
1999 int optind; /* next parameter to be processed by getopts */
2000 int optoff; /* used by getopts */
2002 unsigned char malloced; /* if parameter list dynamically allocated */
2003 char **p; /* parameter list */
2007 * Free the list of positional parameters.
2010 freeparam(volatile struct shparam *param)
2012 if (param->malloced) {
2014 ap = ap1 = param->p;
2021 #if ENABLE_ASH_GETOPTS
2022 static void FAST_FUNC getoptsreset(const char *value);
2026 struct var *next; /* next entry in hash list */
2027 int flags; /* flags are defined above */
2028 const char *var_text; /* name=value */
2029 void (*var_func)(const char *) FAST_FUNC; /* function to be called when */
2030 /* the variable gets set/unset */
2034 struct localvar *next; /* next local variable in list */
2035 struct var *vp; /* the variable that was made local */
2036 int flags; /* saved flags */
2037 const char *text; /* saved text */
2041 #define VEXPORT 0x01 /* variable is exported */
2042 #define VREADONLY 0x02 /* variable cannot be modified */
2043 #define VSTRFIXED 0x04 /* variable struct is statically allocated */
2044 #define VTEXTFIXED 0x08 /* text is statically allocated */
2045 #define VSTACK 0x10 /* text is allocated on the stack */
2046 #define VUNSET 0x20 /* the variable is not set */
2047 #define VNOFUNC 0x40 /* don't call the callback function */
2048 #define VNOSET 0x80 /* do not set variable - just readonly test */
2049 #define VNOSAVE 0x100 /* when text is on the heap before setvareq */
2050 #if ENABLE_ASH_RANDOM_SUPPORT
2051 # define VDYNAMIC 0x200 /* dynamic variable */
2057 /* Need to be before varinit_data[] */
2058 #if ENABLE_LOCALE_SUPPORT
2059 static void FAST_FUNC
2060 change_lc_all(const char *value)
2062 if (value && *value != '\0')
2063 setlocale(LC_ALL, value);
2065 static void FAST_FUNC
2066 change_lc_ctype(const char *value)
2068 if (value && *value != '\0')
2069 setlocale(LC_CTYPE, value);
2073 static void chkmail(void);
2074 static void changemail(const char *var_value) FAST_FUNC;
2076 # define chkmail() ((void)0)
2078 static void changepath(const char *) FAST_FUNC;
2079 #if ENABLE_ASH_RANDOM_SUPPORT
2080 static void change_random(const char *) FAST_FUNC;
2083 static void change_seconds(const char *) FAST_FUNC;
2084 static void change_realtime(const char *) FAST_FUNC;
2087 static const struct {
2089 const char *var_text;
2090 void (*var_func)(const char *) FAST_FUNC;
2091 } varinit_data[] = {
2093 * Note: VEXPORT would not work correctly here for NOFORK applets:
2094 * some environment strings may be constant.
2096 { VSTRFIXED|VTEXTFIXED , defifsvar , NULL },
2098 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail },
2099 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail },
2101 { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath },
2102 { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL },
2103 { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL },
2104 { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL },
2105 #if ENABLE_ASH_GETOPTS
2106 { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset },
2108 { VSTRFIXED|VTEXTFIXED , NULL /* inited to linenovar */, NULL },
2109 #if ENABLE_ASH_RANDOM_SUPPORT
2110 { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random },
2113 { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHSECONDS", change_seconds },
2114 { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "EPOCHREALTIME", change_realtime },
2116 #if ENABLE_LOCALE_SUPPORT
2117 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all },
2118 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype },
2120 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
2121 { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL },
2127 struct globals_var {
2128 struct shparam shellparam; /* $@ current positional parameters */
2129 struct redirtab *redirlist;
2130 int preverrout_fd; /* stderr fd: usually 2, unless redirect moved it */
2131 struct var *vartab[VTABSIZE];
2132 struct var varinit[ARRAY_SIZE(varinit_data)];
2134 char linenovar[sizeof("LINENO=") + sizeof(int)*3];
2136 extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var;
2137 #define G_var (*ash_ptr_to_globals_var)
2138 #define shellparam (G_var.shellparam )
2139 //#define redirlist (G_var.redirlist )
2140 #define preverrout_fd (G_var.preverrout_fd)
2141 #define vartab (G_var.vartab )
2142 #define varinit (G_var.varinit )
2143 #define lineno (G_var.lineno )
2144 #define linenovar (G_var.linenovar )
2145 #define vifs varinit[0]
2147 # define vmail varinit[1]
2148 # define vmpath varinit[2]
2150 #define VAR_OFFSET1 (ENABLE_ASH_MAIL*2)
2151 #define vpath varinit[VAR_OFFSET1 + 1]
2152 #define vps1 varinit[VAR_OFFSET1 + 2]
2153 #define vps2 varinit[VAR_OFFSET1 + 3]
2154 #define vps4 varinit[VAR_OFFSET1 + 4]
2155 #if ENABLE_ASH_GETOPTS
2156 # define voptind varinit[VAR_OFFSET1 + 5]
2158 #define VAR_OFFSET2 (VAR_OFFSET1 + ENABLE_ASH_GETOPTS)
2159 #define vlineno varinit[VAR_OFFSET2 + 5]
2160 #if ENABLE_ASH_RANDOM_SUPPORT
2161 # define vrandom varinit[VAR_OFFSET2 + 6]
2163 #define VAR_OFFSET3 (VAR_OFFSET2 + ENABLE_ASH_RANDOM_SUPPORT)
2165 # define vepochs varinit[VAR_OFFSET3 + 6]
2166 # define vepochr varinit[VAR_OFFSET3 + 7]
2168 #define INIT_G_var() do { \
2170 (*(struct globals_var**)not_const_pp(&ash_ptr_to_globals_var)) = xzalloc(sizeof(G_var)); \
2172 for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
2173 varinit[i].flags = varinit_data[i].flags; \
2174 varinit[i].var_text = varinit_data[i].var_text; \
2175 varinit[i].var_func = varinit_data[i].var_func; \
2177 strcpy(linenovar, "LINENO="); \
2178 vlineno.var_text = linenovar; \
2182 * The following macros access the values of the above variables.
2183 * They have to skip over the name. They return the null string
2184 * for unset variables.
2186 #define ifsval() (vifs.var_text + 4)
2187 #define ifsset() ((vifs.flags & VUNSET) == 0)
2189 # define mailval() (vmail.var_text + 5)
2190 # define mpathval() (vmpath.var_text + 9)
2191 # define mpathset() ((vmpath.flags & VUNSET) == 0)
2193 #define pathval() (vpath.var_text + 5)
2194 #define ps1val() (vps1.var_text + 4)
2195 #define ps2val() (vps2.var_text + 4)
2196 #define ps4val() (vps4.var_text + 4)
2197 #if ENABLE_ASH_GETOPTS
2198 # define optindval() (voptind.var_text + 7)
2201 #if ENABLE_ASH_GETOPTS
2202 static void FAST_FUNC
2203 getoptsreset(const char *value)
2205 shellparam.optind = 1;
2206 if (is_number(value))
2207 shellparam.optind = number(value) ?: 1;
2208 shellparam.optoff = -1;
2213 * Compares two strings up to the first = or '\0'. The first
2214 * string must be terminated by '='; the second may be terminated by
2215 * either '=' or '\0'.
2218 varcmp(const char *p, const char *q)
2222 while ((c = *p) == (d = *q)) {
2223 if (c == '\0' || c == '=')
2237 * Find the appropriate entry in the hash table from the name.
2239 static struct var **
2240 hashvar(const char *p)
2244 hashval = ((unsigned char) *p) << 4;
2245 while (*p && *p != '=')
2246 hashval += (unsigned char) *p++;
2247 return &vartab[hashval % VTABSIZE];
2251 vpcmp(const void *a, const void *b)
2253 return varcmp(*(const char **)a, *(const char **)b);
2257 * This routine initializes the builtin variables.
2267 * PS1 depends on uid
2269 #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT
2270 vps1.var_text = "PS1=\\w \\$ ";
2273 vps1.var_text = "PS1=# ";
2276 end = vp + ARRAY_SIZE(varinit);
2278 vpp = hashvar(vp->var_text);
2281 } while (++vp < end);
2284 static struct var **
2285 findvar(struct var **vpp, const char *name)
2287 for (; *vpp; vpp = &(*vpp)->next) {
2288 if (varcmp((*vpp)->var_text, name) == 0) {
2296 * Find the value of a variable. Returns NULL if not set.
2298 static const char* FAST_FUNC
2299 lookupvar(const char *name)
2303 v = *findvar(hashvar(name), name);
2305 #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS
2307 * Dynamic variables are implemented roughly the same way they are
2308 * in bash. Namely, they're "special" so long as they aren't unset.
2309 * As soon as they're unset, they're no longer dynamic, and dynamic
2310 * lookup will no longer happen at that point. -- PFM.
2312 if (v->flags & VDYNAMIC)
2315 if (!(v->flags & VUNSET)) {
2316 if (v == &vlineno && v->var_text == linenovar) {
2317 fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno);
2319 return var_end(v->var_text);
2325 #if ENABLE_UNICODE_SUPPORT
2327 reinit_unicode_for_ash(void)
2329 /* Unicode support should be activated even if LANG is set
2330 * _during_ shell execution, not only if it was set when
2331 * shell was started. Therefore, re-check LANG every time:
2333 if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
2334 || ENABLE_UNICODE_USING_LOCALE
2336 const char *s = lookupvar("LC_ALL");
2337 if (!s) s = lookupvar("LC_CTYPE");
2338 if (!s) s = lookupvar("LANG");
2343 # define reinit_unicode_for_ash() ((void)0)
2347 * Search the environment of a builtin command.
2349 static ALWAYS_INLINE const char *
2350 bltinlookup(const char *name)
2352 return lookupvar(name);
2356 * Same as setvar except that the variable and value are passed in
2357 * the first argument as name=value. Since the first argument will
2358 * be actually stored in the table, it should not be a string that
2360 * Called with interrupts off.
2363 setvareq(char *s, int flags)
2365 struct var *vp, **vpp;
2368 flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
2369 vpp = findvar(vpp, s);
2372 if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) {
2375 if (flags & VNOSAVE)
2379 ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n);
2385 if (vp->var_func && !(flags & VNOFUNC))
2386 vp->var_func(var_end(s));
2388 if (!(vp->flags & (VTEXTFIXED|VSTACK)))
2389 free((char*)vp->var_text);
2391 if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) {
2395 if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE)
2400 flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET);
2401 #if ENABLE_ASH_RANDOM_SUPPORT || BASH_EPOCH_VARS
2406 /* variable s is not found */
2409 if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
2411 vp = ckzalloc(sizeof(*vp));
2413 /*vp->func = NULL; - ckzalloc did it */
2416 if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
2426 * Set the value of a variable. The flags argument is ored with the
2427 * flags of the variable. If val is NULL, the variable is unset.
2430 setvar(const char *name, const char *val, int flags)
2439 q = endofname(name);
2440 p = strchrnul(q, '=');
2442 if (!namelen || p != q)
2443 ash_msg_and_raise_error("%.*s: bad variable name", namelen, name);
2448 vallen = strlen(val);
2452 nameeq = ckzalloc(namelen + vallen + 2);
2453 p = mempcpy(nameeq, name, namelen);
2456 memcpy(p, val, vallen);
2458 vp = setvareq(nameeq, flags | VNOSAVE);
2464 static void FAST_FUNC
2465 setvar0(const char *name, const char *val)
2467 setvar(name, val, 0);
2471 * Unset the specified variable.
2474 unsetvar(const char *s)
2480 * Generate a list of variables satisfying the given conditions.
2482 #if !ENABLE_FEATURE_SH_NOFORK
2483 # define listvars(on, off, lp, end) listvars(on, off, end)
2486 listvars(int on, int off, struct strlist *lp, char ***end)
2497 for (vp = *vpp; vp; vp = vp->next) {
2498 if ((vp->flags & mask) == on) {
2499 #if ENABLE_FEATURE_SH_NOFORK
2500 /* If variable with the same name is both
2501 * exported and temporarily set for a command:
2504 * then "ZVAR=6" will be both in vartab and
2505 * lp lists. Do not pass it twice to printenv.
2507 struct strlist *lp1 = lp;
2509 if (strcmp(lp1->text, vp->var_text) == 0)
2514 if (ep == stackstrend())
2515 ep = growstackstr();
2516 *ep++ = (char*)vp->var_text;
2517 #if ENABLE_FEATURE_SH_NOFORK
2522 } while (++vpp < vartab + VTABSIZE);
2524 #if ENABLE_FEATURE_SH_NOFORK
2526 if (ep == stackstrend())
2527 ep = growstackstr();
2533 if (ep == stackstrend())
2534 ep = growstackstr();
2538 return grabstackstr(ep);
2542 /* ============ Path search helper */
2544 legal_pathopt(const char *opt, const char *term, int magic)
2552 opt = prefix(opt, "builtin") ?: prefix(opt, "func");
2556 opt += strcspn(opt, term);
2560 if (opt && *opt == '%')
2567 * The variable path (passed by reference) should be set to the start
2568 * of the path before the first call; padvance will update
2569 * this value as it proceeds. Successive calls to padvance will return
2570 * the possible path expansions in sequence. If an option (indicated by
2571 * a percent sign) appears in the path entry then the global variable
2572 * pathopt will be set to point to it; otherwise pathopt will be set to
2575 * If magic is 0 then pathopt recognition will be disabled. If magic is
2576 * 1 we shall recognise %builtin/%func. Otherwise we shall accept any
2579 static const char *pathopt; /* set by padvance */
2582 padvance_magic(const char **path, const char *name, int magic)
2584 const char *term = "%:";
2585 const char *lpathopt;
2598 if (*start == '%' && (p = legal_pathopt(start + 1, term, magic))) {
2599 lpathopt = start + 1;
2604 len = strcspn(start, term);
2608 size_t extra = strchrnul(p, ':') - p;
2610 if (legal_pathopt(p + 1, term, magic))
2619 *path = *p == ':' ? p + 1 : NULL;
2621 /* "2" is for '/' and '\0' */
2622 qlen = len + strlen(name) + 2;
2623 q = growstackto(qlen);
2626 q = mempcpy(q, start, len);
2635 padvance(const char **path, const char *name)
2637 return padvance_magic(path, name, 1);
2641 /* ============ Prompt */
2643 static smallint doprompt; /* if set, prompt the user */
2644 static smallint needprompt; /* true if interactive and at start of line */
2646 #if ENABLE_FEATURE_EDITING
2647 static line_input_t *line_input_state;
2648 static const char *cmdedit_prompt;
2650 putprompt(const char *s)
2652 if (ENABLE_ASH_EXPAND_PRMT) {
2653 free((char*)cmdedit_prompt);
2654 cmdedit_prompt = ckstrdup(s);
2661 putprompt(const char *s)
2667 /* expandstr() needs parsing machinery, so it is far away ahead... */
2668 static const char *expandstr(const char *ps, int syntax_type);
2669 /* Values for syntax param */
2670 #define BASESYNTAX 0 /* not in quotes */
2671 #define DQSYNTAX 1 /* in double quotes */
2672 #define SQSYNTAX 2 /* in single quotes */
2673 #define ARISYNTAX 3 /* in arithmetic */
2674 #if ENABLE_ASH_EXPAND_PRMT
2675 # define PSSYNTAX 4 /* prompt. never passed to SIT() */
2677 /* PSSYNTAX expansion is identical to DQSYNTAX, except keeping '\$' as '\$' */
2680 * called by editline -- any expansions to the prompt should be added here.
2683 setprompt_if(smallint do_set, int whichprompt)
2686 IF_ASH_EXPAND_PRMT(struct stackmark smark;)
2693 switch (whichprompt) {
2703 #if ENABLE_ASH_EXPAND_PRMT
2704 pushstackmark(&smark, stackblocksize());
2705 putprompt(expandstr(prompt, PSSYNTAX));
2706 popstackmark(&smark);
2713 /* ============ The cd and pwd commands */
2715 #define CD_PHYSICAL 1
2725 while ((i = nextopt("LP")) != '\0') {
2727 flags ^= CD_PHYSICAL;
2736 * Update curdir (the name of the current directory) in response to a
2740 updatepwd(const char *dir)
2747 cdcomppath = sstrdup(dir);
2750 if (curdir == nullstr)
2752 new = stack_putstr(curdir, new);
2754 new = makestrspace(strlen(dir) + 2, new);
2755 lim = (char *)stackblock() + 1;
2759 if (new > lim && *lim == '/')
2764 if (dir[1] == '/' && dir[2] != '/') {
2770 p = strtok(cdcomppath, "/");
2774 if (p[1] == '.' && p[2] == '\0') {
2786 new = stack_putstr(p, new);
2789 p = strtok(NULL, "/");
2794 return stackblock();
2798 * Find out what the current directory is. If we already know the current
2799 * directory, this routine returns immediately.
2804 char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */
2805 return dir ? dir : nullstr;
2809 setpwd(const char *val, int setold)
2813 oldcur = dir = curdir;
2816 setvar("OLDPWD", oldcur, VEXPORT);
2819 if (physdir != nullstr) {
2820 if (physdir != oldcur)
2824 if (oldcur == val || !val) {
2830 dir = ckstrdup(val);
2831 if (oldcur != dir && oldcur != nullstr) {
2836 setvar("PWD", dir, VEXPORT);
2839 static void hashcd(void);
2842 * Actually do the chdir. We also call hashcd to let other routines
2843 * know that the current directory has changed.
2846 docd(const char *dest, int flags)
2848 const char *dir = NULL;
2851 TRACE(("docd(\"%s\", %d) called\n", dest, flags));
2854 if (!(flags & CD_PHYSICAL)) {
2855 dir = updatepwd(dest);
2869 static int FAST_FUNC
2870 cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
2883 dest = bltinlookup("HOME");
2884 else if (LONE_DASH(dest)) {
2885 dest = bltinlookup("OLDPWD");
2907 path = bltinlookup("CDPATH");
2908 while (p = path, (len = padvance(&path, dest)) >= 0) {
2912 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
2916 if (!docd(p, flags))
2927 ash_msg_and_raise_perror("can't cd to %s", dest);
2930 if (flags & CD_PRINT)
2931 out1fmt("%s\n", curdir);
2935 static int FAST_FUNC
2936 pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
2939 const char *dir = curdir;
2943 if (physdir == nullstr)
2947 out1fmt("%s\n", dir);
2952 /* ============ ... */
2955 #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024)
2957 /* Syntax classes */
2958 #define CWORD 0 /* character is nothing special */
2959 #define CNL 1 /* newline character */
2960 #define CBACK 2 /* a backslash character */
2961 #define CSQUOTE 3 /* single quote */
2962 #define CDQUOTE 4 /* double quote */
2963 #define CENDQUOTE 5 /* a terminating quote */
2964 #define CBQUOTE 6 /* backwards single quote */
2965 #define CVAR 7 /* a dollar sign */
2966 #define CENDVAR 8 /* a '}' character */
2967 #define CLP 9 /* a left paren in arithmetic */
2968 #define CRP 10 /* a right paren in arithmetic */
2969 #define CENDFILE 11 /* end of file */
2970 #define CCTL 12 /* like CWORD, except it must be escaped */
2971 #define CSPCL 13 /* these terminate a word */
2972 #define CIGN 14 /* character should be ignored */
2975 #if ENABLE_ASH_ALIAS
2979 #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE
2981 #if ENABLE_FEATURE_SH_MATH
2982 # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12))
2984 # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8))
2986 static const uint16_t S_I_T[] ALIGN2 = {
2987 #if ENABLE_ASH_ALIAS
2988 SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */
2990 SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */
2991 SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */
2992 SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */
2993 SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */
2994 SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */
2995 SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */
2996 SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */
2997 SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */
2998 SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */
2999 SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */
3000 SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */
3001 #if !USE_SIT_FUNCTION
3002 SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */
3003 SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */
3004 SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */
3008 /* Constants below must match table above */
3010 #if ENABLE_ASH_ALIAS
3011 CSPCL_CIGN_CIGN_CIGN , /* 0 */
3013 CSPCL_CWORD_CWORD_CWORD , /* 1 */
3014 CNL_CNL_CNL_CNL , /* 2 */
3015 CWORD_CCTL_CCTL_CWORD , /* 3 */
3016 CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */
3017 CVAR_CVAR_CWORD_CVAR , /* 5 */
3018 CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */
3019 CSPCL_CWORD_CWORD_CLP , /* 7 */
3020 CSPCL_CWORD_CWORD_CRP , /* 8 */
3021 CBACK_CBACK_CCTL_CBACK , /* 9 */
3022 CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */
3023 CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */
3024 CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */
3025 CWORD_CWORD_CWORD_CWORD , /* 13 */
3026 CCTL_CCTL_CCTL_CCTL , /* 14 */
3029 /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF,
3030 * caller must ensure proper cast on it if c is *char_ptr!
3032 #if USE_SIT_FUNCTION
3035 SIT(int c, int syntax)
3037 /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */
3038 static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~";
3040 * This causes '/' to be prepended with CTLESC in dquoted string,
3041 * making "./file"* treated incorrectly because we feed
3042 * ".\/file*" string to glob(), confusing it (see expandmeta func).
3043 * The "homegrown" glob implementation is okay with that,
3044 * but glibc one isn't. With '/' always treated as CWORD,
3047 # if ENABLE_ASH_ALIAS
3048 static const uint8_t syntax_index_table[] ALIGN1 = {
3049 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */
3050 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */
3051 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */
3055 static const uint8_t syntax_index_table[] ALIGN1 = {
3056 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */
3057 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */
3058 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */
3067 # if ENABLE_ASH_ALIAS
3073 /* Cast is purely for paranoia here,
3074 * just in case someone passed signed char to us */
3075 if ((unsigned char)c >= CTL_FIRST
3076 && (unsigned char)c <= CTL_LAST
3080 s = strchrnul(spec_symbls, c);
3083 indx = syntax_index_table[s - spec_symbls];
3085 return (S_I_T[indx] >> (syntax*4)) & 0xf;
3088 #else /* !USE_SIT_FUNCTION */
3090 static const uint8_t syntax_index_table[] ALIGN1 = {
3091 /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
3092 /* 0 */ CWORD_CWORD_CWORD_CWORD,
3093 /* 1 */ CWORD_CWORD_CWORD_CWORD,
3094 /* 2 */ CWORD_CWORD_CWORD_CWORD,
3095 /* 3 */ CWORD_CWORD_CWORD_CWORD,
3096 /* 4 */ CWORD_CWORD_CWORD_CWORD,
3097 /* 5 */ CWORD_CWORD_CWORD_CWORD,
3098 /* 6 */ CWORD_CWORD_CWORD_CWORD,
3099 /* 7 */ CWORD_CWORD_CWORD_CWORD,
3100 /* 8 */ CWORD_CWORD_CWORD_CWORD,
3101 /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD,
3102 /* 10 "\n" */ CNL_CNL_CNL_CNL,
3103 /* 11 */ CWORD_CWORD_CWORD_CWORD,
3104 /* 12 */ CWORD_CWORD_CWORD_CWORD,
3105 /* 13 */ CWORD_CWORD_CWORD_CWORD,
3106 /* 14 */ CWORD_CWORD_CWORD_CWORD,
3107 /* 15 */ CWORD_CWORD_CWORD_CWORD,
3108 /* 16 */ CWORD_CWORD_CWORD_CWORD,
3109 /* 17 */ CWORD_CWORD_CWORD_CWORD,
3110 /* 18 */ CWORD_CWORD_CWORD_CWORD,
3111 /* 19 */ CWORD_CWORD_CWORD_CWORD,
3112 /* 20 */ CWORD_CWORD_CWORD_CWORD,
3113 /* 21 */ CWORD_CWORD_CWORD_CWORD,
3114 /* 22 */ CWORD_CWORD_CWORD_CWORD,
3115 /* 23 */ CWORD_CWORD_CWORD_CWORD,
3116 /* 24 */ CWORD_CWORD_CWORD_CWORD,
3117 /* 25 */ CWORD_CWORD_CWORD_CWORD,
3118 /* 26 */ CWORD_CWORD_CWORD_CWORD,
3119 /* 27 */ CWORD_CWORD_CWORD_CWORD,
3120 /* 28 */ CWORD_CWORD_CWORD_CWORD,
3121 /* 29 */ CWORD_CWORD_CWORD_CWORD,
3122 /* 30 */ CWORD_CWORD_CWORD_CWORD,
3123 /* 31 */ CWORD_CWORD_CWORD_CWORD,
3124 /* 32 " " */ CSPCL_CWORD_CWORD_CWORD,
3125 /* 33 "!" */ CWORD_CCTL_CCTL_CWORD,
3126 /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD,
3127 /* 35 "#" */ CWORD_CWORD_CWORD_CWORD,
3128 /* 36 "$" */ CVAR_CVAR_CWORD_CVAR,
3129 /* 37 "%" */ CWORD_CWORD_CWORD_CWORD,
3130 /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD,
3131 /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD,
3132 /* 40 "(" */ CSPCL_CWORD_CWORD_CLP,
3133 /* 41 ")" */ CSPCL_CWORD_CWORD_CRP,
3134 /* 42 "*" */ CWORD_CCTL_CCTL_CWORD,
3135 /* 43 "+" */ CWORD_CWORD_CWORD_CWORD,
3136 /* 44 "," */ CWORD_CWORD_CWORD_CWORD,
3137 /* 45 "-" */ CWORD_CCTL_CCTL_CWORD,
3138 /* 46 "." */ CWORD_CWORD_CWORD_CWORD,
3139 /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */
3140 /* 47 "/" */ CWORD_CWORD_CWORD_CWORD,
3141 /* 48 "0" */ CWORD_CWORD_CWORD_CWORD,
3142 /* 49 "1" */ CWORD_CWORD_CWORD_CWORD,
3143 /* 50 "2" */ CWORD_CWORD_CWORD_CWORD,
3144 /* 51 "3" */ CWORD_CWORD_CWORD_CWORD,
3145 /* 52 "4" */ CWORD_CWORD_CWORD_CWORD,
3146 /* 53 "5" */ CWORD_CWORD_CWORD_CWORD,
3147 /* 54 "6" */ CWORD_CWORD_CWORD_CWORD,
3148 /* 55 "7" */ CWORD_CWORD_CWORD_CWORD,
3149 /* 56 "8" */ CWORD_CWORD_CWORD_CWORD,
3150 /* 57 "9" */ CWORD_CWORD_CWORD_CWORD,
3151 /* 58 ":" */ CWORD_CCTL_CCTL_CWORD,
3152 /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD,
3153 /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD,
3154 /* 61 "=" */ CWORD_CCTL_CCTL_CWORD,
3155 /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD,
3156 /* 63 "?" */ CWORD_CCTL_CCTL_CWORD,
3157 /* 64 "@" */ CWORD_CWORD_CWORD_CWORD,
3158 /* 65 "A" */ CWORD_CWORD_CWORD_CWORD,
3159 /* 66 "B" */ CWORD_CWORD_CWORD_CWORD,
3160 /* 67 "C" */ CWORD_CWORD_CWORD_CWORD,
3161 /* 68 "D" */ CWORD_CWORD_CWORD_CWORD,
3162 /* 69 "E" */ CWORD_CWORD_CWORD_CWORD,
3163 /* 70 "F" */ CWORD_CWORD_CWORD_CWORD,
3164 /* 71 "G" */ CWORD_CWORD_CWORD_CWORD,
3165 /* 72 "H" */ CWORD_CWORD_CWORD_CWORD,
3166 /* 73 "I" */ CWORD_CWORD_CWORD_CWORD,
3167 /* 74 "J" */ CWORD_CWORD_CWORD_CWORD,
3168 /* 75 "K" */ CWORD_CWORD_CWORD_CWORD,
3169 /* 76 "L" */ CWORD_CWORD_CWORD_CWORD,
3170 /* 77 "M" */ CWORD_CWORD_CWORD_CWORD,
3171 /* 78 "N" */ CWORD_CWORD_CWORD_CWORD,
3172 /* 79 "O" */ CWORD_CWORD_CWORD_CWORD,
3173 /* 80 "P" */ CWORD_CWORD_CWORD_CWORD,
3174 /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD,
3175 /* 82 "R" */ CWORD_CWORD_CWORD_CWORD,
3176 /* 83 "S" */ CWORD_CWORD_CWORD_CWORD,
3177 /* 84 "T" */ CWORD_CWORD_CWORD_CWORD,
3178 /* 85 "U" */ CWORD_CWORD_CWORD_CWORD,
3179 /* 86 "V" */ CWORD_CWORD_CWORD_CWORD,
3180 /* 87 "W" */ CWORD_CWORD_CWORD_CWORD,
3181 /* 88 "X" */ CWORD_CWORD_CWORD_CWORD,
3182 /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD,
3183 /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD,
3184 /* 91 "[" */ CWORD_CCTL_CCTL_CWORD,
3185 /* 92 "\" */ CBACK_CBACK_CCTL_CBACK,
3186 /* 93 "]" */ CWORD_CCTL_CCTL_CWORD,
3187 /* 94 "^" */ CWORD_CWORD_CWORD_CWORD,
3188 /* 95 "_" */ CWORD_CWORD_CWORD_CWORD,
3189 /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE,
3190 /* 97 "a" */ CWORD_CWORD_CWORD_CWORD,
3191 /* 98 "b" */ CWORD_CWORD_CWORD_CWORD,
3192 /* 99 "c" */ CWORD_CWORD_CWORD_CWORD,
3193 /* 100 "d" */ CWORD_CWORD_CWORD_CWORD,
3194 /* 101 "e" */ CWORD_CWORD_CWORD_CWORD,
3195 /* 102 "f" */ CWORD_CWORD_CWORD_CWORD,
3196 /* 103 "g" */ CWORD_CWORD_CWORD_CWORD,
3197 /* 104 "h" */ CWORD_CWORD_CWORD_CWORD,
3198 /* 105 "i" */ CWORD_CWORD_CWORD_CWORD,
3199 /* 106 "j" */ CWORD_CWORD_CWORD_CWORD,
3200 /* 107 "k" */ CWORD_CWORD_CWORD_CWORD,
3201 /* 108 "l" */ CWORD_CWORD_CWORD_CWORD,
3202 /* 109 "m" */ CWORD_CWORD_CWORD_CWORD,
3203 /* 110 "n" */ CWORD_CWORD_CWORD_CWORD,
3204 /* 111 "o" */ CWORD_CWORD_CWORD_CWORD,
3205 /* 112 "p" */ CWORD_CWORD_CWORD_CWORD,
3206 /* 113 "q" */ CWORD_CWORD_CWORD_CWORD,
3207 /* 114 "r" */ CWORD_CWORD_CWORD_CWORD,
3208 /* 115 "s" */ CWORD_CWORD_CWORD_CWORD,
3209 /* 116 "t" */ CWORD_CWORD_CWORD_CWORD,
3210 /* 117 "u" */ CWORD_CWORD_CWORD_CWORD,
3211 /* 118 "v" */ CWORD_CWORD_CWORD_CWORD,
3212 /* 119 "w" */ CWORD_CWORD_CWORD_CWORD,
3213 /* 120 "x" */ CWORD_CWORD_CWORD_CWORD,
3214 /* 121 "y" */ CWORD_CWORD_CWORD_CWORD,
3215 /* 122 "z" */ CWORD_CWORD_CWORD_CWORD,
3216 /* 123 "{" */ CWORD_CWORD_CWORD_CWORD,
3217 /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD,
3218 /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR,
3219 /* 126 "~" */ CWORD_CCTL_CCTL_CWORD,
3220 /* 127 del */ CWORD_CWORD_CWORD_CWORD,
3221 /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD,
3222 /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL,
3223 /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL,
3224 /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL,
3225 /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL,
3226 /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL,
3227 /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL,
3228 /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL,
3229 /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL,
3230 /* 137 */ CWORD_CWORD_CWORD_CWORD,
3231 /* 138 */ CWORD_CWORD_CWORD_CWORD,
3232 /* 139 */ CWORD_CWORD_CWORD_CWORD,
3233 /* 140 */ CWORD_CWORD_CWORD_CWORD,
3234 /* 141 */ CWORD_CWORD_CWORD_CWORD,
3235 /* 142 */ CWORD_CWORD_CWORD_CWORD,
3236 /* 143 */ CWORD_CWORD_CWORD_CWORD,
3237 /* 144 */ CWORD_CWORD_CWORD_CWORD,
3238 /* 145 */ CWORD_CWORD_CWORD_CWORD,
3239 /* 146 */ CWORD_CWORD_CWORD_CWORD,
3240 /* 147 */ CWORD_CWORD_CWORD_CWORD,
3241 /* 148 */ CWORD_CWORD_CWORD_CWORD,
3242 /* 149 */ CWORD_CWORD_CWORD_CWORD,
3243 /* 150 */ CWORD_CWORD_CWORD_CWORD,
3244 /* 151 */ CWORD_CWORD_CWORD_CWORD,
3245 /* 152 */ CWORD_CWORD_CWORD_CWORD,
3246 /* 153 */ CWORD_CWORD_CWORD_CWORD,
3247 /* 154 */ CWORD_CWORD_CWORD_CWORD,
3248 /* 155 */ CWORD_CWORD_CWORD_CWORD,
3249 /* 156 */ CWORD_CWORD_CWORD_CWORD,
3250 /* 157 */ CWORD_CWORD_CWORD_CWORD,
3251 /* 158 */ CWORD_CWORD_CWORD_CWORD,
3252 /* 159 */ CWORD_CWORD_CWORD_CWORD,
3253 /* 160 */ CWORD_CWORD_CWORD_CWORD,
3254 /* 161 */ CWORD_CWORD_CWORD_CWORD,
3255 /* 162 */ CWORD_CWORD_CWORD_CWORD,
3256 /* 163 */ CWORD_CWORD_CWORD_CWORD,
3257 /* 164 */ CWORD_CWORD_CWORD_CWORD,
3258 /* 165 */ CWORD_CWORD_CWORD_CWORD,
3259 /* 166 */ CWORD_CWORD_CWORD_CWORD,
3260 /* 167 */ CWORD_CWORD_CWORD_CWORD,
3261 /* 168 */ CWORD_CWORD_CWORD_CWORD,
3262 /* 169 */ CWORD_CWORD_CWORD_CWORD,
3263 /* 170 */ CWORD_CWORD_CWORD_CWORD,
3264 /* 171 */ CWORD_CWORD_CWORD_CWORD,
3265 /* 172 */ CWORD_CWORD_CWORD_CWORD,
3266 /* 173 */ CWORD_CWORD_CWORD_CWORD,
3267 /* 174 */ CWORD_CWORD_CWORD_CWORD,
3268 /* 175 */ CWORD_CWORD_CWORD_CWORD,
3269 /* 176 */ CWORD_CWORD_CWORD_CWORD,
3270 /* 177 */ CWORD_CWORD_CWORD_CWORD,
3271 /* 178 */ CWORD_CWORD_CWORD_CWORD,
3272 /* 179 */ CWORD_CWORD_CWORD_CWORD,
3273 /* 180 */ CWORD_CWORD_CWORD_CWORD,
3274 /* 181 */ CWORD_CWORD_CWORD_CWORD,
3275 /* 182 */ CWORD_CWORD_CWORD_CWORD,
3276 /* 183 */ CWORD_CWORD_CWORD_CWORD,
3277 /* 184 */ CWORD_CWORD_CWORD_CWORD,
3278 /* 185 */ CWORD_CWORD_CWORD_CWORD,
3279 /* 186 */ CWORD_CWORD_CWORD_CWORD,
3280 /* 187 */ CWORD_CWORD_CWORD_CWORD,
3281 /* 188 */ CWORD_CWORD_CWORD_CWORD,
3282 /* 189 */ CWORD_CWORD_CWORD_CWORD,
3283 /* 190 */ CWORD_CWORD_CWORD_CWORD,
3284 /* 191 */ CWORD_CWORD_CWORD_CWORD,
3285 /* 192 */ CWORD_CWORD_CWORD_CWORD,
3286 /* 193 */ CWORD_CWORD_CWORD_CWORD,
3287 /* 194 */ CWORD_CWORD_CWORD_CWORD,
3288 /* 195 */ CWORD_CWORD_CWORD_CWORD,
3289 /* 196 */ CWORD_CWORD_CWORD_CWORD,
3290 /* 197 */ CWORD_CWORD_CWORD_CWORD,
3291 /* 198 */ CWORD_CWORD_CWORD_CWORD,
3292 /* 199 */ CWORD_CWORD_CWORD_CWORD,
3293 /* 200 */ CWORD_CWORD_CWORD_CWORD,
3294 /* 201 */ CWORD_CWORD_CWORD_CWORD,
3295 /* 202 */ CWORD_CWORD_CWORD_CWORD,
3296 /* 203 */ CWORD_CWORD_CWORD_CWORD,
3297 /* 204 */ CWORD_CWORD_CWORD_CWORD,
3298 /* 205 */ CWORD_CWORD_CWORD_CWORD,
3299 /* 206 */ CWORD_CWORD_CWORD_CWORD,
3300 /* 207 */ CWORD_CWORD_CWORD_CWORD,
3301 /* 208 */ CWORD_CWORD_CWORD_CWORD,
3302 /* 209 */ CWORD_CWORD_CWORD_CWORD,
3303 /* 210 */ CWORD_CWORD_CWORD_CWORD,
3304 /* 211 */ CWORD_CWORD_CWORD_CWORD,
3305 /* 212 */ CWORD_CWORD_CWORD_CWORD,
3306 /* 213 */ CWORD_CWORD_CWORD_CWORD,
3307 /* 214 */ CWORD_CWORD_CWORD_CWORD,
3308 /* 215 */ CWORD_CWORD_CWORD_CWORD,
3309 /* 216 */ CWORD_CWORD_CWORD_CWORD,
3310 /* 217 */ CWORD_CWORD_CWORD_CWORD,
3311 /* 218 */ CWORD_CWORD_CWORD_CWORD,
3312 /* 219 */ CWORD_CWORD_CWORD_CWORD,
3313 /* 220 */ CWORD_CWORD_CWORD_CWORD,
3314 /* 221 */ CWORD_CWORD_CWORD_CWORD,
3315 /* 222 */ CWORD_CWORD_CWORD_CWORD,
3316 /* 223 */ CWORD_CWORD_CWORD_CWORD,
3317 /* 224 */ CWORD_CWORD_CWORD_CWORD,
3318 /* 225 */ CWORD_CWORD_CWORD_CWORD,
3319 /* 226 */ CWORD_CWORD_CWORD_CWORD,
3320 /* 227 */ CWORD_CWORD_CWORD_CWORD,
3321 /* 228 */ CWORD_CWORD_CWORD_CWORD,
3322 /* 229 */ CWORD_CWORD_CWORD_CWORD,
3323 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3324 /* 231 */ CWORD_CWORD_CWORD_CWORD,
3325 /* 232 */ CWORD_CWORD_CWORD_CWORD,
3326 /* 233 */ CWORD_CWORD_CWORD_CWORD,
3327 /* 234 */ CWORD_CWORD_CWORD_CWORD,
3328 /* 235 */ CWORD_CWORD_CWORD_CWORD,
3329 /* 236 */ CWORD_CWORD_CWORD_CWORD,
3330 /* 237 */ CWORD_CWORD_CWORD_CWORD,
3331 /* 238 */ CWORD_CWORD_CWORD_CWORD,
3332 /* 239 */ CWORD_CWORD_CWORD_CWORD,
3333 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3334 /* 241 */ CWORD_CWORD_CWORD_CWORD,
3335 /* 242 */ CWORD_CWORD_CWORD_CWORD,
3336 /* 243 */ CWORD_CWORD_CWORD_CWORD,
3337 /* 244 */ CWORD_CWORD_CWORD_CWORD,
3338 /* 245 */ CWORD_CWORD_CWORD_CWORD,
3339 /* 246 */ CWORD_CWORD_CWORD_CWORD,
3340 /* 247 */ CWORD_CWORD_CWORD_CWORD,
3341 /* 248 */ CWORD_CWORD_CWORD_CWORD,
3342 /* 249 */ CWORD_CWORD_CWORD_CWORD,
3343 /* 250 */ CWORD_CWORD_CWORD_CWORD,
3344 /* 251 */ CWORD_CWORD_CWORD_CWORD,
3345 /* 252 */ CWORD_CWORD_CWORD_CWORD,
3346 /* 253 */ CWORD_CWORD_CWORD_CWORD,
3347 /* 254 */ CWORD_CWORD_CWORD_CWORD,
3348 /* 255 */ CWORD_CWORD_CWORD_CWORD,
3349 /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
3350 # if ENABLE_ASH_ALIAS
3351 /* PEOA */ CSPCL_CIGN_CIGN_CIGN,
3356 # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf)
3357 #else /* debug version, caught one signed char bug */
3358 # define SIT(c, syntax) \
3360 if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \
3361 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3362 if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \
3363 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3364 ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \
3368 #endif /* !USE_SIT_FUNCTION */
3371 /* ============ Alias handling */
3373 #if ENABLE_ASH_ALIAS
3375 #define ALIASINUSE 1
3386 static struct alias **atab; // [ATABSIZE];
3387 #define INIT_G_alias() do { \
3388 atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
3392 static struct alias **
3393 __lookupalias(const char *name)
3395 unsigned int hashval;
3402 ch = (unsigned char)*p;
3406 ch = (unsigned char)*++p;
3408 app = &atab[hashval % ATABSIZE];
3410 for (; *app; app = &(*app)->next) {
3411 if (strcmp(name, (*app)->name) == 0) {
3419 static struct alias *
3420 lookupalias(const char *name, int check)
3422 struct alias *ap = *__lookupalias(name);
3424 if (check && ap && (ap->flag & ALIASINUSE))
3429 static struct alias *
3430 freealias(struct alias *ap)
3434 if (ap->flag & ALIASINUSE) {
3435 ap->flag |= ALIASDEAD;
3447 setalias(const char *name, const char *val)
3449 struct alias *ap, **app;
3451 app = __lookupalias(name);
3455 if (!(ap->flag & ALIASINUSE)) {
3458 ap->val = ckstrdup(val);
3459 ap->flag &= ~ALIASDEAD;
3462 ap = ckzalloc(sizeof(struct alias));
3463 ap->name = ckstrdup(name);
3464 ap->val = ckstrdup(val);
3465 /*ap->flag = 0; - ckzalloc did it */
3466 /*ap->next = NULL;*/
3473 unalias(const char *name)
3477 app = __lookupalias(name);
3481 *app = freealias(*app);
3492 struct alias *ap, **app;
3496 for (i = 0; i < ATABSIZE; i++) {
3498 for (ap = *app; ap; ap = *app) {
3499 *app = freealias(*app);
3509 printalias(const struct alias *ap)
3511 out1fmt("%s=%s\n", ap->name, single_quote(ap->val));
3515 * TODO - sort output
3517 static int FAST_FUNC
3518 aliascmd(int argc UNUSED_PARAM, char **argv)
3527 for (i = 0; i < ATABSIZE; i++) {
3528 for (ap = atab[i]; ap; ap = ap->next) {
3534 while ((n = *++argv) != NULL) {
3535 v = strchr(n+1, '=');
3536 if (v == NULL) { /* n+1: funny ksh stuff */
3537 ap = *__lookupalias(n);
3539 fprintf(stderr, "%s: %s not found\n", "alias", n);
3552 static int FAST_FUNC
3553 unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
3557 while (nextopt("a") != '\0') {
3561 for (i = 0; *argptr; argptr++) {
3562 if (unalias(*argptr)) {
3563 fprintf(stderr, "%s: %s not found\n", "unalias", *argptr);
3571 #endif /* ASH_ALIAS */
3574 /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */
3577 #define FORK_NOJOB 2
3579 /* mode flags for showjob(s) */
3580 #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */
3581 #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */
3582 #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */
3583 #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */
3586 * A job structure contains information about a job. A job is either a
3587 * single process or a set of processes contained in a pipeline. In the
3588 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
3592 pid_t ps_pid; /* process id */
3593 int ps_status; /* last process status from wait() */
3594 char *ps_cmd; /* text of command being run */
3598 struct procstat ps0; /* status of process */
3599 struct procstat *ps; /* status of processes when more than one */
3601 int stopstatus; /* status of a stopped job */
3603 unsigned nprocs; /* number of processes */
3605 #define JOBRUNNING 0 /* at least one proc running */
3606 #define JOBSTOPPED 1 /* all procs are stopped */
3607 #define JOBDONE 2 /* all procs are completed */
3611 sigint: 1, /* job was killed by SIGINT */
3612 jobctl: 1, /* job running under job control */
3614 waited: 1, /* true if this entry has been waited for */
3615 used: 1, /* true if this entry is in used */
3616 changed: 1; /* true if status has changed */
3617 struct job *prev_job; /* previous job */
3620 static struct job *makejob(/*union node *,*/ int);
3621 static int forkshell(struct job *, union node *, int);
3622 static int waitforjob(struct job *);
3625 enum { doing_jobctl = 0 };
3626 #define setjobctl(on) do {} while (0)
3628 static smallint doing_jobctl; //references:8
3629 static void setjobctl(int);
3636 ignoresig(int signo)
3638 /* Avoid unnecessary system calls. Is it already SIG_IGNed? */
3639 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
3640 /* No, need to do it */
3641 signal(signo, SIG_IGN);
3643 sigmode[signo - 1] = S_HARD_IGN;
3647 * Only one usage site - in setsignal()
3650 signal_handler(int signo)
3652 if (signo == SIGCHLD) {
3658 gotsig[signo - 1] = 1;
3659 pending_sig = signo;
3661 if (signo == SIGINT && !trap[SIGINT]) {
3662 if (!suppress_int) {
3664 raise_interrupt(); /* does not return */
3671 * Set the signal handler for the specified signal. The routine figures
3672 * out what it should be set to.
3675 setsignal(int signo)
3678 char cur_act, new_act;
3679 struct sigaction act;
3683 if (t != NULL) { /* trap for this sig is set */
3685 if (t[0] == '\0') /* trap is "": ignore this sig */
3689 if (rootshell && new_act == S_DFL) {
3692 if (iflag || minusc || sflag == 0)
3701 * "In all cases, bash ignores SIGQUIT. Non-builtin
3702 * commands run by bash have signal handlers
3703 * set to the values inherited by the shell
3704 * from its parent". */
3720 /* if !rootshell, we reset SIGQUIT to DFL,
3721 * whereas we have to restore it to what shell got on entry.
3722 * This is handled by the fact that if signal was IGNored on entry,
3723 * then cur_act is S_HARD_IGN and we never change its sigaction
3727 if (signo == SIGCHLD)
3730 t = &sigmode[signo - 1];
3733 /* current setting is not yet known */
3734 if (sigaction(signo, NULL, &act)) {
3735 /* pretend it worked; maybe we should give a warning,
3736 * but other shells don't. We don't alter sigmode,
3737 * so we retry every time.
3738 * btw, in Linux it never fails. --vda */
3741 if (act.sa_handler == SIG_IGN) {
3742 cur_act = S_HARD_IGN;
3744 && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)
3746 cur_act = S_IGN; /* don't hard ignore these */
3749 if (act.sa_handler == SIG_DFL && new_act == S_DFL) {
3750 /* installing SIG_DFL over SIG_DFL is a no-op */
3751 /* saves one sigaction call in each "sh -c SCRIPT" invocation */
3756 if (cur_act == S_HARD_IGN || cur_act == new_act)
3761 act.sa_handler = SIG_DFL;
3764 act.sa_handler = signal_handler;
3767 act.sa_handler = SIG_IGN;
3770 /* flags and mask matter only if !DFL and !IGN, but we do it
3771 * for all cases for more deterministic behavior:
3773 act.sa_flags = 0; //TODO: why not SA_RESTART?
3774 sigfillset(&act.sa_mask);
3776 sigaction_set(signo, &act);
3779 /* mode flags for set_curjob */
3780 #define CUR_DELETE 2
3781 #define CUR_RUNNING 1
3782 #define CUR_STOPPED 0
3785 /* pgrp of shell on invocation */
3786 static int initialpgrp; //references:2
3787 static int ttyfd = -1; //5
3790 static struct job *jobtab; //5
3792 static unsigned njobs; //4
3794 static struct job *curjob; //lots
3797 /* Bash has a feature: it restores termios after a successful wait for
3798 * a foreground job which had at least one stopped or sigkilled member.
3799 * The probable rationale is that SIGSTOP and SIGKILL can preclude task from
3800 * properly restoring tty state. Should we do this too?
3801 * A reproducer: ^Z an interactive python:
3804 * Python 2.7.12 (...)
3806 * { python leaves tty in -icanon -echo state. We do survive that... }
3807 * [1]+ Stopped python
3808 * { ...however, next program (python #2) does not survive it well: }
3810 * Python 2.7.12 (...)
3811 * >>> Traceback (most recent call last):
3812 * { above, I typed "qwerty<CR>", but -echo state is still in effect }
3813 * File "<stdin>", line 1, in <module>
3814 * NameError: name 'qwerty' is not defined
3816 * The implementation below is modeled on bash code and seems to work.
3817 * However, I'm not sure we should do this. For one: what if I'd fg
3818 * the stopped python instead? It'll be confused by "restored" tty state.
3820 static struct termios shell_tty_info;
3824 if (rootshell && ttyfd >= 0)
3825 tcgetattr(ttyfd, &shell_tty_info);
3830 /* if (rootshell) - caller ensures this */
3832 tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info);
3835 job_signal_status(struct job *jp)
3839 struct procstat *ps = jp->ps;
3840 for (i = 0; i < jp->nprocs; i++) {
3841 status = ps[i].ps_status;
3842 if (WIFSIGNALED(status) || WIFSTOPPED(status))
3848 restore_tty_if_stopped_or_signaled(struct job *jp)
3850 //TODO: check what happens if we come from waitforjob() in expbackq()
3852 int s = job_signal_status(jp);
3853 if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */
3858 # define get_tty_state() ((void)0)
3859 # define restore_tty_if_stopped_or_signaled(jp) ((void)0)
3863 set_curjob(struct job *jp, unsigned mode)
3866 struct job **jpp, **curp;
3868 /* first remove from list */
3869 jpp = curp = &curjob;
3874 jpp = &jp1->prev_job;
3876 *jpp = jp1->prev_job;
3878 /* Then re-insert in correct position */
3886 /* job being deleted */
3889 /* newly created job or backgrounded job,
3890 * put after all stopped jobs.
3895 if (!jp1 || jp1->state != JOBSTOPPED)
3898 jpp = &jp1->prev_job;
3904 /* newly stopped job - becomes curjob */
3905 jp->prev_job = *jpp;
3913 jobno(const struct job *jp)
3915 return jp - jobtab + 1;
3920 * Convert a job name to a job structure.
3923 #define getjob(name, getctl) getjob(name)
3926 getjob(const char *name, int getctl)
3930 const char *err_msg = "%s: no such job";
3934 char *(*match)(const char *, const char *);
3949 if (c == '+' || c == '%') {
3951 err_msg = "No current job";
3957 err_msg = "No previous job";
3967 if (num > 0 && num <= njobs) {
3968 jp = jobtab + num - 1;
3983 if (match(jp->ps[0].ps_cmd, p)) {
3987 err_msg = "%s: ambiguous";
3997 err_msg = "job %s not created under job control";
3998 if (getctl && jp->jobctl == 0)
4003 ash_msg_and_raise_error(err_msg, name);
4007 * Mark a job structure as unused.
4010 freejob(struct job *jp)
4012 struct procstat *ps;
4016 for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
4017 if (ps->ps_cmd != nullstr)
4020 if (jp->ps != &jp->ps0)
4023 set_curjob(jp, CUR_DELETE);
4029 xtcsetpgrp(int fd, pid_t pgrp)
4031 if (tcsetpgrp(fd, pgrp))
4032 ash_msg_and_raise_perror("can't set tty process group");
4036 * Turn job control on and off.
4038 * Note: This code assumes that the third arg to ioctl is a character
4039 * pointer, which is true on Berkeley systems but not System V. Since
4040 * System V doesn't have job control yet, this isn't a problem now.
4042 * Called with interrupts off.
4050 if (on == doing_jobctl || rootshell == 0)
4054 ofd = fd = open(_PATH_TTY, O_RDWR);
4056 /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails.
4057 * That sometimes helps to acquire controlling tty.
4058 * Obviously, a workaround for bugs when someone
4059 * failed to provide a controlling tty to bash! :) */
4065 /* fd is a tty at this point */
4066 fd = fcntl(fd, F_DUPFD_CLOEXEC, 10);
4067 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */
4070 goto out; /* F_DUPFD failed */
4071 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
4072 close_on_exec_on(fd);
4073 while (1) { /* while we are in the background */
4074 pgrp = tcgetpgrp(fd);
4077 ash_msg("can't access tty; job control turned off");
4081 if (pgrp == getpgrp())
4092 xtcsetpgrp(fd, pgrp);
4094 /* turning job control off */
4097 /* was xtcsetpgrp, but this can make exiting ash
4098 * loop forever if pty is already deleted */
4099 tcsetpgrp(fd, pgrp);
4113 static int FAST_FUNC
4114 killcmd(int argc, char **argv)
4116 if (argv[1] && strcmp(argv[1], "-l") != 0) {
4119 if (argv[i][0] == '%') {
4121 * "kill %N" - job kill
4122 * Converting to pgrp / pid kill
4128 jp = getjob(argv[i], 0);
4130 * In jobs started under job control, we signal
4131 * entire process group by kill -PGRP_ID.
4132 * This happens, f.e., in interactive shell.
4134 * Otherwise, we signal each child via
4135 * kill PID1 PID2 PID3.
4137 * sh -c 'sleep 1|sleep 1 & kill %1'
4138 * sh -c 'true|sleep 2 & sleep 1; kill %1'
4139 * sh -c 'true|sleep 1 & sleep 2; kill %1'
4141 n = jp->nprocs; /* can't be 0 (I hope) */
4144 dst = alloca(n * sizeof(int)*4);
4146 for (j = 0; j < n; j++) {
4147 struct procstat *ps = &jp->ps[j];
4148 /* Skip non-running and not-stopped members
4149 * (i.e. dead members) of the job
4151 if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status))
4154 * kill_main has matching code to expect
4155 * leading space. Needed to not confuse
4156 * negative pids with "kill -SIGNAL_NO" syntax
4158 dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid);
4162 } while (argv[++i]);
4164 return kill_main(argc, argv);
4168 showpipe(struct job *jp /*, FILE *out*/)
4170 struct procstat *ps;
4171 struct procstat *psend;
4173 psend = jp->ps + jp->nprocs;
4174 for (ps = jp->ps + 1; ps < psend; ps++)
4175 printf(" | %s", ps->ps_cmd);
4176 newline_and_flush(stdout);
4177 flush_stdout_stderr();
4182 restartjob(struct job *jp, int mode)
4184 struct procstat *ps;
4190 if (jp->state == JOBDONE)
4192 jp->state = JOBRUNNING;
4193 pgid = jp->ps[0].ps_pid;
4194 if (mode == FORK_FG) {
4196 xtcsetpgrp(ttyfd, pgid);
4198 killpg(pgid, SIGCONT);
4202 if (WIFSTOPPED(ps->ps_status)) {
4208 status = (mode == FORK_FG) ? waitforjob(jp) : 0;
4213 static int FAST_FUNC
4214 fg_bgcmd(int argc UNUSED_PARAM, char **argv)
4220 mode = (**argv == 'f') ? FORK_FG : FORK_BG;
4224 jp = getjob(*argv, 1);
4225 if (mode == FORK_BG) {
4226 set_curjob(jp, CUR_RUNNING);
4227 printf("[%d] ", jobno(jp));
4229 out1str(jp->ps[0].ps_cmd);
4230 showpipe(jp /*, stdout*/);
4231 retval = restartjob(jp, mode);
4232 } while (*argv && *++argv);
4238 sprint_status48(char *os, int status, int sigonly)
4243 if (!WIFEXITED(status)) {
4245 if (WIFSTOPPED(status))
4246 st = WSTOPSIG(status);
4249 st = WTERMSIG(status);
4251 if (st == SIGINT || st == SIGPIPE)
4254 if (WIFSTOPPED(status))
4259 //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata
4260 //s = stpncpy(s, strsignal(st), 32); //not all libc have stpncpy()
4261 s += fmtstr(s, 32, strsignal(st));
4262 if (WCOREDUMP(status)) {
4263 s = stpcpy(s, " (core dumped)");
4265 } else if (!sigonly) {
4266 st = WEXITSTATUS(status);
4267 s += fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st);
4274 wait_block_or_sig(int *status)
4281 /* Poll all children for changes in their state */
4283 /* if job control is active, accept stopped processes too */
4284 pid = waitpid(-1, status, doing_jobctl ? (WNOHANG|WUNTRACED) : WNOHANG);
4286 break; /* Error (e.g. EINTR, ECHILD) or pid */
4288 /* Children exist, but none are ready. Sleep until interesting signal */
4291 sigprocmask2(SIG_SETMASK, &mask); /* mask is updated */
4292 while (!got_sigchld && !pending_sig) {
4294 /* ^^^ add "sigdelset(&mask, SIGCHLD);" before sigsuspend
4295 * to make sure SIGCHLD is not masked off?
4296 * It was reported that this:
4297 * fn() { : | return; }
4301 * under bash 4.4.23 runs SCRIPT with SIGCHLD masked,
4302 * making "wait" commands in SCRIPT block forever.
4305 sigprocmask(SIG_SETMASK, &mask, NULL);
4306 #else /* unsafe: a signal can set pending_sig after check, but before pause() */
4307 while (!got_sigchld && !pending_sig)
4311 /* If it was SIGCHLD, poll children again */
4312 } while (got_sigchld);
4317 #define DOWAIT_NONBLOCK 0
4318 #define DOWAIT_BLOCK 1
4319 #define DOWAIT_BLOCK_OR_SIG 2
4321 # define DOWAIT_JOBSTATUS 0x10 /* OR this to get job's exitstatus instead of pid */
4325 waitone(int block, struct job *job)
4330 struct job *thisjob;
4332 bool want_jobexitstatus = (block & DOWAIT_JOBSTATUS);
4333 block = (block & ~DOWAIT_JOBSTATUS);
4336 TRACE(("dowait(0x%x) called\n", block));
4338 /* It's wrong to call waitpid() outside of INT_OFF region:
4339 * signal can arrive just after syscall return and handler can
4340 * longjmp away, losing stop/exit notification processing.
4341 * Thus, for "jobs" builtin, and for waiting for a fg job,
4342 * we call waitpid() (blocking or non-blocking) inside INT_OFF.
4344 * However, for "wait" builtin it is wrong to simply call waitpid()
4345 * in INT_OFF region: "wait" needs to wait for any running job
4346 * to change state, but should exit on any trap too.
4347 * In INT_OFF region, a signal just before syscall entry can set
4348 * pending_sig variables, but we can't check them, and we would
4349 * either enter a sleeping waitpid() (BUG), or need to busy-loop.
4351 * Because of this, we run inside INT_OFF, but use a special routine
4352 * which combines waitpid() and sigsuspend().
4353 * This is the reason why we need to have a handler for SIGCHLD:
4354 * SIG_DFL handler does not wake sigsuspend().
4357 if (block == DOWAIT_BLOCK_OR_SIG) {
4358 pid = wait_block_or_sig(&status);
4361 if (block == DOWAIT_NONBLOCK)
4362 wait_flags = WNOHANG;
4363 /* if job control is active, accept stopped processes too */
4365 wait_flags |= WUNTRACED;
4366 /* NB: _not_ safe_waitpid, we need to detect EINTR */
4367 pid = waitpid(-1, &status, wait_flags);
4369 TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
4370 pid, status, errno, strerror(errno)));
4375 for (jp = curjob; jp; jp = jp->prev_job) {
4377 struct procstat *ps;
4378 struct procstat *psend;
4379 if (jp->state == JOBDONE)
4383 psend = ps + jp->nprocs;
4385 if (ps->ps_pid == pid) {
4386 TRACE(("Job %d: changing status of proc %d "
4387 "from 0x%x to 0x%x\n",
4388 jobno(jp), pid, ps->ps_status, status));
4389 ps->ps_status = status;
4392 if (ps->ps_status == -1)
4393 jobstate = JOBRUNNING;
4395 if (jobstate == JOBRUNNING)
4397 if (WIFSTOPPED(ps->ps_status)) {
4398 jp->stopstatus = ps->ps_status;
4399 jobstate = JOBSTOPPED;
4402 } while (++ps < psend);
4406 /* Found the job where one of its processes changed its state.
4407 * Is there at least one live and running process in this job? */
4408 if (jobstate != JOBRUNNING) {
4409 /* No. All live processes in the job are stopped
4410 * (JOBSTOPPED) or there are no live processes (JOBDONE)
4412 thisjob->changed = 1;
4413 if (thisjob->state != jobstate) {
4414 TRACE(("Job %d: changing state from %d to %d\n",
4415 jobno(thisjob), thisjob->state, jobstate));
4416 thisjob->state = jobstate;
4418 if (jobstate == JOBSTOPPED)
4419 set_curjob(thisjob, CUR_STOPPED);
4425 /* The process wasn't found in job list */
4430 if (want_jobexitstatus) {
4432 if (thisjob && thisjob->state == JOBDONE)
4433 pid = thisjob->ps[thisjob->nprocs - 1].ps_status;
4436 if (thisjob && thisjob == job) {
4440 len = sprint_status48(s, status, 1);
4451 dowait(int block, struct job *jp)
4453 int pid = block == DOWAIT_NONBLOCK ? got_sigchld : 1;
4455 while (jp ? jp->state == JOBRUNNING : pid > 0) {
4458 pid = waitone(block, jp);
4466 showjob(struct job *jp, int mode)
4468 struct procstat *ps;
4469 struct procstat *psend;
4472 char s[16 + 16 + 48];
4473 FILE *out = (mode & SHOW_STDERR ? stderr : stdout);
4477 if (mode & SHOW_ONLY_PGID) { /* jobs -p */
4478 /* just output process (group) id of pipeline */
4479 fprintf(out, "%d\n", ps->ps_pid);
4483 col = fmtstr(s, 16, "[%d] ", jobno(jp));
4488 else if (curjob && jp == curjob->prev_job)
4491 if (mode & SHOW_PIDS)
4492 col += fmtstr(s + col, 16, "%d ", ps->ps_pid);
4494 psend = ps + jp->nprocs;
4496 if (jp->state == JOBRUNNING) {
4497 strcpy(s + col, "Running");
4498 col += sizeof("Running") - 1;
4500 int status = psend[-1].ps_status;
4501 if (jp->state == JOBSTOPPED)
4502 status = jp->stopstatus;
4503 col += sprint_status48(s + col, status, 0);
4505 /* By now, "[JOBID]* [maybe PID] STATUS" is printed */
4507 /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line
4508 * or prints several "PID | <cmdN>" lines,
4509 * depending on SHOW_PIDS bit.
4510 * We do not print status of individual processes
4511 * between PID and <cmdN>. bash does it, but not very well:
4512 * first line shows overall job status, not process status,
4513 * making it impossible to know 1st process status.
4517 /* for each process */
4520 if (mode & SHOW_PIDS)
4521 col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1;
4523 fprintf(out, "%s%*c%s%s",
4525 33 - col >= 0 ? 33 - col : 0, ' ',
4526 ps == jp->ps ? "" : "| ",
4529 } while (++ps != psend);
4530 newline_and_flush(out);
4534 if (jp->state == JOBDONE) {
4535 TRACE(("showjob: freeing job %d\n", jobno(jp)));
4541 * Print a list of jobs. If "change" is nonzero, only print jobs whose
4542 * statuses have changed since the last call to showjobs.
4549 TRACE(("showjobs(0x%x) called\n", mode));
4551 /* Handle all finished jobs */
4552 dowait(DOWAIT_NONBLOCK, NULL);
4554 for (jp = curjob; jp; jp = jp->prev_job) {
4555 if (!(mode & SHOW_CHANGED) || jp->changed) {
4561 static int FAST_FUNC
4562 jobscmd(int argc UNUSED_PARAM, char **argv)
4567 while ((m = nextopt("lp")) != '\0') {
4571 mode |= SHOW_ONLY_PGID;
4577 showjob(getjob(*argv, 0), mode);
4587 /* Called only on finished or stopped jobs (no members are running) */
4589 getstatus(struct job *job)
4593 struct procstat *ps;
4595 /* Fetch last member's status */
4596 ps = job->ps + job->nprocs - 1;
4597 status = ps->ps_status;
4599 /* "set -o pipefail" mode: use last _nonzero_ status */
4600 while (status == 0 && --ps >= job->ps)
4601 status = ps->ps_status;
4604 retval = WEXITSTATUS(status);
4605 if (!WIFEXITED(status)) {
4607 retval = WSTOPSIG(status);
4608 if (!WIFSTOPPED(status))
4611 /* XXX: limits number of signals */
4612 retval = WTERMSIG(status);
4614 if (retval == SIGINT)
4620 TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n",
4621 jobno(job), job->nprocs, status, retval));
4625 static int FAST_FUNC
4626 waitcmd(int argc UNUSED_PARAM, char **argv)
4633 char one = nextopt("n");
4641 /* wait for all jobs / one job if -n */
4646 /* exitcode of "wait -n" with nothing to wait for is 127, not 0 */
4650 if (!jp) /* no running procs */
4652 if (jp->state == JOBRUNNING)
4658 * "When bash is waiting for an asynchronous command via
4659 * the wait builtin, the reception of a signal for which a trap
4660 * has been set will cause the wait builtin to return immediately
4661 * with an exit status greater than 128, immediately after which
4662 * the trap is executed."
4665 status = dowait(DOWAIT_BLOCK_OR_SIG | DOWAIT_JOBSTATUS, NULL);
4667 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4669 /* if child sends us a signal *and immediately exits*,
4670 * dowait() returns pid > 0. Check this case,
4671 * not "if (dowait() < 0)"!
4677 /* wait -n waits for one _job_, not one _process_.
4678 * date; sleep 3 & sleep 2 | sleep 1 & wait -n; date
4679 * should wait for 2 seconds. Not 1 or 3.
4681 if (status != -1 && !WIFSTOPPED(status)) {
4682 retval = WEXITSTATUS(status);
4683 if (WIFSIGNALED(status))
4684 retval = WTERMSIG(status) + 128;
4694 if (**argv != '%') {
4695 pid_t pid = number(*argv);
4700 if (job->ps[job->nprocs - 1].ps_pid == pid)
4702 job = job->prev_job;
4705 job = getjob(*argv, 0);
4707 /* loop until process terminated or stopped */
4708 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4712 retval = getstatus(job);
4719 retval = 128 + pending_sig;
4728 struct job *jp, *jq;
4730 len = njobs * sizeof(*jp);
4732 jp = ckrealloc(jq, len + 4 * sizeof(*jp));
4734 offset = (char *)jp - (char *)jq;
4736 /* Relocate pointers */
4739 jq = (struct job *)((char *)jq + l);
4743 #define joff(p) ((struct job *)((char *)(p) + l))
4744 #define jmove(p) (p) = (void *)((char *)(p) + offset)
4745 if (joff(jp)->ps == &jq->ps0)
4746 jmove(joff(jp)->ps);
4747 if (joff(jp)->prev_job)
4748 jmove(joff(jp)->prev_job);
4758 jp = (struct job *)((char *)jp + len);
4762 } while (--jq >= jp);
4767 * Return a new job structure.
4768 * Called with interrupts off.
4771 makejob(/*union node *node,*/ int nprocs)
4776 for (i = njobs, jp = jobtab; ; jp++) {
4783 if (jp->state != JOBDONE || !jp->waited)
4792 memset(jp, 0, sizeof(*jp));
4794 /* jp->jobctl is a bitfield.
4795 * "jp->jobctl |= doing_jobctl" likely to give awful code */
4799 jp->prev_job = curjob;
4804 jp->ps = ckmalloc(nprocs * sizeof(struct procstat));
4806 TRACE(("makejob(%d) returns %%%d\n", nprocs,
4813 * Return a string identifying a command (to be printed by the
4816 static char *cmdnextc;
4819 cmdputs(const char *s)
4821 static const char vstype[VSTYPE + 1][3] = {
4822 "", "}", "-", "+", "?", "=",
4823 "%", "%%", "#", "##"
4824 IF_BASH_SUBSTR(, ":")
4825 IF_BASH_PATTERN_SUBST(, "/", "//")
4828 const char *p, *str;
4832 unsigned char subtype = 0;
4836 nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
4838 while ((c = *p++) != '\0') {
4846 if ((subtype & VSTYPE) == VSLENGTH)
4853 str += !(quoted & 1);
4860 #if ENABLE_FEATURE_SH_MATH
4875 if ((subtype & VSTYPE) != VSNORMAL)
4877 str = vstype[subtype & VSTYPE];
4878 if (subtype & VSNUL)
4887 /* These can only happen inside quotes */
4893 // [1]+ Done true ${\$} <<=== BUG: ${\$} is not a valid way to write $$ (${$} would be ok)
4904 while ((c = *str++) != '\0') {
4907 } /* while *p++ not NUL */
4910 USTPUTC('"', nextc);
4916 /* cmdtxt() and cmdlist() call each other */
4917 static void cmdtxt(union node *n);
4920 cmdlist(union node *np, int sep)
4922 for (; np; np = np->narg.next) {
4926 if (sep && np->narg.next)
4932 cmdtxt(union node *n)
4935 struct nodelist *lp;
4946 lp = n->npipe.cmdlist;
4964 cmdtxt(n->nbinary.ch1);
4980 cmdtxt(n->nif.test);
4982 if (n->nif.elsepart) {
4983 cmdtxt(n->nif.ifpart);
4985 n = n->nif.elsepart;
5003 cmdtxt(n->nbinary.ch1);
5013 cmdputs(n->nfor.var);
5015 cmdlist(n->nfor.args, 1);
5020 cmdputs(n->ndefun.text);
5024 cmdlist(n->ncmd.args, 1);
5025 cmdlist(n->ncmd.redirect, 0);
5038 cmdputs(n->ncase.expr->narg.text);
5040 for (np = n->ncase.cases; np; np = np->nclist.next) {
5041 cmdtxt(np->nclist.pattern);
5043 cmdtxt(np->nclist.body);
5057 #if BASH_REDIR_OUTPUT
5072 cmdputs(utoa(n->nfile.fd));
5074 if (n->type == NTOFD || n->type == NFROMFD) {
5075 if (n->ndup.dupfd >= 0)
5076 cmdputs(utoa(n->ndup.dupfd));
5087 commandtext(union node *n)
5091 STARTSTACKSTR(cmdnextc);
5093 name = stackblock();
5094 TRACE(("commandtext: name %p, end %p\n", name, cmdnextc));
5095 return ckstrdup(name);
5100 * Fork off a subshell. If we are doing job control, give the subshell its
5101 * own process group. Jp is a job structure that the job is to be added to.
5102 * N is the command that will be evaluated by the child. Both jp and n may
5103 * be NULL. The mode parameter can be one of the following:
5104 * FORK_FG - Fork off a foreground process.
5105 * FORK_BG - Fork off a background process.
5106 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
5107 * process group even if job control is on.
5109 * When job control is turned off, background processes have their standard
5110 * input redirected to /dev/null (except for the second and later processes
5113 * Called with interrupts off.
5116 * Clear traps on a fork.
5124 for (tp = trap; tp < &trap[NSIG]; tp++) {
5125 if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */
5126 if (trap_ptr == trap)
5128 /* else: it "belongs" to trap_ptr vector, don't free */
5130 if ((tp - trap) != 0)
5131 setsignal(tp - trap);
5138 /* Lives far away from here, needed for forkchild */
5139 static void closescript(void);
5141 /* Called after fork(), in child */
5142 /* jp and n are NULL when called by openhere() for heredoc support */
5143 static NOINLINE void
5144 forkchild(struct job *jp, union node *n, int mode)
5148 TRACE(("Child shell %d\n", getpid()));
5152 /* man bash: "Non-builtin commands run by bash have signal handlers
5153 * set to the values inherited by the shell from its parent".
5154 * Do we do it correctly? */
5158 if (mode == FORK_NOJOB /* is it `xxx` ? */
5159 && n && n->type == NCMD /* is it single cmd? */
5160 /* && n->ncmd.args->type == NARG - always true? */
5161 && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0
5162 && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */
5163 /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */
5165 TRACE(("Trap hack\n"));
5166 /* Awful hack for `trap` or $(trap).
5168 * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
5169 * contains an example where "trap" is executed in a subshell:
5171 * save_traps=$(trap)
5173 * eval "$save_traps"
5175 * Standard does not say that "trap" in subshell shall print
5176 * parent shell's traps. It only says that its output
5177 * must have suitable form, but then, in the above example
5178 * (which is not supposed to be normative), it implies that.
5180 * bash (and probably other shell) does implement it
5181 * (traps are reset to defaults, but "trap" still shows them),
5182 * but as a result, "trap" logic is hopelessly messed up:
5185 * trap -- 'echo Ho' SIGWINCH <--- we have a handler
5186 * # (trap) <--- trap is in subshell - no output (correct, traps are reset)
5187 * # true | trap <--- trap is in subshell - no output (ditto)
5188 * # echo `true | trap` <--- in subshell - output (but traps are reset!)
5189 * trap -- 'echo Ho' SIGWINCH
5190 * # echo `(trap)` <--- in subshell in subshell - output
5191 * trap -- 'echo Ho' SIGWINCH
5192 * # echo `true | (trap)` <--- in subshell in subshell in subshell - output!
5193 * trap -- 'echo Ho' SIGWINCH
5195 * The rules when to forget and when to not forget traps
5196 * get really complex and nonsensical.
5198 * Our solution: ONLY bare $(trap) or `trap` is special.
5200 /* Save trap handler strings for trap builtin to print */
5201 trap_ptr = xmemdup(trap, sizeof(trap));
5202 /* Fall through into clearing traps */
5206 /* do job control only in root shell */
5208 if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) {
5211 if (jp->nprocs == 0)
5214 pgrp = jp->ps[0].ps_pid;
5215 /* this can fail because we are doing it in the parent also */
5217 if (mode == FORK_FG)
5218 xtcsetpgrp(ttyfd, pgrp);
5223 if (mode == FORK_BG) {
5224 /* man bash: "When job control is not in effect,
5225 * asynchronous commands ignore SIGINT and SIGQUIT" */
5228 if (jp->nprocs == 0) {
5230 if (open(bb_dev_null, O_RDONLY) != 0)
5231 ash_msg_and_raise_perror("can't open '%s'", bb_dev_null);
5235 if (iflag) { /* why if iflag only? */
5240 * "In all cases, bash ignores SIGQUIT. Non-builtin
5241 * commands run by bash have signal handlers
5242 * set to the values inherited by the shell
5244 * Take care of the second rule: */
5248 if (n && n->type == NCMD
5249 && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0
5251 TRACE(("Job hack\n"));
5252 /* "jobs": we do not want to clear job list for it,
5253 * instead we remove only _its_ own_ job from job list.
5254 * This makes "jobs .... | cat" more useful.
5260 for (jp = curjob; jp; jp = jp->prev_job)
5264 /* Called after fork(), in parent */
5266 #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid)
5269 forkparent(struct job *jp, union node *n, int mode, pid_t pid)
5271 TRACE(("In parent shell: child = %d\n", pid));
5272 if (!jp) /* jp is NULL when called by openhere() for heredoc support */
5275 if (mode != FORK_NOJOB && jp->jobctl) {
5278 if (jp->nprocs == 0)
5281 pgrp = jp->ps[0].ps_pid;
5282 /* This can fail because we are doing it in the child also */
5286 if (mode == FORK_BG) {
5287 backgndpid = pid; /* set $! */
5288 set_curjob(jp, CUR_RUNNING);
5291 struct procstat *ps = &jp->ps[jp->nprocs++];
5294 ps->ps_cmd = nullstr;
5296 if (doing_jobctl && n)
5297 ps->ps_cmd = commandtext(n);
5302 /* jp and n are NULL when called by openhere() for heredoc support */
5304 forkshell(struct job *jp, union node *n, int mode)
5308 TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode));
5311 TRACE(("Fork failed, errno=%d", errno));
5314 ash_msg_and_raise_perror("can't fork");
5317 CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */
5318 forkchild(jp, n, mode);
5320 forkparent(jp, n, mode, pid);
5326 * Wait for job to finish.
5328 * Under job control we have the problem that while a child process
5329 * is running interrupts generated by the user are sent to the child
5330 * but not to the shell. This means that an infinite loop started by
5331 * an interactive user may be hard to kill. With job control turned off,
5332 * an interactive user may place an interactive program inside a loop.
5333 * If the interactive program catches interrupts, the user doesn't want
5334 * these interrupts to also abort the loop. The approach we take here
5335 * is to have the shell ignore interrupt signals while waiting for a
5336 * foreground process to terminate, and then send itself an interrupt
5337 * signal if the child process was terminated by an interrupt signal.
5338 * Unfortunately, some programs want to do a bit of cleanup and then
5339 * exit on interrupt; unless these processes terminate themselves by
5340 * sending a signal to themselves (instead of calling exit) they will
5341 * confuse this approach.
5343 * Called with interrupts off.
5346 waitforjob(struct job *jp)
5350 TRACE(("waitforjob(%%%d) called\n", jp ? jobno(jp) : 0));
5352 /* In non-interactive shells, we _can_ get
5353 * a keyboard signal here and be EINTRed, but we just loop
5354 * inside dowait(), waiting for command to complete.
5357 * "If bash is waiting for a command to complete and receives
5358 * a signal for which a trap has been set, the trap
5359 * will not be executed until the command completes."
5361 * Reality is that even if trap is not set, bash
5362 * will not act on the signal until command completes.
5363 * Try this. sleep5intoff.c:
5364 * #include <signal.h>
5365 * #include <unistd.h>
5368 * sigemptyset(&set);
5369 * sigaddset(&set, SIGINT);
5370 * sigaddset(&set, SIGQUIT);
5371 * sigprocmask(SIG_BLOCK, &set, NULL);
5375 * $ bash -c './sleep5intoff; echo hi'
5376 * ^C^C^C^C <--- pressing ^C once a second
5378 * $ bash -c './sleep5intoff; echo hi'
5379 * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT)
5382 dowait(jp ? DOWAIT_BLOCK : DOWAIT_NONBLOCK, jp);
5389 xtcsetpgrp(ttyfd, rootpid);
5390 restore_tty_if_stopped_or_signaled(jp);
5393 * This is truly gross.
5394 * If we're doing job control, then we did a TIOCSPGRP which
5395 * caused us (the shell) to no longer be in the controlling
5396 * session -- so we wouldn't have seen any ^C/SIGINT. So, we
5397 * intuit from the subprocess exit status whether a SIGINT
5398 * occurred, and if so interrupt ourselves. Yuck. - mycroft
5400 if (jp->sigint) /* TODO: do the same with all signals */
5401 raise(SIGINT); /* ... by raise(jp->sig) instead? */
5403 if (jp->state == JOBDONE)
5410 * return 1 if there are stopped jobs, otherwise 0
5422 if (jp && jp->state == JOBSTOPPED) {
5423 out2str("You have stopped jobs.\n");
5433 * Code for dealing with input/output redirection.
5438 #define EMPTY -2 /* marks an unused slot in redirtab */
5439 #define CLOSED -1 /* marks a slot of previously-closed fd */
5442 * Handle here documents. Normally we fork off a process to write the
5443 * data to a pipe. If the document is short, we can stuff the data in
5444 * the pipe without forking.
5446 /* openhere needs this forward reference */
5447 static void expandhere(union node *arg, int fd);
5449 openhere(union node *redir)
5455 ash_msg_and_raise_perror("can't create pipe");
5456 if (redir->type == NHERE) {
5457 len = strlen(redir->nhere.doc->narg.text);
5458 if (len <= PIPE_BUF) {
5459 full_write(pip[1], redir->nhere.doc->narg.text, len);
5463 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
5466 ignoresig(SIGINT); //signal(SIGINT, SIG_IGN);
5467 ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN);
5468 ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN);
5469 ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN);
5470 signal(SIGPIPE, SIG_DFL);
5471 if (redir->type == NHERE)
5472 full_write(pip[1], redir->nhere.doc->narg.text, len);
5474 expandhere(redir->nhere.doc, pip[1]);
5475 _exit(EXIT_SUCCESS);
5483 openredirect(union node *redir)
5489 switch (redir->nfile.type) {
5490 /* Can't happen, our single caller does this itself */
5496 return openhere(redir);
5499 /* For N[X]HERE, reading redir->nfile.expfname would touch beyond
5500 * allocated space. Do it only when we know it is safe.
5502 fname = redir->nfile.expfname;
5504 switch (redir->nfile.type) {
5510 f = open(fname, O_RDONLY);
5515 f = open(fname, O_RDWR|O_CREAT, 0666);
5520 #if BASH_REDIR_OUTPUT
5523 /* Take care of noclobber mode. */
5525 if (stat(fname, &sb) < 0) {
5526 f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666);
5529 } else if (!S_ISREG(sb.st_mode)) {
5530 f = open(fname, O_WRONLY, 0666);
5533 if (!fstat(f, &sb) && S_ISREG(sb.st_mode)) {
5546 f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5551 f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666);
5559 ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory"));
5561 ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file"));
5565 * Copy a file descriptor to be >= 10. Throws exception on error.
5573 newfd = fcntl(from, F_DUPFD_CLOEXEC, 10);
5574 err = newfd < 0 ? errno : 0;
5577 ash_msg_and_raise_perror("%d", from);
5579 if (F_DUPFD_CLOEXEC == F_DUPFD)
5580 close_on_exec_on(newfd);
5586 dup2_or_raise(int from, int to)
5590 newfd = (from != to) ? dup2(from, to) : to;
5592 /* Happens when source fd is not open: try "echo >&99" */
5593 ash_msg_and_raise_perror("%d", from);
5598 dup_CLOEXEC(int fd, int avoid_fd)
5602 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
5604 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
5605 close_on_exec_on(newfd);
5606 } else { /* newfd < 0 */
5615 xdup_CLOEXEC_and_close(int fd, int avoid_fd)
5619 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
5625 /* fd was not open? */
5628 ash_msg_and_raise_perror("%d", newfd);
5630 if (F_DUPFD_CLOEXEC == F_DUPFD)
5631 close_on_exec_on(newfd);
5636 /* Struct def and variable are moved down to the first usage site */
5642 struct redirtab *next;
5644 struct squirrel two_fd[];
5646 #define redirlist (G_var.redirlist)
5649 add_squirrel_closed(struct redirtab *sq, int fd)
5656 for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
5657 /* If we collide with an already moved fd... */
5658 if (fd == sq->two_fd[i].orig_fd) {
5660 * "echo 3>FILE 3>&- 3>FILE"
5661 * "echo 3>&- 3>FILE"
5662 * No need for last redirect to insert
5663 * another "need to close 3" indicator.
5665 TRACE(("redirect_fd %d: already moved or closed\n", fd));
5669 TRACE(("redirect_fd %d: previous fd was closed\n", fd));
5670 sq->two_fd[i].orig_fd = fd;
5671 sq->two_fd[i].moved_to = CLOSED;
5675 save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq)
5679 if (avoid_fd < 9) /* the important case here is that it can be -1 */
5684 /* Testcase: "ls -l /proc/$$/fd 10>&-" should work */
5685 ttyfd = xdup_CLOEXEC_and_close(ttyfd, avoid_fd);
5686 TRACE(("redirect_fd %d: matches ttyfd, moving it to %d\n", fd, ttyfd));
5687 return 1; /* "we closed fd" */
5690 /* Are we called from redirect(0)? E.g. redirect
5691 * in a forked child. No need to save fds,
5692 * we aren't going to use them anymore, ok to trash.
5697 /* If this one of script's fds? */
5699 struct parsefile *pf = g_parsefile;
5701 /* We skip fd == 0 case because of the following:
5702 * $ ash # running ash interactively
5704 * and in script.sh: "exec 9>&0".
5705 * Even though top-level pf_fd _is_ 0,
5706 * it's still ok to use it: "read" builtin uses it,
5707 * why should we cripple "exec" builtin?
5709 if (fd == pf->pf_fd) {
5710 pf->pf_fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
5711 return 1; /* "we closed fd" */
5717 /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */
5719 /* First: do we collide with some already moved fds? */
5720 for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
5721 /* If we collide with an already moved fd... */
5722 if (fd == sq->two_fd[i].moved_to) {
5723 new_fd = dup_CLOEXEC(fd, avoid_fd);
5724 sq->two_fd[i].moved_to = new_fd;
5725 TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd));
5726 if (new_fd < 0) /* what? */
5728 return 0; /* "we did not close fd" */
5730 if (fd == sq->two_fd[i].orig_fd) {
5731 /* Example: echo Hello >/dev/null 1>&2 */
5732 TRACE(("redirect_fd %d: already moved\n", fd));
5733 return 0; /* "we did not close fd" */
5737 /* If this fd is open, we move and remember it; if it's closed, new_fd = CLOSED (-1) */
5738 new_fd = dup_CLOEXEC(fd, avoid_fd);
5739 TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd));
5743 /* new_fd = CLOSED; - already is -1 */
5745 sq->two_fd[i].moved_to = new_fd;
5746 sq->two_fd[i].orig_fd = fd;
5748 /* if we move stderr, let "set -x" code know */
5749 if (fd == preverrout_fd)
5750 preverrout_fd = new_fd;
5752 return 0; /* "we did not close fd" */
5756 internally_opened_fd(int fd, struct redirtab *sq)
5763 /* If this one of script's fds? */
5765 struct parsefile *pf = g_parsefile;
5767 if (fd == pf->pf_fd)
5773 if (sq) for (i = 0; i < sq->pair_count && sq->two_fd[i].orig_fd != EMPTY; i++) {
5774 if (fd == sq->two_fd[i].moved_to)
5781 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
5782 * old file descriptors are stashed away so that the redirection can be
5783 * undone by calling popredir.
5785 /* flags passed to redirect */
5786 #define REDIR_PUSH 01 /* save previous values of file descriptors */
5788 redirect(union node *redir, int flags)
5790 struct redirtab *sv;
5797 if (flags & REDIR_PUSH)
5805 fd = redir->nfile.fd;
5806 if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) {
5807 //bb_error_msg("doing %d > %d", fd, newfd);
5808 newfd = redir->ndup.dupfd;
5811 newfd = openredirect(redir); /* always >= 0 */
5813 /* open() gave us precisely the fd we wanted.
5814 * This means that this fd was not busy
5815 * (not opened to anywhere).
5816 * Remember to close it on restore:
5818 add_squirrel_closed(sv, fd);
5827 /* if "N>FILE": move newfd to fd */
5828 /* if "N>&M": dup newfd to fd */
5829 /* if "N>&-": close fd (newfd is -1) */
5831 IF_BASH_REDIR_OUTPUT(redirect_more:)
5833 closed = save_fd_on_redirect(fd, /*avoid:*/ newfd, sv);
5835 /* "N>&-" means "close me" */
5837 /* ^^^ optimization: saving may already
5838 * have closed it. If not... */
5842 /* if newfd is a script fd or saved fd, simulate EBADF */
5843 if (internally_opened_fd(newfd, sv)) {
5845 ash_msg_and_raise_perror("%d", newfd);
5847 dup2_or_raise(newfd, fd);
5848 if (close_fd >= 0) /* "N>FILE" or ">&FILE" or heredoc? */
5850 #if BASH_REDIR_OUTPUT
5851 if (redir->nfile.type == NTO2 && fd == 1) {
5852 /* ">&FILE". we already redirected to 1, now copy 1 to 2 */
5860 } while ((redir = redir->nfile.next) != NULL);
5863 //dash:#define REDIR_SAVEFD2 03 /* set preverrout */
5864 #define REDIR_SAVEFD2 0
5865 // dash has a bug: since REDIR_SAVEFD2=3 and REDIR_PUSH=1, this test
5866 // triggers for pure REDIR_PUSH too. Thus, this is done almost always,
5867 // not only for calls with flags containing REDIR_SAVEFD2.
5868 // We do this unconditionally (see save_fd_on_redirect()).
5869 //if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0)
5870 // preverrout_fd = copied_fd2;
5874 redirectsafe(union node *redir, int flags)
5877 volatile int saveint;
5878 struct jmploc *volatile savehandler = exception_handler;
5879 struct jmploc jmploc;
5882 /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */
5883 err = setjmp(jmploc.loc); /* was = setjmp(jmploc.loc) * 2; */
5885 exception_handler = &jmploc;
5886 redirect(redir, flags);
5888 exception_handler = savehandler;
5889 if (err && exception_type != EXERROR)
5890 longjmp(exception_handler->loc, 1);
5891 RESTORE_INT(saveint);
5895 static struct redirtab*
5896 pushredir(union node *redir)
5898 struct redirtab *sv;
5907 #if BASH_REDIR_OUTPUT
5908 if (redir->nfile.type == NTO2)
5911 redir = redir->nfile.next;
5914 sv = ckzalloc(sizeof(*sv) + i * sizeof(sv->two_fd[0]));
5917 sv->two_fd[i].orig_fd = sv->two_fd[i].moved_to = EMPTY;
5918 sv->next = redirlist;
5924 * Undo the effects of the last redirection.
5929 struct redirtab *rp;
5932 if (redirlist == NULL)
5936 for (i = 0; i < rp->pair_count; i++) {
5937 int fd = rp->two_fd[i].orig_fd;
5938 int copy = rp->two_fd[i].moved_to;
5939 if (copy == CLOSED) {
5944 if (copy != EMPTY) {
5947 dup2_or_raise(copy, fd);
5952 redirlist = rp->next;
5958 unwindredir(struct redirtab *stop)
5960 while (redirlist != stop)
5961 popredir(/*drop:*/ 0);
5965 /* ============ Routines to expand arguments to commands
5967 * We have to deal with backquotes, shell variables, and file metacharacters.
5970 #if ENABLE_FEATURE_SH_MATH
5972 ash_arith(const char *s)
5974 arith_state_t math_state;
5977 math_state.lookupvar = lookupvar;
5978 math_state.setvar = setvar0;
5979 //math_state.endofname = endofname;
5982 result = arith(&math_state, s);
5983 if (math_state.errmsg)
5984 ash_msg_and_raise_error(math_state.errmsg);
5991 # if ENABLE_FEATURE_SH_MATH
5992 static int substr_atoi(const char *s)
5994 arith_t t = ash_arith(s);
5995 if (sizeof(t) > sizeof(int)) {
5996 /* clamp very large or very large negative nums for ${v:N:M}:
5997 * else "${v:0:0x100000001}" would work as "${v:0:1}"
6007 # define substr_atoi(s) number(s)
6014 #define EXP_FULL 0x1 /* perform word splitting & file globbing */
6015 #define EXP_TILDE 0x2 /* do normal tilde expansion */
6016 #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */
6017 #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */
6018 /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt"
6019 * POSIX says for this case:
6020 * Pathname expansion shall not be performed on the word by a
6021 * non-interactive shell; an interactive shell may perform it, but shall
6022 * do so only when the expansion would result in one word.
6023 * Currently, our code complies to the above rule by never globbing
6024 * redirection filenames.
6025 * Bash performs globbing, unless it is non-interactive and in POSIX mode.
6026 * (this means that on a typical Linux distro, bash almost always
6027 * performs globbing, and thus diverges from what we do).
6029 #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */
6030 #define EXP_VARTILDE2 0x20 /* expand tildes after colons only */
6031 #define EXP_WORD 0x40 /* expand word in parameter expansion */
6032 #define EXP_QUOTED 0x100 /* expand word in double quotes */
6033 #define EXP_KEEPNUL 0x200 /* do not skip NUL characters */
6038 #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */
6039 #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */
6040 #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */
6041 #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */
6043 /* Add CTLESC when necessary. */
6044 #define QUOTES_ESC (EXP_FULL | EXP_CASE)
6047 * Structure specifying which parts of the string should be searched
6048 * for IFS characters.
6051 struct ifsregion *next; /* next region in list */
6052 int begoff; /* offset of start of region */
6053 int endoff; /* offset of end of region */
6054 int nulonly; /* search for nul bytes only */
6058 struct strlist *list;
6059 struct strlist **lastp;
6062 /* output of current string */
6063 static char *expdest;
6064 /* list of back quote expressions */
6065 static struct nodelist *argbackq;
6066 /* first struct in list of ifs regions */
6067 static struct ifsregion ifsfirst;
6068 /* last struct in list */
6069 static struct ifsregion *ifslastp;
6070 /* holds expanded arg list */
6071 static struct arglist exparg;
6074 * Break the argument string into pieces based upon IFS and add the
6075 * strings to the argument list. The regions of the string to be
6076 * searched for IFS characters have been stored by recordregion.
6079 ifsbreakup(char *string, struct arglist *arglist)
6081 struct ifsregion *ifsp;
6086 const char *ifs, *realifs;
6091 if (ifslastp != NULL) {
6094 realifs = ifsset() ? ifsval() : defifs;
6099 p = string + ifsp->begoff;
6101 nulonly = ifsp->nulonly;
6102 ifs = nulonly ? nullstr : realifs;
6104 while (p < string + ifsp->endoff) {
6106 if ((unsigned char)*p == CTLESC)
6108 if (!strchr(ifs, *p)) {
6112 if (!(afternul || nulonly))
6113 ifsspc = (strchr(defifs, *p) != NULL);
6114 /* Ignore IFS whitespace at start */
6115 if (q == start && ifsspc) {
6121 sp = stzalloc(sizeof(*sp));
6123 *arglist->lastp = sp;
6124 arglist->lastp = &sp->next;
6128 if (p >= string + ifsp->endoff) {
6132 if ((unsigned char)*p == CTLESC)
6134 if (strchr(ifs, *p) == NULL) {
6138 if (strchr(defifs, *p) == NULL) {
6153 } while (ifsp != NULL);
6162 sp = stzalloc(sizeof(*sp));
6164 *arglist->lastp = sp;
6165 arglist->lastp = &sp->next;
6171 struct ifsregion *p = ifsfirst.next;
6178 struct ifsregion *ifsp;
6183 ifsfirst.next = NULL;
6190 esclen(const char *start, const char *p)
6194 while (p > start && (unsigned char)*--p == CTLESC) {
6201 * Remove any CTLESC characters from a string.
6203 #if !BASH_PATTERN_SUBST
6204 #define rmescapes(str, flag, slash_position) \
6205 rmescapes(str, flag)
6208 rmescapes(char *str, int flag, int *slash_position)
6210 static const char qchars[] ALIGN1 = {
6211 IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' };
6214 unsigned protect_against_glob;
6217 p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash_position));
6223 if (flag & RMESCAPE_ALLOC) {
6224 size_t len = p - str;
6225 size_t fulllen = len + strlen(p) + 1;
6227 if (flag & RMESCAPE_GROW) {
6228 int strloc = str - (char *)stackblock();
6229 r = makestrspace(fulllen, expdest);
6230 /* p and str may be invalidated by makestrspace */
6231 str = (char *)stackblock() + strloc;
6233 } else if (flag & RMESCAPE_HEAP) {
6234 r = ckmalloc(fulllen);
6236 r = stalloc(fulllen);
6240 q = (char *)mempcpy(q, str, len);
6244 globbing = flag & RMESCAPE_GLOB;
6245 protect_against_glob = globbing;
6247 if ((unsigned char)*p == CTLQUOTEMARK) {
6248 // Note: protect_against_glob only affect whether
6249 // CTLESC,<ch> gets converted to <ch> or to \<ch>
6251 protect_against_glob = globbing;
6255 /* naked back slash */
6256 protect_against_glob = 0;
6259 if ((unsigned char)*p == CTLESC) {
6263 ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)");
6265 if (protect_against_glob) {
6267 * We used to trust glob() and fnmatch() to eat
6268 * superfluous escapes (\z where z has no
6269 * special meaning anyway). But this causes
6270 * bugs such as string of one greek letter rho
6271 * (unicode-encoded as two bytes "cf,81")
6272 * getting encoded as "cf,CTLESC,81"
6273 * and here, converted to "cf,\,81" -
6274 * which does not go well with some flavors
6275 * of fnmatch() in unicode locales
6276 * (for example, glibc <= 2.22).
6278 * Lets add "\" only on the chars which need it.
6279 * Testcases for less obvious chars are shown.
6284 || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */
6285 || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */
6286 || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */
6287 || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */
6288 /* Some libc support [^negate], that's why "^" also needs love */
6289 || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */
6295 #if BASH_PATTERN_SUBST
6296 else if (slash_position && p == str + *slash_position) {
6297 /* stop handling globbing */
6299 *slash_position = q - r;
6300 slash_position = NULL;
6303 protect_against_glob = globbing;
6308 if (flag & RMESCAPE_GROW) {
6310 STADJUST(q - r + 1, expdest);
6314 #define pmatch(a, b) !fnmatch((a), (b), 0)
6317 * Prepare a pattern for a expmeta (internal glob(3)) call.
6319 * Returns an stalloced string.
6322 preglob(const char *pattern, int flag)
6324 return rmescapes((char *)pattern, flag | RMESCAPE_GLOB, NULL);
6328 * Put a string on the stack.
6331 memtodest(const char *p, size_t len, int flags)
6333 int syntax = flags & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
6340 q = makestrspace(len * 2, expdest);
6344 unsigned char c = *p++;
6346 if (flags & QUOTES_ESC) {
6347 int n = SIT(c, syntax);
6349 || ((flags & EXP_QUOTED) && n == CBACK)
6354 } else if (!(flags & EXP_KEEPNUL))
6364 strtodest(const char *p, int flags)
6366 size_t len = strlen(p);
6367 memtodest(p, len, flags);
6373 * cvtnum() is used even if math support is off (to prepare $? values and such).
6376 cvtnum(arith_t num, int flags)
6378 /* 32-bit and wider ints require buffer size of bytes*3 (or less) */
6379 /* If narrower: worst case, 1-byte ints: need 5 bytes: "-127<NUL>" */
6380 int len = (sizeof(arith_t) >= 4) ? sizeof(arith_t) * 3 : sizeof(arith_t) * 3 + 2;
6383 len = fmtstr(buf, len, ARITH_FMT, num);
6384 return memtodest(buf, len, flags);
6388 * Record the fact that we have to scan this region of the
6389 * string for IFS characters.
6392 recordregion(int start, int end, int nulonly)
6394 struct ifsregion *ifsp;
6396 if (ifslastp == NULL) {
6400 ifsp = ckzalloc(sizeof(*ifsp));
6401 /*ifsp->next = NULL; - ckzalloc did it */
6402 ifslastp->next = ifsp;
6406 ifslastp->begoff = start;
6407 ifslastp->endoff = end;
6408 ifslastp->nulonly = nulonly;
6412 removerecordregions(int endoff)
6414 if (ifslastp == NULL)
6417 if (ifsfirst.endoff > endoff) {
6418 while (ifsfirst.next) {
6419 struct ifsregion *ifsp;
6421 ifsp = ifsfirst.next->next;
6422 free(ifsfirst.next);
6423 ifsfirst.next = ifsp;
6426 if (ifsfirst.begoff > endoff) {
6429 ifslastp = &ifsfirst;
6430 ifsfirst.endoff = endoff;
6435 ifslastp = &ifsfirst;
6436 while (ifslastp->next && ifslastp->next->begoff < endoff)
6437 ifslastp = ifslastp->next;
6438 while (ifslastp->next) {
6439 struct ifsregion *ifsp;
6441 ifsp = ifslastp->next->next;
6442 free(ifslastp->next);
6443 ifslastp->next = ifsp;
6446 if (ifslastp->endoff > endoff)
6447 ifslastp->endoff = endoff;
6451 exptilde(char *startp, char *p, int flag)
6460 while ((c = *++p) != '\0') {
6467 if (flag & EXP_VARTILDE)
6477 if (*name == '\0') {
6478 home = lookupvar("HOME");
6480 pw = getpwnam(name);
6488 strtodest(home, flag | EXP_QUOTED);
6496 * Execute a command inside back quotes. If it's a builtin command, we
6497 * want to save its output in a block obtained from malloc. Otherwise
6498 * we fork off a subprocess and get the output of the command via a pipe.
6499 * Should be called with interrupts off.
6501 struct backcmd { /* result of evalbackcmd */
6502 int fd; /* file descriptor to read from */
6503 int nleft; /* number of chars in buffer */
6504 char *buf; /* buffer */
6505 struct job *jp; /* job structure for command */
6508 /* These forward decls are needed to use "eval" code for backticks handling: */
6509 /* flags in argument to evaltree */
6510 #define EV_EXIT 01 /* exit after evaluating tree */
6511 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
6512 static int evaltree(union node *, int);
6514 /* An evaltree() which is known to never return.
6515 * Used to use an alias:
6516 * static int evaltreenr(union node *, int) __attribute__((alias("evaltree"),__noreturn__));
6517 * but clang was reported to "transfer" noreturn-ness to evaltree() as well.
6519 static ALWAYS_INLINE NORETURN void
6520 evaltreenr(union node *n, int flags)
6523 bb_unreachable(abort());
6527 static void FAST_FUNC
6528 evalbackcmd(union node *n, struct backcmd *result)
6542 ash_msg_and_raise_perror("can't create pipe");
6543 jp = makejob(/*n,*/ 1);
6544 if (forkshell(jp, n, FORK_NOJOB) == 0) {
6550 dup2_or_raise(pip[1], 1);
6553 /* TODO: eflag clearing makes the following not abort:
6554 * ash -c 'set -e; z=$(false;echo foo); echo $z'
6555 * which is what bash does (unless it is in POSIX mode).
6556 * dash deleted "eflag = 0" line in the commit
6557 * Date: Mon, 28 Jun 2010 17:11:58 +1000
6558 * [EVAL] Don't clear eflag in evalbackcmd
6559 * For now, preserve bash-like behavior, it seems to be somewhat more useful:
6563 evaltreenr(n, EV_EXIT);
6568 result->fd = pip[0];
6572 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
6573 result->fd, result->buf, result->nleft, result->jp));
6577 * Expand stuff in backwards quotes.
6580 expbackq(union node *cmd, int flag)
6588 struct stackmark smark;
6591 startloc = expdest - (char *)stackblock();
6592 pushstackmark(&smark, startloc);
6593 evalbackcmd(cmd, &in);
6594 popstackmark(&smark);
6601 memtodest(p, i, flag);
6605 i = nonblock_immune_read(in.fd, buf, sizeof(buf));
6606 TRACE(("expbackq: read returns %d\n", i));
6615 back_exitstatus = waitforjob(in.jp);
6619 /* Eat all trailing newlines */
6621 for (; dest > ((char *)stackblock() + startloc) && dest[-1] == '\n';)
6625 if (!(flag & EXP_QUOTED))
6626 recordregion(startloc, dest - (char *)stackblock(), 0);
6627 TRACE(("evalbackq: size:%d:'%.*s'\n",
6628 (int)((dest - (char *)stackblock()) - startloc),
6629 (int)((dest - (char *)stackblock()) - startloc),
6630 stackblock() + startloc));
6633 #if ENABLE_FEATURE_SH_MATH
6635 * Expand arithmetic expression. Backup to start of expression,
6636 * evaluate, place result in (backed up) result, adjust string position.
6648 * This routine is slightly over-complicated for
6649 * efficiency. Next we scan backwards looking for the
6650 * start of arithmetic.
6652 start = stackblock();
6659 while ((unsigned char)*p != CTLARI) {
6663 ash_msg_and_raise_error("missing CTLARI (shouldn't happen)");
6668 esc = esclen(start, p);
6678 removerecordregions(begoff);
6682 if (flag & QUOTES_ESC)
6683 rmescapes(p + 1, 0, NULL);
6685 len = cvtnum(ash_arith(p + 1), flag);
6687 if (!(flag & EXP_QUOTED))
6688 recordregion(begoff, begoff + len, 0);
6692 /* argstr needs it */
6693 static char *evalvar(char *p, int flags);
6696 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
6697 * characters to allow for further processing. Otherwise treat
6698 * $@ like $* since no splitting will be performed.
6701 argstr(char *p, int flags)
6703 static const char spclchars[] ALIGN1 = {
6711 #if ENABLE_FEATURE_SH_MATH
6716 const char *reject = spclchars;
6717 int breakall = (flags & (EXP_WORD | EXP_QUOTED)) == EXP_WORD;
6722 if (!(flags & EXP_VARTILDE)) {
6724 } else if (flags & EXP_VARTILDE2) {
6729 if (flags & EXP_TILDE) {
6732 flags &= ~EXP_TILDE;
6736 p = exptilde(p, q, flags);
6739 startloc = expdest - (char *)stackblock();
6743 length += strcspn(p + length, reject);
6747 IF_FEATURE_SH_MATH(|| c == CTLENDARI)
6749 /* c == '=' || c == ':' || c == CTLENDARI */
6755 expdest = stack_nputstr(p, length, expdest);
6756 newloc = expdest - (char *)stackblock();
6757 if (breakall && !inquotes && newloc > startloc) {
6758 recordregion(startloc, newloc, 0);
6769 if (flags & EXP_VARTILDE2) {
6773 flags |= EXP_VARTILDE2;
6778 * sort of a hack - expand tildes in variable
6779 * assignments (after the first '=' and after ':'s).
6788 case CTLENDVAR: /* ??? */
6791 /* "$@" syntax adherence hack */
6792 if (!inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) {
6793 p = evalvar(p + 1, flags | EXP_QUOTED) + 1;
6796 inquotes ^= EXP_QUOTED;
6798 if (flags & QUOTES_ESC) {
6809 TRACE(("argstr: evalvar('%s')\n", p));
6810 p = evalvar(p, flags | inquotes);
6811 TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock()));
6814 expbackq(argbackq->n, flags | inquotes);
6815 argbackq = argbackq->next;
6817 #if ENABLE_FEATURE_SH_MATH
6820 expari(flags | inquotes);
6829 scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM,
6830 char *pattern, int quotes, int zero)
6839 const char *s = loc2;
6846 match = pmatch(pattern, s);
6851 if (quotes && (unsigned char)*loc == CTLESC)
6860 scanright(char *startp, char *rmesc, char *rmescend,
6861 char *pattern, int quotes, int match_at_start)
6863 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6864 int try2optimize = match_at_start;
6870 /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}":
6871 * startp="escaped_value_of_v" rmesc="raw_value_of_v"
6872 * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1
6874 * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc,
6875 * and on each iteration they go back two/one char until they reach the beginning.
6876 * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc.
6878 /* TODO: document in what other circumstances we are called. */
6880 for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) {
6883 const char *s = loc2;
6884 if (match_at_start) {
6888 match = pmatch(pattern, s);
6889 //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match);
6893 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6895 /* Maybe we can optimize this:
6896 * if pattern ends with unescaped *, we can avoid checking
6897 * shorter strings: if "foo*" doesn't match "raw_value_of_v",
6898 * it won't match truncated "raw_value_of_" strings too.
6900 unsigned plen = strlen(pattern);
6901 /* Does it end with "*"? */
6902 if (plen != 0 && pattern[--plen] == '*') {
6903 /* "xxxx*" is not escaped */
6904 /* "xxx\*" is escaped */
6905 /* "xx\\*" is not escaped */
6906 /* "x\\\*" is escaped */
6908 while (plen != 0 && pattern[--plen] == '\\')
6911 break; /* ends with unescaped "*" */
6919 esc = esclen(startp, loc);
6930 static void varunset(const char *, const char *, const char *, int) NORETURN;
6932 varunset(const char *end, const char *var, const char *umsg, int varflags)
6938 msg = "parameter not set";
6940 if ((unsigned char)*end == CTLENDVAR) {
6941 if (varflags & VSNUL)
6947 ash_msg_and_raise_error("%.*s: %s%s", (int)(end - var - 1), var, msg, tail);
6951 subevalvar(char *p, char *varname, int strloc, int subtype,
6952 int startloc, int varflags, int flag)
6954 struct nodelist *saveargbackq = argbackq;
6955 int quotes = flag & QUOTES_ESC;
6958 char *rmesc, *rmescend;
6960 int amount, resetloc;
6962 IF_BASH_PATTERN_SUBST(int workloc;)
6963 IF_BASH_PATTERN_SUBST(int slash_pos;)
6964 IF_BASH_PATTERN_SUBST(char *repl;)
6966 char *(*scan)(char*, char*, char*, char*, int, int);
6968 //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)",
6969 // p, varname, strloc, subtype, startloc, varflags, quotes);
6971 #if BASH_PATTERN_SUBST
6972 /* For "${v/pattern/repl}", we must find the delimiter _before_
6973 * argstr() call expands possible variable references in pattern:
6974 * think about "v=a; a=a/; echo ${v/$a/r}" case.
6977 if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
6978 /* Find '/' and replace with NUL */
6980 /* The pattern can't be empty.
6981 * IOW: if the first char after "${v//" is a slash,
6982 * it does not terminate the pattern - it's the first char of the pattern:
6983 * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/")
6984 * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r")
6989 if (*repl == '\0') {
6997 /* Handle escaped slashes, e.g. "${v/\//_}" (they are CTLESC'ed by this point) */
6998 if ((unsigned char)*repl == CTLESC && repl[1])
7004 argstr_flags = EXP_TILDE;
7005 if (subtype != VSASSIGN
7006 && subtype != VSQUESTION
7008 && subtype != VSSUBSTR
7011 /* EXP_CASE keeps CTLESC's */
7012 argstr_flags = EXP_TILDE | EXP_CASE;
7014 argstr(p, argstr_flags);
7015 //bb_error_msg("str0:'%s'", (char *)stackblock() + strloc);
7016 #if BASH_PATTERN_SUBST
7019 slash_pos = expdest - ((char *)stackblock() + strloc);
7020 STPUTC('/', expdest);
7021 //bb_error_msg("repl+1:'%s'", repl + 1);
7022 argstr(repl + 1, EXP_TILDE); /* EXP_TILDE: echo "${v/x/~}" expands ~ ! */
7026 STPUTC('\0', expdest);
7027 argbackq = saveargbackq;
7028 startp = (char *)stackblock() + startloc;
7029 //bb_error_msg("str1:'%s'", (char *)stackblock() + strloc);
7033 setvar0(varname, startp);
7034 amount = startp - expdest;
7035 STADJUST(amount, expdest);
7039 varunset(p, varname, startp, varflags);
7044 int pos, len, orig_len;
7047 loc = str = stackblock() + strloc;
7049 /* Read POS in ${var:POS:LEN} */
7050 colon = strchr(loc, ':');
7051 if (colon) *colon = '\0';
7052 pos = substr_atoi(loc);
7053 if (colon) *colon = ':';
7055 /* Read LEN in ${var:POS:LEN} */
7056 len = str - startp - 1;
7057 /* *loc != '\0', guaranteed by parser */
7060 /* Adjust the length by the number of escapes */
7061 for (ptr = startp; ptr < (str - 1); ptr++) {
7062 if ((unsigned char)*ptr == CTLESC) {
7069 if (*loc++ == ':') {
7071 len = substr_atoi(loc);
7073 /* Skip POS in ${var:POS:LEN} */
7075 while (*loc && *loc != ':')
7078 len = substr_atoi(loc);
7081 /* ${VAR:$((-n)):l} starts n chars from the end */
7082 pos = orig_len + pos;
7084 if ((unsigned)pos >= orig_len) {
7085 /* apart from obvious ${VAR:999999:l},
7086 * covers ${VAR:$((-9999999)):l} - result is ""
7093 /* ${VAR:N:-M} sets LEN to strlen()-M */
7094 len = (orig_len - pos) + len;
7096 if ((unsigned)len > (orig_len - pos))
7097 len = orig_len - pos;
7099 for (str = startp; pos; str++, pos--) {
7100 if (quotes && (unsigned char)*str == CTLESC)
7103 for (loc = startp; len; len--) {
7104 if (quotes && (unsigned char)*str == CTLESC)
7109 amount = loc - expdest;
7110 STADJUST(amount, expdest);
7113 #endif /* BASH_SUBSTR */
7116 resetloc = expdest - (char *)stackblock();
7118 #if BASH_PATTERN_SUBST
7121 /* We'll comeback here if we grow the stack while handling
7122 * a VSREPLACE or VSREPLACEALL, since our pointers into the
7123 * stack will need rebasing, and we'll need to remove our work
7129 amount = expdest - ((char *)stackblock() + resetloc);
7130 STADJUST(-amount, expdest);
7131 startp = (char *)stackblock() + startloc;
7134 rmescend = (char *)stackblock() + strloc;
7135 //bb_error_msg("str7:'%s'", rmescend);
7137 //TODO: how to handle slash_pos here if string changes (shortens?)
7138 rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW, NULL);
7139 if (rmesc != startp) {
7141 startp = (char *)stackblock() + startloc;
7145 str = (char *)stackblock() + strloc;
7147 * Example: v='a\bc'; echo ${v/\\b/_\\_\z_}
7148 * The result is a_\_z_c (not a\_\_z_c)!
7150 * The search pattern and replace string treat backslashes differently!
7151 * "&slash_pos" causes rmescapes() to work differently on the pattern
7152 * and string. It's only used on the first call.
7154 //bb_error_msg("str8:'%s' slash_pos:%d", str, slash_pos);
7155 rmescapes(str, RMESCAPE_GLOB,
7156 repl ? NULL : (slash_pos < 0 ? NULL : &slash_pos)
7159 #if BASH_PATTERN_SUBST
7160 workloc = expdest - (char *)stackblock();
7161 if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
7166 //bb_error_msg("str9:'%s' slash_pos:%d", str, slash_pos);
7168 if (slash_pos >= 0) {
7169 repl = str + slash_pos;
7173 //bb_error_msg("str:'%s' repl:'%s'", str, repl);
7175 /* If there's no pattern to match, return the expansion unmolested */
7184 loc = scanright(idx, rmesc, rmescend, str, quotes, 1);
7185 //bb_error_msg("scanright('%s'):'%s'", str, loc);
7187 /* No match, advance */
7188 char *restart_detect = stackblock();
7190 STPUTC(*idx, expdest);
7191 if (quotes && (unsigned char)*idx == CTLESC) {
7194 STPUTC(*idx, expdest);
7196 if (stackblock() != restart_detect)
7201 /* continue; - prone to quadratic behavior, smarter code: */
7204 if (str[0] == '*') {
7205 /* Pattern is "*foo". If "*foo" does not match "long_string",
7206 * it would never match "ong_string" etc, no point in trying.
7213 if (subtype == VSREPLACEALL) {
7215 if (quotes && (unsigned char)*idx == CTLESC)
7224 //bb_error_msg("repl:'%s'", repl);
7225 for (loc = (char*)repl; *loc; loc++) {
7226 char *restart_detect = stackblock();
7227 if (quotes && *loc == '\\') {
7228 STPUTC(CTLESC, expdest);
7231 STPUTC(*loc, expdest);
7232 if (stackblock() != restart_detect)
7237 if (subtype == VSREPLACE) {
7238 //bb_error_msg("tail:'%s', quotes:%x", idx, quotes);
7240 char *restart_detect = stackblock();
7241 STPUTC(*idx, expdest);
7242 if (stackblock() != restart_detect)
7251 /* We've put the replaced text into a buffer at workloc, now
7252 * move it to the right place and adjust the stack.
7254 STPUTC('\0', expdest);
7255 startp = (char *)stackblock() + startloc;
7256 memmove(startp, (char *)stackblock() + workloc, len + 1);
7257 //bb_error_msg("startp:'%s'", startp);
7258 amount = expdest - (startp + len);
7259 STADJUST(-amount, expdest);
7262 #endif /* BASH_PATTERN_SUBST */
7264 subtype -= VSTRIMRIGHT;
7266 if (subtype < 0 || subtype > 7)
7269 /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */
7270 zero = subtype >> 1;
7271 /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */
7272 scan = (subtype & 1) ^ zero ? scanleft : scanright;
7274 loc = scan(startp, rmesc, rmescend, str, quotes, zero);
7277 memmove(startp, loc, str - loc);
7278 loc = startp + (str - loc) - 1;
7281 amount = loc - expdest;
7282 STADJUST(amount, expdest);
7288 * Add the value of a specialized variable to the stack string.
7289 * name parameter (examples):
7290 * ash -c 'echo $1' name:'1='
7291 * ash -c 'echo $qwe' name:'qwe='
7292 * ash -c 'echo $$' name:'$='
7293 * ash -c 'echo ${$}' name:'$='
7294 * ash -c 'echo ${$##q}' name:'$=q'
7295 * ash -c 'echo ${#$}' name:'$='
7296 * note: examples with bad shell syntax:
7297 * ash -c 'echo ${#$1}' name:'$=1'
7298 * ash -c 'echo ${#1#}' name:'1=#'
7300 static NOINLINE ssize_t
7301 varvalue(char *name, int varflags, int flags, int quoted)
7308 int subtype = varflags & VSTYPE;
7309 int discard = subtype == VSPLUS || subtype == VSLENGTH;
7311 flags |= EXP_KEEPNUL;
7312 flags &= discard ? ~QUOTES_ESC : ~0;
7313 sep = (flags & EXP_FULL) << CHAR_BIT;
7323 num = shellparam.nparam;
7330 len = cvtnum(num, flags);
7331 goto check_1char_name;
7333 expdest = makestrspace(NOPTS, expdest);
7334 for (i = NOPTS - 1; i >= 0; i--) {
7335 if (optlist[i] && optletters(i)) {
7336 USTPUTC(optletters(i), expdest);
7342 /* handles cases similar to ${#$1} */
7343 if (name[2] != '\0')
7344 raise_error_syntax("bad substitution");
7356 /* We will set c to 0 or ~0 depending on whether
7357 * we're doing field splitting. We won't do field
7358 * splitting if either we're quoted or sep is zero.
7360 * Instead of testing (quoted || !sep) the following
7361 * trick optimises away any branches by using the
7362 * fact that EXP_QUOTED (which is the only bit that
7363 * can be set in quoted) is the same as EXP_FULL <<
7364 * CHAR_BIT (which is the only bit that can be set
7367 #if EXP_QUOTED >> CHAR_BIT != EXP_FULL
7368 #error The following two lines expect EXP_QUOTED == EXP_FULL << CHAR_BIT
7370 c = !((quoted | ~sep) & EXP_QUOTED) - 1;
7372 sep |= ifsset() ? (unsigned char)(c & ifsval()[0]) : ' ';
7378 while ((p = *ap++) != NULL) {
7379 len += strtodest(p, flags);
7383 memtodest(&sepc, 1, flags);
7398 num = atoi(name); /* number(name) fails on ${N#str} etc */
7399 if (num < 0 || num > shellparam.nparam)
7401 p = num ? shellparam.p[num - 1] : arg0;
7404 /* NB: name has form "VAR=..." */
7405 p = lookupvar(name);
7410 len = strtodest(p, flags);
7411 #if ENABLE_UNICODE_SUPPORT
7412 if (subtype == VSLENGTH && len > 0) {
7413 reinit_unicode_for_ash();
7414 if (unicode_status == UNICODE_ON) {
7415 STADJUST(-len, expdest);
7417 len = unicode_strlen(p);
7425 STADJUST(-len, expdest);
7430 * Expand a variable, and return a pointer to the next character in the
7434 evalvar(char *p, int flag)
7444 varflags = (unsigned char) *p++;
7445 subtype = varflags & VSTYPE;
7448 raise_error_syntax("bad substitution");
7450 quoted = flag & EXP_QUOTED;
7452 startloc = expdest - (char *)stackblock();
7453 p = strchr(p, '=') + 1; //TODO: use var_end(p)?
7456 varlen = varvalue(var, varflags, flag, quoted);
7457 if (varflags & VSNUL)
7460 if (subtype == VSPLUS) {
7461 varlen = -1 - varlen;
7465 if (subtype == VSMINUS) {
7470 flag | EXP_TILDE | EXP_WORD
7477 if (subtype == VSASSIGN || subtype == VSQUESTION) {
7481 subevalvar(p, var, 0, subtype, startloc, varflags,
7482 flag & ~QUOTES_ESC);
7485 * Remove any recorded regions beyond
7488 removerecordregions(startloc);
7492 if (varlen < 0 && uflag)
7493 varunset(p, var, 0, 0);
7495 if (subtype == VSLENGTH) {
7496 cvtnum(varlen > 0 ? varlen : 0, flag);
7500 if (subtype == VSNORMAL) {
7503 quoted = *var == '@' && shellparam.nparam;
7507 recordregion(startloc, expdest - (char *)stackblock(), quoted);
7516 case VSTRIMRIGHTMAX:
7520 #if BASH_PATTERN_SUBST
7532 * Terminate the string and start recording the pattern
7535 STPUTC('\0', expdest);
7536 patloc = expdest - (char *)stackblock();
7537 if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype,
7538 startloc, varflags, flag)) {
7539 int amount = expdest - (
7540 (char *)stackblock() + patloc - 1
7542 STADJUST(-amount, expdest);
7544 /* Remove any recorded regions beyond start of variable */
7545 removerecordregions(startloc);
7552 if (subtype != VSNORMAL) { /* skip to end of alternative */
7555 unsigned char c = *p++;
7558 else if (c == CTLBACKQ) {
7560 argbackq = argbackq->next;
7561 } else if (c == CTLVAR) {
7562 if ((*p++ & VSTYPE) != VSNORMAL)
7564 } else if (c == CTLENDVAR) {
7574 * Add a file name to the list.
7577 addfname(const char *name)
7581 sp = stzalloc(sizeof(*sp));
7582 sp->text = sstrdup(name);
7584 exparg.lastp = &sp->next;
7587 /* Avoid glob() (and thus, stat() et al) for words like "echo" */
7589 hasmeta(const char *p)
7591 static const char chars[] ALIGN1 = {
7592 '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0
7596 p = strpbrk(p, chars);
7599 switch ((unsigned char)*p) {
7603 if ((unsigned char)*p == CTLQUOTEMARK)
7605 if ((unsigned char)*p == CTLESC)
7607 if (*p == '\0') /* huh? */
7618 if (!strchr(p + 1, ']')) {
7619 /* It's not a properly closed [] pattern,
7620 * but other metas may follow. Continue checking.
7621 * my[file* _is_ globbed by bash
7622 * and matches filenames like "my[file1".
7638 /* If we want to use glob() from libc... */
7639 #if !ENABLE_ASH_INTERNAL_GLOB
7641 /* Add the result of glob() to the list */
7643 addglob(const glob_t *pglob)
7645 char **p = pglob->gl_pathv;
7652 expandmeta(struct strlist *str /*, int flag*/)
7654 /* TODO - EXP_REDIR */
7664 if (!hasmeta(str->text))
7668 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7669 // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match
7670 // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?)
7672 // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping:
7673 // if you pass it "file\?", it returns "file\?", not "file?", if no match.
7674 // Which means you need to unescape the string, right? Not so fast:
7675 // if there _is_ a file named "file\?" (with backslash), it is returned
7676 // as "file\?" too (whichever pattern you used to find it, say, "file*").
7677 // You DON'T KNOW by looking at the result whether you need to unescape it.
7679 // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?",
7680 // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file.
7681 // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match).
7682 // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly.
7683 // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob);
7684 // i = glob(p, GLOB_NOMAGIC, NULL, &pglob);
7685 i = glob(p, 0, NULL, &pglob);
7686 //bb_error_msg("glob('%s'):%d '%s'...", p, i, pglob.gl_pathv ? pglob.gl_pathv[0] : "-");
7691 #if 0 // glibc 2.24.90 bug? Patterns like "*/file", when match, don't set GLOB_MAGCHAR
7692 /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */
7693 if (!(pglob.gl_flags & GLOB_MAGCHAR))
7705 *exparg.lastp = str;
7706 rmescapes(str->text, 0, NULL);
7707 exparg.lastp = &str->next;
7709 default: /* GLOB_NOSPACE */
7712 ash_msg_and_raise_error(bb_msg_memory_exhausted);
7719 /* ENABLE_ASH_INTERNAL_GLOB: Homegrown globbing code. (dash also has both, uses homegrown one.) */
7722 * Do metacharacter (i.e. *, ?, [...]) expansion.
7724 typedef struct exp_t {
7729 expmeta(exp_t *exp, char *name, unsigned name_len, unsigned expdir_len)
7731 #define expdir exp->dir
7732 #define expdir_max exp->dir_max
7733 char *enddir = expdir + expdir_len;
7748 for (p = name; esc = 0, *p; p += esc + 1) {
7749 if (*p == '*' || *p == '?')
7751 else if (*p == '[') {
7758 if (*q == '/' || *q == '\0')
7766 if (*p == '\\' && p[1])
7768 if (p[esc] == '/') {
7771 start = p + esc + 1;
7775 if (metaflag == 0) { /* we've reached the end of the file name */
7780 if (*p == '\\' && p[1])
7784 if (lstat(expdir, &statb) == 0)
7792 if (*p == '\\' && p[1])
7795 } while (p < start);
7799 expdir_len = enddir - cp;
7805 if (*endname == 0) {
7812 name_len -= endname - name;
7819 while (!pending_int && (dp = readdir(dirp)) != NULL) {
7820 if (dp->d_name[0] == '.' && !matchdot)
7822 if (pmatch(start, dp->d_name)) {
7824 strcpy(enddir, dp->d_name);
7830 p = stpcpy(enddir, dp->d_name);
7833 offset = p - expdir + 1;
7834 len = offset + name_len + NAME_MAX;
7835 if (len > expdir_max) {
7837 expdir = ckrealloc(expdir, len);
7841 expmeta(exp, endname, name_len, offset);
7842 enddir = expdir + expdir_len;
7848 endname[-esc - 1] = esc ? '\\' : '/';
7853 static struct strlist *
7854 msort(struct strlist *list, int len)
7856 struct strlist *p, *q = NULL;
7857 struct strlist **lpp;
7865 for (n = half; --n >= 0;) {
7869 q->next = NULL; /* terminate first half of list */
7870 q = msort(list, half); /* sort first half of list */
7871 p = msort(p, len - half); /* sort second half */
7874 #if ENABLE_LOCALE_SUPPORT
7875 if (strcoll(p->text, q->text) < 0)
7877 if (strcmp(p->text, q->text) < 0)
7901 * Sort the results of file name expansion. It calculates the number of
7902 * strings to sort and then calls msort (short for merge sort) to do the
7905 static struct strlist *
7906 expsort(struct strlist *str)
7912 for (sp = str; sp; sp = sp->next)
7914 return msort(str, len);
7918 expandmeta(struct strlist *str /*, int flag*/)
7920 /* TODO - EXP_REDIR */
7924 struct strlist **savelastp;
7931 if (!hasmeta(str->text))
7933 savelastp = exparg.lastp;
7936 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7938 exp.dir_max = len + PATH_MAX;
7939 exp.dir = ckmalloc(exp.dir_max);
7941 expmeta(&exp, p, len, 0);
7946 if (exparg.lastp == savelastp) {
7951 *exparg.lastp = str;
7952 rmescapes(str->text, 0, NULL);
7953 exparg.lastp = &str->next;
7955 *exparg.lastp = NULL;
7956 *savelastp = sp = expsort(*savelastp);
7957 while (sp->next != NULL)
7959 exparg.lastp = &sp->next;
7964 #endif /* ENABLE_ASH_INTERNAL_GLOB */
7967 * Perform variable substitution and command substitution on an argument,
7968 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
7969 * perform splitting and file name expansion. When arglist is NULL, perform
7970 * here document expansion.
7973 expandarg(union node *arg, struct arglist *arglist, int flag)
7978 argbackq = arg->narg.backquote;
7979 STARTSTACKSTR(expdest);
7980 TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag));
7981 argstr(arg->narg.text, flag);
7982 p = _STPUTC('\0', expdest);
7984 if (arglist == NULL) {
7985 /* here document expanded */
7988 p = grabstackstr(p);
7989 TRACE(("expandarg: p:'%s'\n", p));
7990 exparg.lastp = &exparg.list;
7994 if (flag & EXP_FULL) {
7995 ifsbreakup(p, &exparg);
7996 *exparg.lastp = NULL;
7997 exparg.lastp = &exparg.list;
7998 expandmeta(exparg.list /*, flag*/);
8000 sp = stzalloc(sizeof(*sp));
8003 exparg.lastp = &sp->next;
8005 *exparg.lastp = NULL;
8007 *arglist->lastp = exparg.list;
8008 arglist->lastp = exparg.lastp;
8016 * Expand shell variables and backquotes inside a here document.
8019 expandhere(union node *arg, int fd)
8021 expandarg(arg, (struct arglist *)NULL, EXP_QUOTED);
8022 full_write(fd, stackblock(), expdest - (char *)stackblock());
8026 * Returns true if the pattern matches the string.
8029 patmatch(char *pattern, const char *string)
8031 char *p = preglob(pattern, 0);
8032 int r = pmatch(p, string);
8033 //bb_error_msg("!fnmatch(pattern:'%s',str:'%s',0):%d", p, string, r);
8038 * See if a pattern matches in a case statement.
8041 casematch(union node *pattern, char *val)
8043 struct stackmark smark;
8046 setstackmark(&smark);
8047 argbackq = pattern->narg.backquote;
8048 STARTSTACKSTR(expdest);
8049 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
8050 STACKSTRNUL(expdest);
8052 result = patmatch(stackblock(), val);
8053 popstackmark(&smark);
8058 /* ============ find_command */
8062 int (*builtin)(int, char **) FAST_FUNC;
8063 /* unsigned flags; */
8065 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
8066 /* "regular" builtins always take precedence over commands,
8067 * regardless of PATH=....%builtin... position */
8068 #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2)
8069 #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4)
8072 smallint cmdtype; /* CMDxxx */
8075 /* index >= 0 for commands without path (slashes) */
8076 /* (TODO: what exactly does the value mean? PATH position?) */
8077 /* index == -1 for commands with slashes */
8078 /* index == (-2 - applet_no) for NOFORK applets */
8079 const struct builtincmd *cmd;
8080 struct funcnode *func;
8083 /* values of cmdtype */
8084 #define CMDUNKNOWN -1 /* no entry in table for command */
8085 #define CMDNORMAL 0 /* command is an executable program */
8086 #define CMDFUNCTION 1 /* command is a shell function */
8087 #define CMDBUILTIN 2 /* command is a shell builtin */
8089 /* action to find_command() */
8090 #define DO_ERR 0x01 /* prints errors */
8091 #define DO_ABS 0x02 /* checks absolute paths */
8092 #define DO_NOFUNC 0x04 /* don't return shell functions, for command */
8093 #define DO_ALTPATH 0x08 /* using alternate path */
8094 #define DO_REGBLTIN 0x10 /* regular built-ins and functions only */
8096 static void find_command(char *, struct cmdentry *, int, const char *);
8099 /* ============ Hashing commands */
8102 * When commands are first encountered, they are entered in a hash table.
8103 * This ensures that a full path search will not have to be done for them
8104 * on each invocation.
8106 * We should investigate converting to a linear search, even though that
8107 * would make the command name "hash" a misnomer.
8111 struct tblentry *next; /* next entry in hash chain */
8112 union param param; /* definition of builtin function */
8113 smallint cmdtype; /* CMDxxx */
8114 char rehash; /* if set, cd done since entry created */
8115 char cmdname[1]; /* name of command */
8118 static struct tblentry **cmdtable;
8119 #define INIT_G_cmdtable() do { \
8120 cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \
8123 static int builtinloc = -1; /* index in path of %builtin, or -1 */
8127 tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, char **envp)
8129 #if ENABLE_FEATURE_SH_STANDALONE
8130 if (applet_no >= 0) {
8131 if (APPLET_IS_NOEXEC(applet_no)) {
8135 popredir(/*drop:*/ 1);
8136 run_noexec_applet_and_exit(applet_no, cmd, argv);
8138 /* re-exec ourselves with the new arguments */
8139 execve(bb_busybox_exec_path, argv, envp);
8140 /* If they called chroot or otherwise made the binary no longer
8141 * executable, fall through */
8148 execve(cmd, argv, envp);
8149 } while (errno == EINTR);
8151 execve(cmd, argv, envp);
8154 if (cmd != bb_busybox_exec_path && errno == ENOEXEC) {
8155 /* Run "cmd" as a shell script:
8156 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
8157 * "If the execve() function fails with ENOEXEC, the shell
8158 * shall execute a command equivalent to having a shell invoked
8159 * with the command name as its first operand,
8160 * with any remaining arguments passed to the new shell"
8162 * That is, do not use $SHELL, user's shell, or /bin/sh;
8163 * just call ourselves.
8165 * Note that bash reads ~80 chars of the file, and if it sees
8166 * a zero byte before it sees newline, it doesn't try to
8167 * interpret it, but fails with "cannot execute binary file"
8168 * message and exit code 126. For one, this prevents attempts
8169 * to interpret foreign ELF binaries as shell scripts.
8171 argv[0] = (char*) cmd;
8172 cmd = bb_busybox_exec_path;
8173 /* NB: this is only possible because all callers of shellexec()
8174 * ensure that the argv[-1] slot exists!
8177 argv[0] = (char*) "ash";
8183 * Exec a program. Never returns. If you change this routine, you may
8184 * have to change the find_command routine as well.
8185 * argv[-1] must exist and be writable! See tryexec() for why.
8187 static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN;
8188 static void shellexec(char *prog, char **argv, const char *path, int idx)
8194 int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */
8196 envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL);
8197 if (strchr(prog, '/') != NULL
8198 #if ENABLE_FEATURE_SH_STANDALONE
8199 || (applet_no = find_applet_by_name(prog)) >= 0
8202 tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp);
8203 if (applet_no >= 0) {
8204 /* We tried execing ourself, but it didn't work.
8205 * Maybe /proc/self/exe doesn't exist?
8214 while (padvance(&path, argv[0]) >= 0) {
8215 cmdname = stackblock();
8216 if (--idx < 0 && pathopt == NULL) {
8217 tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
8218 if (errno != ENOENT && errno != ENOTDIR)
8224 /* Map to POSIX errors */
8236 exitstatus = exerrno;
8237 TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n",
8238 prog, e, suppress_int));
8239 ash_msg_and_raise(EXEND, "%s: %s", prog, errmsg(e, "not found"));
8244 printentry(struct tblentry *cmdp)
8250 idx = cmdp->param.index;
8253 padvance(&path, cmdp->cmdname);
8254 } while (--idx >= 0);
8255 name = stackblock();
8256 out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr));
8260 * Clear out command entries.
8265 struct tblentry **tblp;
8266 struct tblentry **pp;
8267 struct tblentry *cmdp;
8270 for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) {
8272 while ((cmdp = *pp) != NULL) {
8273 if (cmdp->cmdtype == CMDNORMAL
8274 || (cmdp->cmdtype == CMDBUILTIN
8275 && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
8290 * Locate a command in the command hash table. If "add" is nonzero,
8291 * add the command to the table if it is not already present. The
8292 * variable "lastcmdentry" is set to point to the address of the link
8293 * pointing to the entry, so that delete_cmd_entry can delete the
8296 * Interrupts must be off if called with add != 0.
8298 static struct tblentry **lastcmdentry;
8300 static struct tblentry *
8301 cmdlookup(const char *name, int add)
8303 unsigned int hashval;
8305 struct tblentry *cmdp;
8306 struct tblentry **pp;
8309 hashval = (unsigned char)*p << 4;
8311 hashval += (unsigned char)*p++;
8313 pp = &cmdtable[hashval % CMDTABLESIZE];
8314 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8315 if (strcmp(cmdp->cmdname, name) == 0)
8319 if (add && cmdp == NULL) {
8320 cmdp = *pp = ckzalloc(sizeof(struct tblentry)
8322 /* + 1 - already done because
8323 * tblentry::cmdname is char[1] */);
8324 /*cmdp->next = NULL; - ckzalloc did it */
8325 cmdp->cmdtype = CMDUNKNOWN;
8326 strcpy(cmdp->cmdname, name);
8333 * Delete the command entry returned on the last lookup.
8336 delete_cmd_entry(void)
8338 struct tblentry *cmdp;
8341 cmdp = *lastcmdentry;
8342 *lastcmdentry = cmdp->next;
8343 if (cmdp->cmdtype == CMDFUNCTION)
8344 freefunc(cmdp->param.func);
8350 * Add a new command entry, replacing any existing command entry for
8351 * the same name - except special builtins.
8354 addcmdentry(char *name, struct cmdentry *entry)
8356 struct tblentry *cmdp;
8358 cmdp = cmdlookup(name, 1);
8359 if (cmdp->cmdtype == CMDFUNCTION) {
8360 freefunc(cmdp->param.func);
8362 cmdp->cmdtype = entry->cmdtype;
8363 cmdp->param = entry->u;
8367 static int FAST_FUNC
8368 hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8370 struct tblentry **pp;
8371 struct tblentry *cmdp;
8373 struct cmdentry entry;
8376 if (nextopt("r") != '\0') {
8381 if (*argptr == NULL) {
8382 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8383 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8384 if (cmdp->cmdtype == CMDNORMAL)
8392 while ((name = *argptr) != NULL) {
8393 cmdp = cmdlookup(name, 0);
8395 && (cmdp->cmdtype == CMDNORMAL
8396 || (cmdp->cmdtype == CMDBUILTIN
8397 && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
8404 find_command(name, &entry, DO_ERR, pathval());
8405 if (entry.cmdtype == CMDUNKNOWN)
8413 * Called when a cd is done. Marks all commands so the next time they
8414 * are executed they will be rehashed.
8419 struct tblentry **pp;
8420 struct tblentry *cmdp;
8422 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8423 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8424 if (cmdp->cmdtype == CMDNORMAL
8425 || (cmdp->cmdtype == CMDBUILTIN
8426 && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
8436 * Fix command hash table when PATH changed.
8437 * Called before PATH is changed. The argument is the new value of PATH;
8438 * pathval() still returns the old value at this point.
8439 * Called with interrupts off.
8441 static void FAST_FUNC
8442 changepath(const char *newval)
8452 if (*new == '%' && prefix(new + 1, "builtin")) {
8456 new = strchr(new, ':');
8499 typedef smallint token_id_t;
8501 /* Nth bit indicates if token marks the end of a list */
8504 /* 0 */ | (1u << TEOF)
8505 /* 1 */ | (0u << TNL)
8506 /* 2 */ | (0u << TREDIR)
8507 /* 3 */ | (0u << TWORD)
8508 /* 4 */ | (0u << TSEMI)
8509 /* 5 */ | (0u << TBACKGND)
8510 /* 6 */ | (0u << TAND)
8511 /* 7 */ | (0u << TOR)
8512 /* 8 */ | (0u << TPIPE)
8513 /* 9 */ | (0u << TLP)
8514 /* 10 */ | (1u << TRP)
8515 /* 11 */ | (1u << TENDCASE)
8516 /* 12 */ | (1u << TENDBQUOTE)
8517 /* 13 */ | (0u << TNOT)
8518 /* 14 */ | (0u << TCASE)
8519 /* 15 */ | (1u << TDO)
8520 /* 16 */ | (1u << TDONE)
8521 /* 17 */ | (1u << TELIF)
8522 /* 18 */ | (1u << TELSE)
8523 /* 19 */ | (1u << TESAC)
8524 /* 20 */ | (1u << TFI)
8525 /* 21 */ | (0u << TFOR)
8527 /* 22 */ | (0u << TFUNCTION)
8529 /* 23 */ | (0u << TIF)
8530 /* 24 */ | (0u << TIN)
8531 /* 25 */ | (1u << TTHEN)
8532 /* 26 */ | (0u << TUNTIL)
8533 /* 27 */ | (0u << TWHILE)
8534 /* 28 */ | (0u << TBEGIN)
8535 /* 29 */ | (1u << TEND)
8536 , /* thus far 29 bits used */
8539 static const char *const tokname_array[] = {
8553 #define KWDOFFSET 13
8554 /* the following are keywords */
8576 /* Wrapper around strcmp for qsort/bsearch/... */
8578 pstrcmp(const void *a, const void *b)
8580 return strcmp((char*)a, *(char**)b);
8583 static const char *const *
8584 findkwd(const char *s)
8586 return bsearch(s, tokname_array + KWDOFFSET,
8587 ARRAY_SIZE(tokname_array) - KWDOFFSET,
8588 sizeof(tokname_array[0]), pstrcmp);
8592 * Locate and print what a word is...
8595 describe_command(char *command, const char *path, int describe_command_verbose)
8597 struct cmdentry entry;
8598 #if ENABLE_ASH_ALIAS
8599 const struct alias *ap;
8602 path = path ? path : pathval();
8604 if (describe_command_verbose) {
8608 /* First look at the keywords */
8609 if (findkwd(command)) {
8610 out1str(describe_command_verbose ? " is a shell keyword" : command);
8614 #if ENABLE_ASH_ALIAS
8615 /* Then look at the aliases */
8616 ap = lookupalias(command, 0);
8618 if (!describe_command_verbose) {
8623 out1fmt(" is an alias for %s", ap->val);
8628 find_command(command, &entry, DO_ABS, path);
8630 switch (entry.cmdtype) {
8632 int j = entry.u.index;
8638 padvance(&path, command);
8642 if (describe_command_verbose) {
8643 out1fmt(" is %s", p);
8651 if (describe_command_verbose) {
8652 /*out1str(" is a shell function");*/
8653 out1str(" is a function"); /* bash says this */
8660 if (describe_command_verbose) {
8661 out1fmt(" is a %sshell builtin",
8662 IS_BUILTIN_SPECIAL(entry.u.cmd) ?
8663 "special " : nullstr
8671 if (describe_command_verbose) {
8672 out1str(": not found\n");
8681 static int FAST_FUNC
8682 typecmd(int argc UNUSED_PARAM, char **argv)
8688 /* type -p ... ? (we don't bother checking for 'p') */
8689 if (argv[1] && argv[1][0] == '-') {
8694 err |= describe_command(argv[i++], NULL, verbose);
8699 #if ENABLE_ASH_CMDCMD
8700 static struct strlist *
8701 fill_arglist(struct arglist *arglist, union node **argpp)
8703 struct strlist **lastp = arglist->lastp;
8706 while ((argp = *argpp) != NULL) {
8707 expandarg(argp, arglist, EXP_FULL | EXP_TILDE);
8708 *argpp = argp->narg.next;
8716 /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */
8718 parse_command_args(struct arglist *arglist, union node **argpp, const char **path)
8720 struct strlist *sp = arglist->list;
8724 sp = sp->next ? sp->next : fill_arglist(arglist, argpp);
8733 if (c == '-' && !*cp) {
8734 if (!sp->next && !fill_arglist(arglist, argpp))
8742 *path = bb_default_path;
8745 /* run 'typecmd' for other options */
8756 static int FAST_FUNC
8757 commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8765 const char *path = NULL;
8767 /* "command [-p] PROG ARGS" (that is, without -V or -v)
8768 * never reaches this function.
8771 while ((c = nextopt("pvV")) != '\0')
8773 verify |= VERIFY_VERBOSE;
8775 /*verify |= VERIFY_BRIEF*/;
8781 path = bb_default_path;
8783 /* Mimic bash: just "command -v" doesn't complain, it's a nop */
8785 if (/*verify && */ cmd)
8786 return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/);
8793 /*static int funcblocksize; // size of structures in function */
8794 /*static int funcstringsize; // size of strings in node */
8795 static void *funcblock; /* block to allocate function from */
8796 static char *funcstring_end; /* end of block to allocate strings from */
8798 static const uint8_t nodesize[N_NUMBER] ALIGN1 = {
8799 [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)),
8800 [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)),
8801 [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)),
8802 [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)),
8803 [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)),
8804 [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)),
8805 [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)),
8806 [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)),
8807 [NIF ] = SHELL_ALIGN(sizeof(struct nif)),
8808 [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)),
8809 [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)),
8810 [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)),
8811 [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)),
8812 [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)),
8813 [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)),
8814 [NARG ] = SHELL_ALIGN(sizeof(struct narg)),
8815 [NTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8816 #if BASH_REDIR_OUTPUT
8817 [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)),
8819 [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)),
8820 [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)),
8821 [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8822 [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)),
8823 [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8824 [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8825 [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8826 [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8827 [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)),
8830 static int calcsize(int funcblocksize, union node *n);
8833 sizenodelist(int funcblocksize, struct nodelist *lp)
8836 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
8837 funcblocksize = calcsize(funcblocksize, lp->n);
8840 return funcblocksize;
8844 calcsize(int funcblocksize, union node *n)
8847 return funcblocksize;
8848 funcblocksize += nodesize[n->type];
8851 funcblocksize = calcsize(funcblocksize, n->ncmd.redirect);
8852 funcblocksize = calcsize(funcblocksize, n->ncmd.args);
8853 funcblocksize = calcsize(funcblocksize, n->ncmd.assign);
8856 funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist);
8861 funcblocksize = calcsize(funcblocksize, n->nredir.redirect);
8862 funcblocksize = calcsize(funcblocksize, n->nredir.n);
8869 funcblocksize = calcsize(funcblocksize, n->nbinary.ch2);
8870 funcblocksize = calcsize(funcblocksize, n->nbinary.ch1);
8873 funcblocksize = calcsize(funcblocksize, n->nif.elsepart);
8874 funcblocksize = calcsize(funcblocksize, n->nif.ifpart);
8875 funcblocksize = calcsize(funcblocksize, n->nif.test);
8878 funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */
8879 funcblocksize = calcsize(funcblocksize, n->nfor.body);
8880 funcblocksize = calcsize(funcblocksize, n->nfor.args);
8883 funcblocksize = calcsize(funcblocksize, n->ncase.cases);
8884 funcblocksize = calcsize(funcblocksize, n->ncase.expr);
8887 funcblocksize = calcsize(funcblocksize, n->nclist.body);
8888 funcblocksize = calcsize(funcblocksize, n->nclist.pattern);
8889 funcblocksize = calcsize(funcblocksize, n->nclist.next);
8892 funcblocksize = calcsize(funcblocksize, n->ndefun.body);
8893 funcblocksize += SHELL_ALIGN(strlen(n->ndefun.text) + 1);
8896 funcblocksize = sizenodelist(funcblocksize, n->narg.backquote);
8897 funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */
8898 funcblocksize = calcsize(funcblocksize, n->narg.next);
8901 #if BASH_REDIR_OUTPUT
8908 funcblocksize = calcsize(funcblocksize, n->nfile.fname);
8909 funcblocksize = calcsize(funcblocksize, n->nfile.next);
8913 funcblocksize = calcsize(funcblocksize, n->ndup.vname);
8914 funcblocksize = calcsize(funcblocksize, n->ndup.next);
8918 funcblocksize = calcsize(funcblocksize, n->nhere.doc);
8919 funcblocksize = calcsize(funcblocksize, n->nhere.next);
8922 funcblocksize = calcsize(funcblocksize, n->nnot.com);
8925 return funcblocksize;
8929 nodeckstrdup(char *s)
8931 funcstring_end -= SHELL_ALIGN(strlen(s) + 1);
8932 return strcpy(funcstring_end, s);
8935 static union node *copynode(union node *);
8937 static struct nodelist *
8938 copynodelist(struct nodelist *lp)
8940 struct nodelist *start;
8941 struct nodelist **lpp;
8946 funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist));
8947 (*lpp)->n = copynode(lp->n);
8949 lpp = &(*lpp)->next;
8956 copynode(union node *n)
8963 funcblock = (char *) funcblock + nodesize[n->type];
8967 new->ncmd.redirect = copynode(n->ncmd.redirect);
8968 new->ncmd.args = copynode(n->ncmd.args);
8969 new->ncmd.assign = copynode(n->ncmd.assign);
8970 new->ncmd.linno = n->ncmd.linno;
8973 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
8974 new->npipe.pipe_backgnd = n->npipe.pipe_backgnd;
8979 new->nredir.redirect = copynode(n->nredir.redirect);
8980 new->nredir.n = copynode(n->nredir.n);
8981 new->nredir.linno = n->nredir.linno;
8988 new->nbinary.ch2 = copynode(n->nbinary.ch2);
8989 new->nbinary.ch1 = copynode(n->nbinary.ch1);
8992 new->nif.elsepart = copynode(n->nif.elsepart);
8993 new->nif.ifpart = copynode(n->nif.ifpart);
8994 new->nif.test = copynode(n->nif.test);
8997 new->nfor.var = nodeckstrdup(n->nfor.var);
8998 new->nfor.body = copynode(n->nfor.body);
8999 new->nfor.args = copynode(n->nfor.args);
9000 new->nfor.linno = n->nfor.linno;
9003 new->ncase.cases = copynode(n->ncase.cases);
9004 new->ncase.expr = copynode(n->ncase.expr);
9005 new->ncase.linno = n->ncase.linno;
9008 new->nclist.body = copynode(n->nclist.body);
9009 new->nclist.pattern = copynode(n->nclist.pattern);
9010 new->nclist.next = copynode(n->nclist.next);
9013 new->ndefun.body = copynode(n->ndefun.body);
9014 new->ndefun.text = nodeckstrdup(n->ndefun.text);
9015 new->ndefun.linno = n->ndefun.linno;
9018 new->narg.backquote = copynodelist(n->narg.backquote);
9019 new->narg.text = nodeckstrdup(n->narg.text);
9020 new->narg.next = copynode(n->narg.next);
9023 #if BASH_REDIR_OUTPUT
9030 new->nfile.fname = copynode(n->nfile.fname);
9031 new->nfile.fd = n->nfile.fd;
9032 new->nfile.next = copynode(n->nfile.next);
9036 new->ndup.vname = copynode(n->ndup.vname);
9037 new->ndup.dupfd = n->ndup.dupfd;
9038 new->ndup.fd = n->ndup.fd;
9039 new->ndup.next = copynode(n->ndup.next);
9043 new->nhere.doc = copynode(n->nhere.doc);
9044 new->nhere.fd = n->nhere.fd;
9045 new->nhere.next = copynode(n->nhere.next);
9048 new->nnot.com = copynode(n->nnot.com);
9051 new->type = n->type;
9056 * Make a copy of a parse tree.
9058 static struct funcnode *
9059 copyfunc(union node *n)
9064 /*funcstringsize = 0;*/
9065 blocksize = offsetof(struct funcnode, n) + calcsize(0, n);
9066 f = ckzalloc(blocksize /* + funcstringsize */);
9067 funcblock = (char *) f + offsetof(struct funcnode, n);
9068 funcstring_end = (char *) f + blocksize;
9070 /* f->count = 0; - ckzalloc did it */
9075 * Define a shell function.
9078 defun(union node *func)
9080 struct cmdentry entry;
9083 entry.cmdtype = CMDFUNCTION;
9084 entry.u.func = copyfunc(func);
9085 addcmdentry(func->ndefun.text, &entry);
9089 /* Reasons for skipping commands (see comment on breakcmd routine) */
9090 #define SKIPBREAK (1 << 0)
9091 #define SKIPCONT (1 << 1)
9092 #define SKIPFUNC (1 << 2)
9093 #define SKIPFUNCDEF (1 << 3)
9094 static smallint evalskip; /* set to SKIPxxx if we are skipping commands */
9095 static int skipcount; /* number of levels to skip */
9096 static int loopnest; /* current loop nesting level */
9097 static int funcline; /* starting line number of current function, or 0 if not in a function */
9099 /* Forward decl way out to parsing code - dotrap needs it */
9100 static int evalstring(char *s, int flags);
9102 /* Called to execute a trap.
9103 * Single callsite - at the end of evaltree().
9104 * If we return non-zero, evaltree raises EXEXIT exception.
9106 * Perhaps we should avoid entering new trap handlers
9107 * while we are executing a trap handler. [is it a TODO?]
9114 int status, last_status;
9119 status = savestatus;
9120 last_status = status;
9122 status = exitstatus;
9123 savestatus = status;
9128 TRACE(("dotrap entered\n"));
9129 for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) {
9141 /* non-trapped SIGINT is handled separately by raise_interrupt,
9142 * don't upset it by resetting gotsig[SIGINT-1] */
9143 if (sig == SIGINT && !p)
9146 TRACE(("sig %d is active, will run handler '%s'\n", sig, p));
9151 if (evalskip != SKIPFUNC)
9152 exitstatus = status;
9155 savestatus = last_status;
9156 TRACE(("dotrap returns\n"));
9159 /* forward declarations - evaluation is fairly recursive business... */
9160 static int evalloop(union node *, int);
9161 static int evalfor(union node *, int);
9162 static int evalcase(union node *, int);
9163 static int evalsubshell(union node *, int);
9164 static void expredir(union node *);
9165 static int evalpipe(union node *, int);
9166 static int evalcommand(union node *, int);
9167 static int evalbltin(const struct builtincmd *, int, char **, int);
9168 static void prehash(union node *);
9171 * Evaluate a parse tree. The value is left in the global variable
9175 evaltree(union node *n, int flags)
9178 int (*evalfn)(union node *, int);
9179 struct stackmark smark;
9182 setstackmark(&smark);
9185 TRACE(("evaltree(NULL) called\n"));
9188 TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags));
9195 out1fmt("Node type = %d\n", n->type);
9200 status = !evaltree(n->nnot.com, EV_TESTED);
9203 errlinno = lineno = n->nredir.linno;
9205 lineno -= funcline - 1;
9206 expredir(n->nredir.redirect);
9207 pushredir(n->nredir.redirect);
9208 status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
9210 status = evaltree(n->nredir.n, flags & EV_TESTED);
9212 if (n->nredir.redirect)
9213 popredir(/*drop:*/ 0);
9216 evalfn = evalcommand;
9218 if (eflag && !(flags & EV_TESTED))
9230 evalfn = evalsubshell;
9243 #error NAND + 1 != NOR
9245 #if NOR + 1 != NSEMI
9246 #error NOR + 1 != NSEMI
9248 unsigned is_or = n->type - NAND;
9251 (flags | ((is_or >> 1) - 1)) & EV_TESTED
9253 if ((!status) == is_or || evalskip)
9259 status = evalfn(n, flags);
9263 status = evaltree(n->nif.test, EV_TESTED);
9270 if (n->nif.elsepart) {
9271 n = n->nif.elsepart;
9278 /* Not necessary. To test it:
9279 * "false; f() { qwerty; }; echo $?" should print 0.
9283 exitstatus = status;
9287 /* Order of checks below is important:
9288 * signal handlers trigger before exit caused by "set -e".
9292 if (checkexit & status)
9293 raise_exception(EXEND);
9294 if (flags & EV_EXIT)
9295 raise_exception(EXEND);
9297 popstackmark(&smark);
9298 TRACE(("leaving evaltree (no interrupts)\n"));
9305 int skip = evalskip;
9312 if (--skipcount <= 0) {
9323 evalloop(union node *n, int flags)
9334 i = evaltree(n->nbinary.ch1, EV_TESTED);
9336 if (skip == SKIPFUNC)
9340 if (n->type != NWHILE)
9344 status = evaltree(n->nbinary.ch2, flags);
9346 } while (!(skip & ~SKIPCONT));
9353 evalfor(union node *n, int flags)
9355 struct arglist arglist;
9360 errlinno = lineno = n->ncase.linno;
9362 lineno -= funcline - 1;
9364 arglist.list = NULL;
9365 arglist.lastp = &arglist.list;
9366 for (argp = n->nfor.args; argp; argp = argp->narg.next) {
9367 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
9369 *arglist.lastp = NULL;
9373 for (sp = arglist.list; sp; sp = sp->next) {
9374 setvar0(n->nfor.var, sp->text);
9375 status = evaltree(n->nfor.body, flags);
9376 if (skiploop() & ~SKIPCONT)
9385 evalcase(union node *n, int flags)
9389 struct arglist arglist;
9392 errlinno = lineno = n->ncase.linno;
9394 lineno -= funcline - 1;
9396 arglist.list = NULL;
9397 arglist.lastp = &arglist.list;
9398 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
9399 for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
9400 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
9401 if (casematch(patp, arglist.list->text)) {
9402 /* Ensure body is non-empty as otherwise
9403 * EV_EXIT may prevent us from setting the
9406 if (evalskip == 0 && cp->nclist.body) {
9407 status = evaltree(cp->nclist.body, flags);
9418 * Kick off a subshell to evaluate a tree.
9421 evalsubshell(union node *n, int flags)
9424 int backgnd = (n->type == NBACKGND); /* FORK_BG(1) if yes, else FORK_FG(0) */
9427 errlinno = lineno = n->nredir.linno;
9429 lineno -= funcline - 1;
9431 expredir(n->nredir.redirect);
9432 if (!backgnd && (flags & EV_EXIT) && !may_have_traps)
9435 if (backgnd == FORK_FG)
9437 jp = makejob(/*n,*/ 1);
9438 if (forkshell(jp, n, backgnd) == 0) {
9443 flags &= ~EV_TESTED;
9445 redirect(n->nredir.redirect, 0);
9446 evaltreenr(n->nredir.n, flags);
9451 if (backgnd == FORK_FG)
9452 status = waitforjob(jp);
9458 * Compute the names of the files in a redirection list.
9460 static void fixredir(union node *, const char *, int);
9462 expredir(union node *n)
9466 for (redir = n; redir; redir = redir->nfile.next) {
9470 fn.lastp = &fn.list;
9471 switch (redir->type) {
9475 #if BASH_REDIR_OUTPUT
9480 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
9481 TRACE(("expredir expanded to '%s'\n", fn.list->text));
9482 #if BASH_REDIR_OUTPUT
9486 // By the design of stack allocator, the loop of this kind:
9487 // while true; do while true; do break; done </dev/null; done
9488 // will look like a memory leak: ash plans to free expfname's
9489 // of "/dev/null" as soon as it finishes running the loop
9490 // (in this case, never).
9491 // This "fix" is wrong:
9492 if (redir->nfile.expfname)
9493 stunalloc(redir->nfile.expfname);
9494 // It results in corrupted state of stacked allocations.
9496 redir->nfile.expfname = fn.list->text;
9499 case NTOFD: /* >& */
9500 if (redir->ndup.vname) {
9501 expandarg(redir->ndup.vname, &fn, EXP_TILDE | EXP_REDIR);
9502 if (fn.list == NULL)
9503 ash_msg_and_raise_error("redir error");
9504 #if BASH_REDIR_OUTPUT
9505 if (!isdigit_str9(fn.list->text)) {
9506 /* >&file, not >&fd */
9507 if (redir->nfile.fd != 1) /* 123>&file - BAD */
9508 ash_msg_and_raise_error("redir error");
9510 goto store_expfname;
9513 fixredir(redir, fn.list->text, 1);
9521 * Evaluate a pipeline. All the processes in the pipeline are children
9522 * of the process creating the pipeline. (This differs from some versions
9523 * of the shell, which make the last process in a pipeline the parent
9527 evalpipe(union node *n, int flags)
9530 struct nodelist *lp;
9536 TRACE(("evalpipe(0x%lx) called\n", (long)n));
9538 for (lp = n->npipe.cmdlist; lp; lp = lp->next)
9542 if (n->npipe.pipe_backgnd == 0)
9544 jp = makejob(/*n,*/ pipelen);
9546 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
9550 if (pipe(pip) < 0) {
9552 ash_msg_and_raise_perror("can't create pipe");
9555 if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) {
9569 evaltreenr(lp->n, flags);
9576 /* Don't want to trigger debugging */
9580 if (n->npipe.pipe_backgnd == 0) {
9581 status = waitforjob(jp);
9582 TRACE(("evalpipe: job done exit status %d\n", status));
9589 /* setinteractive needs this forward reference */
9590 #if EDITING_HAS_get_exe_name
9591 static const char *get_builtin_name(int i) FAST_FUNC;
9595 * Controls whether the shell is interactive or not.
9598 setinteractive(int on)
9600 static smallint is_interactive;
9602 if (++on == is_interactive)
9604 is_interactive = on;
9608 if (is_interactive > 1) {
9609 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
9610 /* Looks like they want an interactive shell */
9611 static smallint did_banner;
9614 /* note: ash and hush share this string */
9615 out1fmt("\n\n%s %s\n"
9616 IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n")
9619 "built-in shell (ash)"
9624 #if ENABLE_FEATURE_EDITING
9625 if (!line_input_state) {
9626 line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
9627 # if EDITING_HAS_get_exe_name
9628 line_input_state->get_exe_name = get_builtin_name;
9641 setinteractive(iflag);
9643 #if ENABLE_FEATURE_EDITING_VI
9644 if (line_input_state) {
9646 line_input_state->flags |= VI_MODE;
9648 line_input_state->flags &= ~VI_MODE;
9651 viflag = 0; /* forcibly keep the option off */
9655 struct localvar_list {
9656 struct localvar_list *next;
9657 struct localvar *lv;
9660 static struct localvar_list *localvar_stack;
9663 * Called after a function returns.
9664 * Interrupts must be off.
9667 poplocalvars(int keep)
9669 struct localvar_list *ll;
9670 struct localvar *lvp, *next;
9674 ll = localvar_stack;
9675 localvar_stack = ll->next;
9680 while ((lvp = next) != NULL) {
9683 TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-"));
9685 int bits = VSTRFIXED;
9687 if (lvp->flags != VUNSET) {
9688 if (vp->var_text == lvp->text)
9690 else if (!(lvp->flags & (VTEXTFIXED|VSTACK)))
9691 free((char*)lvp->text);
9695 vp->flags |= (lvp->flags & bits);
9698 (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
9699 unsetvar(vp->var_text);
9700 } else if (vp == NULL) { /* $- saved */
9701 memcpy(optlist, lvp->text, sizeof(optlist));
9702 free((char*)lvp->text);
9704 } else if (lvp->flags == VUNSET) {
9705 vp->flags &= ~(VSTRFIXED|VREADONLY);
9706 unsetvar(vp->var_text);
9709 vp->var_func(var_end(lvp->text));
9710 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
9711 free((char*)vp->var_text);
9712 vp->flags = lvp->flags;
9713 vp->var_text = lvp->text;
9721 * Create a new localvar environment.
9723 static struct localvar_list *
9724 pushlocalvars(int push)
9726 struct localvar_list *ll;
9727 struct localvar_list *top;
9729 top = localvar_stack;
9734 ll = ckzalloc(sizeof(*ll));
9735 /*ll->lv = NULL; - zalloc did it */
9737 localvar_stack = ll;
9744 unwindlocalvars(struct localvar_list *stop)
9746 while (localvar_stack != stop)
9751 evalfun(struct funcnode *func, int argc, char **argv, int flags)
9753 volatile struct shparam saveparam;
9754 struct jmploc *volatile savehandler;
9755 struct jmploc jmploc;
9759 saveparam = shellparam;
9760 savefuncline = funcline;
9761 savehandler = exception_handler;
9762 e = setjmp(jmploc.loc);
9767 exception_handler = &jmploc;
9768 shellparam.malloced = 0;
9770 funcline = func->n.ndefun.linno;
9772 shellparam.nparam = argc - 1;
9773 shellparam.p = argv + 1;
9774 #if ENABLE_ASH_GETOPTS
9775 shellparam.optind = 1;
9776 shellparam.optoff = -1;
9778 evaltree(func->n.ndefun.body, flags & EV_TESTED);
9781 funcline = savefuncline;
9783 freeparam(&shellparam);
9784 shellparam = saveparam;
9785 exception_handler = savehandler;
9787 evalskip &= ~(SKIPFUNC | SKIPFUNCDEF);
9792 * Make a variable a local variable. When a variable is made local, it's
9793 * value and flags are saved in a localvar structure. The saved values
9794 * will be restored when the shell function returns. We handle the name
9795 * "-" as a special case: it makes changes to "set +-options" local
9796 * (options will be restored on return from the function).
9799 mklocal(char *name, int flags)
9801 struct localvar *lvp;
9804 char *eq = strchr(name, '=');
9807 /* Cater for duplicate "local". Examples:
9808 * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x
9809 * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x
9811 lvp = localvar_stack->lv;
9813 if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) {
9817 * it's a duplicate "local VAR" declaration, do nothing
9824 lvp = ckzalloc(sizeof(*lvp));
9825 if (LONE_DASH(name)) {
9827 p = ckmalloc(sizeof(optlist));
9828 lvp->text = memcpy(p, optlist, sizeof(optlist));
9831 vpp = hashvar(name);
9832 vp = *findvar(vpp, name);
9834 /* variable did not exist yet */
9836 vp = setvareq(name, VSTRFIXED | flags);
9838 vp = setvar(name, NULL, VSTRFIXED | flags);
9839 lvp->flags = VUNSET;
9841 lvp->text = vp->var_text;
9842 lvp->flags = vp->flags;
9843 /* make sure neither "struct var" nor string gets freed
9844 * during (un)setting:
9846 vp->flags |= VSTRFIXED|VTEXTFIXED;
9848 setvareq(name, flags);
9850 /* "local VAR" unsets VAR: */
9851 setvar0(name, NULL);
9855 lvp->next = localvar_stack->lv;
9856 localvar_stack->lv = lvp;
9862 * The "local" command.
9864 static int FAST_FUNC
9865 localcmd(int argc UNUSED_PARAM, char **argv)
9869 if (!localvar_stack)
9870 ash_msg_and_raise_error("not in a function");
9873 while ((name = *argv++) != NULL) {
9879 static int FAST_FUNC
9880 falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9885 static int FAST_FUNC
9886 truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9891 static int FAST_FUNC
9892 execcmd(int argc UNUSED_PARAM, char **argv)
9895 while (nextopt("a:") != '\0')
9896 /* nextopt() sets optionarg to "-a ARGV0" */;
9902 iflag = 0; /* exit on error */
9905 /* We should set up signals for "exec CMD"
9906 * the same way as for "CMD" without "exec".
9907 * But optschanged->setinteractive->setsignal
9908 * still thought we are a root shell. Therefore, for example,
9909 * SIGQUIT is still set to IGN. Fix it:
9913 /*setsignal(SIGTERM); - unnecessary because of iflag=0 */
9914 /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */
9915 /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */
9919 argv[0] = optionarg;
9920 shellexec(prog, argv, pathval(), 0);
9927 * The return command.
9929 static int FAST_FUNC
9930 returncmd(int argc UNUSED_PARAM, char **argv)
9936 * If called outside a function, do what ksh does;
9937 * skip the rest of the file.
9941 status = number(argv[1]);
9944 status = exitstatus;
9951 /* Forward declarations for builtintab[] */
9952 static int breakcmd(int, char **) FAST_FUNC;
9953 static int dotcmd(int, char **) FAST_FUNC;
9954 static int evalcmd(int, char **, int) FAST_FUNC;
9955 static int exitcmd(int, char **) FAST_FUNC;
9956 static int exportcmd(int, char **) FAST_FUNC;
9957 #if ENABLE_ASH_GETOPTS
9958 static int getoptscmd(int, char **) FAST_FUNC;
9961 static int helpcmd(int, char **) FAST_FUNC;
9964 static int historycmd(int, char **) FAST_FUNC;
9966 #if ENABLE_FEATURE_SH_MATH
9967 static int letcmd(int, char **) FAST_FUNC;
9969 static int readcmd(int, char **) FAST_FUNC;
9970 static int setcmd(int, char **) FAST_FUNC;
9971 static int shiftcmd(int, char **) FAST_FUNC;
9972 static int timescmd(int, char **) FAST_FUNC;
9973 static int trapcmd(int, char **) FAST_FUNC;
9974 static int umaskcmd(int, char **) FAST_FUNC;
9975 static int unsetcmd(int, char **) FAST_FUNC;
9976 static int ulimitcmd(int, char **) FAST_FUNC;
9978 #define BUILTIN_NOSPEC "0"
9979 #define BUILTIN_SPECIAL "1"
9980 #define BUILTIN_REGULAR "2"
9981 #define BUILTIN_SPEC_REG "3"
9982 #define BUILTIN_ASSIGN "4"
9983 #define BUILTIN_SPEC_ASSG "5"
9984 #define BUILTIN_REG_ASSG "6"
9985 #define BUILTIN_SPEC_REG_ASSG "7"
9987 /* Stubs for calling non-FAST_FUNC's */
9989 static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); }
9991 #if ENABLE_ASH_PRINTF
9992 static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); }
9994 #if ENABLE_ASH_TEST || BASH_TEST2
9995 static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); }
9998 /* Keep these in proper order since it is searched via bsearch() */
9999 static const struct builtincmd builtintab[] = {
10000 { BUILTIN_SPEC_REG "." , dotcmd },
10001 { BUILTIN_SPEC_REG ":" , truecmd },
10002 #if ENABLE_ASH_TEST
10003 { BUILTIN_REGULAR "[" , testcmd },
10006 { BUILTIN_REGULAR "[[" , testcmd },
10008 #if ENABLE_ASH_ALIAS
10009 { BUILTIN_REG_ASSG "alias" , aliascmd },
10012 { BUILTIN_REGULAR "bg" , fg_bgcmd },
10014 { BUILTIN_SPEC_REG "break" , breakcmd },
10015 { BUILTIN_REGULAR "cd" , cdcmd },
10016 { BUILTIN_NOSPEC "chdir" , cdcmd },
10017 #if ENABLE_ASH_CMDCMD
10018 { BUILTIN_REGULAR "command" , commandcmd },
10020 { BUILTIN_SPEC_REG "continue", breakcmd },
10021 #if ENABLE_ASH_ECHO
10022 { BUILTIN_REGULAR "echo" , echocmd },
10024 { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/
10025 { BUILTIN_SPEC_REG "exec" , execcmd },
10026 { BUILTIN_SPEC_REG "exit" , exitcmd },
10027 { BUILTIN_SPEC_REG_ASSG "export" , exportcmd },
10028 { BUILTIN_REGULAR "false" , falsecmd },
10030 { BUILTIN_REGULAR "fg" , fg_bgcmd },
10032 #if ENABLE_ASH_GETOPTS
10033 { BUILTIN_REGULAR "getopts" , getoptscmd },
10035 { BUILTIN_REGULAR "hash" , hashcmd },
10036 #if ENABLE_ASH_HELP
10037 { BUILTIN_NOSPEC "help" , helpcmd },
10040 { BUILTIN_NOSPEC "history" , historycmd },
10043 { BUILTIN_REGULAR "jobs" , jobscmd },
10044 { BUILTIN_REGULAR "kill" , killcmd },
10046 #if ENABLE_FEATURE_SH_MATH
10047 { BUILTIN_NOSPEC "let" , letcmd },
10049 { BUILTIN_SPEC_REG_ASSG "local" , localcmd },
10050 #if ENABLE_ASH_PRINTF
10051 { BUILTIN_REGULAR "printf" , printfcmd },
10053 { BUILTIN_REGULAR "pwd" , pwdcmd },
10054 { BUILTIN_REGULAR "read" , readcmd },
10055 { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
10056 { BUILTIN_SPEC_REG "return" , returncmd },
10057 { BUILTIN_SPEC_REG "set" , setcmd },
10058 { BUILTIN_SPEC_REG "shift" , shiftcmd },
10060 { BUILTIN_SPEC_REG "source" , dotcmd },
10062 #if ENABLE_ASH_TEST
10063 { BUILTIN_REGULAR "test" , testcmd },
10065 { BUILTIN_SPEC_REG "times" , timescmd },
10066 { BUILTIN_SPEC_REG "trap" , trapcmd },
10067 { BUILTIN_REGULAR "true" , truecmd },
10068 { BUILTIN_REGULAR "type" , typecmd },
10069 { BUILTIN_REGULAR "ulimit" , ulimitcmd },
10070 { BUILTIN_REGULAR "umask" , umaskcmd },
10071 #if ENABLE_ASH_ALIAS
10072 { BUILTIN_REGULAR "unalias" , unaliascmd },
10074 { BUILTIN_SPEC_REG "unset" , unsetcmd },
10075 { BUILTIN_REGULAR "wait" , waitcmd },
10078 /* Should match the above table! */
10079 #define COMMANDCMD (builtintab + \
10081 /* [ */ 1 * ENABLE_ASH_TEST + \
10082 /* [[ */ 1 * BASH_TEST2 + \
10083 /* alias */ 1 * ENABLE_ASH_ALIAS + \
10084 /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \
10085 /* break cd cddir */ 3)
10086 #define EVALCMD (COMMANDCMD + \
10087 /* command */ 1 * ENABLE_ASH_CMDCMD + \
10088 /* continue */ 1 + \
10089 /* echo */ 1 * ENABLE_ASH_ECHO + \
10091 #define EXECCMD (EVALCMD + \
10095 * Search the table of builtin commands.
10098 pstrcmp1(const void *a, const void *b)
10100 return strcmp((char*)a, *(char**)b + 1);
10102 static struct builtincmd *
10103 find_builtin(const char *name)
10105 struct builtincmd *bp;
10108 name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]),
10114 #if EDITING_HAS_get_exe_name
10115 static const char * FAST_FUNC
10116 get_builtin_name(int i)
10118 return /*i >= 0 &&*/ i < ARRAY_SIZE(builtintab) ? builtintab[i].name + 1 : NULL;
10123 * Execute a simple command.
10125 static void unwindfiles(struct parsefile *stop);
10127 isassignment(const char *p)
10129 const char *q = endofname(p);
10134 static int FAST_FUNC
10135 bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
10137 /* Preserve exitstatus of a previous possible redirection
10138 * as POSIX mandates */
10139 return back_exitstatus;
10142 evalcommand(union node *cmd, int flags)
10144 static const struct builtincmd null_bltin = {
10145 BUILTIN_REGULAR "", bltincmd
10147 struct localvar_list *localvar_stop;
10148 struct parsefile *file_stop;
10149 struct redirtab *redir_stop;
10151 struct arglist arglist;
10152 struct arglist varlist;
10155 struct strlist *osp;
10156 const struct strlist *sp;
10157 struct cmdentry cmdentry;
10165 smallint cmd_is_exec;
10169 errlinno = lineno = cmd->ncmd.linno;
10171 lineno -= funcline - 1;
10173 /* First expand the arguments. */
10174 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
10175 file_stop = g_parsefile;
10176 back_exitstatus = 0;
10178 cmdentry.cmdtype = CMDBUILTIN;
10179 cmdentry.u.cmd = &null_bltin;
10180 varlist.lastp = &varlist.list;
10181 *varlist.lastp = NULL;
10182 arglist.lastp = &arglist.list;
10183 *arglist.lastp = NULL;
10193 argp = cmd->ncmd.args;
10194 osp = fill_arglist(&arglist, &argp);
10196 int pseudovarflag = 0;
10199 find_command(arglist.list->text, &cmdentry,
10200 cmd_flag | DO_REGBLTIN, pathval());
10204 /* implement bltin and command here */
10205 if (cmdentry.cmdtype != CMDBUILTIN)
10208 pseudovarflag = IS_BUILTIN_ASSIGN(cmdentry.u.cmd);
10209 if (spclbltin < 0) {
10210 spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd);
10211 vlocal = !spclbltin;
10213 cmd_is_exec = cmdentry.u.cmd == EXECCMD;
10214 if (cmdentry.u.cmd != COMMANDCMD)
10217 cmd_flag = parse_command_args(&arglist, &argp, &path);
10222 for (; argp; argp = argp->narg.next)
10223 expandarg(argp, &arglist,
10225 isassignment(argp->narg.text) ?
10226 EXP_VARTILDE : EXP_FULL | EXP_TILDE);
10228 for (sp = arglist.list; sp; sp = sp->next)
10231 if (cmd_is_exec && argc > 1)
10235 localvar_stop = pushlocalvars(vlocal);
10237 /* Reserve one extra spot at the front for shellexec. */
10238 nargv = stalloc(sizeof(char *) * (argc + 2));
10240 for (sp = arglist.list; sp; sp = sp->next) {
10241 TRACE(("evalcommand arg: %s\n", sp->text));
10242 *nargv++ = sp->text;
10247 if (iflag && funcline == 0 && argc > 0)
10248 lastarg = nargv[-1];
10250 expredir(cmd->ncmd.redirect);
10251 redir_stop = pushredir(cmd->ncmd.redirect);
10253 if (BASH_XTRACEFD && xflag) {
10254 /* NB: bash closes fd == $BASH_XTRACEFD when it is changed.
10255 * we do not emulate this. We only use its value.
10257 const char *xtracefd = lookupvar("BASH_XTRACEFD");
10258 if (xtracefd && is_number(xtracefd))
10259 preverrout_fd = atoi(xtracefd);
10262 status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2);
10266 exitstatus = status;
10268 /* We have a redirection error. */
10270 raise_exception(EXERROR);
10275 for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
10276 struct strlist **spp;
10278 spp = varlist.lastp;
10279 expandarg(argp, &varlist, EXP_VARTILDE);
10282 mklocal((*spp)->text, VEXPORT);
10284 setvareq((*spp)->text, vflags);
10287 /* Print the command if xflag is set. */
10289 const char *pfx = "";
10291 fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX));
10295 char *varval = sp->text;
10296 char *eq = strchrnul(varval, '=');
10299 fdprintf(preverrout_fd, "%s%.*s%s",
10301 (int)(eq - varval), varval,
10302 maybe_single_quote(eq)
10310 fdprintf(preverrout_fd, "%s%s",
10312 /* always quote if matches reserved word: */
10314 ? single_quote(sp->text)
10315 : maybe_single_quote(sp->text)
10320 safe_write(preverrout_fd, "\n", 1);
10323 /* Now locate the command. */
10324 if (cmdentry.cmdtype != CMDBUILTIN
10325 || !(IS_BUILTIN_REGULAR(cmdentry.u.cmd))
10327 path = path ? path : pathval();
10328 find_command(argv[0], &cmdentry, cmd_flag | DO_ERR, path);
10333 /* Execute the command. */
10334 switch (cmdentry.cmdtype) {
10337 flush_stdout_stderr();
10342 #if ENABLE_FEATURE_SH_STANDALONE \
10343 && ENABLE_FEATURE_SH_NOFORK \
10345 /* (1) BUG: if variables are set, we need to fork, or save/restore them
10346 * around run_nofork_applet() call.
10347 * (2) Should this check also be done in forkshell()?
10348 * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...)
10350 /* find_command() encodes applet_no as (-2 - applet_no) */
10351 int applet_no = (- cmdentry.u.index - 2);
10352 if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
10356 sv_environ = environ;
10357 environ = listvars(VEXPORT, VUNSET, varlist.list, /*end:*/ NULL);
10359 * Run <applet>_main().
10360 * Signals (^C) can't interrupt here.
10361 * Otherwise we can mangle stdio or malloc internal state.
10362 * This makes applets which can run for a long time
10363 * and/or wait for user input ineligible for NOFORK:
10364 * for example, "yes" or "rm" (rm -i waits for input).
10366 exitstatus = run_nofork_applet(applet_no, argv);
10367 environ = sv_environ;
10369 * Try enabling NOFORK for "yes" applet.
10370 * ^C _will_ stop it (write returns EINTR),
10371 * but this causes stdout FILE to be stuck
10372 * and needing clearerr(). What if other applets
10373 * also can get EINTRs? Do we need to switch
10374 * our signals to SA_RESTART?
10376 /*clearerr(stdout);*/
10381 /* Can we avoid forking? For example, very last command
10382 * in a script or a subshell does not need forking,
10383 * we can just exec it.
10385 if (!(flags & EV_EXIT) || may_have_traps) {
10386 /* No, forking off a child is necessary */
10389 jp = makejob(/*cmd,*/ 1);
10390 if (forkshell(jp, cmd, FORK_FG) != 0) {
10392 TRACE(("forked child exited with %d\n", status));
10397 /* fall through to exec'ing external program */
10399 shellexec(argv[0], argv, path, cmdentry.u.index);
10403 if (evalbltin(cmdentry.u.cmd, argc, argv, flags)
10404 && !(exception_type == EXERROR && spclbltin <= 0)
10407 longjmp(exception_handler->loc, 1);
10412 if (evalfun(cmdentry.u.func, argc, argv, flags))
10417 status = waitforjob(jp);
10421 if (cmd->ncmd.redirect)
10422 popredir(/*drop:*/ cmd_is_exec);
10423 unwindredir(redir_stop);
10424 unwindfiles(file_stop);
10425 unwindlocalvars(localvar_stop);
10427 /* dsl: I think this is intended to be used to support
10428 * '_' in 'vi' command mode during line editing...
10429 * However I implemented that within libedit itself.
10431 setvar0("_", lastarg);
10438 evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags)
10440 char *volatile savecmdname;
10441 struct jmploc *volatile savehandler;
10442 struct jmploc jmploc;
10446 savecmdname = commandname;
10447 savehandler = exception_handler;
10448 i = setjmp(jmploc.loc);
10451 exception_handler = &jmploc;
10452 commandname = argv[0];
10454 optptr = NULL; /* initialize nextopt */
10455 if (cmd == EVALCMD)
10456 status = evalcmd(argc, argv, flags);
10458 status = (*cmd->builtin)(argc, argv);
10459 flush_stdout_stderr();
10460 status |= ferror(stdout);
10461 exitstatus = status;
10464 commandname = savecmdname;
10465 exception_handler = savehandler;
10471 goodname(const char *p)
10473 return endofname(p)[0] == '\0';
10478 * Search for a command. This is called before we fork so that the
10479 * location of the command will be available in the parent as well as
10480 * the child. The check for "goodname" is an overly conservative
10481 * check that the name will not be subject to expansion.
10484 prehash(union node *n)
10486 struct cmdentry entry;
10488 if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text))
10489 find_command(n->ncmd.args->narg.text, &entry, 0, pathval());
10493 /* ============ Builtin commands
10495 * Builtin commands whose functions are closely tied to evaluation
10496 * are implemented here.
10500 * Handle break and continue commands. Break, continue, and return are
10501 * all handled by setting the evalskip flag. The evaluation routines
10502 * above all check this flag, and if it is set they start skipping
10503 * commands rather than executing them. The variable skipcount is
10504 * the number of loops to break/continue, or the number of function
10505 * levels to return. (The latter is always 1.) It should probably
10506 * be an error to break out of more loops than exist, but it isn't
10507 * in the standard shell so we don't make it one here.
10509 static int FAST_FUNC
10510 breakcmd(int argc UNUSED_PARAM, char **argv)
10512 int n = argv[1] ? number(argv[1]) : 1;
10515 ash_msg_and_raise_error(msg_illnum, argv[1]);
10519 evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK;
10527 * This implements the input routines used by the parser.
10531 INPUT_PUSH_FILE = 1,
10532 INPUT_NOFILE_OK = 2,
10535 static smallint checkkwd;
10536 /* values of checkkwd variable */
10537 #define CHKALIAS 0x1
10540 #define CHKEOFMARK 0x8
10543 * Push a string back onto the input at this current parsefile level.
10544 * We handle aliases this way.
10546 #if !ENABLE_ASH_ALIAS
10547 #define pushstring(s, ap) pushstring(s)
10550 pushstring(char *s, struct alias *ap)
10552 struct strpush *sp;
10557 if (g_parsefile->strpush) {
10558 sp = ckzalloc(sizeof(*sp));
10559 sp->prev = g_parsefile->strpush;
10561 sp = &(g_parsefile->basestrpush);
10563 g_parsefile->strpush = sp;
10564 sp->prev_string = g_parsefile->next_to_pgetc;
10565 sp->prev_left_in_line = g_parsefile->left_in_line;
10566 sp->unget = g_parsefile->unget;
10567 memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc));
10568 #if ENABLE_ASH_ALIAS
10571 ap->flag |= ALIASINUSE;
10575 g_parsefile->next_to_pgetc = s;
10576 g_parsefile->left_in_line = len;
10577 g_parsefile->unget = 0;
10584 struct strpush *sp = g_parsefile->strpush;
10587 #if ENABLE_ASH_ALIAS
10589 if (g_parsefile->next_to_pgetc[-1] == ' '
10590 || g_parsefile->next_to_pgetc[-1] == '\t'
10592 checkkwd |= CHKALIAS;
10594 if (sp->string != sp->ap->val) {
10597 sp->ap->flag &= ~ALIASINUSE;
10598 if (sp->ap->flag & ALIASDEAD) {
10599 unalias(sp->ap->name);
10603 g_parsefile->next_to_pgetc = sp->prev_string;
10604 g_parsefile->left_in_line = sp->prev_left_in_line;
10605 g_parsefile->unget = sp->unget;
10606 memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc));
10607 g_parsefile->strpush = sp->prev;
10608 if (sp != &(g_parsefile->basestrpush))
10617 char *buf = g_parsefile->buf;
10619 g_parsefile->next_to_pgetc = buf;
10620 #if ENABLE_FEATURE_EDITING
10622 if (!iflag || g_parsefile->pf_fd != STDIN_FILENO)
10623 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10625 # if ENABLE_ASH_IDLE_TIMEOUT
10627 const char *tmout_var = lookupvar("TMOUT");
10629 timeout = atoi(tmout_var) * 1000;
10633 line_input_state->timeout = timeout;
10635 # if ENABLE_FEATURE_TAB_COMPLETION
10636 line_input_state->path_lookup = pathval();
10638 reinit_unicode_for_ash();
10639 nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ);
10641 /* ^C pressed, "convert" to SIGINT */
10642 write(STDOUT_FILENO, "^C", 2);
10643 if (trap[SIGINT]) {
10649 exitstatus = 128 + SIGINT;
10655 /* Ctrl+D pressed */
10658 # if ENABLE_ASH_IDLE_TIMEOUT
10659 else if (errno == EAGAIN && timeout > 0) {
10660 puts("\007timed out waiting for input: auto-logout");
10667 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10670 #if 0 /* disabled: nonblock_immune_read() handles this problem */
10672 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
10673 int flags = fcntl(0, F_GETFL);
10674 if (flags >= 0 && (flags & O_NONBLOCK)) {
10675 flags &= ~O_NONBLOCK;
10676 if (fcntl(0, F_SETFL, flags) >= 0) {
10677 out2str("sh: turning off NDELAY mode\n");
10688 * Refill the input buffer and return the next input character:
10690 * 1) If a string was pushed back on the input, pop it;
10691 * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM)
10692 * or we are reading from a string so we can't refill the buffer,
10694 * 3) If there is more stuff in this buffer, use it else call read to fill it.
10695 * 4) Process input up to the next newline, deleting nul characters.
10697 //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__)
10698 #define pgetc_debug(...) ((void)0)
10699 static int pgetc(void);
10706 if (g_parsefile->strpush) {
10707 #if ENABLE_ASH_ALIAS
10708 if (g_parsefile->left_in_line == -1
10709 && g_parsefile->strpush->ap
10710 && g_parsefile->next_to_pgetc[-1] != ' '
10711 && g_parsefile->next_to_pgetc[-1] != '\t'
10713 pgetc_debug("preadbuffer PEOA");
10720 /* on both branches above g_parsefile->left_in_line < 0.
10721 * "pgetc" needs refilling.
10724 /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read",
10725 * pungetc() may increment it a few times.
10726 * Assuming it won't increment it to less than -90.
10728 if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) {
10729 pgetc_debug("preadbuffer PEOF1");
10730 /* even in failure keep left_in_line and next_to_pgetc
10731 * in lock step, for correct multi-layer pungetc.
10732 * left_in_line was decremented before preadbuffer(),
10733 * must inc next_to_pgetc: */
10734 g_parsefile->next_to_pgetc++;
10738 more = g_parsefile->left_in_buffer;
10740 flush_stdout_stderr();
10744 /* don't try reading again */
10745 g_parsefile->left_in_line = -99;
10746 pgetc_debug("preadbuffer PEOF2");
10747 g_parsefile->next_to_pgetc++;
10752 /* Find out where's the end of line.
10753 * Set g_parsefile->left_in_line
10754 * and g_parsefile->left_in_buffer acordingly.
10755 * NUL chars are deleted.
10757 q = g_parsefile->next_to_pgetc;
10765 memmove(q, q + 1, more);
10769 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10775 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10776 if (g_parsefile->left_in_line < 0)
10781 g_parsefile->left_in_buffer = more;
10786 out2str(g_parsefile->next_to_pgetc);
10790 pgetc_debug("preadbuffer at %d:%p'%s'",
10791 g_parsefile->left_in_line,
10792 g_parsefile->next_to_pgetc,
10793 g_parsefile->next_to_pgetc);
10794 return (unsigned char)*g_parsefile->next_to_pgetc++;
10800 g_parsefile->linno++;
10801 setprompt_if(doprompt, 2);
10806 g_parsefile->linno++;
10807 needprompt = doprompt;
10815 pgetc_debug("pgetc at %d:%p'%s'",
10816 g_parsefile->left_in_line,
10817 g_parsefile->next_to_pgetc,
10818 g_parsefile->next_to_pgetc);
10819 if (g_parsefile->unget)
10820 return g_parsefile->lastc[--g_parsefile->unget];
10822 if (--g_parsefile->left_in_line >= 0)
10823 c = (unsigned char)*g_parsefile->next_to_pgetc++;
10827 g_parsefile->lastc[1] = g_parsefile->lastc[0];
10828 g_parsefile->lastc[0] = c;
10833 #if ENABLE_ASH_ALIAS
10835 pgetc_without_PEOA(void)
10839 pgetc_debug("pgetc at %d:%p'%s'",
10840 g_parsefile->left_in_line,
10841 g_parsefile->next_to_pgetc,
10842 g_parsefile->next_to_pgetc);
10844 } while (c == PEOA);
10848 # define pgetc_without_PEOA() pgetc()
10852 * Undo a call to pgetc. Only two characters may be pushed back.
10853 * PEOF may be pushed back.
10858 g_parsefile->unget++;
10861 /* This one eats backslash+newline */
10867 while ((c = pgetc()) == '\\') {
10868 if (pgetc() != '\n') {
10881 uint8_t innerdq :1;
10882 uint8_t varpushed :1;
10883 uint8_t dblquote :1;
10884 int varnest; /* levels of variables expansion */
10885 int dqvarnest; /* levels of variables expansion within double quotes */
10886 int parenlevel; /* levels of parens in arithmetic */
10887 struct synstack *prev;
10888 struct synstack *next;
10892 pgetc_top(struct synstack *stack)
10894 return stack->syntax == SQSYNTAX ? pgetc() : pgetc_eatbnl();
10898 synstack_push(struct synstack **stack, struct synstack *next, int syntax)
10900 memset(next, 0, sizeof(*next));
10901 next->syntax = syntax;
10902 next->next = *stack;
10903 (*stack)->prev = next;
10907 static ALWAYS_INLINE void
10908 synstack_pop(struct synstack **stack)
10910 *stack = (*stack)->next;
10914 * To handle the "." command, a stack of input files is used. Pushfile
10915 * adds a new entry to the stack and popfile restores the previous level.
10920 struct parsefile *pf;
10922 pf = ckzalloc(sizeof(*pf));
10923 pf->prev = g_parsefile;
10925 /*pf->strpush = NULL; - ckzalloc did it */
10926 /*pf->basestrpush.prev = NULL;*/
10934 struct parsefile *pf = g_parsefile;
10940 if (pf->pf_fd >= 0)
10943 while (pf->strpush)
10945 g_parsefile = pf->prev;
10951 unwindfiles(struct parsefile *stop)
10953 while (g_parsefile != stop)
10958 * Return to top level.
10963 unwindfiles(&basepf);
10967 * Close the file(s) that the shell is reading commands from. Called
10968 * after a fork is done.
10974 if (g_parsefile->pf_fd > 0) {
10975 close(g_parsefile->pf_fd);
10976 g_parsefile->pf_fd = 0;
10981 * Like setinputfile, but takes an open file descriptor. Call this with
10985 setinputfd(int fd, int push)
10989 g_parsefile->buf = NULL;
10991 g_parsefile->pf_fd = fd;
10992 if (g_parsefile->buf == NULL)
10993 g_parsefile->buf = ckmalloc(IBUFSIZ);
10994 g_parsefile->left_in_buffer = 0;
10995 g_parsefile->left_in_line = 0;
10996 g_parsefile->linno = 1;
11000 * Set the input to take input from a file. If push is set, push the
11001 * old input onto the stack first.
11004 setinputfile(const char *fname, int flags)
11009 fd = open(fname, O_RDONLY | O_CLOEXEC);
11011 if (flags & INPUT_NOFILE_OK)
11014 ash_msg_and_raise_perror("can't open '%s'", fname);
11018 else if (O_CLOEXEC == 0) /* old libc */
11019 close_on_exec_on(fd);
11021 setinputfd(fd, flags & INPUT_PUSH_FILE);
11028 * Like setinputfile, but takes input from a string.
11031 setinputstring(char *string)
11035 g_parsefile->next_to_pgetc = string;
11036 g_parsefile->left_in_line = strlen(string);
11037 g_parsefile->buf = NULL;
11038 g_parsefile->linno = 1;
11044 * Routines to check for mail.
11047 #if ENABLE_ASH_MAIL
11049 /* Hash of mtimes of mailboxes */
11050 static unsigned mailtime_hash;
11051 /* Set if MAIL or MAILPATH is changed. */
11052 static smallint mail_var_path_changed;
11055 * Print appropriate message(s) if mail has arrived.
11056 * If mail_var_path_changed is set,
11057 * then the value of MAIL has mail_var_path_changed,
11058 * so we just update the values.
11067 struct stackmark smark;
11070 setstackmark(&smark);
11071 mpath = mpathset() ? mpathval() : mailval();
11076 len = padvance_magic(&mpath, nullstr, 2);
11083 for (q = p; *q; q++)
11089 q[-1] = '\0'; /* delete trailing '/' */
11090 if (stat(p, &statb) < 0) {
11093 /* Very simplistic "hash": just a sum of all mtimes */
11094 new_hash += (unsigned)statb.st_mtime;
11096 if (!mail_var_path_changed && mailtime_hash != new_hash) {
11097 if (mailtime_hash != 0)
11098 out2str("you have mail\n");
11099 mailtime_hash = new_hash;
11101 mail_var_path_changed = 0;
11102 popstackmark(&smark);
11105 static void FAST_FUNC
11106 changemail(const char *val UNUSED_PARAM)
11108 mail_var_path_changed = 1;
11111 #endif /* ASH_MAIL */
11114 /* ============ ??? */
11117 * Set the shell parameters.
11120 setparam(char **argv)
11126 for (nparam = 0; argv[nparam]; nparam++)
11128 ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap));
11130 *ap++ = ckstrdup(*argv++);
11133 freeparam(&shellparam);
11134 shellparam.malloced = 1;
11135 shellparam.nparam = nparam;
11136 shellparam.p = newparam;
11137 #if ENABLE_ASH_GETOPTS
11138 shellparam.optind = 1;
11139 shellparam.optoff = -1;
11144 * Process shell options. The global variable argptr contains a pointer
11145 * to the argument list; we advance it past the options.
11147 * SUSv3 section 2.8.1 "Consequences of Shell Errors" says:
11148 * For a non-interactive shell, an error condition encountered
11149 * by a special built-in ... shall cause the shell to write a diagnostic message
11150 * to standard error and exit as shown in the following table:
11151 * Error Special Built-In
11153 * Utility syntax error (option or operand error) Shall exit
11155 * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142)
11156 * we see that bash does not do that (set "finishes" with error code 1 instead,
11157 * and shell continues), and people rely on this behavior!
11159 * set -o barfoo 2>/dev/null
11162 * Oh well. Let's mimic that.
11165 plus_minus_o(char *name, int val)
11170 for (i = 0; i < NOPTS; i++) {
11171 if (strcmp(name, optnames(i)) == 0) {
11176 ash_msg("illegal option %co %s", val ? '-' : '+', name);
11179 for (i = 0; i < NOPTS; i++) {
11180 if (optnames(i)[0] == '\0')
11183 out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off");
11185 out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i));
11191 setoption(int flag, int val)
11195 for (i = 0; i < NOPTS; i++) {
11196 if (optletters(i) == flag && optnames(i)[0] != '\0') {
11201 ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag);
11204 /* If login_sh is not NULL, we are called to parse command line opts,
11208 options(int *login_sh)
11216 while ((p = *argptr) != NULL) {
11218 if (c != '-' && c != '+')
11221 val = 0; /* val = 0 if c == '+' */
11224 if (p[0] == '\0' || LONE_DASH(p)) {
11226 /* "-" means turn off -x and -v */
11229 /* "--" means reset params */
11230 else if (*argptr == NULL)
11233 break; /* "-" or "--" terminates options */
11236 /* first char was + or - */
11237 while ((c = *p++) != '\0') {
11239 /* bash 3.2 indeed handles -c CMD and +c CMD the same */
11241 minusc = p; /* command is after shell args */
11245 if (c == 's') { /* -s, +s */
11249 if (c == 'i') { /* -i, +i */
11254 *login_sh = 1; /* -l or +l == --login */
11257 /* bash does not accept +-login, we also won't */
11258 if (val && (c == '-')) { /* long options */
11259 if (strcmp(p, "login") == 0) {
11266 if (plus_minus_o(*argptr, val)) {
11267 /* it already printed err message */
11268 return 1; /* error */
11281 * The shift builtin command.
11283 static int FAST_FUNC
11284 shiftcmd(int argc UNUSED_PARAM, char **argv)
11291 n = number(argv[1]);
11292 if (n > shellparam.nparam)
11295 shellparam.nparam -= n;
11296 for (ap1 = shellparam.p; --n >= 0; ap1++) {
11297 if (shellparam.malloced)
11300 ap2 = shellparam.p;
11301 while ((*ap2++ = *ap1++) != NULL)
11303 #if ENABLE_ASH_GETOPTS
11304 shellparam.optind = 1;
11305 shellparam.optoff = -1;
11312 * POSIX requires that 'set' (but not export or readonly) output the
11313 * variables in lexicographic order - by the locale's collating order (sigh).
11314 * Maybe we could keep them in an ordered balanced binary tree
11315 * instead of hashed lists.
11316 * For now just roll 'em through qsort for printing...
11319 showvars(const char *sep_prefix, int on, int off)
11322 char **ep, **epend;
11324 ep = listvars(on, off, /*strlist:*/ NULL, &epend);
11325 qsort(ep, epend - ep, sizeof(char *), vpcmp);
11327 sep = *sep_prefix ? " " : sep_prefix;
11329 for (; ep < epend; ep++) {
11333 p = endofname(*ep);
11334 /* Used to have simple "p = strchrnul(*ep, '=')" here instead, but this
11335 * makes "export -p" to have output not suitable for "eval":
11337 * os.environ["test-test"]="test"
11338 * if os.fork() == 0:
11339 * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this
11340 * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ])
11344 q = single_quote(++p);
11345 out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q);
11351 * The set command builtin.
11353 static int FAST_FUNC
11354 setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
11359 return showvars(nullstr, 0, VUNSET);
11362 retval = options(/*login_sh:*/ NULL);
11363 if (retval == 0) { /* if no parse error... */
11365 if (*argptr != NULL) {
11373 #if ENABLE_ASH_RANDOM_SUPPORT
11374 static void FAST_FUNC
11375 change_random(const char *value)
11379 if (value == NULL) {
11380 /* "get", generate */
11381 t = next_random(&random_gen);
11382 /* set without recursion */
11383 setvar(vrandom.var_text, utoa(t), VNOFUNC);
11384 vrandom.flags &= ~VNOFUNC;
11387 t = strtoul(value, NULL, 10);
11388 INIT_RANDOM_T(&random_gen, (t ? t : 1), t);
11393 #if BASH_EPOCH_VARS
11394 static void FAST_FUNC
11395 change_epoch(struct var *vepoch, const char *fmt)
11398 char buffer[sizeof("%lu.nnnnnn") + sizeof(long)*3];
11400 gettimeofday(&tv, NULL);
11401 sprintf(buffer, fmt, (unsigned long)tv.tv_sec, (unsigned)tv.tv_usec);
11402 setvar(vepoch->var_text, buffer, VNOFUNC);
11403 vepoch->flags &= ~VNOFUNC;
11406 static void FAST_FUNC
11407 change_seconds(const char *value UNUSED_PARAM)
11409 change_epoch(&vepochs, "%lu");
11412 static void FAST_FUNC
11413 change_realtime(const char *value UNUSED_PARAM)
11415 change_epoch(&vepochr, "%lu.%06u");
11419 #if ENABLE_ASH_GETOPTS
11421 getopts(char *optstr, char *optvar, char **optfirst)
11428 int ind = shellparam.optind;
11429 int off = shellparam.optoff;
11433 shellparam.optind = -1;
11434 optnext = optfirst + ind - 1;
11436 if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off)
11439 p = optnext[-1] + off;
11440 if (p == NULL || *p == '\0') {
11441 /* Current word is done, advance */
11443 if (p == NULL || *p != '-' || *++p == '\0') {
11445 unsetvar("OPTARG");
11451 if (LONE_DASH(p)) /* check for "--" */
11456 for (q = optstr; *q != c;) {
11458 /* OPTERR is a bashism */
11459 const char *cp = lookupvar("OPTERR");
11460 if ((cp && LONE_CHAR(cp, '0'))
11461 || (optstr[0] == ':')
11464 /*sbuf[1] = '\0'; - already is */
11465 setvar0("OPTARG", sbuf);
11467 fprintf(stderr, "Illegal option -%c\n", c);
11468 unsetvar("OPTARG");
11478 if (*p == '\0' && (p = *optnext) == NULL) {
11479 /* OPTERR is a bashism */
11480 const char *cp = lookupvar("OPTERR");
11481 if ((cp && LONE_CHAR(cp, '0'))
11482 || (optstr[0] == ':')
11485 /*sbuf[1] = '\0'; - already is */
11486 setvar0("OPTARG", sbuf);
11489 fprintf(stderr, "No arg for -%c option\n", c);
11490 unsetvar("OPTARG");
11498 setvar0("OPTARG", p);
11501 setvar0("OPTARG", nullstr);
11503 ind = optnext - optfirst + 1;
11504 setvar("OPTIND", itoa(ind), VNOFUNC);
11506 /*sbuf[1] = '\0'; - already is */
11507 setvar0(optvar, sbuf);
11509 shellparam.optoff = p ? p - *(optnext - 1) : -1;
11510 shellparam.optind = ind;
11516 * The getopts builtin. Shellparam.optnext points to the next argument
11517 * to be processed. Shellparam.optptr points to the next character to
11518 * be processed in the current argument. If shellparam.optnext is NULL,
11519 * then it's the first time getopts has been called.
11521 static int FAST_FUNC
11522 getoptscmd(int argc, char **argv)
11527 ash_msg_and_raise_error("usage: getopts optstring var [arg]");
11529 optbase = shellparam.p;
11530 if ((unsigned)shellparam.optind > shellparam.nparam + 1) {
11531 shellparam.optind = 1;
11532 shellparam.optoff = -1;
11535 optbase = &argv[3];
11536 if ((unsigned)shellparam.optind > argc - 2) {
11537 shellparam.optind = 1;
11538 shellparam.optoff = -1;
11542 return getopts(argv[1], argv[2], optbase);
11544 #endif /* ASH_GETOPTS */
11547 /* ============ Shell parser */
11550 struct heredoc *next; /* next here document in list */
11551 union node *here; /* redirection node */
11552 char *eofmark; /* string indicating end of input */
11553 smallint striptabs; /* if set, strip leading tabs */
11556 static smallint tokpushback; /* last token pushed back */
11557 static smallint quoteflag; /* set if (part of) last token was quoted */
11558 static token_id_t lasttoken; /* last token read (integer id Txxx) */
11559 static struct heredoc *heredoclist; /* list of here documents to read */
11560 static char *wordtext; /* text of last word returned by readtoken */
11561 static struct nodelist *backquotelist;
11562 static union node *redirnode;
11563 static struct heredoc *heredoc;
11565 static const char *
11566 tokname(char *buf, int tok)
11569 return tokname_array[tok];
11570 sprintf(buf, "\"%s\"", tokname_array[tok]);
11574 /* raise_error_unexpected_syntax:
11575 * Called when an unexpected token is read during the parse. The argument
11576 * is the token that is expected, or -1 if more than one type of token can
11577 * occur at this point.
11579 static void raise_error_unexpected_syntax(int) NORETURN;
11581 raise_error_unexpected_syntax(int token)
11587 l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken));
11589 sprintf(msg + l, " (expecting %s)", tokname(buf, token));
11590 raise_error_syntax(msg);
11594 /* parsing is heavily cross-recursive, need these forward decls */
11595 static union node *andor(void);
11596 static union node *pipeline(void);
11597 static union node *parse_command(void);
11598 static void parseheredoc(void);
11599 static int peektoken(void);
11600 static int readtoken(void);
11602 static union node *
11605 union node *n1, *n2, *n3;
11610 switch (peektoken()) {
11618 if (!n1 && (nlflag & 1))
11624 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11625 if (nlflag == 2 && ((1 << peektoken()) & tokendlist))
11631 if (tok == TBACKGND) {
11632 if (n2->type == NPIPE) {
11633 n2->npipe.pipe_backgnd = 1;
11635 if (n2->type != NREDIR) {
11636 n3 = stzalloc(sizeof(struct nredir));
11638 /*n3->nredir.redirect = NULL; - stzalloc did it */
11641 n2->type = NBACKGND;
11647 n3 = stzalloc(sizeof(struct nbinary));
11649 n3->nbinary.ch1 = n1;
11650 n3->nbinary.ch2 = n2;
11663 raise_error_unexpected_syntax(-1);
11670 static union node *
11673 union node *n1, *n2, *n3;
11681 } else if (t == TOR) {
11687 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11689 n3 = stzalloc(sizeof(struct nbinary));
11691 n3->nbinary.ch1 = n1;
11692 n3->nbinary.ch2 = n2;
11697 static union node *
11700 union node *n1, *n2, *pipenode;
11701 struct nodelist *lp, *prev;
11705 TRACE(("pipeline: entered\n"));
11706 if (readtoken() == TNOT) {
11708 checkkwd = CHKKWD | CHKALIAS;
11711 n1 = parse_command();
11712 if (readtoken() == TPIPE) {
11713 pipenode = stzalloc(sizeof(struct npipe));
11714 pipenode->type = NPIPE;
11715 /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */
11716 lp = stzalloc(sizeof(struct nodelist));
11717 pipenode->npipe.cmdlist = lp;
11721 lp = stzalloc(sizeof(struct nodelist));
11722 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11723 lp->n = parse_command();
11725 } while (readtoken() == TPIPE);
11731 n2 = stzalloc(sizeof(struct nnot));
11739 static union node *
11744 n = stzalloc(sizeof(struct narg));
11746 /*n->narg.next = NULL; - stzalloc did it */
11747 n->narg.text = wordtext;
11748 n->narg.backquote = backquotelist;
11753 fixredir(union node *n, const char *text, int err)
11757 TRACE(("Fix redir %s %d\n", text, err));
11759 n->ndup.vname = NULL;
11761 fd = bb_strtou(text, NULL, 10);
11762 if (!errno && fd >= 0)
11763 n->ndup.dupfd = fd;
11764 else if (LONE_DASH(text))
11765 n->ndup.dupfd = -1;
11768 raise_error_syntax("bad fd number");
11769 n->ndup.vname = makename();
11776 union node *n = redirnode;
11778 if (n->type == NHERE)
11779 checkkwd = CHKEOFMARK;
11780 if (readtoken() != TWORD)
11781 raise_error_unexpected_syntax(-1);
11782 if (n->type == NHERE) {
11783 struct heredoc *here = heredoc;
11786 if (quoteflag == 0)
11788 TRACE(("Here document %d\n", n->type));
11789 rmescapes(wordtext, 0, NULL);
11790 here->eofmark = wordtext;
11792 if (heredoclist == NULL)
11793 heredoclist = here;
11795 for (p = heredoclist; p->next; p = p->next)
11799 } else if (n->type == NTOFD || n->type == NFROMFD) {
11800 fixredir(n, wordtext, 0);
11802 n->nfile.fname = makename();
11806 static union node *
11809 union node *args, **app;
11810 union node *n = NULL;
11811 union node *vars, **vpp;
11812 union node **rpp, *redir;
11816 smallint double_brackets_flag = 0;
11818 IF_BASH_FUNCTION(smallint function_flag = 0;)
11827 savecheckkwd = CHKALIAS;
11828 savelinno = g_parsefile->linno;
11831 checkkwd = savecheckkwd;
11836 if (peektoken() != TWORD)
11837 raise_error_unexpected_syntax(TWORD);
11842 case TAND: /* "&&" */
11843 case TOR: /* "||" */
11844 if (!double_brackets_flag) {
11848 wordtext = (char *) (t == TAND ? "-a" : "-o");
11851 n = stzalloc(sizeof(struct narg));
11853 /*n->narg.next = NULL; - stzalloc did it */
11854 n->narg.text = wordtext;
11856 if (strcmp("[[", wordtext) == 0)
11857 double_brackets_flag = 1;
11858 else if (strcmp("]]", wordtext) == 0)
11859 double_brackets_flag = 0;
11861 n->narg.backquote = backquotelist;
11862 if (savecheckkwd && isassignment(wordtext)) {
11864 vpp = &n->narg.next;
11867 app = &n->narg.next;
11871 if (function_flag) {
11872 checkkwd = CHKNL | CHKKWD;
11873 switch (peektoken()) {
11886 if (strcmp("[[", wordtext) == 0)
11891 raise_error_unexpected_syntax(-1);
11897 *rpp = n = redirnode;
11898 rpp = &n->nfile.next;
11899 parsefname(); /* read name of redirection file */
11902 IF_BASH_FUNCTION(do_func:)
11903 if (args && app == &args->narg.next
11906 struct builtincmd *bcmd;
11909 /* We have a function */
11910 if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP)
11911 raise_error_unexpected_syntax(TRP);
11912 name = n->narg.text;
11913 if (!goodname(name)
11914 || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd))
11916 raise_error_syntax("bad function name");
11919 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11920 n->ndefun.text = n->narg.text;
11921 n->ndefun.linno = g_parsefile->linno;
11922 n->ndefun.body = parse_command();
11925 IF_BASH_FUNCTION(function_flag = 0;)
11936 n = stzalloc(sizeof(struct ncmd));
11939 n->ncmd.linno = savelinno;
11940 n->ncmd.args = args;
11941 n->ncmd.assign = vars;
11942 n->ncmd.redirect = redir;
11946 static union node *
11947 parse_command(void)
11949 union node *n1, *n2;
11950 union node *ap, **app;
11951 union node *cp, **cpp;
11952 union node *redir, **rpp;
11960 savelinno = g_parsefile->linno;
11962 switch (readtoken()) {
11964 raise_error_unexpected_syntax(-1);
11967 n1 = stzalloc(sizeof(struct nif));
11969 n1->nif.test = list(0);
11970 if (readtoken() != TTHEN)
11971 raise_error_unexpected_syntax(TTHEN);
11972 n1->nif.ifpart = list(0);
11974 while (readtoken() == TELIF) {
11975 n2->nif.elsepart = stzalloc(sizeof(struct nif));
11976 n2 = n2->nif.elsepart;
11978 n2->nif.test = list(0);
11979 if (readtoken() != TTHEN)
11980 raise_error_unexpected_syntax(TTHEN);
11981 n2->nif.ifpart = list(0);
11983 if (lasttoken == TELSE)
11984 n2->nif.elsepart = list(0);
11986 n2->nif.elsepart = NULL;
11994 n1 = stzalloc(sizeof(struct nbinary));
11995 n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
11996 n1->nbinary.ch1 = list(0);
11999 TRACE(("expecting DO got '%s' %s\n", tokname_array[got],
12000 got == TWORD ? wordtext : ""));
12001 raise_error_unexpected_syntax(TDO);
12003 n1->nbinary.ch2 = list(0);
12008 if (readtoken() != TWORD || quoteflag || !goodname(wordtext))
12009 raise_error_syntax("bad for loop variable");
12010 n1 = stzalloc(sizeof(struct nfor));
12012 n1->nfor.linno = savelinno;
12013 n1->nfor.var = wordtext;
12014 checkkwd = CHKNL | CHKKWD | CHKALIAS;
12015 if (readtoken() == TIN) {
12017 while (readtoken() == TWORD) {
12018 n2 = stzalloc(sizeof(struct narg));
12020 /*n2->narg.next = NULL; - stzalloc did it */
12021 n2->narg.text = wordtext;
12022 n2->narg.backquote = backquotelist;
12024 app = &n2->narg.next;
12027 n1->nfor.args = ap;
12028 if (lasttoken != TNL && lasttoken != TSEMI)
12029 raise_error_unexpected_syntax(-1);
12031 n2 = stzalloc(sizeof(struct narg));
12033 /*n2->narg.next = NULL; - stzalloc did it */
12034 n2->narg.text = (char *)dolatstr;
12035 /*n2->narg.backquote = NULL;*/
12036 n1->nfor.args = n2;
12038 * Newline or semicolon here is optional (but note
12039 * that the original Bourne shell only allowed NL).
12041 if (lasttoken != TSEMI)
12044 checkkwd = CHKNL | CHKKWD | CHKALIAS;
12045 if (readtoken() != TDO)
12046 raise_error_unexpected_syntax(TDO);
12047 n1->nfor.body = list(0);
12051 n1 = stzalloc(sizeof(struct ncase));
12053 n1->ncase.linno = savelinno;
12054 if (readtoken() != TWORD)
12055 raise_error_unexpected_syntax(TWORD);
12056 n1->ncase.expr = n2 = stzalloc(sizeof(struct narg));
12058 /*n2->narg.next = NULL; - stzalloc did it */
12059 n2->narg.text = wordtext;
12060 n2->narg.backquote = backquotelist;
12061 checkkwd = CHKNL | CHKKWD | CHKALIAS;
12062 if (readtoken() != TIN)
12063 raise_error_unexpected_syntax(TIN);
12064 cpp = &n1->ncase.cases;
12066 checkkwd = CHKNL | CHKKWD;
12068 while (t != TESAC) {
12069 if (lasttoken == TLP)
12071 *cpp = cp = stzalloc(sizeof(struct nclist));
12073 app = &cp->nclist.pattern;
12075 *app = ap = stzalloc(sizeof(struct narg));
12077 /*ap->narg.next = NULL; - stzalloc did it */
12078 ap->narg.text = wordtext;
12079 ap->narg.backquote = backquotelist;
12080 if (readtoken() != TPIPE)
12082 app = &ap->narg.next;
12085 //ap->narg.next = NULL;
12086 if (lasttoken != TRP)
12087 raise_error_unexpected_syntax(TRP);
12088 cp->nclist.body = list(2);
12090 cpp = &cp->nclist.next;
12092 checkkwd = CHKNL | CHKKWD;
12096 raise_error_unexpected_syntax(TENDCASE);
12103 n1 = stzalloc(sizeof(struct nredir));
12104 n1->type = NSUBSHELL;
12105 n1->nredir.linno = savelinno;
12106 n1->nredir.n = list(0);
12107 /*n1->nredir.redirect = NULL; - stzalloc did it */
12114 IF_BASH_FUNCTION(case TFUNCTION:)
12118 return simplecmd();
12121 if (readtoken() != t)
12122 raise_error_unexpected_syntax(t);
12125 /* Now check for redirection which may follow command */
12126 checkkwd = CHKKWD | CHKALIAS;
12128 while (readtoken() == TREDIR) {
12129 *rpp = n2 = redirnode;
12130 rpp = &n2->nfile.next;
12136 if (n1->type != NSUBSHELL) {
12137 n2 = stzalloc(sizeof(struct nredir));
12139 n2->nredir.linno = savelinno;
12143 n1->nredir.redirect = redir;
12148 #if BASH_DOLLAR_SQUOTE
12150 decode_dollar_squote(void)
12152 static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
12158 p = strchr(C_escapes, c);
12163 if ((unsigned char)(c - '0') <= 7) { /* \ooo */
12167 } while ((unsigned char)(c - '0') <= 7 && --cnt);
12169 } else if (c == 'x') { /* \xHH */
12173 } while (isxdigit(c) && --cnt);
12175 if (cnt == 3) { /* \x but next char is "bad" */
12179 } else { /* simple seq like \\ or \t */
12184 c = bb_process_escape_sequence((void*)&p);
12185 } else { /* unrecognized "\z": print both chars unless ' or " */
12186 if (c != '\'' && c != '"') {
12188 c |= 0x100; /* "please encode \, then me" */
12195 /* Used by expandstr to get here-doc like behaviour. */
12196 #define FAKEEOFMARK ((char*)(uintptr_t)1)
12198 static ALWAYS_INLINE int
12199 realeofmark(const char *eofmark)
12201 return eofmark && eofmark != FAKEEOFMARK;
12205 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
12206 * is not NULL, read a here document. In the latter case, eofmark is the
12207 * word which marks the end of the document and striptabs is true if
12208 * leading tabs should be stripped from the document. The argument c
12209 * is the first character of the input token or document.
12211 * Because C does not have internal subroutines, I have simulated them
12212 * using goto's to implement the subroutine linkage. The following macros
12213 * will run code that appears at the end of readtoken1.
12215 #define CHECKEND() {goto checkend; checkend_return:;}
12216 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
12217 #define PARSESUB() {goto parsesub; parsesub_return:;}
12218 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
12219 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
12220 #define PARSEARITH() {goto parsearith; parsearith_return:;}
12222 readtoken1(int c, int syntax, char *eofmark, int striptabs)
12224 /* NB: syntax parameter fits into smallint */
12225 /* c parameter is an unsigned char or PEOF or PEOA */
12228 struct nodelist *bqlist;
12231 smallint pssyntax; /* we are expanding a prompt string */
12232 IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;)
12234 struct synstack synbase = { };
12235 struct synstack *synstack = &synbase;
12237 #if ENABLE_ASH_EXPAND_PRMT
12238 pssyntax = (syntax == PSSYNTAX);
12242 pssyntax = 0; /* constant */
12244 synstack->syntax = syntax;
12246 if (syntax == DQSYNTAX)
12247 synstack->dblquote = 1;
12251 STARTSTACKSTR(out);
12253 /* For each line, until end of word */
12254 CHECKEND(); /* set c to PEOF if at end of here document */
12255 for (;;) { /* until end of line or end of word */
12256 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
12257 switch (SIT(c, synstack->syntax)) {
12258 case CNL: /* '\n' */
12259 if (synstack->syntax == BASESYNTAX
12260 && !synstack->varnest
12262 goto endword; /* exit outer loop */
12266 c = pgetc_top(synstack);
12267 goto loop; /* continue outer loop */
12272 #if BASH_DOLLAR_SQUOTE
12273 if (c == '\\' && bash_dollar_squote) {
12274 c = decode_dollar_squote();
12276 /* skip $'\000', $'\x00' (like bash) */
12280 /* Unknown escape. Encode as '\z' */
12281 c = (unsigned char)c;
12282 if (eofmark == NULL || synstack->dblquote)
12283 USTPUTC(CTLESC, out);
12284 USTPUTC('\\', out);
12288 if (!eofmark || synstack->dblquote || synstack->varnest)
12289 USTPUTC(CTLESC, out);
12292 case CBACK: /* backslash */
12293 c = pgetc_without_PEOA();
12295 USTPUTC(CTLESC, out);
12296 USTPUTC('\\', out);
12299 if (pssyntax && c == '$') {
12300 USTPUTC(CTLESC, out);
12301 USTPUTC('\\', out);
12303 /* Backslash is retained if we are in "str"
12304 * and next char isn't dquote-special.
12306 if (synstack->dblquote
12310 && (c != '"' || (eofmark != NULL && !synstack->varnest))
12311 && (c != '}' || !synstack->varnest)
12313 USTPUTC(CTLESC, out); /* protect '\' from glob */
12314 USTPUTC('\\', out);
12316 USTPUTC(CTLESC, out);
12322 synstack->syntax = SQSYNTAX;
12324 if (eofmark == NULL) {
12325 USTPUTC(CTLQUOTEMARK, out);
12329 synstack->syntax = DQSYNTAX;
12330 synstack->dblquote = 1;
12332 if (synstack->varnest)
12333 synstack->innerdq ^= 1;
12336 IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;)
12337 if (eofmark != NULL && synstack->varnest == 0) {
12342 if (synstack->dqvarnest == 0) {
12343 synstack->syntax = BASESYNTAX;
12344 synstack->dblquote = 0;
12353 case CVAR: /* '$' */
12354 PARSESUB(); /* parse substitution */
12356 case CENDVAR: /* '}' */
12357 if (!synstack->innerdq && synstack->varnest > 0) {
12358 if (!--synstack->varnest && synstack->varpushed)
12359 synstack_pop(&synstack);
12360 else if (synstack->dqvarnest > 0)
12361 synstack->dqvarnest--;
12366 #if ENABLE_FEATURE_SH_MATH
12367 case CLP: /* '(' in arithmetic */
12368 synstack->parenlevel++;
12371 case CRP: /* ')' in arithmetic */
12372 if (synstack->parenlevel > 0) {
12373 synstack->parenlevel--;
12375 if (pgetc_eatbnl() == ')') {
12377 synstack_pop(&synstack);
12380 * unbalanced parens
12381 * (don't 2nd guess - no error)
12389 case CBQUOTE: /* '`' */
12390 if (checkkwd & CHKEOFMARK) {
12399 goto endword; /* exit outer loop */
12403 if (synstack->varnest == 0) {
12404 #if BASH_REDIR_OUTPUT
12406 //Can't call pgetc_eatbnl() here, this requires three-deep pungetc()
12407 if (pgetc() == '>')
12408 c = 0x100 + '>'; /* flag &> */
12412 goto endword; /* exit outer loop */
12414 IF_ASH_ALIAS(if (c != PEOA))
12417 c = pgetc_top(synstack);
12421 #if ENABLE_FEATURE_SH_MATH
12422 if (synstack->syntax == ARISYNTAX)
12423 raise_error_syntax("missing '))'");
12425 if (synstack->syntax != BASESYNTAX && eofmark == NULL)
12426 raise_error_syntax("unterminated quoted string");
12427 if (synstack->varnest != 0) {
12429 raise_error_syntax("missing '}'");
12431 USTPUTC('\0', out);
12432 len = out - (char *)stackblock();
12433 out = stackblock();
12434 if (eofmark == NULL) {
12435 if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>'))
12438 if (isdigit_str9(out)) {
12439 PARSEREDIR(); /* passed as params: out, c */
12440 lasttoken = TREDIR;
12443 /* else: non-number X seen, interpret it
12444 * as "NNNX>file" = "NNNX >file" */
12448 quoteflag = quotef;
12449 backquotelist = bqlist;
12450 grabstackblock(len);
12454 /* end of readtoken routine */
12457 * Check to see whether we are at the end of the here document. When this
12458 * is called, c is set to the first character of the next input line. If
12459 * we are at the end of the here document, this routine sets the c to PEOF.
12462 if (realeofmark(eofmark)) {
12466 #if ENABLE_ASH_ALIAS
12468 c = pgetc_without_PEOA();
12471 while (c == '\t') {
12472 c = pgetc_without_PEOA();
12476 markloc = out - (char *)stackblock();
12477 for (p = eofmark; STPUTC(c, out), *p; p++) {
12480 /* FIXME: fails for backslash-newlined terminator:
12485 * (see heredoc_bkslash_newline2.tests)
12487 c = pgetc_without_PEOA();
12490 if (c == '\n' || c == PEOF) {
12492 g_parsefile->linno++;
12493 needprompt = doprompt;
12498 p = (char *)stackblock() + markloc + 1;
12499 len_here = out - p;
12502 len_here -= (c >= PEOF);
12508 str = alloca(len_here + 1);
12509 *(char *)mempcpy(str, p, len_here) = '\0';
12511 pushstring(str, NULL);
12516 STADJUST((char *)stackblock() + markloc - out, out);
12518 goto checkend_return;
12522 * Parse a redirection operator. The variable "out" points to a string
12523 * specifying the fd to be redirected. The variable "c" contains the
12524 * first character of the redirection operator.
12527 /* out is already checked to be a valid number or "" */
12528 int fd = (*out == '\0' ? -1 : atoi(out));
12531 np = stzalloc(sizeof(struct nfile));
12534 c = pgetc_eatbnl();
12536 np->type = NAPPEND;
12538 np->type = NCLOBBER;
12541 /* it also can be NTO2 (>&file), but we can't figure it out yet */
12547 #if BASH_REDIR_OUTPUT
12548 else if (c == 0x100 + '>') { /* this flags &> redirection */
12550 pgetc(); /* this is '>', no need to check */
12554 else { /* c == '<' */
12555 /*np->nfile.fd = 0; - stzalloc did it */
12556 c = pgetc_eatbnl();
12559 if (sizeof(struct nfile) != sizeof(struct nhere)) {
12560 np = stzalloc(sizeof(struct nhere));
12561 /*np->nfile.fd = 0; - stzalloc did it */
12564 heredoc = stzalloc(sizeof(struct heredoc));
12565 heredoc->here = np;
12566 c = pgetc_eatbnl();
12568 heredoc->striptabs = 1;
12570 /*heredoc->striptabs = 0; - stzalloc did it */
12576 np->type = NFROMFD;
12580 np->type = NFROMTO;
12592 goto parseredir_return;
12596 * Parse a substitution. At this point, we have read the dollar sign
12597 * and nothing else.
12600 /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
12601 * (assuming ascii char codes, as the original implementation did) */
12602 #define is_special(c) \
12603 (((unsigned)(c) - 33 < 32) \
12604 && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1))
12606 unsigned char subtype;
12609 c = pgetc_eatbnl();
12610 if ((checkkwd & CHKEOFMARK)
12611 || c > 255 /* PEOA or PEOF */
12612 || (c != '(' && c != '{' && !is_name(c) && !is_special(c))
12614 #if BASH_DOLLAR_SQUOTE
12615 if (synstack->syntax != DQSYNTAX && c == '\'')
12616 bash_dollar_squote = 1;
12621 } else if (c == '(') {
12622 /* $(command) or $((arith)) */
12623 if (pgetc_eatbnl() == '(') {
12624 #if ENABLE_FEATURE_SH_MATH
12627 raise_error_syntax("support for $((arith)) is disabled");
12634 /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */
12635 smalluint newsyn = synstack->syntax;
12637 USTPUTC(CTLVAR, out);
12638 typeloc = out - (char *)stackblock();
12640 subtype = VSNORMAL;
12642 c = pgetc_eatbnl();
12647 /* $[{[#]]NAME[}] */
12650 c = pgetc_eatbnl();
12651 } while (is_in_name(c));
12652 } else if (isdigit(c)) {
12653 /* $[{[#]]NUM[}] */
12656 c = pgetc_eatbnl();
12657 } while (!subtype && isdigit(c));
12658 } else if (c != '}') {
12659 /* $[{[#]]<specialchar>[}] */
12662 c = pgetc_eatbnl();
12663 if (!subtype && cc == '#') {
12664 subtype = VSLENGTH;
12665 if (c == '_' || isalnum(c))
12668 c = pgetc_eatbnl();
12669 if (cc == '}' || c != '}') {
12677 if (!is_special(cc)) {
12678 if (subtype == VSLENGTH)
12687 if (c != '}' && subtype == VSLENGTH) {
12688 /* ${#VAR didn't end with } */
12692 if (subtype == 0) {
12693 static const char types[] ALIGN1 = "}-+?=";
12694 /* ${VAR...} but not $VAR or ${#VAR} */
12695 /* c == first char after VAR */
12700 c = pgetc_eatbnl();
12702 /* This check is only needed to not misinterpret
12703 * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD}
12706 if (!strchr(types, c)) {
12707 subtype = VSSUBSTR;
12709 break; /* "goto badsub" is bigger (!) */
12715 const char *p = strchr(types, c);
12718 subtype |= p - types + VSNORMAL;
12723 subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT);
12724 c = pgetc_eatbnl();
12730 newsyn = BASESYNTAX;
12732 #if BASH_PATTERN_SUBST
12734 /* ${v/[/]pattern/repl} */
12735 //TODO: encode pattern and repl separately.
12736 // Currently cases like: v=1;echo ${v/$((1/1))/ONE}
12737 // are broken (should print "ONE")
12738 subtype = VSREPLACE;
12739 newsyn = BASESYNTAX;
12740 c = pgetc_eatbnl();
12743 subtype++; /* VSREPLACEALL */
12752 if (newsyn == ARISYNTAX)
12755 if ((newsyn != synstack->syntax || synstack->innerdq)
12756 && subtype != VSNORMAL
12758 synstack_push(&synstack,
12759 synstack->prev ?: alloca(sizeof(*synstack)),
12762 synstack->varpushed = 1;
12763 synstack->dblquote = newsyn != BASESYNTAX;
12766 ((unsigned char *)stackblock())[typeloc] = subtype;
12767 if (subtype != VSNORMAL) {
12768 synstack->varnest++;
12769 if (synstack->dblquote)
12770 synstack->dqvarnest++;
12774 goto parsesub_return;
12778 * Called to parse command substitutions. Newstyle is set if the command
12779 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12780 * list of commands (passed by reference), and savelen is the number of
12781 * characters on the top of the stack which must be preserved.
12784 struct nodelist **nlpp;
12788 struct heredoc *saveheredoclist;
12789 smallint saveprompt = 0;
12792 savelen = out - (char *)stackblock();
12795 * FIXME: this can allocate very large block on stack and SEGV.
12797 * echo "..<100kbytes>..`true` $(true) `true` ..."
12798 * allocates 100kb for every command subst. With about
12799 * a hundred command substitutions stack overflows.
12800 * With larger prepended string, SEGV happens sooner.
12802 str = alloca(savelen);
12803 memcpy(str, stackblock(), savelen);
12807 /* We must read until the closing backquote, giving special
12808 * treatment to some slashes, and then push the string and
12809 * reread it as input, interpreting it normally.
12815 STARTSTACKSTR(pout);
12819 setprompt_if(needprompt, 2);
12820 pc = pgetc_eatbnl();
12826 pc = pgetc(); /* or pgetc_eatbnl()? why (example)? */
12827 if (pc != '\\' && pc != '`' && pc != '$'
12828 && (!synstack->dblquote || pc != '"')
12830 STPUTC('\\', pout);
12832 if (pc <= 255 /* not PEOA or PEOF */) {
12838 IF_ASH_ALIAS(case PEOA:)
12839 raise_error_syntax("EOF in backquote substitution");
12851 STPUTC('\0', pout);
12852 psavelen = pout - (char *)stackblock();
12853 if (psavelen > 0) {
12854 pstr = grabstackstr(pout);
12855 setinputstring(pstr);
12860 nlpp = &(*nlpp)->next;
12861 *nlpp = stzalloc(sizeof(**nlpp));
12862 /* (*nlpp)->next = NULL; - stzalloc did it */
12864 saveheredoclist = heredoclist;
12865 heredoclist = NULL;
12868 saveprompt = doprompt;
12875 doprompt = saveprompt;
12877 if (readtoken() != TRP)
12878 raise_error_unexpected_syntax(TRP);
12879 setinputstring(nullstr);
12883 heredoclist = saveheredoclist;
12886 /* Start reading from old file again. */
12888 /* Ignore any pushed back tokens left from the backquote parsing. */
12891 out = growstackto(savelen + 1);
12893 memcpy(out, str, savelen);
12894 STADJUST(savelen, out);
12896 USTPUTC(CTLBACKQ, out);
12898 goto parsebackq_oldreturn;
12899 goto parsebackq_newreturn;
12902 #if ENABLE_FEATURE_SH_MATH
12904 * Parse an arithmetic expansion (indicate start of one and set state)
12908 synstack_push(&synstack,
12909 synstack->prev ?: alloca(sizeof(*synstack)),
12911 synstack->dblquote = 1;
12912 USTPUTC(CTLARI, out);
12913 goto parsearith_return;
12916 } /* end of readtoken */
12919 * Read the next input token.
12920 * If the token is a word, we set backquotelist to the list of cmds in
12921 * backquotes. We set quoteflag to true if any part of the word was
12923 * If the token is TREDIR, then we set redirnode to a structure containing
12926 * [Change comment: here documents and internal procedures]
12927 * [Readtoken shouldn't have any arguments. Perhaps we should make the
12928 * word parsing code into a separate routine. In this case, readtoken
12929 * doesn't need to have any internal procedures, but parseword does.
12930 * We could also make parseoperator in essence the main routine, and
12931 * have parseword (readtoken1?) handle both words and redirection.]
12933 #define NEW_xxreadtoken
12934 #ifdef NEW_xxreadtoken
12935 /* singles must be first! */
12936 static const char xxreadtoken_chars[7] ALIGN1 = {
12937 '\n', '(', ')', /* singles */
12938 '&', '|', ';', /* doubles */
12942 #define xxreadtoken_singles 3
12943 #define xxreadtoken_doubles 3
12945 static const char xxreadtoken_tokens[] ALIGN1 = {
12946 TNL, TLP, TRP, /* only single occurrence allowed */
12947 TBACKGND, TPIPE, TSEMI, /* if single occurrence */
12948 TEOF, /* corresponds to trailing nul */
12949 TAND, TOR, TENDCASE /* if double occurrence */
12961 setprompt_if(needprompt, 2);
12962 for (;;) { /* until token or start of word found */
12963 c = pgetc_eatbnl();
12964 if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA))
12968 while ((c = pgetc()) != '\n' && c != PEOF)
12971 } else if (c == '\\') {
12972 break; /* return readtoken1(...) */
12976 p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
12982 p = strchr(xxreadtoken_chars, c);
12984 break; /* return readtoken1(...) */
12986 if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) {
12987 int cc = pgetc_eatbnl();
12988 if (cc == c) { /* double occurrence? */
12989 p += xxreadtoken_doubles + 1;
12992 #if BASH_REDIR_OUTPUT
12993 if (c == '&' && cc == '>') /* &> */
12994 break; /* return readtoken1(...) */
12999 lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
13004 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
13006 #else /* old xxreadtoken */
13007 #define RETURN(token) return lasttoken = token
13017 setprompt_if(needprompt, 2);
13018 for (;;) { /* until token or start of word found */
13019 c = pgetc_eatbnl();
13021 case ' ': case '\t':
13022 IF_ASH_ALIAS(case PEOA:)
13025 while ((c = pgetc()) != '\n' && c != PEOF)
13035 if (pgetc_eatbnl() == '&')
13040 if (pgetc_eatbnl() == '|')
13045 if (pgetc_eatbnl() == ';')
13056 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
13059 #endif /* old xxreadtoken */
13065 int kwd = checkkwd;
13067 smallint alreadyseen = tokpushback;
13070 #if ENABLE_ASH_ALIAS
13086 if (t != TWORD || quoteflag) {
13091 * check for keywords
13093 if (kwd & CHKKWD) {
13094 const char *const *pp;
13096 pp = findkwd(wordtext);
13098 lasttoken = t = pp - tokname_array;
13099 TRACE(("keyword '%s' recognized\n", tokname_array[t]));
13104 if (checkkwd & CHKALIAS) {
13105 #if ENABLE_ASH_ALIAS
13107 ap = lookupalias(wordtext, 1);
13110 pushstring(ap->val, ap);
13120 TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
13122 TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
13138 * Read and parse a command. Returns NODE_EOF on end of file.
13139 * (NULL is a valid parse tree indicating a blank line.)
13141 static union node *
13142 parsecmd(int interact)
13147 doprompt = interact;
13148 setprompt_if(doprompt, doprompt);
13154 * Input any here documents.
13159 struct heredoc *here;
13162 here = heredoclist;
13163 heredoclist = NULL;
13167 setprompt_if(needprompt, 2);
13168 if (here->here->type == NHERE)
13169 readtoken1(pgetc(), SQSYNTAX, here->eofmark, here->striptabs);
13171 readtoken1(pgetc_eatbnl(), DQSYNTAX, here->eofmark, here->striptabs);
13172 n = stzalloc(sizeof(struct narg));
13173 n->narg.type = NARG;
13174 /*n->narg.next = NULL; - stzalloc did it */
13175 n->narg.text = wordtext;
13176 n->narg.backquote = backquotelist;
13177 here->here->nhere.doc = n;
13183 static const char *
13184 expandstr(const char *ps, int syntax_type)
13188 struct parsefile *file_stop = g_parsefile;
13189 volatile int saveint;
13190 struct jmploc *volatile savehandler = exception_handler;
13191 struct jmploc jmploc;
13192 const char *volatile result;
13195 /* XXX Fix (char *) cast. */
13196 setinputstring((char *)ps);
13198 saveprompt = doprompt;
13203 err = setjmp(jmploc.loc);
13207 /* readtoken1() might die horribly.
13208 * Try a prompt with syntactically wrong command:
13209 * PS1='$(date "+%H:%M:%S) > '
13211 exception_handler = &jmploc;
13212 readtoken1(pgetc(), syntax_type, FAKEEOFMARK, 0);
13214 n.narg.type = NARG;
13215 n.narg.next = NULL;
13216 n.narg.text = wordtext;
13217 n.narg.backquote = backquotelist;
13219 /* expandarg() might fail too:
13222 expandarg(&n, NULL, EXP_QUOTED);
13223 result = stackblock();
13226 exception_handler = savehandler;
13227 if (err && exception_type != EXERROR)
13228 longjmp(exception_handler->loc, 1);
13229 RESTORE_INT(saveint);
13231 doprompt = saveprompt;
13232 /* Try: PS1='`xxx(`' */
13233 unwindfiles(file_stop);
13241 return tokpushback && lasttoken == TEOF;
13245 * Execute a command or commands contained in a string.
13248 evalstring(char *s, int flags)
13250 struct jmploc *volatile savehandler;
13251 struct jmploc jmploc;
13255 struct stackmark smark;
13260 setstackmark(&smark);
13263 /* On exception inside execution loop, we must popfile().
13264 * Try interactively:
13266 * command eval "a=b" # throws "is read only" error
13267 * "command BLTIN" is not supposed to abort (even in non-interactive use).
13268 * But if we skip popfile(), we hit EOF in eval's string, and exit.
13270 savehandler = exception_handler;
13271 ex = setjmp(jmploc.loc);
13274 exception_handler = &jmploc;
13276 while ((n = parsecmd(0)) != NODE_EOF) {
13279 i = evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT));
13282 popstackmark(&smark);
13287 popstackmark(&smark);
13291 exception_handler = savehandler;
13293 longjmp(exception_handler->loc, ex);
13299 * The eval command.
13301 static int FAST_FUNC
13302 evalcmd(int argc UNUSED_PARAM, char **argv, int flags)
13311 STARTSTACKSTR(concat);
13313 concat = stack_putstr(p, concat);
13317 STPUTC(' ', concat);
13319 STPUTC('\0', concat);
13320 p = grabstackstr(concat);
13322 return evalstring(p, flags & EV_TESTED);
13328 * Read and execute commands.
13329 * "Top" is nonzero for the top level command loop;
13330 * it turns on prompting if the shell is interactive.
13336 struct stackmark smark;
13341 TRACE(("cmdloop(%d) called\n", top));
13345 setstackmark(&smark);
13348 showjobs(SHOW_CHANGED|SHOW_STDERR);
13351 if (iflag && top) {
13355 n = parsecmd(inter);
13357 if (DEBUG > 2 && debug && (n != NODE_EOF))
13360 if (n == NODE_EOF) {
13361 if (!top || numeof >= 50)
13363 if (!stoppedjobs()) {
13366 newline_and_flush(stderr);
13370 out2str("\nUse \"exit\" to leave shell.\n");
13373 } else if (nflag == 0) {
13376 /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */
13379 i = evaltree(n, 0);
13383 popstackmark(&smark);
13387 evalskip &= ~(SKIPFUNC | SKIPFUNCDEF);
13395 * Take commands from a file. To be compatible we should do a path
13396 * search for the file, which is necessary to find sub-commands.
13399 find_dot_file(char *basename)
13402 const char *path = pathval();
13406 /* don't try this for absolute or relative paths */
13407 if (strchr(basename, '/'))
13410 while ((len = padvance(&path, basename)) >= 0) {
13411 fullname = stackblock();
13412 if ((!pathopt || *pathopt == 'f')
13413 && !stat(fullname, &statb) && S_ISREG(statb.st_mode)
13415 /* This will be freed by the caller. */
13416 return stalloc(len);
13419 /* not found in PATH */
13421 #if ENABLE_ASH_BASH_SOURCE_CURDIR
13424 ash_msg_and_raise_error("%s: not found", basename);
13429 static int FAST_FUNC
13430 dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM)
13432 /* "false; . empty_file; echo $?" should print 0, not 1: */
13436 char *args_need_save;
13437 volatile struct shparam saveparam;
13440 // struct strlist *sp;
13441 // for (sp = cmdenviron; sp; sp = sp->next)
13442 // setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
13444 nextopt(nullstr); /* handle possible "--" */
13448 /* bash says: "bash: .: filename argument required" */
13449 return 2; /* bash compat */
13452 /* This aborts if file isn't found, which is POSIXly correct.
13453 * bash returns exitcode 1 instead.
13455 fullname = find_dot_file(argv[0]);
13457 args_need_save = argv[0];
13458 if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */
13460 saveparam = shellparam;
13461 shellparam.malloced = 0;
13465 shellparam.nparam = argc;
13466 shellparam.p = argv;
13469 /* This aborts if file can't be opened, which is POSIXly correct.
13470 * bash returns exitcode 1 instead.
13472 setinputfile(fullname, INPUT_PUSH_FILE);
13473 commandname = fullname;
13474 status = cmdloop(0);
13477 if (args_need_save) {
13478 freeparam(&shellparam);
13479 shellparam = saveparam;
13485 static int FAST_FUNC
13486 exitcmd(int argc UNUSED_PARAM, char **argv)
13492 savestatus = number(argv[1]);
13494 raise_exception(EXEXIT);
13499 * Read a file containing shell functions.
13502 readcmdfile(char *name)
13504 setinputfile(name, INPUT_PUSH_FILE);
13510 /* ============ find_command inplementation */
13513 * Resolve a command name. If you change this routine, you may have to
13514 * change the shellexec routine as well.
13517 find_command(char *name, struct cmdentry *entry, int act, const char *path)
13519 struct tblentry *cmdp;
13526 struct builtincmd *bcmd;
13529 /* If name contains a slash, don't use PATH or hash table */
13530 if (strchr(name, '/') != NULL) {
13531 entry->u.index = -1;
13532 if (act & DO_ABS) {
13533 while (stat(name, &statb) < 0) {
13535 if (errno == EINTR)
13538 entry->cmdtype = CMDUNKNOWN;
13542 entry->cmdtype = CMDNORMAL;
13546 /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */
13548 updatetbl = (path == pathval());
13552 /* If name is in the table, check answer will be ok */
13553 cmdp = cmdlookup(name, 0);
13554 if (cmdp != NULL) {
13557 switch (cmdp->cmdtype) {
13563 bit = DO_ALTPATH | DO_REGBLTIN;
13569 bit = IS_BUILTIN_REGULAR(cmdp->param.cmd) ? 0 : DO_REGBLTIN;
13573 if (act & bit & DO_REGBLTIN)
13578 } else if (cmdp->rehash == 0)
13579 /* if not invalidated by cd, we're done */
13583 /* If %builtin not in path, check for builtin next */
13584 bcmd = find_builtin(name);
13586 if (IS_BUILTIN_REGULAR(bcmd))
13587 goto builtin_success;
13588 if (act & DO_ALTPATH)
13589 goto builtin_success;
13590 if (builtinloc <= 0)
13591 goto builtin_success;
13594 if (act & DO_REGBLTIN)
13597 #if ENABLE_FEATURE_SH_STANDALONE
13599 int applet_no = find_applet_by_name(name);
13600 if (applet_no >= 0) {
13601 entry->cmdtype = CMDNORMAL;
13602 entry->u.index = -2 - applet_no;
13608 /* We have to search path. */
13609 prev = -1; /* where to start */
13610 if (cmdp && cmdp->rehash) { /* doing a rehash */
13611 if (cmdp->cmdtype == CMDBUILTIN)
13614 prev = cmdp->param.index;
13620 while ((len = padvance(&path, name)) >= 0) {
13621 const char *lpathopt = pathopt;
13623 fullname = stackblock();
13626 if (*lpathopt == 'b') {
13628 goto builtin_success;
13630 } else if (!(act & DO_NOFUNC)) {
13631 /* handled below */
13633 /* ignore unimplemented options */
13637 /* if rehash, don't redo absolute path names */
13638 if (fullname[0] == '/' && idx <= prev) {
13641 TRACE(("searchexec \"%s\": no change\n", name));
13644 while (stat(fullname, &statb) < 0) {
13646 if (errno == EINTR)
13649 if (errno != ENOENT && errno != ENOTDIR)
13653 e = EACCES; /* if we fail, this will be the error */
13654 if (!S_ISREG(statb.st_mode))
13656 if (lpathopt) { /* this is a %func directory */
13658 /* NB: stalloc will return space pointed by fullname
13659 * (because we don't have any intervening allocations
13660 * between stunalloc above and this stalloc) */
13661 readcmdfile(fullname);
13662 cmdp = cmdlookup(name, 0);
13663 if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION)
13664 ash_msg_and_raise_error("%s not defined in %s", name, fullname);
13665 stunalloc(fullname);
13668 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
13670 entry->cmdtype = CMDNORMAL;
13671 entry->u.index = idx;
13675 cmdp = cmdlookup(name, 1);
13676 cmdp->cmdtype = CMDNORMAL;
13677 cmdp->param.index = idx;
13682 /* We failed. If there was an entry for this command, delete it */
13683 if (cmdp && updatetbl)
13684 delete_cmd_entry();
13685 if (act & DO_ERR) {
13686 #if ENABLE_ASH_BASH_NOT_FOUND_HOOK
13687 struct tblentry *hookp = cmdlookup("command_not_found_handle", 0);
13688 if (hookp && hookp->cmdtype == CMDFUNCTION) {
13690 argv[0] = (char*) "command_not_found_handle";
13693 evalfun(hookp->param.func, 2, argv, 0);
13694 entry->cmdtype = CMDUNKNOWN;
13698 ash_msg("%s: %s", name, errmsg(e, "not found"));
13701 entry->cmdtype = CMDUNKNOWN;
13706 entry->cmdtype = CMDBUILTIN;
13707 entry->u.cmd = bcmd;
13711 cmdp = cmdlookup(name, 1);
13712 cmdp->cmdtype = CMDBUILTIN;
13713 cmdp->param.cmd = bcmd;
13717 entry->cmdtype = cmdp->cmdtype;
13718 entry->u = cmdp->param;
13723 * The trap builtin.
13725 static int FAST_FUNC
13726 trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13730 int signo, exitcode;
13735 for (signo = 0; signo < NSIG; signo++) {
13736 char *tr = trap_ptr[signo];
13738 /* note: bash adds "SIG", but only if invoked
13739 * as "bash". If called as "sh", or if set -o posix,
13740 * then it prints short signal names.
13741 * We are printing short names: */
13742 out1fmt("trap -- %s %s\n",
13744 get_signame(signo));
13745 /* trap_ptr != trap only if we are in special-cased `trap` code.
13746 * In this case, we will exit very soon, no need to free(). */
13747 /* if (trap_ptr != trap && tp[0]) */
13752 if (trap_ptr != trap) {
13760 /* Why the second check?
13761 * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..."
13762 * In this case, NUM is signal no, not an action.
13765 if (ap[1] && !is_number(ap[0]))
13770 signo = get_signum(*ap);
13772 /* Mimic bash message exactly */
13773 ash_msg("%s: invalid signal specification", *ap);
13779 if (LONE_DASH(action))
13782 if (action[0]) /* not NULL and not "" and not "-" */
13783 may_have_traps = 1;
13784 action = ckstrdup(action);
13788 trap[signo] = action;
13799 /* ============ Builtins */
13801 #if ENABLE_ASH_HELP
13802 static int FAST_FUNC
13803 helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13809 "Built-in commands:\n"
13810 "------------------\n");
13811 for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
13812 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '),
13813 builtintab[i].name + 1);
13819 # if ENABLE_FEATURE_SH_STANDALONE
13821 const char *a = applet_names;
13823 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a);
13828 while (*a++ != '\0')
13833 newline_and_flush(stdout);
13834 return EXIT_SUCCESS;
13839 static int FAST_FUNC
13840 historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13842 if (line_input_state)
13843 show_history(line_input_state);
13844 return EXIT_SUCCESS;
13849 * The export and readonly commands.
13851 static int FAST_FUNC
13852 exportcmd(int argc UNUSED_PARAM, char **argv)
13862 /* "readonly" in bash accepts, but ignores -n.
13863 * We do the same: it saves a conditional in nextopt's param.
13866 while ((opt = nextopt("np")) != '\0') {
13868 flag_off = VEXPORT;
13871 if (argv[0][0] == 'r') {
13873 flag_off = 0; /* readonly ignores -n */
13875 flag_off = ~flag_off;
13877 /*if (opt_p_not_specified) - bash doesn't check this. Try "export -p NAME" */
13883 p = strchr(name, '=');
13887 vp = *findvar(hashvar(name), name);
13889 vp->flags = ((vp->flags | flag) & flag_off);
13893 setvar(name, p, (flag & flag_off));
13894 } while ((name = *++aptr) != NULL);
13899 /* No arguments. Show the list of exported or readonly vars.
13902 showvars(argv[0], flag, 0);
13907 * Delete a function if it exists.
13910 unsetfunc(const char *name)
13912 struct tblentry *cmdp;
13914 cmdp = cmdlookup(name, 0);
13915 if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION)
13916 delete_cmd_entry();
13920 * The unset builtin command. We unset the function before we unset the
13921 * variable to allow a function to be unset when there is a readonly variable
13922 * with the same name.
13924 static int FAST_FUNC
13925 unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13931 while ((i = nextopt("vf")) != 0) {
13935 for (ap = argptr; *ap; ap++) {
13946 static const unsigned char timescmd_str[] ALIGN1 = {
13947 ' ', offsetof(struct tms, tms_utime),
13948 '\n', offsetof(struct tms, tms_stime),
13949 ' ', offsetof(struct tms, tms_cutime),
13950 '\n', offsetof(struct tms, tms_cstime),
13953 static int FAST_FUNC
13954 timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13957 const unsigned char *p;
13960 clk_tck = bb_clk_tck();
13965 unsigned sec, frac;
13967 t = *(clock_t *)(((char *) &buf) + p[1]);
13969 frac = t % clk_tck;
13970 out1fmt("%um%u.%03us%c",
13971 sec / 60, sec % 60,
13972 (frac * 1000) / clk_tck,
13980 #if ENABLE_FEATURE_SH_MATH
13982 * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell.
13983 * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
13985 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
13987 static int FAST_FUNC
13988 letcmd(int argc UNUSED_PARAM, char **argv)
13994 ash_msg_and_raise_error("expression expected");
13996 i = ash_arith(*argv);
14004 * The read builtin. Options:
14005 * -r Do not interpret '\' specially
14006 * -s Turn off echo (tty only)
14007 * -n NCHARS Read NCHARS max
14008 * -p PROMPT Display PROMPT on stderr (if input is from tty)
14009 * -t SECONDS Timeout after SECONDS (tty or pipe only)
14010 * -u FD Read from given FD instead of fd 0
14011 * -d DELIM End on DELIM char, not newline
14012 * This uses unbuffered input, which may be avoidable in some cases.
14013 * TODO: bash also has:
14014 * -a ARRAY Read into array[0],[1],etc
14015 * -e Use line editing (tty only)
14017 static int FAST_FUNC
14018 readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
14020 struct builtin_read_params params;
14024 memset(¶ms, 0, sizeof(params));
14026 while ((i = nextopt("p:u:rt:n:sd:")) != '\0') {
14029 params.opt_p = optionarg;
14032 params.opt_n = optionarg;
14035 params.read_flags |= BUILTIN_READ_SILENT;
14038 params.opt_t = optionarg;
14041 params.read_flags |= BUILTIN_READ_RAW;
14044 params.opt_u = optionarg;
14048 params.opt_d = optionarg;
14056 params.argv = argptr;
14057 params.setvar = setvar0;
14058 params.ifs = bltinlookup("IFS"); /* can be NULL */
14060 /* "read -s" needs to save/restore termios, can't allow ^C
14061 * to jump out of it.
14065 r = shell_builtin_read(¶ms);
14068 if ((uintptr_t)r == 1 && errno == EINTR) {
14069 /* To get SIGCHLD: sleep 1 & read x; echo $x
14070 * Correct behavior is to not exit "read"
14072 if (pending_sig == 0)
14076 if ((uintptr_t)r > 1)
14077 ash_msg_and_raise_error(r);
14079 return (uintptr_t)r;
14082 static int FAST_FUNC
14083 umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
14085 static const char permuser[3] ALIGN1 = "ogu";
14088 int symbolic_mode = 0;
14090 while (nextopt("S") != '\0') {
14099 if (*argptr == NULL) {
14100 if (symbolic_mode) {
14101 char buf[sizeof(",u=rwx,g=rwx,o=rwx")];
14108 *p++ = permuser[i];
14110 /* mask is 0..0uuugggooo. i=2 selects uuu bits */
14111 if (!(mask & 0400)) *p++ = 'r';
14112 if (!(mask & 0200)) *p++ = 'w';
14113 if (!(mask & 0100)) *p++ = 'x';
14121 out1fmt("%04o\n", mask);
14124 char *modestr = *argptr;
14125 /* numeric umasks are taken as-is */
14126 /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
14127 if (!isdigit(modestr[0]))
14129 mask = bb_parse_mode(modestr, mask);
14130 if ((unsigned)mask > 0777) {
14131 ash_msg_and_raise_error("illegal mode: %s", modestr);
14133 if (!isdigit(modestr[0]))
14140 static int FAST_FUNC
14141 ulimitcmd(int argc UNUSED_PARAM, char **argv)
14143 return shell_builtin_ulimit(argv);
14146 /* ============ main() and helpers */
14149 * This routine is called when an error or an interrupt occurs in an
14150 * interactive shell and control is returned to the main command loop
14151 * but prior to exitshell.
14157 if (savestatus >= 0) {
14158 if (exception_type == EXEXIT || evalskip == SKIPFUNCDEF)
14159 exitstatus = savestatus;
14165 /* from expand.c: */
14168 /* from redir.c: */
14173 * This routine is called when an error or an interrupt occurs in an
14174 * interactive shell and control is returned to the main command loop.
14175 * (In dash, this function is auto-generated by build machinery).
14180 /* from input.c: */
14181 g_parsefile->left_in_buffer = 0;
14182 g_parsefile->left_in_line = 0; /* clear input buffer */
14186 unwindlocalvars(NULL);
14190 * Called to exit the shell.
14198 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
14199 if (line_input_state)
14200 save_history(line_input_state);
14202 savestatus = exitstatus;
14203 TRACE(("pid %d, exitshell(%d)\n", getpid(), savestatus));
14204 if (setjmp(loc.loc))
14206 exception_handler = &loc;
14212 evalskip = SKIPFUNCDEF;
14213 /*free(p); - we'll exit soon */
14217 /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}".
14218 * our setjobctl(0) does not panic if tcsetpgrp fails inside it.
14221 flush_stdout_stderr();
14226 /* Don't inline: conserve stack of caller from having our locals too */
14227 static NOINLINE void
14230 /* we will never free this */
14231 basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ);
14234 sigmode[SIGCHLD - 1] = S_DFL; /* ensure we install handler even if it is SIG_IGNed */
14235 setsignal(SIGCHLD);
14242 for (envp = environ; envp && *envp; envp++) {
14244 * p = endofname(*envp);
14245 * if (p != *envp && *p == '=') {
14246 * here to weed out badly-named variables, but this breaks
14247 * scenarios where people do want them passed to children:
14249 * os.environ["test-test"]="test"
14250 * if os.fork() == 0:
14251 * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this
14252 * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) # breaks this
14254 if (strchr(*envp, '=')) {
14255 setvareq(*envp, VEXPORT|VTEXTFIXED);
14259 setvareq((char*)defifsvar, VTEXTFIXED);
14260 setvareq((char*)defoptindvar, VTEXTFIXED);
14262 setvar0("PPID", utoa(getppid()));
14264 p = lookupvar("SHLVL");
14265 setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT);
14267 #if BASH_HOSTNAME_VAR
14268 if (!lookupvar("HOSTNAME")) {
14269 struct utsname uts;
14271 setvar0("HOSTNAME", uts.nodename);
14274 p = lookupvar("PWD");
14276 struct stat st1, st2;
14277 if (p[0] != '/' || stat(p, &st1) || stat(".", &st2)
14278 || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino
14288 //usage:#define ash_trivial_usage
14289 //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]"
14290 //usage:#define ash_full_usage "\n\n"
14291 //usage: "Unix shell interpreter"
14294 * Process the shell command line arguments.
14297 procargs(char **argv)
14300 const char *xminusc;
14305 login_sh = xargv[0] && xargv[0][0] == '-';
14306 #if NUM_SCRIPTS > 0
14311 /* if (xargv[0]) - mmm, this is always true! */
14314 for (i = 0; i < NOPTS; i++)
14316 if (options(&login_sh)) {
14317 /* it already printed err message */
14318 raise_exception(EXERROR);
14322 if (*xargv == NULL) {
14324 ash_msg_and_raise_error(bb_msg_requires_arg, "-c");
14327 if (iflag == 2 /* no explicit -i given */
14328 && sflag == 1 /* -s given (or implied) */
14329 && !minusc /* bash compat: ash -sc 'echo $-' is not interactive (dash is) */
14330 && isatty(0) && isatty(1) /* we are on tty */
14336 for (i = 0; i < NOPTS; i++)
14337 if (optlist[i] == 2)
14342 /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */
14347 } else if (!sflag) {
14348 setinputfile(*xargv, 0);
14351 commandname = arg0;
14354 shellparam.p = xargv;
14355 #if ENABLE_ASH_GETOPTS
14356 shellparam.optind = 1;
14357 shellparam.optoff = -1;
14359 /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */
14361 shellparam.nparam++;
14370 * Read /etc/profile, ~/.profile, $ENV.
14373 read_profile(const char *name)
14375 name = expandstr(name, DQSYNTAX);
14376 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
14383 static short profile_buf[16384];
14384 extern int etext();
14388 * Main routine. We initialize things, parse the arguments, execute
14389 * profiles if we're a login shell, and then call cmdloop to execute
14390 * commands. The setjmp call sets up the location to jump to when an
14391 * exception occurs. When an exception occurs the variable "state"
14392 * is used to figure out how far we had gotten.
14394 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
14395 #if NUM_SCRIPTS > 0
14396 int ash_main(int argc, char **argv)
14398 int ash_main(int argc UNUSED_PARAM, char **argv)
14400 /* note: 'argc' is used only if embedded scripts are enabled */
14402 volatile smallint state;
14403 struct jmploc jmploc;
14404 struct stackmark smark;
14407 /* Initialize global data */
14411 #if ENABLE_ASH_ALIAS
14417 monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
14421 if (setjmp(jmploc.loc)) {
14427 e = exception_type;
14429 if (e == EXEND || e == EXEXIT || s == 0 || iflag == 0 || shlvl) {
14436 newline_and_flush(stderr);
14439 popstackmark(&smark);
14440 FORCE_INT_ON; /* enable interrupts */
14449 exception_handler = &jmploc;
14450 rootpid = getpid();
14453 setstackmark(&smark);
14455 #if NUM_SCRIPTS > 0
14457 /* Non-NULL minusc tells procargs that an embedded script is being run */
14458 minusc = get_script_content(-argc - 1);
14460 login_sh = procargs(argv);
14462 TRACE(("Shell args: "));
14463 trace_puts_args(argv);
14470 read_profile("/etc/profile");
14473 hp = lookupvar("HOME");
14475 read_profile("$HOME/.profile");
14481 getuid() == geteuid() && getgid() == getegid() &&
14485 const char *shinit = lookupvar("ENV");
14486 if (shinit != NULL && *shinit != '\0')
14487 read_profile(shinit);
14489 popstackmark(&smark);
14493 /* evalstring pushes parsefile stack.
14494 * Ensure we don't falsely claim that 0 (stdin)
14495 * is one of stacked source fds.
14496 * Testcase: ash -c 'exec 1>&0' must not complain. */
14498 // if (!sflag) g_parsefile->pf_fd = -1;
14499 // ^^ not necessary since now we special-case fd 0
14500 // in save_fd_on_redirect()
14502 // dash: evalstring(minusc, sflag ? 0 : EV_EXIT);
14504 // ash -sc 'echo $-'
14505 // continue reading input from stdin after running 'echo'.
14506 // bash does not do this: it prints "hBcs" and exits.
14507 evalstring(minusc, EV_EXIT);
14510 if (sflag || minusc == NULL) {
14511 #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
14513 const char *hp = lookupvar("HISTFILE");
14515 hp = lookupvar("HOME");
14518 hp = concat_path_file(hp, ".ash_history");
14519 setvar0("HISTFILE", hp);
14522 hp = lookupvar("HISTFILE");
14526 line_input_state->hist_file = hp;
14527 # if ENABLE_FEATURE_SH_HISTFILESIZE
14528 hp = lookupvar("HISTFILESIZE");
14529 line_input_state->max_history = size_from_HISTFILESIZE(hp);
14533 state4: /* XXX ??? - why isn't this before the "if" statement */
14535 /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry.
14537 * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect
14538 * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed
14540 signal(SIGHUP, SIG_DFL);
14549 extern void _mcleanup(void);
14553 TRACE(("End of main reached\n"));
14560 * Copyright (c) 1989, 1991, 1993, 1994
14561 * The Regents of the University of California. All rights reserved.
14563 * This code is derived from software contributed to Berkeley by
14564 * Kenneth Almquist.
14566 * Redistribution and use in source and binary forms, with or without
14567 * modification, are permitted provided that the following conditions
14569 * 1. Redistributions of source code must retain the above copyright
14570 * notice, this list of conditions and the following disclaimer.
14571 * 2. Redistributions in binary form must reproduce the above copyright
14572 * notice, this list of conditions and the following disclaimer in the
14573 * documentation and/or other materials provided with the distribution.
14574 * 3. Neither the name of the University nor the names of its contributors
14575 * may be used to endorse or promote products derived from this software
14576 * without specific prior written permission.
14578 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
14579 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14580 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14581 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
14582 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14583 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14584 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14585 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14586 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
14587 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF