1 /* vi: set sw=4 ts=4: */
3 * ash shell port for busybox
5 * This code is derived from software contributed to Berkeley by
8 * Original BSD copyright notice is retained at the end of this file.
10 * Copyright (c) 1989, 1991, 1993, 1994
11 * The Regents of the University of California. All rights reserved.
13 * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
14 * was re-ported from NetBSD and debianized.
16 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
19 //config: bool "ash (77 kb)"
21 //config: depends on !NOMMU
23 //config: The most complete and most pedantically correct shell included with
24 //config: busybox. This shell is actually a derivative of the Debian 'dash'
25 //config: shell (by Herbert Xu), which was created by porting the 'ash' shell
26 //config: (written by Kenneth Almquist) from NetBSD.
28 //config:# ash options
29 //config:# note: Don't remove !NOMMU part in the next line; it would break
30 //config:# menuconfig's indenting.
31 //config:if !NOMMU && (ASH || SH_IS_ASH || BASH_IS_ASH)
33 //config:config ASH_OPTIMIZE_FOR_SIZE
34 //config: bool "Optimize for size instead of speed"
36 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
38 //config:config ASH_INTERNAL_GLOB
39 //config: bool "Use internal glob() implementation"
40 //config: default y # Y is bigger, but because of uclibc glob() bug, let Y be default for now
41 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
43 //config: Do not use glob() function from libc, use internal implementation.
44 //config: Use this if you are getting "glob.h: No such file or directory"
45 //config: or similar build errors.
46 //config: Note that as of now (2017-01), uclibc and musl glob() both have bugs
47 //config: which would break ash if you select N here.
49 //config:config ASH_BASH_COMPAT
50 //config: bool "bash-compatible extensions"
52 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
54 //config:config ASH_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:config ASH_EMBEDDED_SCRIPTS
152 //config: bool "Embed scripts in the binary"
154 //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH
156 //config: Allow scripts to be compressed and embedded in the busybox
157 //config: binary. The scripts should be placed in the 'embed' directory
158 //config: at build time. Like applets, scripts can be run as
159 //config: 'busybox SCRIPT ...' or by linking their name to the binary.
161 //config:endif # ash options
163 //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP))
164 // APPLET_ODDNAME:name main location suid_type help
165 //applet:IF_SH_IS_ASH( APPLET_ODDNAME(sh, ash, BB_DIR_BIN, BB_SUID_DROP, ash))
166 //applet:IF_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, BB_DIR_BIN, BB_SUID_DROP, ash))
168 //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o
169 //kbuild:lib-$(CONFIG_SH_IS_ASH) += ash.o ash_ptr_hack.o shell_common.o
170 //kbuild:lib-$(CONFIG_BASH_IS_ASH) += ash.o ash_ptr_hack.o shell_common.o
171 //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o
174 * DEBUG=1 to compile in debugging ('set -o debug' turns on)
175 * DEBUG=2 to compile in and turn on debugging.
176 * When debugging is on ("set -o debug" was executed, or DEBUG=2),
177 * debugging info is written to ./trace, quit signal generates core dump.
180 /* Tweak debug output verbosity here */
184 #define DEBUG_INTONOFF 0
188 #define JOBS ENABLE_ASH_JOB_CONTROL
191 #include <sys/times.h>
192 #include <sys/utsname.h> /* for setting $HOSTNAME */
193 #include "busybox.h" /* for applet_names */
194 #if ENABLE_ASH_EMBEDDED_SCRIPTS
195 # include "embedded_scripts.h"
197 # define NUM_SCRIPTS 0
200 /* So far, all bash compat is controlled by one config option */
201 /* Separate defines document which part of code implements what */
202 /* function keyword */
203 #define BASH_FUNCTION ENABLE_ASH_BASH_COMPAT
204 #define IF_BASH_FUNCTION IF_ASH_BASH_COMPAT
206 #define BASH_REDIR_OUTPUT ENABLE_ASH_BASH_COMPAT
207 #define IF_BASH_REDIR_OUTPUT IF_ASH_BASH_COMPAT
209 #define BASH_DOLLAR_SQUOTE ENABLE_ASH_BASH_COMPAT
210 #define IF_BASH_DOLLAR_SQUOTE IF_ASH_BASH_COMPAT
211 #define BASH_PATTERN_SUBST ENABLE_ASH_BASH_COMPAT
212 #define IF_BASH_PATTERN_SUBST IF_ASH_BASH_COMPAT
213 #define BASH_SUBSTR ENABLE_ASH_BASH_COMPAT
214 #define IF_BASH_SUBSTR IF_ASH_BASH_COMPAT
215 /* BASH_TEST2: [[ EXPR ]]
216 * Status of [[ support:
217 * We replace && and || with -a and -o
219 * singleword+noglob expansion:
220 * v='a b'; [[ $v = 'a b' ]]; echo 0:$?
221 * [[ /bin/n* ]]; echo 0:$?
222 * -a/-o are not AND/OR ops! (they are just strings)
223 * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc)
224 * = is glob match operator, not equality operator: STR = GLOB
225 * (in GLOB, quoting is significant on char-by-char basis: a*cd"*")
227 * add =~ regex match operator: STR =~ REGEX
229 #define BASH_TEST2 (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST)
230 #define BASH_SOURCE ENABLE_ASH_BASH_COMPAT
231 #define BASH_PIPEFAIL ENABLE_ASH_BASH_COMPAT
232 #define BASH_HOSTNAME_VAR ENABLE_ASH_BASH_COMPAT
233 #define BASH_SHLVL_VAR ENABLE_ASH_BASH_COMPAT
234 #define BASH_XTRACEFD ENABLE_ASH_BASH_COMPAT
235 #define BASH_READ_D ENABLE_ASH_BASH_COMPAT
236 #define IF_BASH_READ_D IF_ASH_BASH_COMPAT
238 #if defined(__ANDROID_API__) && __ANDROID_API__ <= 24
239 /* Bionic at least up to version 24 has no glob() */
240 # undef ENABLE_ASH_INTERNAL_GLOB
241 # define ENABLE_ASH_INTERNAL_GLOB 1
244 #if !ENABLE_ASH_INTERNAL_GLOB && defined(__UCLIBC__)
245 # error uClibc glob() is buggy, use ASH_INTERNAL_GLOB.
246 # error The bug is: for "$PWD"/<pattern> ash will escape e.g. dashes in "$PWD"
247 # error with backslash, even ones which do not need to be: "/a-b" -> "/a\-b"
248 # error glob() should unbackslash them and match. uClibc does not unbackslash,
249 # error fails to match dirname, subsequently not expanding <pattern> in it.
251 // if (glob("/etc/polkit\\-1", 0, NULL, &pglob)) - this returns 0 on uclibc, no bug
252 // if (glob("/etc/polkit\\-1/*", 0, NULL, &pglob)) printf("uclibc bug!\n");
255 #if !ENABLE_ASH_INTERNAL_GLOB
260 #include "shell_common.h"
261 #if ENABLE_FEATURE_SH_MATH
264 typedef long arith_t;
265 # define ARITH_FMT "%ld"
267 #if ENABLE_ASH_RANDOM_SUPPORT
270 # define CLEAR_RANDOM_T(rnd) ((void)0)
273 #include "NUM_APPLETS.h"
275 /* STANDALONE does not make sense, and won't compile */
276 # undef CONFIG_FEATURE_SH_STANDALONE
277 # undef ENABLE_FEATURE_SH_STANDALONE
278 # undef IF_FEATURE_SH_STANDALONE
279 # undef IF_NOT_FEATURE_SH_STANDALONE
280 # define ENABLE_FEATURE_SH_STANDALONE 0
281 # define IF_FEATURE_SH_STANDALONE(...)
282 # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
285 #ifndef F_DUPFD_CLOEXEC
286 # define F_DUPFD_CLOEXEC F_DUPFD
292 # define PIPE_BUF 4096 /* amount of buffering in a pipe */
296 # error "Do not even bother, ash will not run on NOMMU machine"
299 /* We use a trick to have more optimized code (fewer pointer reloads):
300 * ash.c: extern struct globals *const ash_ptr_to_globals;
301 * ash_ptr_hack.c: struct globals *ash_ptr_to_globals;
302 * This way, compiler in ash.c knows the pointer can not change.
304 * However, this may break on weird arches or toolchains. In this case,
305 * set "-DBB_GLOBAL_CONST=''" in CONFIG_EXTRA_CFLAGS to disable
308 #ifndef BB_GLOBAL_CONST
309 # define BB_GLOBAL_CONST const
313 /* ============ Hash table sizes. Configurable. */
317 #define CMDTABLESIZE 31 /* should be prime */
320 /* ============ Shell options */
322 static const char *const optletters_optnames[] = {
346 #define optletters(n) optletters_optnames[n][0]
347 #define optnames(n) (optletters_optnames[n] + 1)
349 enum { NOPTS = ARRAY_SIZE(optletters_optnames) };
352 /* ============ Misc data */
354 #define msg_illnum "Illegal number: %s"
357 * We enclose jmp_buf in a structure so that we can declare pointers to
358 * jump locations. The global variable handler contains the location to
359 * jump to when an exception occurs, and the global variable exception_type
360 * contains a code identifying the exception. To implement nested
361 * exception handlers, the user should save the value of handler on entry
362 * to an inner scope, set handler to point to a jmploc structure for the
363 * inner scope, and restore handler on exit from the scope.
369 struct globals_misc {
370 uint8_t exitstatus; /* exit status of last command */
371 uint8_t back_exitstatus;/* exit status of backquoted command */
372 smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */
373 int rootpid; /* pid of main shell */
374 /* shell level: 0 for the main shell, 1 for its children, and so on */
376 #define rootshell (!shlvl)
379 char *minusc; /* argument to -c option */
381 char *curdir; // = nullstr; /* current working directory */
382 char *physdir; // = nullstr; /* physical working directory */
384 char *arg0; /* value of $0 */
386 struct jmploc *exception_handler;
388 volatile int suppress_int; /* counter */
389 volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */
390 volatile /*sig_atomic_t*/ smallint got_sigchld; /* 1 = got SIGCHLD */
391 volatile /*sig_atomic_t*/ smallint pending_sig; /* last pending signal */
392 smallint exception_type; /* kind of exception (0..5) */
394 #define EXINT 0 /* SIGINT received */
395 #define EXERROR 1 /* a generic error */
396 #define EXEXIT 4 /* exit the shell */
398 char nullstr[1]; /* zero length string */
401 #define eflag optlist[0]
402 #define fflag optlist[1]
403 #define Iflag optlist[2]
404 #define iflag optlist[3]
405 #define mflag optlist[4]
406 #define nflag optlist[5]
407 #define sflag optlist[6]
408 #define xflag optlist[7]
409 #define vflag optlist[8]
410 #define Cflag optlist[9]
411 #define aflag optlist[10]
412 #define bflag optlist[11]
413 #define uflag optlist[12]
414 #define viflag optlist[13]
416 # define pipefail optlist[14]
421 # define nolog optlist[14 + BASH_PIPEFAIL]
422 # define debug optlist[15 + BASH_PIPEFAIL]
425 /* trap handler commands */
427 * Sigmode records the current value of the signal handlers for the various
428 * modes. A value of zero means that the current handler is not known.
429 * S_HARD_IGN indicates that the signal was ignored on entry to the shell.
431 char sigmode[NSIG - 1];
432 #define S_DFL 1 /* default signal handling (SIG_DFL) */
433 #define S_CATCH 2 /* signal is caught */
434 #define S_IGN 3 /* signal is ignored (SIG_IGN) */
435 #define S_HARD_IGN 4 /* signal is ignored permanently (it was SIG_IGN on entry to shell) */
437 /* indicates specified signal received */
438 uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */
439 uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */
441 char **trap_ptr; /* used only by "trap hack" */
443 /* Rarely referenced stuff */
444 #if ENABLE_ASH_RANDOM_SUPPORT
447 pid_t backgndpid; /* pid of last background process */
449 extern struct globals_misc *BB_GLOBAL_CONST ash_ptr_to_globals_misc;
450 #define G_misc (*ash_ptr_to_globals_misc)
451 #define exitstatus (G_misc.exitstatus )
452 #define back_exitstatus (G_misc.back_exitstatus )
453 #define job_warning (G_misc.job_warning)
454 #define rootpid (G_misc.rootpid )
455 #define shlvl (G_misc.shlvl )
456 #define errlinno (G_misc.errlinno )
457 #define minusc (G_misc.minusc )
458 #define curdir (G_misc.curdir )
459 #define physdir (G_misc.physdir )
460 #define arg0 (G_misc.arg0 )
461 #define exception_handler (G_misc.exception_handler)
462 #define exception_type (G_misc.exception_type )
463 #define suppress_int (G_misc.suppress_int )
464 #define pending_int (G_misc.pending_int )
465 #define got_sigchld (G_misc.got_sigchld )
466 #define pending_sig (G_misc.pending_sig )
467 #define nullstr (G_misc.nullstr )
468 #define optlist (G_misc.optlist )
469 #define sigmode (G_misc.sigmode )
470 #define gotsig (G_misc.gotsig )
471 #define may_have_traps (G_misc.may_have_traps )
472 #define trap (G_misc.trap )
473 #define trap_ptr (G_misc.trap_ptr )
474 #define random_gen (G_misc.random_gen )
475 #define backgndpid (G_misc.backgndpid )
476 #define INIT_G_misc() do { \
477 (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \
485 /* ============ DEBUG */
487 static void trace_printf(const char *fmt, ...);
488 static void trace_vprintf(const char *fmt, va_list va);
489 # define TRACE(param) trace_printf param
490 # define TRACEV(param) trace_vprintf param
491 # define close(fd) do { \
493 if (close(dfd) < 0) \
494 bb_error_msg("bug on %d: closing %d(0x%x)", \
495 __LINE__, dfd, dfd); \
498 # define TRACE(param)
499 # define TRACEV(param)
503 /* ============ Utility functions */
504 #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
505 #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
508 isdigit_str9(const char *str)
510 int maxlen = 9 + 1; /* max 9 digits: 999999999 */
511 while (--maxlen && isdigit(*str))
513 return (*str == '\0');
517 var_end(const char *var)
526 /* ============ Interrupts / exceptions */
528 static void exitshell(void) NORETURN;
531 * These macros allow the user to suspend the handling of interrupt signals
532 * over a period of time. This is similar to SIGHOLD or to sigblock, but
533 * much more efficient and portable. (But hacking the kernel is so much
534 * more fun than worrying about efficiency and portability. :-))
537 # define INT_OFF do { \
538 TRACE(("%s:%d INT_OFF(%d)\n", __func__, __LINE__, suppress_int)); \
543 # define INT_OFF do { \
550 * Called to raise an exception. Since C doesn't include exceptions, we
551 * just do a longjmp to the exception handler. The type of exception is
552 * stored in the global variable "exception_type".
554 static void raise_exception(int) NORETURN;
556 raise_exception(int e)
559 if (exception_handler == NULL)
564 longjmp(exception_handler->loc, 1);
567 #define raise_exception(e) do { \
568 TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \
569 raise_exception(e); \
574 * Called when a SIGINT is received. (If the user specifies
575 * that SIGINT is to be trapped or ignored using the trap builtin, then
576 * this routine is not called.) Suppressint is nonzero when interrupts
577 * are held using the INT_OFF macro. (The test for iflag is just
578 * defensive programming.)
580 static void raise_interrupt(void) NORETURN;
582 raise_interrupt(void)
585 /* Signal is not automatically unmasked after it is raised,
586 * do it ourself - unmask all signals */
587 sigprocmask_allsigs(SIG_UNBLOCK);
588 /* pending_sig = 0; - now done in signal_handler() */
590 if (!(rootshell && iflag)) {
591 /* Kill ourself with SIGINT */
592 signal(SIGINT, SIG_DFL);
595 /* bash: ^C even on empty command line sets $? */
596 exitstatus = SIGINT + 128;
597 raise_exception(EXINT);
601 #define raise_interrupt() do { \
602 TRACE(("raising interrupt on line %d\n", __LINE__)); \
607 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
611 if (--suppress_int == 0 && pending_int) {
616 # define INT_ON do { \
617 TRACE(("%s:%d INT_ON(%d)\n", __func__, __LINE__, suppress_int-1)); \
621 # define INT_ON int_on()
623 static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void
631 #define FORCE_INT_ON force_int_on()
633 #define SAVE_INT(v) ((v) = suppress_int)
635 #define RESTORE_INT(v) do { \
637 suppress_int = (v); \
638 if (suppress_int == 0 && pending_int) \
643 /* ============ Stdout/stderr output */
646 outstr(const char *p, FILE *file)
654 flush_stdout_stderr(void)
661 /* Was called outcslow(c,FILE*), but c was always '\n' */
663 newline_and_flush(FILE *dest)
671 static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2)));
673 out1fmt(const char *fmt, ...)
680 r = vprintf(fmt, ap);
686 static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4)));
688 fmtstr(char *outbuf, size_t length, const char *fmt, ...)
695 ret = vsnprintf(outbuf, length, fmt, ap);
702 out1str(const char *p)
708 out2str(const char *p)
711 flush_stdout_stderr();
715 /* ============ Parser structures */
717 /* control characters in argument strings */
718 #define CTL_FIRST CTLESC
719 #define CTLESC ((unsigned char)'\201') /* escape next character */
720 #define CTLVAR ((unsigned char)'\202') /* variable defn */
721 #define CTLENDVAR ((unsigned char)'\203')
722 #define CTLBACKQ ((unsigned char)'\204')
723 #define CTLARI ((unsigned char)'\206') /* arithmetic expression */
724 #define CTLENDARI ((unsigned char)'\207')
725 #define CTLQUOTEMARK ((unsigned char)'\210')
726 #define CTL_LAST CTLQUOTEMARK
728 /* variable substitution byte (follows CTLVAR) */
729 #define VSTYPE 0x0f /* type of variable substitution */
730 #define VSNUL 0x10 /* colon--treat the empty string as unset */
732 /* values of VSTYPE field */
733 #define VSNORMAL 0x1 /* normal variable: $var or ${var} */
734 #define VSMINUS 0x2 /* ${var-text} */
735 #define VSPLUS 0x3 /* ${var+text} */
736 #define VSQUESTION 0x4 /* ${var?message} */
737 #define VSASSIGN 0x5 /* ${var=text} */
738 #define VSTRIMRIGHT 0x6 /* ${var%pattern} */
739 #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */
740 #define VSTRIMLEFT 0x8 /* ${var#pattern} */
741 #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */
742 #define VSLENGTH 0xa /* ${#var} */
744 #define VSSUBSTR 0xc /* ${var:position:length} */
746 #if BASH_PATTERN_SUBST
747 #define VSREPLACE 0xd /* ${var/pattern/replacement} */
748 #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */
751 static const char dolatstr[] ALIGN1 = {
752 CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0'
754 #define DOLATSTRLEN 6
773 #if BASH_REDIR_OUTPUT
790 smallint type; /* Nxxxx */
794 union node *redirect;
799 smallint pipe_backgnd;
800 struct nodelist *cmdlist;
807 union node *redirect;
820 union node *elsepart;
856 struct nodelist *backquote;
859 /* nfile and ndup layout must match!
860 * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight
861 * that it is actually NTO2 (>&file), and change its type.
878 char *_unused_expfname;
897 struct nredir nredir;
898 struct nbinary nbinary;
902 struct nclist nclist;
903 struct ndefun ndefun;
912 * NODE_EOF is returned by parsecmd when it encounters an end of file.
913 * It must be distinct from NULL.
915 #define NODE_EOF ((union node *) -1L)
918 struct nodelist *next;
931 freefunc(struct funcnode *f)
933 if (f && --f->count < 0)
938 /* ============ Debugging output */
942 static FILE *tracefile;
945 trace_printf(const char *fmt, ...)
952 fprintf(tracefile, "%u ", (int) time(NULL));
954 fprintf(tracefile, "[%u] ", (int) getpid());
956 fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int);
958 vfprintf(tracefile, fmt, va);
963 trace_vprintf(const char *fmt, va_list va)
967 vfprintf(tracefile, fmt, va);
968 fprintf(tracefile, "\n");
972 trace_puts(const char *s)
980 trace_puts_quoted(char *s)
987 putc('"', tracefile);
988 for (p = s; *p; p++) {
989 switch ((unsigned char)*p) {
990 case '\n': c = 'n'; goto backslash;
991 case '\t': c = 't'; goto backslash;
992 case '\r': c = 'r'; goto backslash;
993 case '\"': c = '\"'; goto backslash;
994 case '\\': c = '\\'; goto backslash;
995 case CTLESC: c = 'e'; goto backslash;
996 case CTLVAR: c = 'v'; goto backslash;
997 case CTLBACKQ: c = 'q'; goto backslash;
999 putc('\\', tracefile);
1003 if (*p >= ' ' && *p <= '~')
1004 putc(*p, tracefile);
1006 putc('\\', tracefile);
1007 putc((*p >> 6) & 03, tracefile);
1008 putc((*p >> 3) & 07, tracefile);
1009 putc(*p & 07, tracefile);
1014 putc('"', tracefile);
1018 trace_puts_args(char **ap)
1025 trace_puts_quoted(*ap);
1027 putc('\n', tracefile);
1030 putc(' ', tracefile);
1045 /* leave open because libedit might be using it */
1048 strcpy(s, "./trace");
1050 if (!freopen(s, "a", tracefile)) {
1051 fprintf(stderr, "Can't re-open %s\n", s);
1056 tracefile = fopen(s, "a");
1057 if (tracefile == NULL) {
1058 fprintf(stderr, "Can't open %s\n", s);
1064 flags = fcntl(fileno(tracefile), F_GETFL);
1066 fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
1068 setlinebuf(tracefile);
1069 fputs("\nTracing started.\n", tracefile);
1073 indent(int amount, char *pfx, FILE *fp)
1077 for (i = 0; i < amount; i++) {
1078 if (pfx && i == amount - 1)
1084 /* little circular references here... */
1085 static void shtree(union node *n, int ind, char *pfx, FILE *fp);
1088 sharg(union node *arg, FILE *fp)
1091 struct nodelist *bqlist;
1092 unsigned char subtype;
1094 if (arg->type != NARG) {
1095 out1fmt("<node type %d>\n", arg->type);
1098 bqlist = arg->narg.backquote;
1099 for (p = arg->narg.text; *p; p++) {
1100 switch ((unsigned char)*p) {
1109 if (subtype == VSLENGTH)
1117 if (subtype & VSNUL)
1120 switch (subtype & VSTYPE) {
1146 case VSTRIMRIGHTMAX:
1153 out1fmt("<subtype %d>", subtype);
1162 shtree(bqlist->n, -1, NULL, fp);
1173 shcmd(union node *cmd, FILE *fp)
1181 for (np = cmd->ncmd.args; np; np = np->narg.next) {
1187 for (np = cmd->ncmd.redirect; np; np = np->nfile.next) {
1191 switch (np->nfile.type) {
1192 case NTO: s = ">>"+1; dftfd = 1; break;
1193 case NCLOBBER: s = ">|"; dftfd = 1; break;
1194 case NAPPEND: s = ">>"; dftfd = 1; break;
1195 #if BASH_REDIR_OUTPUT
1198 case NTOFD: s = ">&"; dftfd = 1; break;
1199 case NFROM: s = "<"; break;
1200 case NFROMFD: s = "<&"; break;
1201 case NFROMTO: s = "<>"; break;
1202 default: s = "*error*"; break;
1204 if (np->nfile.fd != dftfd)
1205 fprintf(fp, "%d", np->nfile.fd);
1207 if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1208 fprintf(fp, "%d", np->ndup.dupfd);
1210 sharg(np->nfile.fname, fp);
1217 shtree(union node *n, int ind, char *pfx, FILE *fp)
1219 struct nodelist *lp;
1225 indent(ind, pfx, fp);
1227 if (n == NODE_EOF) {
1242 shtree(n->nbinary.ch1, ind, NULL, fp);
1245 shtree(n->nbinary.ch2, ind, NULL, fp);
1253 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
1254 shtree(lp->n, 0, NULL, fp);
1258 if (n->npipe.pipe_backgnd)
1264 fprintf(fp, "<node type %d>", n->type);
1272 showtree(union node *n)
1274 trace_puts("showtree called\n");
1275 shtree(n, 1, NULL, stderr);
1281 /* ============ Parser data */
1284 * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up.
1287 struct strlist *next;
1294 struct strpush *prev; /* preceding string on stack */
1296 int prev_left_in_line;
1297 #if ENABLE_ASH_ALIAS
1298 struct alias *ap; /* if push was associated with an alias */
1300 char *string; /* remember the string since it may change */
1302 /* Remember last two characters for pungetc. */
1305 /* Number of outstanding calls to pungetc. */
1310 * The parsefile structure pointed to by the global variable parsefile
1311 * contains information about the current file being read.
1314 struct parsefile *prev; /* preceding file on stack */
1315 int linno; /* current line */
1316 int pf_fd; /* file descriptor (or -1 if string) */
1317 int left_in_line; /* number of chars left in this line */
1318 int left_in_buffer; /* number of chars left in this buffer past the line */
1319 char *next_to_pgetc; /* next char in buffer */
1320 char *buf; /* input buffer */
1321 struct strpush *strpush; /* for pushing strings at this level */
1322 struct strpush basestrpush; /* so pushing one is fast */
1324 /* Remember last two characters for pungetc. */
1327 /* Number of outstanding calls to pungetc. */
1331 static struct parsefile basepf; /* top level input file */
1332 static struct parsefile *g_parsefile = &basepf; /* current input file */
1333 static char *commandname; /* currently executing command */
1336 /* ============ Message printing */
1339 ash_vmsg(const char *msg, va_list ap)
1341 fprintf(stderr, "%s: ", arg0);
1343 if (strcmp(arg0, commandname))
1344 fprintf(stderr, "%s: ", commandname);
1345 if (!iflag || g_parsefile->pf_fd > 0)
1346 fprintf(stderr, "line %d: ", errlinno);
1348 vfprintf(stderr, msg, ap);
1349 newline_and_flush(stderr);
1353 * Exverror is called to raise the error exception. If the second argument
1354 * is not NULL then error prints an error message using printf style
1355 * formatting. It then raises the error exception.
1357 static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN;
1359 ash_vmsg_and_raise(int cond, const char *msg, va_list ap)
1363 TRACE(("ash_vmsg_and_raise(%d):", cond));
1366 TRACE(("ash_vmsg_and_raise(%d):NULL\n", cond));
1371 flush_stdout_stderr();
1372 raise_exception(cond);
1376 static void ash_msg_and_raise_error(const char *, ...) NORETURN;
1378 ash_msg_and_raise_error(const char *msg, ...)
1385 ash_vmsg_and_raise(EXERROR, msg, ap);
1391 * 'fmt' must be a string literal.
1393 #define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": "STRERROR_FMT, ##__VA_ARGS__ STRERROR_ERRNO)
1395 static void raise_error_syntax(const char *) NORETURN;
1397 raise_error_syntax(const char *msg)
1399 errlinno = g_parsefile->linno;
1400 ash_msg_and_raise_error("syntax error: %s", msg);
1404 static void ash_msg_and_raise(int, const char *, ...) NORETURN;
1406 ash_msg_and_raise(int cond, const char *msg, ...)
1411 ash_vmsg_and_raise(cond, msg, ap);
1417 * error/warning routines for external builtins
1420 ash_msg(const char *fmt, ...)
1430 * Return a string describing an error. The returned string may be a
1431 * pointer to a static buffer that will be overwritten on the next call.
1432 * Action describes the operation that got the error.
1435 errmsg(int e, const char *em)
1437 if (e == ENOENT || e == ENOTDIR) {
1444 /* ============ Memory allocation */
1447 /* I consider these wrappers nearly useless:
1448 * ok, they return you to nearest exception handler, but
1449 * how much memory do you leak in the process, making
1450 * memory starvation worse?
1453 ckrealloc(void * p, size_t nbytes)
1455 p = realloc(p, nbytes);
1457 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1462 ckmalloc(size_t nbytes)
1464 return ckrealloc(NULL, nbytes);
1468 ckzalloc(size_t nbytes)
1470 return memset(ckmalloc(nbytes), 0, nbytes);
1474 ckstrdup(const char *s)
1476 char *p = strdup(s);
1478 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1482 /* Using bbox equivalents. They exit if out of memory */
1483 # define ckrealloc xrealloc
1484 # define ckmalloc xmalloc
1485 # define ckzalloc xzalloc
1486 # define ckstrdup xstrdup
1490 * It appears that grabstackstr() will barf with such alignments
1491 * because stalloc() will return a string allocated in a new stackblock.
1493 #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE)
1495 /* Most machines require the value returned from malloc to be aligned
1496 * in some way. The following macro will get this right
1497 * on many machines. */
1498 SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1,
1499 /* Minimum size of a block */
1500 MINSIZE = SHELL_ALIGN(504),
1503 struct stack_block {
1504 struct stack_block *prev;
1505 char space[MINSIZE];
1509 struct stack_block *stackp;
1515 struct globals_memstack {
1516 struct stack_block *g_stackp; // = &stackbase;
1517 char *g_stacknxt; // = stackbase.space;
1518 char *sstrend; // = stackbase.space + MINSIZE;
1519 size_t g_stacknleft; // = MINSIZE;
1520 struct stack_block stackbase;
1522 extern struct globals_memstack *BB_GLOBAL_CONST ash_ptr_to_globals_memstack;
1523 #define G_memstack (*ash_ptr_to_globals_memstack)
1524 #define g_stackp (G_memstack.g_stackp )
1525 #define g_stacknxt (G_memstack.g_stacknxt )
1526 #define sstrend (G_memstack.sstrend )
1527 #define g_stacknleft (G_memstack.g_stacknleft)
1528 #define stackbase (G_memstack.stackbase )
1529 #define INIT_G_memstack() do { \
1530 (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \
1532 g_stackp = &stackbase; \
1533 g_stacknxt = stackbase.space; \
1534 g_stacknleft = MINSIZE; \
1535 sstrend = stackbase.space + MINSIZE; \
1539 #define stackblock() ((void *)g_stacknxt)
1540 #define stackblocksize() g_stacknleft
1543 * Parse trees for commands are allocated in lifo order, so we use a stack
1544 * to make this more efficient, and also to avoid all sorts of exception
1545 * handling code to handle interrupts in the middle of a parse.
1547 * The size 504 was chosen because the Ultrix malloc handles that size
1551 stalloc(size_t nbytes)
1556 aligned = SHELL_ALIGN(nbytes);
1557 if (aligned > g_stacknleft) {
1560 struct stack_block *sp;
1562 blocksize = aligned;
1563 if (blocksize < MINSIZE)
1564 blocksize = MINSIZE;
1565 len = sizeof(struct stack_block) - MINSIZE + blocksize;
1566 if (len < blocksize)
1567 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1570 sp->prev = g_stackp;
1571 g_stacknxt = sp->space;
1572 g_stacknleft = blocksize;
1573 sstrend = g_stacknxt + blocksize;
1578 g_stacknxt += aligned;
1579 g_stacknleft -= aligned;
1584 stzalloc(size_t nbytes)
1586 return memset(stalloc(nbytes), 0, nbytes);
1593 if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) {
1594 write(STDERR_FILENO, "stunalloc\n", 10);
1598 g_stacknleft += g_stacknxt - (char *)p;
1603 * Like strdup but works with the ash stack.
1606 sstrdup(const char *p)
1608 size_t len = strlen(p) + 1;
1609 return memcpy(stalloc(len), p, len);
1612 static ALWAYS_INLINE void
1613 grabstackblock(size_t len)
1619 pushstackmark(struct stackmark *mark, size_t len)
1621 mark->stackp = g_stackp;
1622 mark->stacknxt = g_stacknxt;
1623 mark->stacknleft = g_stacknleft;
1624 grabstackblock(len);
1628 setstackmark(struct stackmark *mark)
1630 pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase);
1634 popstackmark(struct stackmark *mark)
1636 struct stack_block *sp;
1642 while (g_stackp != mark->stackp) {
1644 g_stackp = sp->prev;
1647 g_stacknxt = mark->stacknxt;
1648 g_stacknleft = mark->stacknleft;
1649 sstrend = mark->stacknxt + mark->stacknleft;
1654 * When the parser reads in a string, it wants to stick the string on the
1655 * stack and only adjust the stack pointer when it knows how big the
1656 * string is. Stackblock (defined in stack.h) returns a pointer to a block
1657 * of space on top of the stack and stackblocklen returns the length of
1658 * this block. Growstackblock will grow this space by at least one byte,
1659 * possibly moving it (like realloc). Grabstackblock actually allocates the
1660 * part of the block that has been used.
1663 growstackblock(void)
1667 newlen = g_stacknleft * 2;
1668 if (newlen < g_stacknleft)
1669 ash_msg_and_raise_error(bb_msg_memory_exhausted);
1673 if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) {
1674 struct stack_block *sp;
1675 struct stack_block *prevstackp;
1680 prevstackp = sp->prev;
1681 grosslen = newlen + sizeof(struct stack_block) - MINSIZE;
1682 sp = ckrealloc(sp, grosslen);
1683 sp->prev = prevstackp;
1685 g_stacknxt = sp->space;
1686 g_stacknleft = newlen;
1687 sstrend = sp->space + newlen;
1690 char *oldspace = g_stacknxt;
1691 size_t oldlen = g_stacknleft;
1692 char *p = stalloc(newlen);
1694 /* free the space we just allocated */
1695 g_stacknxt = memcpy(p, oldspace, oldlen);
1696 g_stacknleft += newlen;
1701 * The following routines are somewhat easier to use than the above.
1702 * The user declares a variable of type STACKSTR, which may be declared
1703 * to be a register. The macro STARTSTACKSTR initializes things. Then
1704 * the user uses the macro STPUTC to add characters to the string. In
1705 * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is
1706 * grown as necessary. When the user is done, she can just leave the
1707 * string there and refer to it using stackblock(). Or she can allocate
1708 * the space for it using grabstackstr(). If it is necessary to allow
1709 * someone else to use the stack temporarily and then continue to grow
1710 * the string, the user should use grabstack to allocate the space, and
1711 * then call ungrabstr(p) to return to the previous mode of operation.
1713 * USTPUTC is like STPUTC except that it doesn't check for overflow.
1714 * CHECKSTACKSPACE can be called before USTPUTC to ensure that there
1715 * is space for at least one character.
1720 size_t len = stackblocksize();
1722 return (char *)stackblock() + len;
1726 * Called from CHECKSTRSPACE.
1729 makestrspace(size_t newlen, char *p)
1731 size_t len = p - g_stacknxt;
1737 size = stackblocksize();
1739 if (nleft >= newlen)
1743 return (char *)stackblock() + len;
1747 stack_nputstr(const char *s, size_t n, char *p)
1749 p = makestrspace(n, p);
1750 p = (char *)mempcpy(p, s, n);
1755 stack_putstr(const char *s, char *p)
1757 return stack_nputstr(s, strlen(s), p);
1761 _STPUTC(int c, char *p)
1769 #define STARTSTACKSTR(p) ((p) = stackblock())
1770 #define STPUTC(c, p) ((p) = _STPUTC((c), (p)))
1771 #define CHECKSTRSPACE(n, p) do { \
1774 size_t m = sstrend - q; \
1776 (p) = makestrspace(l, q); \
1778 #define USTPUTC(c, p) (*(p)++ = (c))
1779 #define STACKSTRNUL(p) do { \
1780 if ((p) == sstrend) \
1781 (p) = growstackstr(); \
1784 #define STUNPUTC(p) (--(p))
1785 #define STTOPC(p) ((p)[-1])
1786 #define STADJUST(amount, p) ((p) += (amount))
1788 #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock())
1789 #define ungrabstackstr(s, p) stunalloc(s)
1790 #define stackstrend() ((void *)sstrend)
1793 /* ============ String helpers */
1796 * prefix -- see if pfx is a prefix of string.
1799 prefix(const char *string, const char *pfx)
1802 if (*pfx++ != *string++)
1805 return (char *) string;
1809 * Check for a valid number. This should be elsewhere.
1812 is_number(const char *p)
1817 } while (*++p != '\0');
1822 * Convert a string of digits to an integer, printing an error message on
1826 number(const char *s)
1829 ash_msg_and_raise_error(msg_illnum, s);
1834 * Produce a single quoted string suitable as input to the shell.
1835 * The return string is allocated on the stack.
1838 single_quote(const char *s)
1848 len = strchrnul(s, '\'') - s;
1850 q = p = makestrspace(len + 3, p);
1853 q = (char *)mempcpy(q, s, len);
1862 do len++; while (*++s == '\'');
1864 q = p = makestrspace(len + 3, p);
1867 q = (char *)mempcpy(q, s - len, len);
1875 return stackblock();
1879 * Produce a possibly single quoted string suitable as input to the shell.
1880 * If quoting was done, the return string is allocated on the stack,
1881 * otherwise a pointer to the original string is returned.
1884 maybe_single_quote(const char *s)
1889 /* Assuming ACSII */
1890 /* quote ctrl_chars space !"#$%&'()* */
1894 if (*p >= ';' && *p <= '?')
1903 /* quote {|}~ DEL and high bytes */
1906 /* Not quoting these: +,-./ 0-9 :@ A-Z ]^_ a-z */
1907 /* TODO: maybe avoid quoting % */
1913 return single_quote(s);
1917 /* ============ nextopt */
1919 static char **argptr; /* argument list for builtin commands */
1920 static char *optionarg; /* set by nextopt (like getopt) */
1921 static char *optptr; /* used by nextopt */
1924 * XXX - should get rid of. Have all builtins use getopt(3).
1925 * The library getopt must have the BSD extension static variable
1926 * "optreset", otherwise it can't be used within the shell safely.
1928 * Standard option processing (a la getopt) for builtin routines.
1929 * The only argument that is passed to nextopt is the option string;
1930 * the other arguments are unnecessary. It returns the character,
1931 * or '\0' on end of input.
1934 nextopt(const char *optstring)
1941 if (p == NULL || *p == '\0') {
1942 /* We ate entire "-param", take next one */
1948 if (*++p == '\0') /* just "-" ? */
1951 if (LONE_DASH(p)) /* "--" ? */
1953 /* p => next "-param" */
1955 /* p => some option char in the middle of a "-param" */
1957 for (q = optstring; *q != c;) {
1959 ash_msg_and_raise_error("illegal option -%c", c);
1967 ash_msg_and_raise_error("no arg for -%c option", c);
1977 /* ============ Shell variables */
1980 int nparam; /* # of positional parameters (without $0) */
1981 #if ENABLE_ASH_GETOPTS
1982 int optind; /* next parameter to be processed by getopts */
1983 int optoff; /* used by getopts */
1985 unsigned char malloced; /* if parameter list dynamically allocated */
1986 char **p; /* parameter list */
1990 * Free the list of positional parameters.
1993 freeparam(volatile struct shparam *param)
1995 if (param->malloced) {
1997 ap = ap1 = param->p;
2004 #if ENABLE_ASH_GETOPTS
2005 static void FAST_FUNC getoptsreset(const char *value);
2009 struct var *next; /* next entry in hash list */
2010 int flags; /* flags are defined above */
2011 const char *var_text; /* name=value */
2012 void (*var_func)(const char *) FAST_FUNC; /* function to be called when */
2013 /* the variable gets set/unset */
2017 struct localvar *next; /* next local variable in list */
2018 struct var *vp; /* the variable that was made local */
2019 int flags; /* saved flags */
2020 const char *text; /* saved text */
2024 #define VEXPORT 0x01 /* variable is exported */
2025 #define VREADONLY 0x02 /* variable cannot be modified */
2026 #define VSTRFIXED 0x04 /* variable struct is statically allocated */
2027 #define VTEXTFIXED 0x08 /* text is statically allocated */
2028 #define VSTACK 0x10 /* text is allocated on the stack */
2029 #define VUNSET 0x20 /* the variable is not set */
2030 #define VNOFUNC 0x40 /* don't call the callback function */
2031 #define VNOSET 0x80 /* do not set variable - just readonly test */
2032 #define VNOSAVE 0x100 /* when text is on the heap before setvareq */
2033 #if ENABLE_ASH_RANDOM_SUPPORT
2034 # define VDYNAMIC 0x200 /* dynamic variable */
2040 /* Need to be before varinit_data[] */
2041 #if ENABLE_LOCALE_SUPPORT
2042 static void FAST_FUNC
2043 change_lc_all(const char *value)
2045 if (value && *value != '\0')
2046 setlocale(LC_ALL, value);
2048 static void FAST_FUNC
2049 change_lc_ctype(const char *value)
2051 if (value && *value != '\0')
2052 setlocale(LC_CTYPE, value);
2056 static void chkmail(void);
2057 static void changemail(const char *var_value) FAST_FUNC;
2059 # define chkmail() ((void)0)
2061 static void changepath(const char *) FAST_FUNC;
2062 #if ENABLE_ASH_RANDOM_SUPPORT
2063 static void change_random(const char *) FAST_FUNC;
2066 static const struct {
2068 const char *var_text;
2069 void (*var_func)(const char *) FAST_FUNC;
2070 } varinit_data[] = {
2072 * Note: VEXPORT would not work correctly here for NOFORK applets:
2073 * some environment strings may be constant.
2075 { VSTRFIXED|VTEXTFIXED , defifsvar , NULL },
2077 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail },
2078 { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail },
2080 { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath },
2081 { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL },
2082 { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL },
2083 { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL },
2084 #if ENABLE_ASH_GETOPTS
2085 { VSTRFIXED|VTEXTFIXED , defoptindvar, getoptsreset },
2087 { VSTRFIXED|VTEXTFIXED , NULL /* inited to linenovar */, NULL },
2088 #if ENABLE_ASH_RANDOM_SUPPORT
2089 { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random },
2091 #if ENABLE_LOCALE_SUPPORT
2092 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all },
2093 { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype },
2095 #if ENABLE_FEATURE_EDITING_SAVEHISTORY
2096 { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL },
2102 struct globals_var {
2103 struct shparam shellparam; /* $@ current positional parameters */
2104 struct redirtab *redirlist;
2105 int preverrout_fd; /* stderr fd: usually 2, unless redirect moved it */
2106 struct var *vartab[VTABSIZE];
2107 struct var varinit[ARRAY_SIZE(varinit_data)];
2109 char linenovar[sizeof("LINENO=") + sizeof(int)*3];
2111 extern struct globals_var *BB_GLOBAL_CONST ash_ptr_to_globals_var;
2112 #define G_var (*ash_ptr_to_globals_var)
2113 #define shellparam (G_var.shellparam )
2114 //#define redirlist (G_var.redirlist )
2115 #define preverrout_fd (G_var.preverrout_fd)
2116 #define vartab (G_var.vartab )
2117 #define varinit (G_var.varinit )
2118 #define lineno (G_var.lineno )
2119 #define linenovar (G_var.linenovar )
2120 #define vifs varinit[0]
2122 # define vmail (&vifs)[1]
2123 # define vmpath (&vmail)[1]
2124 # define vpath (&vmpath)[1]
2126 # define vpath (&vifs)[1]
2128 #define vps1 (&vpath)[1]
2129 #define vps2 (&vps1)[1]
2130 #define vps4 (&vps2)[1]
2131 #if ENABLE_ASH_GETOPTS
2132 # define voptind (&vps4)[1]
2133 # define vlineno (&voptind)[1]
2134 # if ENABLE_ASH_RANDOM_SUPPORT
2135 # define vrandom (&vlineno)[1]
2138 # define vlineno (&vps4)[1]
2139 # if ENABLE_ASH_RANDOM_SUPPORT
2140 # define vrandom (&vlineno)[1]
2143 #define INIT_G_var() do { \
2145 (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \
2147 for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \
2148 varinit[i].flags = varinit_data[i].flags; \
2149 varinit[i].var_text = varinit_data[i].var_text; \
2150 varinit[i].var_func = varinit_data[i].var_func; \
2152 strcpy(linenovar, "LINENO="); \
2153 vlineno.var_text = linenovar; \
2157 * The following macros access the values of the above variables.
2158 * They have to skip over the name. They return the null string
2159 * for unset variables.
2161 #define ifsval() (vifs.var_text + 4)
2162 #define ifsset() ((vifs.flags & VUNSET) == 0)
2164 # define mailval() (vmail.var_text + 5)
2165 # define mpathval() (vmpath.var_text + 9)
2166 # define mpathset() ((vmpath.flags & VUNSET) == 0)
2168 #define pathval() (vpath.var_text + 5)
2169 #define ps1val() (vps1.var_text + 4)
2170 #define ps2val() (vps2.var_text + 4)
2171 #define ps4val() (vps4.var_text + 4)
2172 #if ENABLE_ASH_GETOPTS
2173 # define optindval() (voptind.var_text + 7)
2176 #if ENABLE_ASH_GETOPTS
2177 static void FAST_FUNC
2178 getoptsreset(const char *value)
2180 shellparam.optind = 1;
2181 if (is_number(value))
2182 shellparam.optind = number(value) ?: 1;
2183 shellparam.optoff = -1;
2188 * Compares two strings up to the first = or '\0'. The first
2189 * string must be terminated by '='; the second may be terminated by
2190 * either '=' or '\0'.
2193 varcmp(const char *p, const char *q)
2197 while ((c = *p) == (d = *q)) {
2198 if (c == '\0' || c == '=')
2212 * Find the appropriate entry in the hash table from the name.
2214 static struct var **
2215 hashvar(const char *p)
2219 hashval = ((unsigned char) *p) << 4;
2220 while (*p && *p != '=')
2221 hashval += (unsigned char) *p++;
2222 return &vartab[hashval % VTABSIZE];
2226 vpcmp(const void *a, const void *b)
2228 return varcmp(*(const char **)a, *(const char **)b);
2232 * This routine initializes the builtin variables.
2242 * PS1 depends on uid
2244 #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT
2245 vps1.var_text = "PS1=\\w \\$ ";
2248 vps1.var_text = "PS1=# ";
2251 end = vp + ARRAY_SIZE(varinit);
2253 vpp = hashvar(vp->var_text);
2256 } while (++vp < end);
2259 static struct var **
2260 findvar(struct var **vpp, const char *name)
2262 for (; *vpp; vpp = &(*vpp)->next) {
2263 if (varcmp((*vpp)->var_text, name) == 0) {
2271 * Find the value of a variable. Returns NULL if not set.
2273 static const char* FAST_FUNC
2274 lookupvar(const char *name)
2278 v = *findvar(hashvar(name), name);
2280 #if ENABLE_ASH_RANDOM_SUPPORT
2282 * Dynamic variables are implemented roughly the same way they are
2283 * in bash. Namely, they're "special" so long as they aren't unset.
2284 * As soon as they're unset, they're no longer dynamic, and dynamic
2285 * lookup will no longer happen at that point. -- PFM.
2287 if (v->flags & VDYNAMIC)
2290 if (!(v->flags & VUNSET)) {
2291 if (v == &vlineno && v->var_text == linenovar) {
2292 fmtstr(linenovar+7, sizeof(linenovar)-7, "%d", lineno);
2294 return var_end(v->var_text);
2300 #if ENABLE_UNICODE_SUPPORT
2302 reinit_unicode_for_ash(void)
2304 /* Unicode support should be activated even if LANG is set
2305 * _during_ shell execution, not only if it was set when
2306 * shell was started. Therefore, re-check LANG every time:
2308 if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
2309 || ENABLE_UNICODE_USING_LOCALE
2311 const char *s = lookupvar("LC_ALL");
2312 if (!s) s = lookupvar("LC_CTYPE");
2313 if (!s) s = lookupvar("LANG");
2318 # define reinit_unicode_for_ash() ((void)0)
2322 * Search the environment of a builtin command.
2324 static ALWAYS_INLINE const char *
2325 bltinlookup(const char *name)
2327 return lookupvar(name);
2331 * Same as setvar except that the variable and value are passed in
2332 * the first argument as name=value. Since the first argument will
2333 * be actually stored in the table, it should not be a string that
2335 * Called with interrupts off.
2338 setvareq(char *s, int flags)
2340 struct var *vp, **vpp;
2343 flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1));
2344 vpp = findvar(vpp, s);
2347 if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) {
2350 if (flags & VNOSAVE)
2354 ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n);
2360 if (vp->var_func && !(flags & VNOFUNC))
2361 vp->var_func(var_end(s));
2363 if (!(vp->flags & (VTEXTFIXED|VSTACK)))
2364 free((char*)vp->var_text);
2366 if (((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) | (vp->flags & VSTRFIXED)) == VUNSET) {
2370 if ((flags & (VTEXTFIXED|VSTACK|VNOSAVE)) == VNOSAVE)
2375 flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET);
2377 /* variable s is not found */
2380 if ((flags & (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
2382 vp = ckzalloc(sizeof(*vp));
2384 /*vp->func = NULL; - ckzalloc did it */
2387 if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE)))
2397 * Set the value of a variable. The flags argument is ored with the
2398 * flags of the variable. If val is NULL, the variable is unset.
2401 setvar(const char *name, const char *val, int flags)
2410 q = endofname(name);
2411 p = strchrnul(q, '=');
2413 if (!namelen || p != q)
2414 ash_msg_and_raise_error("%.*s: bad variable name", namelen, name);
2419 vallen = strlen(val);
2423 nameeq = ckmalloc(namelen + vallen + 2);
2424 p = mempcpy(nameeq, name, namelen);
2427 p = mempcpy(p, val, vallen);
2430 vp = setvareq(nameeq, flags | VNOSAVE);
2436 static void FAST_FUNC
2437 setvar0(const char *name, const char *val)
2439 setvar(name, val, 0);
2443 * Unset the specified variable.
2446 unsetvar(const char *s)
2452 * Process a linked list of variable assignments.
2455 listsetvar(struct strlist *list_set_var, int flags)
2457 struct strlist *lp = list_set_var;
2463 setvareq(lp->text, flags);
2470 * Generate a list of variables satisfying the given conditions.
2472 #if !ENABLE_FEATURE_SH_NOFORK
2473 # define listvars(on, off, lp, end) listvars(on, off, end)
2476 listvars(int on, int off, struct strlist *lp, char ***end)
2487 for (vp = *vpp; vp; vp = vp->next) {
2488 if ((vp->flags & mask) == on) {
2489 #if ENABLE_FEATURE_SH_NOFORK
2490 /* If variable with the same name is both
2491 * exported and temporarily set for a command:
2494 * then "ZVAR=6" will be both in vartab and
2495 * lp lists. Do not pass it twice to printenv.
2497 struct strlist *lp1 = lp;
2499 if (strcmp(lp1->text, vp->var_text) == 0)
2504 if (ep == stackstrend())
2505 ep = growstackstr();
2506 *ep++ = (char*)vp->var_text;
2507 #if ENABLE_FEATURE_SH_NOFORK
2512 } while (++vpp < vartab + VTABSIZE);
2514 #if ENABLE_FEATURE_SH_NOFORK
2516 if (ep == stackstrend())
2517 ep = growstackstr();
2523 if (ep == stackstrend())
2524 ep = growstackstr();
2528 return grabstackstr(ep);
2532 /* ============ Path search helper
2534 * The variable path (passed by reference) should be set to the start
2535 * of the path before the first call; path_advance will update
2536 * this value as it proceeds. Successive calls to path_advance will return
2537 * the possible path expansions in sequence. If an option (indicated by
2538 * a percent sign) appears in the path entry then the global variable
2539 * pathopt will be set to point to it; otherwise pathopt will be set to
2542 static const char *pathopt; /* set by path_advance */
2545 path_advance(const char **path, const char *name)
2555 for (p = start; *p && *p != ':' && *p != '%'; p++)
2557 len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */
2558 while (stackblocksize() < len)
2562 q = mempcpy(q, start, p - start);
2569 while (*p && *p != ':')
2576 return stalloc(len);
2580 /* ============ Prompt */
2582 static smallint doprompt; /* if set, prompt the user */
2583 static smallint needprompt; /* true if interactive and at start of line */
2585 #if ENABLE_FEATURE_EDITING
2586 static line_input_t *line_input_state;
2587 static const char *cmdedit_prompt;
2589 putprompt(const char *s)
2591 if (ENABLE_ASH_EXPAND_PRMT) {
2592 free((char*)cmdedit_prompt);
2593 cmdedit_prompt = ckstrdup(s);
2600 putprompt(const char *s)
2606 /* expandstr() needs parsing machinery, so it is far away ahead... */
2607 static const char *expandstr(const char *ps, int syntax_type);
2608 /* Values for syntax param */
2609 #define BASESYNTAX 0 /* not in quotes */
2610 #define DQSYNTAX 1 /* in double quotes */
2611 #define SQSYNTAX 2 /* in single quotes */
2612 #define ARISYNTAX 3 /* in arithmetic */
2613 #if ENABLE_ASH_EXPAND_PRMT
2614 # define PSSYNTAX 4 /* prompt. never passed to SIT() */
2616 /* PSSYNTAX expansion is identical to DQSYNTAX, except keeping '\$' as '\$' */
2619 * called by editline -- any expansions to the prompt should be added here.
2622 setprompt_if(smallint do_set, int whichprompt)
2625 IF_ASH_EXPAND_PRMT(struct stackmark smark;)
2632 switch (whichprompt) {
2642 #if ENABLE_ASH_EXPAND_PRMT
2643 pushstackmark(&smark, stackblocksize());
2644 putprompt(expandstr(prompt, PSSYNTAX));
2645 popstackmark(&smark);
2652 /* ============ The cd and pwd commands */
2654 #define CD_PHYSICAL 1
2664 while ((i = nextopt("LP")) != '\0') {
2666 flags ^= CD_PHYSICAL;
2675 * Update curdir (the name of the current directory) in response to a
2679 updatepwd(const char *dir)
2686 cdcomppath = sstrdup(dir);
2689 if (curdir == nullstr)
2691 new = stack_putstr(curdir, new);
2693 new = makestrspace(strlen(dir) + 2, new);
2694 lim = (char *)stackblock() + 1;
2698 if (new > lim && *lim == '/')
2703 if (dir[1] == '/' && dir[2] != '/') {
2709 p = strtok(cdcomppath, "/");
2713 if (p[1] == '.' && p[2] == '\0') {
2725 new = stack_putstr(p, new);
2728 p = strtok(NULL, "/");
2733 return stackblock();
2737 * Find out what the current directory is. If we already know the current
2738 * directory, this routine returns immediately.
2743 char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */
2744 return dir ? dir : nullstr;
2748 setpwd(const char *val, int setold)
2752 oldcur = dir = curdir;
2755 setvar("OLDPWD", oldcur, VEXPORT);
2758 if (physdir != nullstr) {
2759 if (physdir != oldcur)
2763 if (oldcur == val || !val) {
2769 dir = ckstrdup(val);
2770 if (oldcur != dir && oldcur != nullstr) {
2775 setvar("PWD", dir, VEXPORT);
2778 static void hashcd(void);
2781 * Actually do the chdir. We also call hashcd to let other routines
2782 * know that the current directory has changed.
2785 docd(const char *dest, int flags)
2787 const char *dir = NULL;
2790 TRACE(("docd(\"%s\", %d) called\n", dest, flags));
2793 if (!(flags & CD_PHYSICAL)) {
2794 dir = updatepwd(dest);
2808 static int FAST_FUNC
2809 cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
2821 dest = bltinlookup("HOME");
2822 else if (LONE_DASH(dest)) {
2823 dest = bltinlookup("OLDPWD");
2845 path = bltinlookup("CDPATH");
2848 p = path_advance(&path, dest);
2849 if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
2853 if (!docd(p, flags))
2864 ash_msg_and_raise_perror("can't cd to %s", dest);
2867 if (flags & CD_PRINT)
2868 out1fmt("%s\n", curdir);
2872 static int FAST_FUNC
2873 pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
2876 const char *dir = curdir;
2880 if (physdir == nullstr)
2884 out1fmt("%s\n", dir);
2889 /* ============ ... */
2892 #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024)
2894 /* Syntax classes */
2895 #define CWORD 0 /* character is nothing special */
2896 #define CNL 1 /* newline character */
2897 #define CBACK 2 /* a backslash character */
2898 #define CSQUOTE 3 /* single quote */
2899 #define CDQUOTE 4 /* double quote */
2900 #define CENDQUOTE 5 /* a terminating quote */
2901 #define CBQUOTE 6 /* backwards single quote */
2902 #define CVAR 7 /* a dollar sign */
2903 #define CENDVAR 8 /* a '}' character */
2904 #define CLP 9 /* a left paren in arithmetic */
2905 #define CRP 10 /* a right paren in arithmetic */
2906 #define CENDFILE 11 /* end of file */
2907 #define CCTL 12 /* like CWORD, except it must be escaped */
2908 #define CSPCL 13 /* these terminate a word */
2909 #define CIGN 14 /* character should be ignored */
2912 #if ENABLE_ASH_ALIAS
2916 #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE
2918 #if ENABLE_FEATURE_SH_MATH
2919 # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12))
2921 # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8))
2923 static const uint16_t S_I_T[] ALIGN2 = {
2924 #if ENABLE_ASH_ALIAS
2925 SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */
2927 SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */
2928 SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */
2929 SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */
2930 SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */
2931 SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */
2932 SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */
2933 SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */
2934 SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */
2935 SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */
2936 SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */
2937 SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */
2938 #if !USE_SIT_FUNCTION
2939 SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */
2940 SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */
2941 SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */
2945 /* Constants below must match table above */
2947 #if ENABLE_ASH_ALIAS
2948 CSPCL_CIGN_CIGN_CIGN , /* 0 */
2950 CSPCL_CWORD_CWORD_CWORD , /* 1 */
2951 CNL_CNL_CNL_CNL , /* 2 */
2952 CWORD_CCTL_CCTL_CWORD , /* 3 */
2953 CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */
2954 CVAR_CVAR_CWORD_CVAR , /* 5 */
2955 CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */
2956 CSPCL_CWORD_CWORD_CLP , /* 7 */
2957 CSPCL_CWORD_CWORD_CRP , /* 8 */
2958 CBACK_CBACK_CCTL_CBACK , /* 9 */
2959 CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */
2960 CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */
2961 CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */
2962 CWORD_CWORD_CWORD_CWORD , /* 13 */
2963 CCTL_CCTL_CCTL_CCTL , /* 14 */
2966 /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF,
2967 * caller must ensure proper cast on it if c is *char_ptr!
2969 #if USE_SIT_FUNCTION
2972 SIT(int c, int syntax)
2974 /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */
2975 static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~";
2977 * This causes '/' to be prepended with CTLESC in dquoted string,
2978 * making "./file"* treated incorrectly because we feed
2979 * ".\/file*" string to glob(), confusing it (see expandmeta func).
2980 * The "homegrown" glob implementation is okay with that,
2981 * but glibc one isn't. With '/' always treated as CWORD,
2984 # if ENABLE_ASH_ALIAS
2985 static const uint8_t syntax_index_table[] ALIGN1 = {
2986 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */
2987 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */
2988 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */
2992 static const uint8_t syntax_index_table[] ALIGN1 = {
2993 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */
2994 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */
2995 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */
3004 # if ENABLE_ASH_ALIAS
3010 /* Cast is purely for paranoia here,
3011 * just in case someone passed signed char to us */
3012 if ((unsigned char)c >= CTL_FIRST
3013 && (unsigned char)c <= CTL_LAST
3017 s = strchrnul(spec_symbls, c);
3020 indx = syntax_index_table[s - spec_symbls];
3022 return (S_I_T[indx] >> (syntax*4)) & 0xf;
3025 #else /* !USE_SIT_FUNCTION */
3027 static const uint8_t syntax_index_table[] ALIGN1 = {
3028 /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */
3029 /* 0 */ CWORD_CWORD_CWORD_CWORD,
3030 /* 1 */ CWORD_CWORD_CWORD_CWORD,
3031 /* 2 */ CWORD_CWORD_CWORD_CWORD,
3032 /* 3 */ CWORD_CWORD_CWORD_CWORD,
3033 /* 4 */ CWORD_CWORD_CWORD_CWORD,
3034 /* 5 */ CWORD_CWORD_CWORD_CWORD,
3035 /* 6 */ CWORD_CWORD_CWORD_CWORD,
3036 /* 7 */ CWORD_CWORD_CWORD_CWORD,
3037 /* 8 */ CWORD_CWORD_CWORD_CWORD,
3038 /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD,
3039 /* 10 "\n" */ CNL_CNL_CNL_CNL,
3040 /* 11 */ CWORD_CWORD_CWORD_CWORD,
3041 /* 12 */ CWORD_CWORD_CWORD_CWORD,
3042 /* 13 */ CWORD_CWORD_CWORD_CWORD,
3043 /* 14 */ CWORD_CWORD_CWORD_CWORD,
3044 /* 15 */ CWORD_CWORD_CWORD_CWORD,
3045 /* 16 */ CWORD_CWORD_CWORD_CWORD,
3046 /* 17 */ CWORD_CWORD_CWORD_CWORD,
3047 /* 18 */ CWORD_CWORD_CWORD_CWORD,
3048 /* 19 */ CWORD_CWORD_CWORD_CWORD,
3049 /* 20 */ CWORD_CWORD_CWORD_CWORD,
3050 /* 21 */ CWORD_CWORD_CWORD_CWORD,
3051 /* 22 */ CWORD_CWORD_CWORD_CWORD,
3052 /* 23 */ CWORD_CWORD_CWORD_CWORD,
3053 /* 24 */ CWORD_CWORD_CWORD_CWORD,
3054 /* 25 */ CWORD_CWORD_CWORD_CWORD,
3055 /* 26 */ CWORD_CWORD_CWORD_CWORD,
3056 /* 27 */ CWORD_CWORD_CWORD_CWORD,
3057 /* 28 */ CWORD_CWORD_CWORD_CWORD,
3058 /* 29 */ CWORD_CWORD_CWORD_CWORD,
3059 /* 30 */ CWORD_CWORD_CWORD_CWORD,
3060 /* 31 */ CWORD_CWORD_CWORD_CWORD,
3061 /* 32 " " */ CSPCL_CWORD_CWORD_CWORD,
3062 /* 33 "!" */ CWORD_CCTL_CCTL_CWORD,
3063 /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD,
3064 /* 35 "#" */ CWORD_CWORD_CWORD_CWORD,
3065 /* 36 "$" */ CVAR_CVAR_CWORD_CVAR,
3066 /* 37 "%" */ CWORD_CWORD_CWORD_CWORD,
3067 /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD,
3068 /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD,
3069 /* 40 "(" */ CSPCL_CWORD_CWORD_CLP,
3070 /* 41 ")" */ CSPCL_CWORD_CWORD_CRP,
3071 /* 42 "*" */ CWORD_CCTL_CCTL_CWORD,
3072 /* 43 "+" */ CWORD_CWORD_CWORD_CWORD,
3073 /* 44 "," */ CWORD_CWORD_CWORD_CWORD,
3074 /* 45 "-" */ CWORD_CCTL_CCTL_CWORD,
3075 /* 46 "." */ CWORD_CWORD_CWORD_CWORD,
3076 /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */
3077 /* 47 "/" */ CWORD_CWORD_CWORD_CWORD,
3078 /* 48 "0" */ CWORD_CWORD_CWORD_CWORD,
3079 /* 49 "1" */ CWORD_CWORD_CWORD_CWORD,
3080 /* 50 "2" */ CWORD_CWORD_CWORD_CWORD,
3081 /* 51 "3" */ CWORD_CWORD_CWORD_CWORD,
3082 /* 52 "4" */ CWORD_CWORD_CWORD_CWORD,
3083 /* 53 "5" */ CWORD_CWORD_CWORD_CWORD,
3084 /* 54 "6" */ CWORD_CWORD_CWORD_CWORD,
3085 /* 55 "7" */ CWORD_CWORD_CWORD_CWORD,
3086 /* 56 "8" */ CWORD_CWORD_CWORD_CWORD,
3087 /* 57 "9" */ CWORD_CWORD_CWORD_CWORD,
3088 /* 58 ":" */ CWORD_CCTL_CCTL_CWORD,
3089 /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD,
3090 /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD,
3091 /* 61 "=" */ CWORD_CCTL_CCTL_CWORD,
3092 /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD,
3093 /* 63 "?" */ CWORD_CCTL_CCTL_CWORD,
3094 /* 64 "@" */ CWORD_CWORD_CWORD_CWORD,
3095 /* 65 "A" */ CWORD_CWORD_CWORD_CWORD,
3096 /* 66 "B" */ CWORD_CWORD_CWORD_CWORD,
3097 /* 67 "C" */ CWORD_CWORD_CWORD_CWORD,
3098 /* 68 "D" */ CWORD_CWORD_CWORD_CWORD,
3099 /* 69 "E" */ CWORD_CWORD_CWORD_CWORD,
3100 /* 70 "F" */ CWORD_CWORD_CWORD_CWORD,
3101 /* 71 "G" */ CWORD_CWORD_CWORD_CWORD,
3102 /* 72 "H" */ CWORD_CWORD_CWORD_CWORD,
3103 /* 73 "I" */ CWORD_CWORD_CWORD_CWORD,
3104 /* 74 "J" */ CWORD_CWORD_CWORD_CWORD,
3105 /* 75 "K" */ CWORD_CWORD_CWORD_CWORD,
3106 /* 76 "L" */ CWORD_CWORD_CWORD_CWORD,
3107 /* 77 "M" */ CWORD_CWORD_CWORD_CWORD,
3108 /* 78 "N" */ CWORD_CWORD_CWORD_CWORD,
3109 /* 79 "O" */ CWORD_CWORD_CWORD_CWORD,
3110 /* 80 "P" */ CWORD_CWORD_CWORD_CWORD,
3111 /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD,
3112 /* 82 "R" */ CWORD_CWORD_CWORD_CWORD,
3113 /* 83 "S" */ CWORD_CWORD_CWORD_CWORD,
3114 /* 84 "T" */ CWORD_CWORD_CWORD_CWORD,
3115 /* 85 "U" */ CWORD_CWORD_CWORD_CWORD,
3116 /* 86 "V" */ CWORD_CWORD_CWORD_CWORD,
3117 /* 87 "W" */ CWORD_CWORD_CWORD_CWORD,
3118 /* 88 "X" */ CWORD_CWORD_CWORD_CWORD,
3119 /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD,
3120 /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD,
3121 /* 91 "[" */ CWORD_CCTL_CCTL_CWORD,
3122 /* 92 "\" */ CBACK_CBACK_CCTL_CBACK,
3123 /* 93 "]" */ CWORD_CCTL_CCTL_CWORD,
3124 /* 94 "^" */ CWORD_CWORD_CWORD_CWORD,
3125 /* 95 "_" */ CWORD_CWORD_CWORD_CWORD,
3126 /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE,
3127 /* 97 "a" */ CWORD_CWORD_CWORD_CWORD,
3128 /* 98 "b" */ CWORD_CWORD_CWORD_CWORD,
3129 /* 99 "c" */ CWORD_CWORD_CWORD_CWORD,
3130 /* 100 "d" */ CWORD_CWORD_CWORD_CWORD,
3131 /* 101 "e" */ CWORD_CWORD_CWORD_CWORD,
3132 /* 102 "f" */ CWORD_CWORD_CWORD_CWORD,
3133 /* 103 "g" */ CWORD_CWORD_CWORD_CWORD,
3134 /* 104 "h" */ CWORD_CWORD_CWORD_CWORD,
3135 /* 105 "i" */ CWORD_CWORD_CWORD_CWORD,
3136 /* 106 "j" */ CWORD_CWORD_CWORD_CWORD,
3137 /* 107 "k" */ CWORD_CWORD_CWORD_CWORD,
3138 /* 108 "l" */ CWORD_CWORD_CWORD_CWORD,
3139 /* 109 "m" */ CWORD_CWORD_CWORD_CWORD,
3140 /* 110 "n" */ CWORD_CWORD_CWORD_CWORD,
3141 /* 111 "o" */ CWORD_CWORD_CWORD_CWORD,
3142 /* 112 "p" */ CWORD_CWORD_CWORD_CWORD,
3143 /* 113 "q" */ CWORD_CWORD_CWORD_CWORD,
3144 /* 114 "r" */ CWORD_CWORD_CWORD_CWORD,
3145 /* 115 "s" */ CWORD_CWORD_CWORD_CWORD,
3146 /* 116 "t" */ CWORD_CWORD_CWORD_CWORD,
3147 /* 117 "u" */ CWORD_CWORD_CWORD_CWORD,
3148 /* 118 "v" */ CWORD_CWORD_CWORD_CWORD,
3149 /* 119 "w" */ CWORD_CWORD_CWORD_CWORD,
3150 /* 120 "x" */ CWORD_CWORD_CWORD_CWORD,
3151 /* 121 "y" */ CWORD_CWORD_CWORD_CWORD,
3152 /* 122 "z" */ CWORD_CWORD_CWORD_CWORD,
3153 /* 123 "{" */ CWORD_CWORD_CWORD_CWORD,
3154 /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD,
3155 /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR,
3156 /* 126 "~" */ CWORD_CCTL_CCTL_CWORD,
3157 /* 127 del */ CWORD_CWORD_CWORD_CWORD,
3158 /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD,
3159 /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL,
3160 /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL,
3161 /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL,
3162 /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL,
3163 /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL,
3164 /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL,
3165 /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL,
3166 /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL,
3167 /* 137 */ CWORD_CWORD_CWORD_CWORD,
3168 /* 138 */ CWORD_CWORD_CWORD_CWORD,
3169 /* 139 */ CWORD_CWORD_CWORD_CWORD,
3170 /* 140 */ CWORD_CWORD_CWORD_CWORD,
3171 /* 141 */ CWORD_CWORD_CWORD_CWORD,
3172 /* 142 */ CWORD_CWORD_CWORD_CWORD,
3173 /* 143 */ CWORD_CWORD_CWORD_CWORD,
3174 /* 144 */ CWORD_CWORD_CWORD_CWORD,
3175 /* 145 */ CWORD_CWORD_CWORD_CWORD,
3176 /* 146 */ CWORD_CWORD_CWORD_CWORD,
3177 /* 147 */ CWORD_CWORD_CWORD_CWORD,
3178 /* 148 */ CWORD_CWORD_CWORD_CWORD,
3179 /* 149 */ CWORD_CWORD_CWORD_CWORD,
3180 /* 150 */ CWORD_CWORD_CWORD_CWORD,
3181 /* 151 */ CWORD_CWORD_CWORD_CWORD,
3182 /* 152 */ CWORD_CWORD_CWORD_CWORD,
3183 /* 153 */ CWORD_CWORD_CWORD_CWORD,
3184 /* 154 */ CWORD_CWORD_CWORD_CWORD,
3185 /* 155 */ CWORD_CWORD_CWORD_CWORD,
3186 /* 156 */ CWORD_CWORD_CWORD_CWORD,
3187 /* 157 */ CWORD_CWORD_CWORD_CWORD,
3188 /* 158 */ CWORD_CWORD_CWORD_CWORD,
3189 /* 159 */ CWORD_CWORD_CWORD_CWORD,
3190 /* 160 */ CWORD_CWORD_CWORD_CWORD,
3191 /* 161 */ CWORD_CWORD_CWORD_CWORD,
3192 /* 162 */ CWORD_CWORD_CWORD_CWORD,
3193 /* 163 */ CWORD_CWORD_CWORD_CWORD,
3194 /* 164 */ CWORD_CWORD_CWORD_CWORD,
3195 /* 165 */ CWORD_CWORD_CWORD_CWORD,
3196 /* 166 */ CWORD_CWORD_CWORD_CWORD,
3197 /* 167 */ CWORD_CWORD_CWORD_CWORD,
3198 /* 168 */ CWORD_CWORD_CWORD_CWORD,
3199 /* 169 */ CWORD_CWORD_CWORD_CWORD,
3200 /* 170 */ CWORD_CWORD_CWORD_CWORD,
3201 /* 171 */ CWORD_CWORD_CWORD_CWORD,
3202 /* 172 */ CWORD_CWORD_CWORD_CWORD,
3203 /* 173 */ CWORD_CWORD_CWORD_CWORD,
3204 /* 174 */ CWORD_CWORD_CWORD_CWORD,
3205 /* 175 */ CWORD_CWORD_CWORD_CWORD,
3206 /* 176 */ CWORD_CWORD_CWORD_CWORD,
3207 /* 177 */ CWORD_CWORD_CWORD_CWORD,
3208 /* 178 */ CWORD_CWORD_CWORD_CWORD,
3209 /* 179 */ CWORD_CWORD_CWORD_CWORD,
3210 /* 180 */ CWORD_CWORD_CWORD_CWORD,
3211 /* 181 */ CWORD_CWORD_CWORD_CWORD,
3212 /* 182 */ CWORD_CWORD_CWORD_CWORD,
3213 /* 183 */ CWORD_CWORD_CWORD_CWORD,
3214 /* 184 */ CWORD_CWORD_CWORD_CWORD,
3215 /* 185 */ CWORD_CWORD_CWORD_CWORD,
3216 /* 186 */ CWORD_CWORD_CWORD_CWORD,
3217 /* 187 */ CWORD_CWORD_CWORD_CWORD,
3218 /* 188 */ CWORD_CWORD_CWORD_CWORD,
3219 /* 189 */ CWORD_CWORD_CWORD_CWORD,
3220 /* 190 */ CWORD_CWORD_CWORD_CWORD,
3221 /* 191 */ CWORD_CWORD_CWORD_CWORD,
3222 /* 192 */ CWORD_CWORD_CWORD_CWORD,
3223 /* 193 */ CWORD_CWORD_CWORD_CWORD,
3224 /* 194 */ CWORD_CWORD_CWORD_CWORD,
3225 /* 195 */ CWORD_CWORD_CWORD_CWORD,
3226 /* 196 */ CWORD_CWORD_CWORD_CWORD,
3227 /* 197 */ CWORD_CWORD_CWORD_CWORD,
3228 /* 198 */ CWORD_CWORD_CWORD_CWORD,
3229 /* 199 */ CWORD_CWORD_CWORD_CWORD,
3230 /* 200 */ CWORD_CWORD_CWORD_CWORD,
3231 /* 201 */ CWORD_CWORD_CWORD_CWORD,
3232 /* 202 */ CWORD_CWORD_CWORD_CWORD,
3233 /* 203 */ CWORD_CWORD_CWORD_CWORD,
3234 /* 204 */ CWORD_CWORD_CWORD_CWORD,
3235 /* 205 */ CWORD_CWORD_CWORD_CWORD,
3236 /* 206 */ CWORD_CWORD_CWORD_CWORD,
3237 /* 207 */ CWORD_CWORD_CWORD_CWORD,
3238 /* 208 */ CWORD_CWORD_CWORD_CWORD,
3239 /* 209 */ CWORD_CWORD_CWORD_CWORD,
3240 /* 210 */ CWORD_CWORD_CWORD_CWORD,
3241 /* 211 */ CWORD_CWORD_CWORD_CWORD,
3242 /* 212 */ CWORD_CWORD_CWORD_CWORD,
3243 /* 213 */ CWORD_CWORD_CWORD_CWORD,
3244 /* 214 */ CWORD_CWORD_CWORD_CWORD,
3245 /* 215 */ CWORD_CWORD_CWORD_CWORD,
3246 /* 216 */ CWORD_CWORD_CWORD_CWORD,
3247 /* 217 */ CWORD_CWORD_CWORD_CWORD,
3248 /* 218 */ CWORD_CWORD_CWORD_CWORD,
3249 /* 219 */ CWORD_CWORD_CWORD_CWORD,
3250 /* 220 */ CWORD_CWORD_CWORD_CWORD,
3251 /* 221 */ CWORD_CWORD_CWORD_CWORD,
3252 /* 222 */ CWORD_CWORD_CWORD_CWORD,
3253 /* 223 */ CWORD_CWORD_CWORD_CWORD,
3254 /* 224 */ CWORD_CWORD_CWORD_CWORD,
3255 /* 225 */ CWORD_CWORD_CWORD_CWORD,
3256 /* 226 */ CWORD_CWORD_CWORD_CWORD,
3257 /* 227 */ CWORD_CWORD_CWORD_CWORD,
3258 /* 228 */ CWORD_CWORD_CWORD_CWORD,
3259 /* 229 */ CWORD_CWORD_CWORD_CWORD,
3260 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3261 /* 231 */ CWORD_CWORD_CWORD_CWORD,
3262 /* 232 */ CWORD_CWORD_CWORD_CWORD,
3263 /* 233 */ CWORD_CWORD_CWORD_CWORD,
3264 /* 234 */ CWORD_CWORD_CWORD_CWORD,
3265 /* 235 */ CWORD_CWORD_CWORD_CWORD,
3266 /* 236 */ CWORD_CWORD_CWORD_CWORD,
3267 /* 237 */ CWORD_CWORD_CWORD_CWORD,
3268 /* 238 */ CWORD_CWORD_CWORD_CWORD,
3269 /* 239 */ CWORD_CWORD_CWORD_CWORD,
3270 /* 230 */ CWORD_CWORD_CWORD_CWORD,
3271 /* 241 */ CWORD_CWORD_CWORD_CWORD,
3272 /* 242 */ CWORD_CWORD_CWORD_CWORD,
3273 /* 243 */ CWORD_CWORD_CWORD_CWORD,
3274 /* 244 */ CWORD_CWORD_CWORD_CWORD,
3275 /* 245 */ CWORD_CWORD_CWORD_CWORD,
3276 /* 246 */ CWORD_CWORD_CWORD_CWORD,
3277 /* 247 */ CWORD_CWORD_CWORD_CWORD,
3278 /* 248 */ CWORD_CWORD_CWORD_CWORD,
3279 /* 249 */ CWORD_CWORD_CWORD_CWORD,
3280 /* 250 */ CWORD_CWORD_CWORD_CWORD,
3281 /* 251 */ CWORD_CWORD_CWORD_CWORD,
3282 /* 252 */ CWORD_CWORD_CWORD_CWORD,
3283 /* 253 */ CWORD_CWORD_CWORD_CWORD,
3284 /* 254 */ CWORD_CWORD_CWORD_CWORD,
3285 /* 255 */ CWORD_CWORD_CWORD_CWORD,
3286 /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE,
3287 # if ENABLE_ASH_ALIAS
3288 /* PEOA */ CSPCL_CIGN_CIGN_CIGN,
3293 # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf)
3294 #else /* debug version, caught one signed char bug */
3295 # define SIT(c, syntax) \
3297 if ((c) < 0 || (c) > (PEOF + ENABLE_ASH_ALIAS)) \
3298 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3299 if ((syntax) < 0 || (syntax) > (2 + ENABLE_FEATURE_SH_MATH)) \
3300 bb_error_msg_and_die("line:%d c:%d", __LINE__, (c)); \
3301 ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf); \
3305 #endif /* !USE_SIT_FUNCTION */
3308 /* ============ Alias handling */
3310 #if ENABLE_ASH_ALIAS
3312 #define ALIASINUSE 1
3323 static struct alias **atab; // [ATABSIZE];
3324 #define INIT_G_alias() do { \
3325 atab = xzalloc(ATABSIZE * sizeof(atab[0])); \
3329 static struct alias **
3330 __lookupalias(const char *name)
3332 unsigned int hashval;
3339 ch = (unsigned char)*p;
3343 ch = (unsigned char)*++p;
3345 app = &atab[hashval % ATABSIZE];
3347 for (; *app; app = &(*app)->next) {
3348 if (strcmp(name, (*app)->name) == 0) {
3356 static struct alias *
3357 lookupalias(const char *name, int check)
3359 struct alias *ap = *__lookupalias(name);
3361 if (check && ap && (ap->flag & ALIASINUSE))
3366 static struct alias *
3367 freealias(struct alias *ap)
3371 if (ap->flag & ALIASINUSE) {
3372 ap->flag |= ALIASDEAD;
3384 setalias(const char *name, const char *val)
3386 struct alias *ap, **app;
3388 app = __lookupalias(name);
3392 if (!(ap->flag & ALIASINUSE)) {
3395 ap->val = ckstrdup(val);
3396 ap->flag &= ~ALIASDEAD;
3399 ap = ckzalloc(sizeof(struct alias));
3400 ap->name = ckstrdup(name);
3401 ap->val = ckstrdup(val);
3402 /*ap->flag = 0; - ckzalloc did it */
3403 /*ap->next = NULL;*/
3410 unalias(const char *name)
3414 app = __lookupalias(name);
3418 *app = freealias(*app);
3429 struct alias *ap, **app;
3433 for (i = 0; i < ATABSIZE; i++) {
3435 for (ap = *app; ap; ap = *app) {
3436 *app = freealias(*app);
3446 printalias(const struct alias *ap)
3448 out1fmt("%s=%s\n", ap->name, single_quote(ap->val));
3452 * TODO - sort output
3454 static int FAST_FUNC
3455 aliascmd(int argc UNUSED_PARAM, char **argv)
3464 for (i = 0; i < ATABSIZE; i++) {
3465 for (ap = atab[i]; ap; ap = ap->next) {
3471 while ((n = *++argv) != NULL) {
3472 v = strchr(n+1, '=');
3473 if (v == NULL) { /* n+1: funny ksh stuff */
3474 ap = *__lookupalias(n);
3476 fprintf(stderr, "%s: %s not found\n", "alias", n);
3489 static int FAST_FUNC
3490 unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
3494 while (nextopt("a") != '\0') {
3498 for (i = 0; *argptr; argptr++) {
3499 if (unalias(*argptr)) {
3500 fprintf(stderr, "%s: %s not found\n", "unalias", *argptr);
3508 #endif /* ASH_ALIAS */
3511 /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */
3514 #define FORK_NOJOB 2
3516 /* mode flags for showjob(s) */
3517 #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */
3518 #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */
3519 #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */
3520 #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */
3523 * A job structure contains information about a job. A job is either a
3524 * single process or a set of processes contained in a pipeline. In the
3525 * latter case, pidlist will be non-NULL, and will point to a -1 terminated
3529 pid_t ps_pid; /* process id */
3530 int ps_status; /* last process status from wait() */
3531 char *ps_cmd; /* text of command being run */
3535 struct procstat ps0; /* status of process */
3536 struct procstat *ps; /* status or processes when more than one */
3538 int stopstatus; /* status of a stopped job */
3540 unsigned nprocs; /* number of processes */
3542 #define JOBRUNNING 0 /* at least one proc running */
3543 #define JOBSTOPPED 1 /* all procs are stopped */
3544 #define JOBDONE 2 /* all procs are completed */
3548 sigint: 1, /* job was killed by SIGINT */
3549 jobctl: 1, /* job running under job control */
3551 waited: 1, /* true if this entry has been waited for */
3552 used: 1, /* true if this entry is in used */
3553 changed: 1; /* true if status has changed */
3554 struct job *prev_job; /* previous job */
3557 static struct job *makejob(/*union node *,*/ int);
3558 static int forkshell(struct job *, union node *, int);
3559 static int waitforjob(struct job *);
3562 enum { doing_jobctl = 0 };
3563 #define setjobctl(on) do {} while (0)
3565 static smallint doing_jobctl; //references:8
3566 static void setjobctl(int);
3573 ignoresig(int signo)
3575 /* Avoid unnecessary system calls. Is it already SIG_IGNed? */
3576 if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) {
3577 /* No, need to do it */
3578 signal(signo, SIG_IGN);
3580 sigmode[signo - 1] = S_HARD_IGN;
3584 * Only one usage site - in setsignal()
3587 signal_handler(int signo)
3589 if (signo == SIGCHLD) {
3595 gotsig[signo - 1] = 1;
3596 pending_sig = signo;
3598 if (signo == SIGINT && !trap[SIGINT]) {
3599 if (!suppress_int) {
3601 raise_interrupt(); /* does not return */
3608 * Set the signal handler for the specified signal. The routine figures
3609 * out what it should be set to.
3612 setsignal(int signo)
3615 char cur_act, new_act;
3616 struct sigaction act;
3620 if (t != NULL) { /* trap for this sig is set */
3622 if (t[0] == '\0') /* trap is "": ignore this sig */
3626 if (rootshell && new_act == S_DFL) {
3629 if (iflag || minusc || sflag == 0)
3638 * "In all cases, bash ignores SIGQUIT. Non-builtin
3639 * commands run by bash have signal handlers
3640 * set to the values inherited by the shell
3641 * from its parent". */
3657 /* if !rootshell, we reset SIGQUIT to DFL,
3658 * whereas we have to restore it to what shell got on entry.
3659 * This is handled by the fact that if signal was IGNored on entry,
3660 * then cur_act is S_HARD_IGN and we never change its sigaction
3664 if (signo == SIGCHLD)
3667 t = &sigmode[signo - 1];
3670 /* current setting is not yet known */
3671 if (sigaction(signo, NULL, &act)) {
3672 /* pretend it worked; maybe we should give a warning,
3673 * but other shells don't. We don't alter sigmode,
3674 * so we retry every time.
3675 * btw, in Linux it never fails. --vda */
3678 if (act.sa_handler == SIG_IGN) {
3679 cur_act = S_HARD_IGN;
3681 && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU)
3683 cur_act = S_IGN; /* don't hard ignore these */
3686 if (act.sa_handler == SIG_DFL && new_act == S_DFL) {
3687 /* installing SIG_DFL over SIG_DFL is a no-op */
3688 /* saves one sigaction call in each "sh -c SCRIPT" invocation */
3693 if (cur_act == S_HARD_IGN || cur_act == new_act)
3698 act.sa_handler = SIG_DFL;
3701 act.sa_handler = signal_handler;
3704 act.sa_handler = SIG_IGN;
3707 /* flags and mask matter only if !DFL and !IGN, but we do it
3708 * for all cases for more deterministic behavior:
3710 act.sa_flags = 0; //TODO: why not SA_RESTART?
3711 sigfillset(&act.sa_mask);
3713 sigaction_set(signo, &act);
3716 /* mode flags for set_curjob */
3717 #define CUR_DELETE 2
3718 #define CUR_RUNNING 1
3719 #define CUR_STOPPED 0
3722 /* pgrp of shell on invocation */
3723 static int initialpgrp; //references:2
3724 static int ttyfd = -1; //5
3727 static struct job *jobtab; //5
3729 static unsigned njobs; //4
3731 static struct job *curjob; //lots
3732 /* number of presumed living untracked jobs */
3733 static int jobless; //4
3736 /* Bash has a feature: it restores termios after a successful wait for
3737 * a foreground job which had at least one stopped or sigkilled member.
3738 * The probable rationale is that SIGSTOP and SIGKILL can preclude task from
3739 * properly restoring tty state. Should we do this too?
3740 * A reproducer: ^Z an interactive python:
3743 * Python 2.7.12 (...)
3745 * { python leaves tty in -icanon -echo state. We do survive that... }
3746 * [1]+ Stopped python
3747 * { ...however, next program (python #2) does not survive it well: }
3749 * Python 2.7.12 (...)
3750 * >>> Traceback (most recent call last):
3751 * { above, I typed "qwerty<CR>", but -echo state is still in effect }
3752 * File "<stdin>", line 1, in <module>
3753 * NameError: name 'qwerty' is not defined
3755 * The implementation below is modeled on bash code and seems to work.
3756 * However, I'm not sure we should do this. For one: what if I'd fg
3757 * the stopped python instead? It'll be confused by "restored" tty state.
3759 static struct termios shell_tty_info;
3763 if (rootshell && ttyfd >= 0)
3764 tcgetattr(ttyfd, &shell_tty_info);
3769 /* if (rootshell) - caller ensures this */
3771 tcsetattr(ttyfd, TCSADRAIN, &shell_tty_info);
3774 job_signal_status(struct job *jp)
3778 struct procstat *ps = jp->ps;
3779 for (i = 0; i < jp->nprocs; i++) {
3780 status = ps[i].ps_status;
3781 if (WIFSIGNALED(status) || WIFSTOPPED(status))
3787 restore_tty_if_stopped_or_signaled(struct job *jp)
3789 //TODO: check what happens if we come from waitforjob() in expbackq()
3791 int s = job_signal_status(jp);
3792 if (s) /* WIFSIGNALED(s) || WIFSTOPPED(s) */
3797 # define get_tty_state() ((void)0)
3798 # define restore_tty_if_stopped_or_signaled(jp) ((void)0)
3802 set_curjob(struct job *jp, unsigned mode)
3805 struct job **jpp, **curp;
3807 /* first remove from list */
3808 jpp = curp = &curjob;
3813 jpp = &jp1->prev_job;
3815 *jpp = jp1->prev_job;
3817 /* Then re-insert in correct position */
3825 /* job being deleted */
3828 /* newly created job or backgrounded job,
3829 * put after all stopped jobs.
3834 if (!jp1 || jp1->state != JOBSTOPPED)
3837 jpp = &jp1->prev_job;
3843 /* newly stopped job - becomes curjob */
3844 jp->prev_job = *jpp;
3852 jobno(const struct job *jp)
3854 return jp - jobtab + 1;
3859 * Convert a job name to a job structure.
3862 #define getjob(name, getctl) getjob(name)
3865 getjob(const char *name, int getctl)
3869 const char *err_msg = "%s: no such job";
3873 char *(*match)(const char *, const char *);
3888 if (c == '+' || c == '%') {
3890 err_msg = "No current job";
3896 err_msg = "No previous job";
3906 if (num > 0 && num <= njobs) {
3907 jp = jobtab + num - 1;
3922 if (match(jp->ps[0].ps_cmd, p)) {
3926 err_msg = "%s: ambiguous";
3936 err_msg = "job %s not created under job control";
3937 if (getctl && jp->jobctl == 0)
3942 ash_msg_and_raise_error(err_msg, name);
3946 * Mark a job structure as unused.
3949 freejob(struct job *jp)
3951 struct procstat *ps;
3955 for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) {
3956 if (ps->ps_cmd != nullstr)
3959 if (jp->ps != &jp->ps0)
3962 set_curjob(jp, CUR_DELETE);
3968 xtcsetpgrp(int fd, pid_t pgrp)
3970 if (tcsetpgrp(fd, pgrp))
3971 ash_msg_and_raise_perror("can't set tty process group");
3975 * Turn job control on and off.
3977 * Note: This code assumes that the third arg to ioctl is a character
3978 * pointer, which is true on Berkeley systems but not System V. Since
3979 * System V doesn't have job control yet, this isn't a problem now.
3981 * Called with interrupts off.
3989 if (on == doing_jobctl || rootshell == 0)
3993 ofd = fd = open(_PATH_TTY, O_RDWR);
3995 /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails.
3996 * That sometimes helps to acquire controlling tty.
3997 * Obviously, a workaround for bugs when someone
3998 * failed to provide a controlling tty to bash! :) */
4004 /* fd is a tty at this point */
4005 fd = fcntl(fd, F_DUPFD_CLOEXEC, 10);
4006 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */
4009 goto out; /* F_DUPFD failed */
4010 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
4011 close_on_exec_on(fd);
4012 while (1) { /* while we are in the background */
4013 pgrp = tcgetpgrp(fd);
4016 ash_msg("can't access tty; job control turned off");
4020 if (pgrp == getpgrp())
4031 xtcsetpgrp(fd, pgrp);
4033 /* turning job control off */
4036 /* was xtcsetpgrp, but this can make exiting ash
4037 * loop forever if pty is already deleted */
4038 tcsetpgrp(fd, pgrp);
4052 static int FAST_FUNC
4053 killcmd(int argc, char **argv)
4055 if (argv[1] && strcmp(argv[1], "-l") != 0) {
4058 if (argv[i][0] == '%') {
4060 * "kill %N" - job kill
4061 * Converting to pgrp / pid kill
4067 jp = getjob(argv[i], 0);
4069 * In jobs started under job control, we signal
4070 * entire process group by kill -PGRP_ID.
4071 * This happens, f.e., in interactive shell.
4073 * Otherwise, we signal each child via
4074 * kill PID1 PID2 PID3.
4076 * sh -c 'sleep 1|sleep 1 & kill %1'
4077 * sh -c 'true|sleep 2 & sleep 1; kill %1'
4078 * sh -c 'true|sleep 1 & sleep 2; kill %1'
4080 n = jp->nprocs; /* can't be 0 (I hope) */
4083 dst = alloca(n * sizeof(int)*4);
4085 for (j = 0; j < n; j++) {
4086 struct procstat *ps = &jp->ps[j];
4087 /* Skip non-running and not-stopped members
4088 * (i.e. dead members) of the job
4090 if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status))
4093 * kill_main has matching code to expect
4094 * leading space. Needed to not confuse
4095 * negative pids with "kill -SIGNAL_NO" syntax
4097 dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid);
4101 } while (argv[++i]);
4103 return kill_main(argc, argv);
4107 showpipe(struct job *jp /*, FILE *out*/)
4109 struct procstat *ps;
4110 struct procstat *psend;
4112 psend = jp->ps + jp->nprocs;
4113 for (ps = jp->ps + 1; ps < psend; ps++)
4114 printf(" | %s", ps->ps_cmd);
4115 newline_and_flush(stdout);
4116 flush_stdout_stderr();
4121 restartjob(struct job *jp, int mode)
4123 struct procstat *ps;
4129 if (jp->state == JOBDONE)
4131 jp->state = JOBRUNNING;
4132 pgid = jp->ps[0].ps_pid;
4133 if (mode == FORK_FG) {
4135 xtcsetpgrp(ttyfd, pgid);
4137 killpg(pgid, SIGCONT);
4141 if (WIFSTOPPED(ps->ps_status)) {
4147 status = (mode == FORK_FG) ? waitforjob(jp) : 0;
4152 static int FAST_FUNC
4153 fg_bgcmd(int argc UNUSED_PARAM, char **argv)
4159 mode = (**argv == 'f') ? FORK_FG : FORK_BG;
4163 jp = getjob(*argv, 1);
4164 if (mode == FORK_BG) {
4165 set_curjob(jp, CUR_RUNNING);
4166 printf("[%d] ", jobno(jp));
4168 out1str(jp->ps[0].ps_cmd);
4169 showpipe(jp /*, stdout*/);
4170 retval = restartjob(jp, mode);
4171 } while (*argv && *++argv);
4177 sprint_status48(char *s, int status, int sigonly)
4183 if (!WIFEXITED(status)) {
4185 if (WIFSTOPPED(status))
4186 st = WSTOPSIG(status);
4189 st = WTERMSIG(status);
4191 if (st == SIGINT || st == SIGPIPE)
4194 if (WIFSTOPPED(status))
4199 //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata
4200 col = fmtstr(s, 32, strsignal(st));
4201 if (WCOREDUMP(status)) {
4202 strcpy(s + col, " (core dumped)");
4203 col += sizeof(" (core dumped)")-1;
4205 } else if (!sigonly) {
4206 st = WEXITSTATUS(status);
4207 col = fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st);
4214 wait_block_or_sig(int *status)
4221 /* Poll all children for changes in their state */
4223 /* if job control is active, accept stopped processes too */
4224 pid = waitpid(-1, status, doing_jobctl ? (WNOHANG|WUNTRACED) : WNOHANG);
4226 break; /* Error (e.g. EINTR, ECHILD) or pid */
4228 /* Children exist, but none are ready. Sleep until interesting signal */
4231 sigprocmask(SIG_SETMASK, &mask, &mask);
4232 while (!got_sigchld && !pending_sig)
4234 sigprocmask(SIG_SETMASK, &mask, NULL);
4235 #else /* unsafe: a signal can set pending_sig after check, but before pause() */
4236 while (!got_sigchld && !pending_sig)
4240 /* If it was SIGCHLD, poll children again */
4241 } while (got_sigchld);
4246 #define DOWAIT_NONBLOCK 0
4247 #define DOWAIT_BLOCK 1
4248 #define DOWAIT_BLOCK_OR_SIG 2
4251 dowait(int block, struct job *job)
4256 struct job *thisjob = NULL;
4258 TRACE(("dowait(0x%x) called\n", block));
4260 /* It's wrong to call waitpid() outside of INT_OFF region:
4261 * signal can arrive just after syscall return and handler can
4262 * longjmp away, losing stop/exit notification processing.
4263 * Thus, for "jobs" builtin, and for waiting for a fg job,
4264 * we call waitpid() (blocking or non-blocking) inside INT_OFF.
4266 * However, for "wait" builtin it is wrong to simply call waitpid()
4267 * in INT_OFF region: "wait" needs to wait for any running job
4268 * to change state, but should exit on any trap too.
4269 * In INT_OFF region, a signal just before syscall entry can set
4270 * pending_sig variables, but we can't check them, and we would
4271 * either enter a sleeping waitpid() (BUG), or need to busy-loop.
4273 * Because of this, we run inside INT_OFF, but use a special routine
4274 * which combines waitpid() and sigsuspend().
4275 * This is the reason why we need to have a handler for SIGCHLD:
4276 * SIG_DFL handler does not wake sigsuspend().
4279 if (block == DOWAIT_BLOCK_OR_SIG) {
4280 pid = wait_block_or_sig(&status);
4283 if (block == DOWAIT_NONBLOCK)
4284 wait_flags = WNOHANG;
4285 /* if job control is active, accept stopped processes too */
4287 wait_flags |= WUNTRACED;
4288 /* NB: _not_ safe_waitpid, we need to detect EINTR */
4289 pid = waitpid(-1, &status, wait_flags);
4291 TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n",
4292 pid, status, errno, strerror(errno)));
4297 for (jp = curjob; jp; jp = jp->prev_job) {
4299 struct procstat *ps;
4300 struct procstat *psend;
4301 if (jp->state == JOBDONE)
4305 psend = ps + jp->nprocs;
4307 if (ps->ps_pid == pid) {
4308 TRACE(("Job %d: changing status of proc %d "
4309 "from 0x%x to 0x%x\n",
4310 jobno(jp), pid, ps->ps_status, status));
4311 ps->ps_status = status;
4314 if (ps->ps_status == -1)
4315 jobstate = JOBRUNNING;
4317 if (jobstate == JOBRUNNING)
4319 if (WIFSTOPPED(ps->ps_status)) {
4320 jp->stopstatus = ps->ps_status;
4321 jobstate = JOBSTOPPED;
4324 } while (++ps < psend);
4328 /* Found the job where one of its processes changed its state.
4329 * Is there at least one live and running process in this job? */
4330 if (jobstate != JOBRUNNING) {
4331 /* No. All live processes in the job are stopped
4332 * (JOBSTOPPED) or there are no live processes (JOBDONE)
4334 thisjob->changed = 1;
4335 if (thisjob->state != jobstate) {
4336 TRACE(("Job %d: changing state from %d to %d\n",
4337 jobno(thisjob), thisjob->state, jobstate));
4338 thisjob->state = jobstate;
4340 if (jobstate == JOBSTOPPED)
4341 set_curjob(thisjob, CUR_STOPPED);
4347 /* The process wasn't found in job list */
4349 if (!WIFSTOPPED(status))
4355 if (thisjob && thisjob == job) {
4359 len = sprint_status48(s, status, 1);
4371 showjob(struct job *jp, int mode)
4373 struct procstat *ps;
4374 struct procstat *psend;
4377 char s[16 + 16 + 48];
4378 FILE *out = (mode & SHOW_STDERR ? stderr : stdout);
4382 if (mode & SHOW_ONLY_PGID) { /* jobs -p */
4383 /* just output process (group) id of pipeline */
4384 fprintf(out, "%d\n", ps->ps_pid);
4388 col = fmtstr(s, 16, "[%d] ", jobno(jp));
4393 else if (curjob && jp == curjob->prev_job)
4396 if (mode & SHOW_PIDS)
4397 col += fmtstr(s + col, 16, "%d ", ps->ps_pid);
4399 psend = ps + jp->nprocs;
4401 if (jp->state == JOBRUNNING) {
4402 strcpy(s + col, "Running");
4403 col += sizeof("Running") - 1;
4405 int status = psend[-1].ps_status;
4406 if (jp->state == JOBSTOPPED)
4407 status = jp->stopstatus;
4408 col += sprint_status48(s + col, status, 0);
4410 /* By now, "[JOBID]* [maybe PID] STATUS" is printed */
4412 /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line
4413 * or prints several "PID | <cmdN>" lines,
4414 * depending on SHOW_PIDS bit.
4415 * We do not print status of individual processes
4416 * between PID and <cmdN>. bash does it, but not very well:
4417 * first line shows overall job status, not process status,
4418 * making it impossible to know 1st process status.
4422 /* for each process */
4425 if (mode & SHOW_PIDS)
4426 col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1;
4428 fprintf(out, "%s%*c%s%s",
4430 33 - col >= 0 ? 33 - col : 0, ' ',
4431 ps == jp->ps ? "" : "| ",
4434 } while (++ps != psend);
4435 newline_and_flush(out);
4439 if (jp->state == JOBDONE) {
4440 TRACE(("showjob: freeing job %d\n", jobno(jp)));
4446 * Print a list of jobs. If "change" is nonzero, only print jobs whose
4447 * statuses have changed since the last call to showjobs.
4454 TRACE(("showjobs(0x%x) called\n", mode));
4456 /* Handle all finished jobs */
4457 while (dowait(DOWAIT_NONBLOCK, NULL) > 0)
4460 for (jp = curjob; jp; jp = jp->prev_job) {
4461 if (!(mode & SHOW_CHANGED) || jp->changed) {
4467 static int FAST_FUNC
4468 jobscmd(int argc UNUSED_PARAM, char **argv)
4473 while ((m = nextopt("lp")) != '\0') {
4477 mode |= SHOW_ONLY_PGID;
4483 showjob(getjob(*argv, 0), mode);
4493 /* Called only on finished or stopped jobs (no members are running) */
4495 getstatus(struct job *job)
4499 struct procstat *ps;
4501 /* Fetch last member's status */
4502 ps = job->ps + job->nprocs - 1;
4503 status = ps->ps_status;
4505 /* "set -o pipefail" mode: use last _nonzero_ status */
4506 while (status == 0 && --ps >= job->ps)
4507 status = ps->ps_status;
4510 retval = WEXITSTATUS(status);
4511 if (!WIFEXITED(status)) {
4513 retval = WSTOPSIG(status);
4514 if (!WIFSTOPPED(status))
4517 /* XXX: limits number of signals */
4518 retval = WTERMSIG(status);
4520 if (retval == SIGINT)
4526 TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n",
4527 jobno(job), job->nprocs, status, retval));
4531 static int FAST_FUNC
4532 waitcmd(int argc UNUSED_PARAM, char **argv)
4543 /* wait for all jobs */
4547 if (!jp) /* no running procs */
4549 if (jp->state == JOBRUNNING)
4555 * "When bash is waiting for an asynchronous command via
4556 * the wait builtin, the reception of a signal for which a trap
4557 * has been set will cause the wait builtin to return immediately
4558 * with an exit status greater than 128, immediately after which
4559 * the trap is executed."
4561 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4562 /* if child sends us a signal *and immediately exits*,
4563 * dowait() returns pid > 0. Check this case,
4564 * not "if (dowait() < 0)"!
4573 if (**argv != '%') {
4574 pid_t pid = number(*argv);
4579 if (job->ps[job->nprocs - 1].ps_pid == pid)
4581 job = job->prev_job;
4584 job = getjob(*argv, 0);
4586 /* loop until process terminated or stopped */
4587 while (job->state == JOBRUNNING) {
4588 dowait(DOWAIT_BLOCK_OR_SIG, NULL);
4593 retval = getstatus(job);
4600 retval = 128 + pending_sig;
4609 struct job *jp, *jq;
4611 len = njobs * sizeof(*jp);
4613 jp = ckrealloc(jq, len + 4 * sizeof(*jp));
4615 offset = (char *)jp - (char *)jq;
4617 /* Relocate pointers */
4620 jq = (struct job *)((char *)jq + l);
4624 #define joff(p) ((struct job *)((char *)(p) + l))
4625 #define jmove(p) (p) = (void *)((char *)(p) + offset)
4626 if (joff(jp)->ps == &jq->ps0)
4627 jmove(joff(jp)->ps);
4628 if (joff(jp)->prev_job)
4629 jmove(joff(jp)->prev_job);
4639 jp = (struct job *)((char *)jp + len);
4643 } while (--jq >= jp);
4648 * Return a new job structure.
4649 * Called with interrupts off.
4652 makejob(/*union node *node,*/ int nprocs)
4657 for (i = njobs, jp = jobtab; ; jp++) {
4664 if (jp->state != JOBDONE || !jp->waited)
4673 memset(jp, 0, sizeof(*jp));
4675 /* jp->jobctl is a bitfield.
4676 * "jp->jobctl |= doing_jobctl" likely to give awful code */
4680 jp->prev_job = curjob;
4685 jp->ps = ckmalloc(nprocs * sizeof(struct procstat));
4687 TRACE(("makejob(%d) returns %%%d\n", nprocs,
4694 * Return a string identifying a command (to be printed by the
4697 static char *cmdnextc;
4700 cmdputs(const char *s)
4702 static const char vstype[VSTYPE + 1][3] = {
4703 "", "}", "-", "+", "?", "=",
4704 "%", "%%", "#", "##"
4705 IF_BASH_SUBSTR(, ":")
4706 IF_BASH_PATTERN_SUBST(, "/", "//")
4709 const char *p, *str;
4713 unsigned char subtype = 0;
4717 nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc);
4719 while ((c = *p++) != '\0') {
4727 if ((subtype & VSTYPE) == VSLENGTH)
4733 str = "\"}" + !(quoted & 1);
4740 #if ENABLE_FEATURE_SH_MATH
4755 if ((subtype & VSTYPE) != VSNORMAL)
4757 str = vstype[subtype & VSTYPE];
4758 if (subtype & VSNUL)
4767 /* These can only happen inside quotes */
4773 // [1]+ Done true ${\$} <<=== BUG: ${\$} is not a valid way to write $$ (${$} would be ok)
4784 while ((c = *str++) != '\0') {
4787 } /* while *p++ not NUL */
4790 USTPUTC('"', nextc);
4796 /* cmdtxt() and cmdlist() call each other */
4797 static void cmdtxt(union node *n);
4800 cmdlist(union node *np, int sep)
4802 for (; np; np = np->narg.next) {
4806 if (sep && np->narg.next)
4812 cmdtxt(union node *n)
4815 struct nodelist *lp;
4826 lp = n->npipe.cmdlist;
4844 cmdtxt(n->nbinary.ch1);
4860 cmdtxt(n->nif.test);
4862 if (n->nif.elsepart) {
4863 cmdtxt(n->nif.ifpart);
4865 n = n->nif.elsepart;
4883 cmdtxt(n->nbinary.ch1);
4893 cmdputs(n->nfor.var);
4895 cmdlist(n->nfor.args, 1);
4900 cmdputs(n->ndefun.text);
4904 cmdlist(n->ncmd.args, 1);
4905 cmdlist(n->ncmd.redirect, 0);
4918 cmdputs(n->ncase.expr->narg.text);
4920 for (np = n->ncase.cases; np; np = np->nclist.next) {
4921 cmdtxt(np->nclist.pattern);
4923 cmdtxt(np->nclist.body);
4937 #if BASH_REDIR_OUTPUT
4952 cmdputs(utoa(n->nfile.fd));
4954 if (n->type == NTOFD || n->type == NFROMFD) {
4955 if (n->ndup.dupfd >= 0)
4956 cmdputs(utoa(n->ndup.dupfd));
4967 commandtext(union node *n)
4971 STARTSTACKSTR(cmdnextc);
4973 name = stackblock();
4974 TRACE(("commandtext: name %p, end %p\n", name, cmdnextc));
4975 return ckstrdup(name);
4980 * Fork off a subshell. If we are doing job control, give the subshell its
4981 * own process group. Jp is a job structure that the job is to be added to.
4982 * N is the command that will be evaluated by the child. Both jp and n may
4983 * be NULL. The mode parameter can be one of the following:
4984 * FORK_FG - Fork off a foreground process.
4985 * FORK_BG - Fork off a background process.
4986 * FORK_NOJOB - Like FORK_FG, but don't give the process its own
4987 * process group even if job control is on.
4989 * When job control is turned off, background processes have their standard
4990 * input redirected to /dev/null (except for the second and later processes
4993 * Called with interrupts off.
4996 * Clear traps on a fork.
5004 for (tp = trap; tp < &trap[NSIG]; tp++) {
5005 if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */
5006 if (trap_ptr == trap)
5008 /* else: it "belongs" to trap_ptr vector, don't free */
5010 if ((tp - trap) != 0)
5011 setsignal(tp - trap);
5018 /* Lives far away from here, needed for forkchild */
5019 static void closescript(void);
5021 /* Called after fork(), in child */
5022 /* jp and n are NULL when called by openhere() for heredoc support */
5023 static NOINLINE void
5024 forkchild(struct job *jp, union node *n, int mode)
5028 TRACE(("Child shell %d\n", getpid()));
5032 /* man bash: "Non-builtin commands run by bash have signal handlers
5033 * set to the values inherited by the shell from its parent".
5034 * Do we do it correctly? */
5038 if (mode == FORK_NOJOB /* is it `xxx` ? */
5039 && n && n->type == NCMD /* is it single cmd? */
5040 /* && n->ncmd.args->type == NARG - always true? */
5041 && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0
5042 && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */
5043 /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */
5045 TRACE(("Trap hack\n"));
5046 /* Awful hack for `trap` or $(trap).
5048 * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
5049 * contains an example where "trap" is executed in a subshell:
5051 * save_traps=$(trap)
5053 * eval "$save_traps"
5055 * Standard does not say that "trap" in subshell shall print
5056 * parent shell's traps. It only says that its output
5057 * must have suitable form, but then, in the above example
5058 * (which is not supposed to be normative), it implies that.
5060 * bash (and probably other shell) does implement it
5061 * (traps are reset to defaults, but "trap" still shows them),
5062 * but as a result, "trap" logic is hopelessly messed up:
5065 * trap -- 'echo Ho' SIGWINCH <--- we have a handler
5066 * # (trap) <--- trap is in subshell - no output (correct, traps are reset)
5067 * # true | trap <--- trap is in subshell - no output (ditto)
5068 * # echo `true | trap` <--- in subshell - output (but traps are reset!)
5069 * trap -- 'echo Ho' SIGWINCH
5070 * # echo `(trap)` <--- in subshell in subshell - output
5071 * trap -- 'echo Ho' SIGWINCH
5072 * # echo `true | (trap)` <--- in subshell in subshell in subshell - output!
5073 * trap -- 'echo Ho' SIGWINCH
5075 * The rules when to forget and when to not forget traps
5076 * get really complex and nonsensical.
5078 * Our solution: ONLY bare $(trap) or `trap` is special.
5080 /* Save trap handler strings for trap builtin to print */
5081 trap_ptr = xmemdup(trap, sizeof(trap));
5082 /* Fall through into clearing traps */
5086 /* do job control only in root shell */
5088 if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) {
5091 if (jp->nprocs == 0)
5094 pgrp = jp->ps[0].ps_pid;
5095 /* this can fail because we are doing it in the parent also */
5097 if (mode == FORK_FG)
5098 xtcsetpgrp(ttyfd, pgrp);
5103 if (mode == FORK_BG) {
5104 /* man bash: "When job control is not in effect,
5105 * asynchronous commands ignore SIGINT and SIGQUIT" */
5108 if (jp->nprocs == 0) {
5110 if (open(bb_dev_null, O_RDONLY) != 0)
5111 ash_msg_and_raise_perror("can't open '%s'", bb_dev_null);
5115 if (iflag) { /* why if iflag only? */
5120 * "In all cases, bash ignores SIGQUIT. Non-builtin
5121 * commands run by bash have signal handlers
5122 * set to the values inherited by the shell
5124 * Take care of the second rule: */
5128 if (n && n->type == NCMD
5129 && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0
5131 TRACE(("Job hack\n"));
5132 /* "jobs": we do not want to clear job list for it,
5133 * instead we remove only _its_ own_ job from job list.
5134 * This makes "jobs .... | cat" more useful.
5140 for (jp = curjob; jp; jp = jp->prev_job)
5145 /* Called after fork(), in parent */
5147 #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid)
5150 forkparent(struct job *jp, union node *n, int mode, pid_t pid)
5152 TRACE(("In parent shell: child = %d\n", pid));
5154 /* jp is NULL when called by openhere() for heredoc support */
5155 while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0)
5161 if (mode != FORK_NOJOB && jp->jobctl) {
5164 if (jp->nprocs == 0)
5167 pgrp = jp->ps[0].ps_pid;
5168 /* This can fail because we are doing it in the child also */
5172 if (mode == FORK_BG) {
5173 backgndpid = pid; /* set $! */
5174 set_curjob(jp, CUR_RUNNING);
5177 struct procstat *ps = &jp->ps[jp->nprocs++];
5180 ps->ps_cmd = nullstr;
5182 if (doing_jobctl && n)
5183 ps->ps_cmd = commandtext(n);
5188 /* jp and n are NULL when called by openhere() for heredoc support */
5190 forkshell(struct job *jp, union node *n, int mode)
5194 TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode));
5197 TRACE(("Fork failed, errno=%d", errno));
5200 ash_msg_and_raise_perror("can't fork");
5203 CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */
5204 forkchild(jp, n, mode);
5206 forkparent(jp, n, mode, pid);
5212 * Wait for job to finish.
5214 * Under job control we have the problem that while a child process
5215 * is running interrupts generated by the user are sent to the child
5216 * but not to the shell. This means that an infinite loop started by
5217 * an interactive user may be hard to kill. With job control turned off,
5218 * an interactive user may place an interactive program inside a loop.
5219 * If the interactive program catches interrupts, the user doesn't want
5220 * these interrupts to also abort the loop. The approach we take here
5221 * is to have the shell ignore interrupt signals while waiting for a
5222 * foreground process to terminate, and then send itself an interrupt
5223 * signal if the child process was terminated by an interrupt signal.
5224 * Unfortunately, some programs want to do a bit of cleanup and then
5225 * exit on interrupt; unless these processes terminate themselves by
5226 * sending a signal to themselves (instead of calling exit) they will
5227 * confuse this approach.
5229 * Called with interrupts off.
5232 waitforjob(struct job *jp)
5236 TRACE(("waitforjob(%%%d) called\n", jobno(jp)));
5239 while (jp->state == JOBRUNNING) {
5240 /* In non-interactive shells, we _can_ get
5241 * a keyboard signal here and be EINTRed,
5242 * but we just loop back, waiting for command to complete.
5245 * "If bash is waiting for a command to complete and receives
5246 * a signal for which a trap has been set, the trap
5247 * will not be executed until the command completes."
5249 * Reality is that even if trap is not set, bash
5250 * will not act on the signal until command completes.
5251 * Try this. sleep5intoff.c:
5252 * #include <signal.h>
5253 * #include <unistd.h>
5256 * sigemptyset(&set);
5257 * sigaddset(&set, SIGINT);
5258 * sigaddset(&set, SIGQUIT);
5259 * sigprocmask(SIG_BLOCK, &set, NULL);
5263 * $ bash -c './sleep5intoff; echo hi'
5264 * ^C^C^C^C <--- pressing ^C once a second
5266 * $ bash -c './sleep5intoff; echo hi'
5267 * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT)
5270 dowait(DOWAIT_BLOCK, jp);
5277 xtcsetpgrp(ttyfd, rootpid);
5278 restore_tty_if_stopped_or_signaled(jp);
5281 * This is truly gross.
5282 * If we're doing job control, then we did a TIOCSPGRP which
5283 * caused us (the shell) to no longer be in the controlling
5284 * session -- so we wouldn't have seen any ^C/SIGINT. So, we
5285 * intuit from the subprocess exit status whether a SIGINT
5286 * occurred, and if so interrupt ourselves. Yuck. - mycroft
5288 if (jp->sigint) /* TODO: do the same with all signals */
5289 raise(SIGINT); /* ... by raise(jp->sig) instead? */
5291 if (jp->state == JOBDONE)
5298 * return 1 if there are stopped jobs, otherwise 0
5310 if (jp && jp->state == JOBSTOPPED) {
5311 out2str("You have stopped jobs.\n");
5321 * Code for dealing with input/output redirection.
5326 #define EMPTY -2 /* marks an unused slot in redirtab */
5327 #define CLOSED -1 /* marks a slot of previously-closed fd */
5330 * Handle here documents. Normally we fork off a process to write the
5331 * data to a pipe. If the document is short, we can stuff the data in
5332 * the pipe without forking.
5334 /* openhere needs this forward reference */
5335 static void expandhere(union node *arg, int fd);
5337 openhere(union node *redir)
5343 ash_msg_and_raise_perror("can't create pipe");
5344 if (redir->type == NHERE) {
5345 len = strlen(redir->nhere.doc->narg.text);
5346 if (len <= PIPE_BUF) {
5347 full_write(pip[1], redir->nhere.doc->narg.text, len);
5351 if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
5354 ignoresig(SIGINT); //signal(SIGINT, SIG_IGN);
5355 ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN);
5356 ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN);
5357 ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN);
5358 signal(SIGPIPE, SIG_DFL);
5359 if (redir->type == NHERE)
5360 full_write(pip[1], redir->nhere.doc->narg.text, len);
5362 expandhere(redir->nhere.doc, pip[1]);
5363 _exit(EXIT_SUCCESS);
5371 openredirect(union node *redir)
5377 switch (redir->nfile.type) {
5378 /* Can't happen, our single caller does this itself */
5384 return openhere(redir);
5387 /* For N[X]HERE, reading redir->nfile.expfname would touch beyond
5388 * allocated space. Do it only when we know it is safe.
5390 fname = redir->nfile.expfname;
5392 switch (redir->nfile.type) {
5398 f = open(fname, O_RDONLY);
5403 f = open(fname, O_RDWR|O_CREAT, 0666);
5408 #if BASH_REDIR_OUTPUT
5411 /* Take care of noclobber mode. */
5413 if (stat(fname, &sb) < 0) {
5414 f = open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666);
5417 } else if (!S_ISREG(sb.st_mode)) {
5418 f = open(fname, O_WRONLY, 0666);
5421 if (!fstat(f, &sb) && S_ISREG(sb.st_mode)) {
5434 f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5439 f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666);
5447 ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory"));
5449 ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file"));
5453 * Copy a file descriptor to be >= 10. Throws exception on error.
5461 newfd = fcntl(from, F_DUPFD_CLOEXEC, 10);
5462 err = newfd < 0 ? errno : 0;
5465 ash_msg_and_raise_perror("%d", from);
5467 if (F_DUPFD_CLOEXEC == F_DUPFD)
5468 close_on_exec_on(newfd);
5474 dup2_or_raise(int from, int to)
5478 newfd = (from != to) ? dup2(from, to) : to;
5480 /* Happens when source fd is not open: try "echo >&99" */
5481 ash_msg_and_raise_perror("%d", from);
5486 dup_CLOEXEC(int fd, int avoid_fd)
5490 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
5492 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
5493 close_on_exec_on(newfd);
5494 } else { /* newfd < 0 */
5503 xdup_CLOEXEC_and_close(int fd, int avoid_fd)
5507 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
5513 /* fd was not open? */
5516 ash_msg_and_raise_perror("%d", newfd);
5518 if (F_DUPFD_CLOEXEC == F_DUPFD)
5519 close_on_exec_on(newfd);
5524 /* Struct def and variable are moved down to the first usage site */
5530 struct redirtab *next;
5532 struct squirrel two_fd[];
5534 #define redirlist (G_var.redirlist)
5537 add_squirrel_closed(struct redirtab *sq, int fd)
5544 for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
5545 /* If we collide with an already moved fd... */
5546 if (fd == sq->two_fd[i].orig_fd) {
5548 * "echo 3>FILE 3>&- 3>FILE"
5549 * "echo 3>&- 3>FILE"
5550 * No need for last redirect to insert
5551 * another "need to close 3" indicator.
5553 TRACE(("redirect_fd %d: already moved or closed\n", fd));
5557 TRACE(("redirect_fd %d: previous fd was closed\n", fd));
5558 sq->two_fd[i].orig_fd = fd;
5559 sq->two_fd[i].moved_to = CLOSED;
5563 save_fd_on_redirect(int fd, int avoid_fd, struct redirtab *sq)
5567 if (avoid_fd < 9) /* the important case here is that it can be -1 */
5572 /* Testcase: "ls -l /proc/$$/fd 10>&-" should work */
5573 ttyfd = xdup_CLOEXEC_and_close(ttyfd, avoid_fd);
5574 TRACE(("redirect_fd %d: matches ttyfd, moving it to %d\n", fd, ttyfd));
5575 return 1; /* "we closed fd" */
5578 /* Are we called from redirect(0)? E.g. redirect
5579 * in a forked child. No need to save fds,
5580 * we aren't going to use them anymore, ok to trash.
5585 /* If this one of script's fds? */
5587 struct parsefile *pf = g_parsefile;
5589 /* We skip fd == 0 case because of the following:
5590 * $ ash # running ash interactively
5592 * and in script.sh: "exec 9>&0".
5593 * Even though top-level pf_fd _is_ 0,
5594 * it's still ok to use it: "read" builtin uses it,
5595 * why should we cripple "exec" builtin?
5597 if (fd == pf->pf_fd) {
5598 pf->pf_fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
5599 return 1; /* "we closed fd" */
5605 /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */
5607 /* First: do we collide with some already moved fds? */
5608 for (i = 0; sq->two_fd[i].orig_fd != EMPTY; i++) {
5609 /* If we collide with an already moved fd... */
5610 if (fd == sq->two_fd[i].moved_to) {
5611 new_fd = dup_CLOEXEC(fd, avoid_fd);
5612 sq->two_fd[i].moved_to = new_fd;
5613 TRACE(("redirect_fd %d: already busy, moving to %d\n", fd, new_fd));
5614 if (new_fd < 0) /* what? */
5616 return 0; /* "we did not close fd" */
5618 if (fd == sq->two_fd[i].orig_fd) {
5619 /* Example: echo Hello >/dev/null 1>&2 */
5620 TRACE(("redirect_fd %d: already moved\n", fd));
5621 return 0; /* "we did not close fd" */
5625 /* If this fd is open, we move and remember it; if it's closed, new_fd = CLOSED (-1) */
5626 new_fd = dup_CLOEXEC(fd, avoid_fd);
5627 TRACE(("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, new_fd));
5631 /* new_fd = CLOSED; - already is -1 */
5633 sq->two_fd[i].moved_to = new_fd;
5634 sq->two_fd[i].orig_fd = fd;
5636 /* if we move stderr, let "set -x" code know */
5637 if (fd == preverrout_fd)
5638 preverrout_fd = new_fd;
5640 return 0; /* "we did not close fd" */
5644 internally_opened_fd(int fd, struct redirtab *sq)
5651 /* If this one of script's fds? */
5653 struct parsefile *pf = g_parsefile;
5655 if (fd == pf->pf_fd)
5661 if (sq) for (i = 0; i < sq->pair_count && sq->two_fd[i].orig_fd != EMPTY; i++) {
5662 if (fd == sq->two_fd[i].moved_to)
5669 * Process a list of redirection commands. If the REDIR_PUSH flag is set,
5670 * old file descriptors are stashed away so that the redirection can be
5671 * undone by calling popredir.
5673 /* flags passed to redirect */
5674 #define REDIR_PUSH 01 /* save previous values of file descriptors */
5676 redirect(union node *redir, int flags)
5678 struct redirtab *sv;
5685 if (flags & REDIR_PUSH)
5693 fd = redir->nfile.fd;
5694 if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) {
5695 //bb_error_msg("doing %d > %d", fd, newfd);
5696 newfd = redir->ndup.dupfd;
5699 newfd = openredirect(redir); /* always >= 0 */
5701 /* open() gave us precisely the fd we wanted.
5702 * This means that this fd was not busy
5703 * (not opened to anywhere).
5704 * Remember to close it on restore:
5706 add_squirrel_closed(sv, fd);
5715 /* if "N>FILE": move newfd to fd */
5716 /* if "N>&M": dup newfd to fd */
5717 /* if "N>&-": close fd (newfd is -1) */
5719 IF_BASH_REDIR_OUTPUT(redirect_more:)
5721 closed = save_fd_on_redirect(fd, /*avoid:*/ newfd, sv);
5723 /* "N>&-" means "close me" */
5725 /* ^^^ optimization: saving may already
5726 * have closed it. If not... */
5730 /* if newfd is a script fd or saved fd, simulate EBADF */
5731 if (internally_opened_fd(newfd, sv)) {
5733 ash_msg_and_raise_perror("%d", newfd);
5735 dup2_or_raise(newfd, fd);
5736 if (close_fd >= 0) /* "N>FILE" or ">&FILE" or heredoc? */
5738 #if BASH_REDIR_OUTPUT
5739 if (redir->nfile.type == NTO2 && fd == 1) {
5740 /* ">&FILE". we already redirected to 1, now copy 1 to 2 */
5748 } while ((redir = redir->nfile.next) != NULL);
5751 //dash:#define REDIR_SAVEFD2 03 /* set preverrout */
5752 #define REDIR_SAVEFD2 0
5753 // dash has a bug: since REDIR_SAVEFD2=3 and REDIR_PUSH=1, this test
5754 // triggers for pure REDIR_PUSH too. Thus, this is done almost always,
5755 // not only for calls with flags containing REDIR_SAVEFD2.
5756 // We do this unconditionally (see save_fd_on_redirect()).
5757 //if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0)
5758 // preverrout_fd = copied_fd2;
5762 redirectsafe(union node *redir, int flags)
5765 volatile int saveint;
5766 struct jmploc *volatile savehandler = exception_handler;
5767 struct jmploc jmploc;
5770 /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */
5771 err = setjmp(jmploc.loc); /* was = setjmp(jmploc.loc) * 2; */
5773 exception_handler = &jmploc;
5774 redirect(redir, flags);
5776 exception_handler = savehandler;
5777 if (err && exception_type != EXERROR)
5778 longjmp(exception_handler->loc, 1);
5779 RESTORE_INT(saveint);
5783 static struct redirtab*
5784 pushredir(union node *redir)
5786 struct redirtab *sv;
5795 #if BASH_REDIR_OUTPUT
5796 if (redir->nfile.type == NTO2)
5799 redir = redir->nfile.next;
5802 sv = ckzalloc(sizeof(*sv) + i * sizeof(sv->two_fd[0]));
5805 sv->two_fd[i].orig_fd = sv->two_fd[i].moved_to = EMPTY;
5806 sv->next = redirlist;
5812 * Undo the effects of the last redirection.
5817 struct redirtab *rp;
5820 if (redirlist == NULL)
5824 for (i = 0; i < rp->pair_count; i++) {
5825 int fd = rp->two_fd[i].orig_fd;
5826 int copy = rp->two_fd[i].moved_to;
5827 if (copy == CLOSED) {
5832 if (copy != EMPTY) {
5835 dup2_or_raise(copy, fd);
5840 redirlist = rp->next;
5846 unwindredir(struct redirtab *stop)
5848 while (redirlist != stop)
5849 popredir(/*drop:*/ 0);
5853 /* ============ Routines to expand arguments to commands
5855 * We have to deal with backquotes, shell variables, and file metacharacters.
5858 #if ENABLE_FEATURE_SH_MATH
5860 ash_arith(const char *s)
5862 arith_state_t math_state;
5865 math_state.lookupvar = lookupvar;
5866 math_state.setvar = setvar0;
5867 //math_state.endofname = endofname;
5870 result = arith(&math_state, s);
5871 if (math_state.errmsg)
5872 ash_msg_and_raise_error(math_state.errmsg);
5879 # if ENABLE_FEATURE_SH_MATH
5880 static int substr_atoi(const char *s)
5882 arith_t t = ash_arith(s);
5883 if (sizeof(t) > sizeof(int)) {
5884 /* clamp very large or very large negative nums for ${v:N:M}:
5885 * else "${v:0:0x100000001}" would work as "${v:0:1}"
5895 # define substr_atoi(s) number(s)
5902 #define EXP_FULL 0x1 /* perform word splitting & file globbing */
5903 #define EXP_TILDE 0x2 /* do normal tilde expansion */
5904 #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */
5905 #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */
5906 /* ^^^^^^^^^^^^^^ this is meant to support constructs such as "cmd >file*.txt"
5907 * POSIX says for this case:
5908 * Pathname expansion shall not be performed on the word by a
5909 * non-interactive shell; an interactive shell may perform it, but shall
5910 * do so only when the expansion would result in one word.
5911 * Currently, our code complies to the above rule by never globbing
5912 * redirection filenames.
5913 * Bash performs globbing, unless it is non-interactive and in POSIX mode.
5914 * (this means that on a typical Linux distro, bash almost always
5915 * performs globbing, and thus diverges from what we do).
5917 #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */
5918 #define EXP_VARTILDE2 0x20 /* expand tildes after colons only */
5919 #define EXP_WORD 0x40 /* expand word in parameter expansion */
5920 #define EXP_QUOTED 0x100 /* expand word in double quotes */
5924 #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */
5925 #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */
5926 #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */
5927 #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */
5929 /* Add CTLESC when necessary. */
5930 #define QUOTES_ESC (EXP_FULL | EXP_CASE)
5931 /* Do not skip NUL characters. */
5932 #define QUOTES_KEEPNUL EXP_TILDE
5935 * Structure specifying which parts of the string should be searched
5936 * for IFS characters.
5939 struct ifsregion *next; /* next region in list */
5940 int begoff; /* offset of start of region */
5941 int endoff; /* offset of end of region */
5942 int nulonly; /* search for nul bytes only */
5946 struct strlist *list;
5947 struct strlist **lastp;
5950 /* output of current string */
5951 static char *expdest;
5952 /* list of back quote expressions */
5953 static struct nodelist *argbackq;
5954 /* first struct in list of ifs regions */
5955 static struct ifsregion ifsfirst;
5956 /* last struct in list */
5957 static struct ifsregion *ifslastp;
5958 /* holds expanded arg list */
5959 static struct arglist exparg;
5963 * cvtnum() is used even if math support is off (to prepare $? values and such).
5970 /* 32-bit and wider ints require buffer size of bytes*3 (or less) */
5971 len = sizeof(arith_t) * 3;
5972 /* If narrower: worst case, 1-byte ints: need 5 bytes: "-127<NUL>" */
5973 if (sizeof(arith_t) < 4) len += 2;
5975 expdest = makestrspace(len, expdest);
5976 len = fmtstr(expdest, len, ARITH_FMT, num);
5977 STADJUST(len, expdest);
5982 * Break the argument string into pieces based upon IFS and add the
5983 * strings to the argument list. The regions of the string to be
5984 * searched for IFS characters have been stored by recordregion.
5987 ifsbreakup(char *string, struct arglist *arglist)
5989 struct ifsregion *ifsp;
5994 const char *ifs, *realifs;
5999 if (ifslastp != NULL) {
6002 realifs = ifsset() ? ifsval() : defifs;
6007 p = string + ifsp->begoff;
6009 nulonly = ifsp->nulonly;
6010 ifs = nulonly ? nullstr : realifs;
6012 while (p < string + ifsp->endoff) {
6014 if ((unsigned char)*p == CTLESC)
6016 if (!strchr(ifs, *p)) {
6020 if (!(afternul || nulonly))
6021 ifsspc = (strchr(defifs, *p) != NULL);
6022 /* Ignore IFS whitespace at start */
6023 if (q == start && ifsspc) {
6029 sp = stzalloc(sizeof(*sp));
6031 *arglist->lastp = sp;
6032 arglist->lastp = &sp->next;
6036 if (p >= string + ifsp->endoff) {
6040 if ((unsigned char)*p == CTLESC)
6042 if (strchr(ifs, *p) == NULL) {
6046 if (strchr(defifs, *p) == NULL) {
6061 } while (ifsp != NULL);
6070 sp = stzalloc(sizeof(*sp));
6072 *arglist->lastp = sp;
6073 arglist->lastp = &sp->next;
6079 struct ifsregion *p = ifsfirst.next;
6086 struct ifsregion *ifsp;
6091 ifsfirst.next = NULL;
6098 esclen(const char *start, const char *p)
6102 while (p > start && (unsigned char)*--p == CTLESC) {
6109 * Remove any CTLESC characters from a string.
6111 #if !BASH_PATTERN_SUBST
6112 #define rmescapes(str, flag, slash_position) \
6113 rmescapes(str, flag)
6116 rmescapes(char *str, int flag, int *slash_position)
6118 static const char qchars[] ALIGN1 = {
6119 IF_BASH_PATTERN_SUBST('/',) CTLESC, CTLQUOTEMARK, '\0' };
6122 unsigned protect_against_glob;
6125 p = strpbrk(str, qchars IF_BASH_PATTERN_SUBST(+ !slash_position));
6131 if (flag & RMESCAPE_ALLOC) {
6132 size_t len = p - str;
6133 size_t fulllen = len + strlen(p) + 1;
6135 if (flag & RMESCAPE_GROW) {
6136 int strloc = str - (char *)stackblock();
6137 r = makestrspace(fulllen, expdest);
6138 /* p and str may be invalidated by makestrspace */
6139 str = (char *)stackblock() + strloc;
6141 } else if (flag & RMESCAPE_HEAP) {
6142 r = ckmalloc(fulllen);
6144 r = stalloc(fulllen);
6148 q = (char *)mempcpy(q, str, len);
6152 globbing = flag & RMESCAPE_GLOB;
6153 protect_against_glob = globbing;
6155 if ((unsigned char)*p == CTLQUOTEMARK) {
6156 // Note: protect_against_glob only affect whether
6157 // CTLESC,<ch> gets converted to <ch> or to \<ch>
6159 protect_against_glob = globbing;
6163 /* naked back slash */
6164 protect_against_glob = 0;
6167 if ((unsigned char)*p == CTLESC) {
6171 ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)");
6173 if (protect_against_glob) {
6175 * We used to trust glob() and fnmatch() to eat
6176 * superfluous escapes (\z where z has no
6177 * special meaning anyway). But this causes
6178 * bugs such as string of one greek letter rho
6179 * (unicode-encoded as two bytes "cf,81")
6180 * getting encoded as "cf,CTLESC,81"
6181 * and here, converted to "cf,\,81" -
6182 * which does not go well with some flavors
6183 * of fnmatch() in unicode locales
6184 * (for example, glibc <= 2.22).
6186 * Lets add "\" only on the chars which need it.
6187 * Testcases for less obvious chars are shown.
6192 || *p == '\\' /* case '\' in \\ ) echo ok;; *) echo WRONG;; esac */
6193 || *p == ']' /* case ']' in [a\]] ) echo ok;; *) echo WRONG;; esac */
6194 || *p == '-' /* case '-' in [a\-c]) echo ok;; *) echo WRONG;; esac */
6195 || *p == '!' /* case '!' in [\!] ) echo ok;; *) echo WRONG;; esac */
6196 /* Some libc support [^negate], that's why "^" also needs love */
6197 || *p == '^' /* case '^' in [\^] ) echo ok;; *) echo WRONG;; esac */
6203 #if BASH_PATTERN_SUBST
6204 else if (slash_position && p == str + *slash_position) {
6205 /* stop handling globbing */
6207 *slash_position = q - r;
6208 slash_position = NULL;
6211 protect_against_glob = globbing;
6216 if (flag & RMESCAPE_GROW) {
6218 STADJUST(q - r + 1, expdest);
6222 #define pmatch(a, b) !fnmatch((a), (b), 0)
6225 * Prepare a pattern for a expmeta (internal glob(3)) call.
6227 * Returns an stalloced string.
6230 preglob(const char *pattern, int flag)
6232 return rmescapes((char *)pattern, flag | RMESCAPE_GLOB, NULL);
6236 * Put a string on the stack.
6239 memtodest(const char *p, size_t len, int syntax, int quotes)
6246 q = makestrspace((quotes & QUOTES_ESC) ? len * 2 : len, expdest);
6249 unsigned char c = *p++;
6251 if (quotes & QUOTES_ESC) {
6252 int n = SIT(c, syntax);
6254 || (syntax != BASESYNTAX && n == CBACK)
6259 } else if (!(quotes & QUOTES_KEEPNUL))
6268 strtodest(const char *p, int syntax, int quotes)
6270 size_t len = strlen(p);
6271 memtodest(p, len, syntax, quotes);
6276 * Record the fact that we have to scan this region of the
6277 * string for IFS characters.
6280 recordregion(int start, int end, int nulonly)
6282 struct ifsregion *ifsp;
6284 if (ifslastp == NULL) {
6288 ifsp = ckzalloc(sizeof(*ifsp));
6289 /*ifsp->next = NULL; - ckzalloc did it */
6290 ifslastp->next = ifsp;
6294 ifslastp->begoff = start;
6295 ifslastp->endoff = end;
6296 ifslastp->nulonly = nulonly;
6300 removerecordregions(int endoff)
6302 if (ifslastp == NULL)
6305 if (ifsfirst.endoff > endoff) {
6306 while (ifsfirst.next) {
6307 struct ifsregion *ifsp;
6309 ifsp = ifsfirst.next->next;
6310 free(ifsfirst.next);
6311 ifsfirst.next = ifsp;
6314 if (ifsfirst.begoff > endoff) {
6317 ifslastp = &ifsfirst;
6318 ifsfirst.endoff = endoff;
6323 ifslastp = &ifsfirst;
6324 while (ifslastp->next && ifslastp->next->begoff < endoff)
6325 ifslastp = ifslastp->next;
6326 while (ifslastp->next) {
6327 struct ifsregion *ifsp;
6329 ifsp = ifslastp->next->next;
6330 free(ifslastp->next);
6331 ifslastp->next = ifsp;
6334 if (ifslastp->endoff > endoff)
6335 ifslastp->endoff = endoff;
6339 exptilde(char *startp, char *p, int flags)
6345 int quotes = flags & QUOTES_ESC;
6349 while ((c = *++p) != '\0') {
6356 if (flags & EXP_VARTILDE)
6366 if (*name == '\0') {
6367 home = lookupvar("HOME");
6369 pw = getpwnam(name);
6374 if (!home || !*home)
6377 strtodest(home, SQSYNTAX, quotes);
6385 * Execute a command inside back quotes. If it's a builtin command, we
6386 * want to save its output in a block obtained from malloc. Otherwise
6387 * we fork off a subprocess and get the output of the command via a pipe.
6388 * Should be called with interrupts off.
6390 struct backcmd { /* result of evalbackcmd */
6391 int fd; /* file descriptor to read from */
6392 int nleft; /* number of chars in buffer */
6393 char *buf; /* buffer */
6394 struct job *jp; /* job structure for command */
6397 /* These forward decls are needed to use "eval" code for backticks handling: */
6398 /* flags in argument to evaltree */
6399 #define EV_EXIT 01 /* exit after evaluating tree */
6400 #define EV_TESTED 02 /* exit status is checked; ignore -e flag */
6401 static int evaltree(union node *, int);
6403 /* An evaltree() which is known to never return.
6404 * Used to use an alias:
6405 * static int evaltreenr(union node *, int) __attribute__((alias("evaltree"),__noreturn__));
6406 * but clang was reported to "transfer" noreturn-ness to evaltree() as well.
6408 static ALWAYS_INLINE NORETURN void
6409 evaltreenr(union node *n, int flags)
6412 bb_unreachable(abort());
6416 static void FAST_FUNC
6417 evalbackcmd(union node *n, struct backcmd *result)
6431 ash_msg_and_raise_perror("can't create pipe");
6432 jp = makejob(/*n,*/ 1);
6433 if (forkshell(jp, n, FORK_NOJOB) == 0) {
6439 dup2_or_raise(pip[1], 1);
6442 /* TODO: eflag clearing makes the following not abort:
6443 * ash -c 'set -e; z=$(false;echo foo); echo $z'
6444 * which is what bash does (unless it is in POSIX mode).
6445 * dash deleted "eflag = 0" line in the commit
6446 * Date: Mon, 28 Jun 2010 17:11:58 +1000
6447 * [EVAL] Don't clear eflag in evalbackcmd
6448 * For now, preserve bash-like behavior, it seems to be somewhat more useful:
6452 evaltreenr(n, EV_EXIT);
6457 result->fd = pip[0];
6461 TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n",
6462 result->fd, result->buf, result->nleft, result->jp));
6466 * Expand stuff in backwards quotes.
6469 expbackq(union node *cmd, int flag)
6477 int syntax = flag & EXP_QUOTED ? DQSYNTAX : BASESYNTAX;
6478 struct stackmark smark;
6481 startloc = expdest - (char *)stackblock();
6482 pushstackmark(&smark, startloc);
6483 evalbackcmd(cmd, &in);
6484 popstackmark(&smark);
6491 memtodest(p, i, syntax, flag & QUOTES_ESC);
6495 i = nonblock_immune_read(in.fd, buf, sizeof(buf));
6496 TRACE(("expbackq: read returns %d\n", i));
6505 back_exitstatus = waitforjob(in.jp);
6509 /* Eat all trailing newlines */
6511 for (; dest > (char *)stackblock() && dest[-1] == '\n';)
6515 if (!(flag & EXP_QUOTED))
6516 recordregion(startloc, dest - (char *)stackblock(), 0);
6517 TRACE(("evalbackq: size:%d:'%.*s'\n",
6518 (int)((dest - (char *)stackblock()) - startloc),
6519 (int)((dest - (char *)stackblock()) - startloc),
6520 stackblock() + startloc));
6523 #if ENABLE_FEATURE_SH_MATH
6525 * Expand arithmetic expression. Backup to start of expression,
6526 * evaluate, place result in (backed up) result, adjust string position.
6538 * This routine is slightly over-complicated for
6539 * efficiency. Next we scan backwards looking for the
6540 * start of arithmetic.
6542 start = stackblock();
6549 while ((unsigned char)*p != CTLARI) {
6553 ash_msg_and_raise_error("missing CTLARI (shouldn't happen)");
6558 esc = esclen(start, p);
6568 removerecordregions(begoff);
6572 if (flag & QUOTES_ESC)
6573 rmescapes(p + 1, 0, NULL);
6575 len = cvtnum(ash_arith(p + 1));
6577 if (!(flag & EXP_QUOTED))
6578 recordregion(begoff, begoff + len, 0);
6582 /* argstr needs it */
6583 static char *evalvar(char *p, int flags);
6586 * Perform variable and command substitution. If EXP_FULL is set, output CTLESC
6587 * characters to allow for further processing. Otherwise treat
6588 * $@ like $* since no splitting will be performed.
6591 argstr(char *p, int flags)
6593 static const char spclchars[] ALIGN1 = {
6601 #if ENABLE_FEATURE_SH_MATH
6606 const char *reject = spclchars;
6607 int breakall = (flags & (EXP_WORD | EXP_QUOTED)) == EXP_WORD;
6612 if (!(flags & EXP_VARTILDE)) {
6614 } else if (flags & EXP_VARTILDE2) {
6619 if (flags & EXP_TILDE) {
6622 flags &= ~EXP_TILDE;
6626 p = exptilde(p, q, flags);
6629 startloc = expdest - (char *)stackblock();
6633 length += strcspn(p + length, reject);
6637 IF_FEATURE_SH_MATH(|| c == CTLENDARI)
6639 /* c == '=' || c == ':' || c == CTLENDARI */
6645 expdest = stack_nputstr(p, length, expdest);
6646 newloc = expdest - (char *)stackblock();
6647 if (breakall && !inquotes && newloc > startloc) {
6648 recordregion(startloc, newloc, 0);
6659 if (flags & EXP_VARTILDE2) {
6663 flags |= EXP_VARTILDE2;
6668 * sort of a hack - expand tildes in variable
6669 * assignments (after the first '=' and after ':'s).
6678 case CTLENDVAR: /* ??? */
6681 /* "$@" syntax adherence hack */
6682 if (!inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) {
6683 p = evalvar(p + 1, flags | EXP_QUOTED) + 1;
6686 inquotes ^= EXP_QUOTED;
6688 if (flags & QUOTES_ESC) {
6699 TRACE(("argstr: evalvar('%s')\n", p));
6700 p = evalvar(p, flags | inquotes);
6701 TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock()));
6704 expbackq(argbackq->n, flags | inquotes);
6705 argbackq = argbackq->next;
6707 #if ENABLE_FEATURE_SH_MATH
6710 expari(flags | inquotes);
6719 scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM,
6720 char *pattern, int quotes, int zero)
6729 const char *s = loc2;
6736 match = pmatch(pattern, s);
6741 if (quotes && (unsigned char)*loc == CTLESC)
6750 scanright(char *startp, char *rmesc, char *rmescend,
6751 char *pattern, int quotes, int match_at_start)
6753 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6754 int try2optimize = match_at_start;
6760 /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}":
6761 * startp="escaped_value_of_v" rmesc="raw_value_of_v"
6762 * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1
6764 * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc,
6765 * and on each iteration they go back two/one char until they reach the beginning.
6766 * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc.
6768 /* TODO: document in what other circumstances we are called. */
6770 for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) {
6773 const char *s = loc2;
6774 if (match_at_start) {
6778 match = pmatch(pattern, s);
6779 //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match);
6783 #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE
6785 /* Maybe we can optimize this:
6786 * if pattern ends with unescaped *, we can avoid checking
6787 * shorter strings: if "foo*" doesn't match "raw_value_of_v",
6788 * it won't match truncated "raw_value_of_" strings too.
6790 unsigned plen = strlen(pattern);
6791 /* Does it end with "*"? */
6792 if (plen != 0 && pattern[--plen] == '*') {
6793 /* "xxxx*" is not escaped */
6794 /* "xxx\*" is escaped */
6795 /* "xx\\*" is not escaped */
6796 /* "x\\\*" is escaped */
6798 while (plen != 0 && pattern[--plen] == '\\')
6801 break; /* ends with unescaped "*" */
6809 esc = esclen(startp, loc);
6820 static void varunset(const char *, const char *, const char *, int) NORETURN;
6822 varunset(const char *end, const char *var, const char *umsg, int varflags)
6828 msg = "parameter not set";
6830 if ((unsigned char)*end == CTLENDVAR) {
6831 if (varflags & VSNUL)
6837 ash_msg_and_raise_error("%.*s: %s%s", (int)(end - var - 1), var, msg, tail);
6841 subevalvar(char *p, char *varname, int strloc, int subtype,
6842 int startloc, int varflags, int flag)
6844 struct nodelist *saveargbackq = argbackq;
6845 int quotes = flag & QUOTES_ESC;
6848 char *rmesc, *rmescend;
6850 int amount, resetloc;
6852 IF_BASH_PATTERN_SUBST(int workloc;)
6853 IF_BASH_PATTERN_SUBST(int slash_pos;)
6854 IF_BASH_PATTERN_SUBST(char *repl;)
6856 char *(*scan)(char*, char*, char*, char*, int, int);
6858 //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)",
6859 // p, varname, strloc, subtype, startloc, varflags, quotes);
6861 #if BASH_PATTERN_SUBST
6862 /* For "${v/pattern/repl}", we must find the delimiter _before_
6863 * argstr() call expands possible variable references in pattern:
6864 * think about "v=a; a=a/; echo ${v/$a/r}" case.
6867 if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
6868 /* Find '/' and replace with NUL */
6870 /* The pattern can't be empty.
6871 * IOW: if the first char after "${v//" is a slash,
6872 * it does not terminate the pattern - it's the first char of the pattern:
6873 * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/")
6874 * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r")
6879 if (*repl == '\0') {
6887 /* Handle escaped slashes, e.g. "${v/\//_}" (they are CTLESC'ed by this point) */
6888 if ((unsigned char)*repl == CTLESC && repl[1])
6894 argstr_flags = EXP_TILDE;
6895 if (subtype != VSASSIGN
6896 && subtype != VSQUESTION
6898 && subtype != VSSUBSTR
6901 /* EXP_CASE keeps CTLESC's */
6902 argstr_flags = EXP_TILDE | EXP_CASE;
6904 argstr(p, argstr_flags);
6905 //bb_error_msg("str0:'%s'", (char *)stackblock() + strloc);
6906 #if BASH_PATTERN_SUBST
6909 slash_pos = expdest - ((char *)stackblock() + strloc);
6910 STPUTC('/', expdest);
6911 //bb_error_msg("repl+1:'%s'", repl + 1);
6912 argstr(repl + 1, EXP_TILDE); /* EXP_TILDE: echo "${v/x/~}" expands ~ ! */
6916 STPUTC('\0', expdest);
6917 argbackq = saveargbackq;
6918 startp = (char *)stackblock() + startloc;
6919 //bb_error_msg("str1:'%s'", (char *)stackblock() + strloc);
6923 setvar0(varname, startp);
6924 amount = startp - expdest;
6925 STADJUST(amount, expdest);
6929 varunset(p, varname, startp, varflags);
6934 int pos, len, orig_len;
6937 loc = str = stackblock() + strloc;
6939 /* Read POS in ${var:POS:LEN} */
6940 colon = strchr(loc, ':');
6941 if (colon) *colon = '\0';
6942 pos = substr_atoi(loc);
6943 if (colon) *colon = ':';
6945 /* Read LEN in ${var:POS:LEN} */
6946 len = str - startp - 1;
6947 /* *loc != '\0', guaranteed by parser */
6950 /* Adjust the length by the number of escapes */
6951 for (ptr = startp; ptr < (str - 1); ptr++) {
6952 if ((unsigned char)*ptr == CTLESC) {
6959 if (*loc++ == ':') {
6961 len = substr_atoi(loc);
6963 /* Skip POS in ${var:POS:LEN} */
6965 while (*loc && *loc != ':')
6968 len = substr_atoi(loc);
6971 /* ${VAR:$((-n)):l} starts n chars from the end */
6972 pos = orig_len + pos;
6974 if ((unsigned)pos >= orig_len) {
6975 /* apart from obvious ${VAR:999999:l},
6976 * covers ${VAR:$((-9999999)):l} - result is ""
6983 /* ${VAR:N:-M} sets LEN to strlen()-M */
6984 len = (orig_len - pos) + len;
6986 if ((unsigned)len > (orig_len - pos))
6987 len = orig_len - pos;
6989 for (str = startp; pos; str++, pos--) {
6990 if (quotes && (unsigned char)*str == CTLESC)
6993 for (loc = startp; len; len--) {
6994 if (quotes && (unsigned char)*str == CTLESC)
6999 amount = loc - expdest;
7000 STADJUST(amount, expdest);
7003 #endif /* BASH_SUBSTR */
7006 resetloc = expdest - (char *)stackblock();
7008 #if BASH_PATTERN_SUBST
7011 /* We'll comeback here if we grow the stack while handling
7012 * a VSREPLACE or VSREPLACEALL, since our pointers into the
7013 * stack will need rebasing, and we'll need to remove our work
7019 amount = expdest - ((char *)stackblock() + resetloc);
7020 STADJUST(-amount, expdest);
7021 startp = (char *)stackblock() + startloc;
7024 rmescend = (char *)stackblock() + strloc;
7025 //bb_error_msg("str7:'%s'", rmescend);
7027 //TODO: how to handle slash_pos here if string changes (shortens?)
7028 rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW, NULL);
7029 if (rmesc != startp) {
7031 startp = (char *)stackblock() + startloc;
7035 str = (char *)stackblock() + strloc;
7037 * Example: v='a\bc'; echo ${v/\\b/_\\_\z_}
7038 * The result is a_\_z_c (not a\_\_z_c)!
7040 * The search pattern and replace string treat backslashes differently!
7041 * "&slash_pos" causes rmescapes() to work differently on the pattern
7042 * and string. It's only used on the first call.
7044 //bb_error_msg("str8:'%s' slash_pos:%d", str, slash_pos);
7045 rmescapes(str, RMESCAPE_GLOB,
7046 repl ? NULL : (slash_pos < 0 ? NULL : &slash_pos)
7049 #if BASH_PATTERN_SUBST
7050 workloc = expdest - (char *)stackblock();
7051 if (subtype == VSREPLACE || subtype == VSREPLACEALL) {
7056 //bb_error_msg("str9:'%s' slash_pos:%d", str, slash_pos);
7058 if (slash_pos >= 0) {
7059 repl = str + slash_pos;
7063 //bb_error_msg("str:'%s' repl:'%s'", str, repl);
7065 /* If there's no pattern to match, return the expansion unmolested */
7074 loc = scanright(idx, rmesc, rmescend, str, quotes, 1);
7075 //bb_error_msg("scanright('%s'):'%s'", str, loc);
7077 /* No match, advance */
7078 char *restart_detect = stackblock();
7080 STPUTC(*idx, expdest);
7081 if (quotes && (unsigned char)*idx == CTLESC) {
7084 STPUTC(*idx, expdest);
7086 if (stackblock() != restart_detect)
7091 /* continue; - prone to quadratic behavior, smarter code: */
7094 if (str[0] == '*') {
7095 /* Pattern is "*foo". If "*foo" does not match "long_string",
7096 * it would never match "ong_string" etc, no point in trying.
7103 if (subtype == VSREPLACEALL) {
7105 if (quotes && (unsigned char)*idx == CTLESC)
7114 //bb_error_msg("repl:'%s'", repl);
7115 for (loc = (char*)repl; *loc; loc++) {
7116 char *restart_detect = stackblock();
7117 if (quotes && *loc == '\\') {
7118 STPUTC(CTLESC, expdest);
7121 STPUTC(*loc, expdest);
7122 if (stackblock() != restart_detect)
7127 if (subtype == VSREPLACE) {
7128 //bb_error_msg("tail:'%s', quotes:%x", idx, quotes);
7130 char *restart_detect = stackblock();
7131 STPUTC(*idx, expdest);
7132 if (stackblock() != restart_detect)
7141 /* We've put the replaced text into a buffer at workloc, now
7142 * move it to the right place and adjust the stack.
7144 STPUTC('\0', expdest);
7145 startp = (char *)stackblock() + startloc;
7146 memmove(startp, (char *)stackblock() + workloc, len + 1);
7147 //bb_error_msg("startp:'%s'", startp);
7148 amount = expdest - (startp + len);
7149 STADJUST(-amount, expdest);
7152 #endif /* BASH_PATTERN_SUBST */
7154 subtype -= VSTRIMRIGHT;
7156 if (subtype < 0 || subtype > 7)
7159 /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */
7160 zero = subtype >> 1;
7161 /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */
7162 scan = (subtype & 1) ^ zero ? scanleft : scanright;
7164 loc = scan(startp, rmesc, rmescend, str, quotes, zero);
7167 memmove(startp, loc, str - loc);
7168 loc = startp + (str - loc) - 1;
7171 amount = loc - expdest;
7172 STADJUST(amount, expdest);
7178 * Add the value of a specialized variable to the stack string.
7179 * name parameter (examples):
7180 * ash -c 'echo $1' name:'1='
7181 * ash -c 'echo $qwe' name:'qwe='
7182 * ash -c 'echo $$' name:'$='
7183 * ash -c 'echo ${$}' name:'$='
7184 * ash -c 'echo ${$##q}' name:'$=q'
7185 * ash -c 'echo ${#$}' name:'$='
7186 * note: examples with bad shell syntax:
7187 * ash -c 'echo ${#$1}' name:'$=1'
7188 * ash -c 'echo ${#1#}' name:'1=#'
7190 static NOINLINE ssize_t
7191 varvalue(char *name, int varflags, int flags, int quoted)
7198 int subtype = varflags & VSTYPE;
7199 int discard = subtype == VSPLUS || subtype == VSLENGTH;
7200 int quotes = (discard ? 0 : (flags & QUOTES_ESC)) | QUOTES_KEEPNUL;
7203 sep = (flags & EXP_FULL) << CHAR_BIT;
7204 syntax = quoted ? DQSYNTAX : BASESYNTAX;
7214 num = shellparam.nparam;
7222 goto check_1char_name;
7224 expdest = makestrspace(NOPTS, expdest);
7225 for (i = NOPTS - 1; i >= 0; i--) {
7226 if (optlist[i] && optletters(i)) {
7227 USTPUTC(optletters(i), expdest);
7233 /* handles cases similar to ${#$1} */
7234 if (name[2] != '\0')
7235 raise_error_syntax("bad substitution");
7247 /* We will set c to 0 or ~0 depending on whether
7248 * we're doing field splitting. We won't do field
7249 * splitting if either we're quoted or sep is zero.
7251 * Instead of testing (quoted || !sep) the following
7252 * trick optimises away any branches by using the
7253 * fact that EXP_QUOTED (which is the only bit that
7254 * can be set in quoted) is the same as EXP_FULL <<
7255 * CHAR_BIT (which is the only bit that can be set
7258 #if EXP_QUOTED >> CHAR_BIT != EXP_FULL
7259 #error The following two lines expect EXP_QUOTED == EXP_FULL << CHAR_BIT
7261 c = !((quoted | ~sep) & EXP_QUOTED) - 1;
7263 sep |= ifsset() ? (unsigned char)(c & ifsval()[0]) : ' ';
7269 while ((p = *ap++) != NULL) {
7270 len += strtodest(p, syntax, quotes);
7274 memtodest(&sepc, 1, syntax, quotes);
7289 num = atoi(name); /* number(name) fails on ${N#str} etc */
7290 if (num < 0 || num > shellparam.nparam)
7292 p = num ? shellparam.p[num - 1] : arg0;
7295 /* NB: name has form "VAR=..." */
7296 p = lookupvar(name);
7301 len = strtodest(p, syntax, quotes);
7302 #if ENABLE_UNICODE_SUPPORT
7303 if (subtype == VSLENGTH && len > 0) {
7304 reinit_unicode_for_ash();
7305 if (unicode_status == UNICODE_ON) {
7306 STADJUST(-len, expdest);
7308 len = unicode_strlen(p);
7316 STADJUST(-len, expdest);
7321 * Expand a variable, and return a pointer to the next character in the
7325 evalvar(char *p, int flag)
7335 varflags = (unsigned char) *p++;
7336 subtype = varflags & VSTYPE;
7339 raise_error_syntax("bad substitution");
7341 quoted = flag & EXP_QUOTED;
7343 startloc = expdest - (char *)stackblock();
7344 p = strchr(p, '=') + 1; //TODO: use var_end(p)?
7347 varlen = varvalue(var, varflags, flag, quoted);
7348 if (varflags & VSNUL)
7351 if (subtype == VSPLUS) {
7352 varlen = -1 - varlen;
7356 if (subtype == VSMINUS) {
7361 flag | EXP_TILDE | EXP_WORD
7368 if (subtype == VSASSIGN || subtype == VSQUESTION) {
7372 subevalvar(p, var, 0, subtype, startloc, varflags,
7373 flag & ~QUOTES_ESC);
7376 * Remove any recorded regions beyond
7379 removerecordregions(startloc);
7383 if (varlen < 0 && uflag)
7384 varunset(p, var, 0, 0);
7386 if (subtype == VSLENGTH) {
7387 cvtnum(varlen > 0 ? varlen : 0);
7391 if (subtype == VSNORMAL) {
7394 quoted = *var == '@' && shellparam.nparam;
7398 recordregion(startloc, expdest - (char *)stackblock(), quoted);
7407 case VSTRIMRIGHTMAX:
7411 #if BASH_PATTERN_SUBST
7423 * Terminate the string and start recording the pattern
7426 STPUTC('\0', expdest);
7427 patloc = expdest - (char *)stackblock();
7428 if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype,
7429 startloc, varflags, flag)) {
7430 int amount = expdest - (
7431 (char *)stackblock() + patloc - 1
7433 STADJUST(-amount, expdest);
7435 /* Remove any recorded regions beyond start of variable */
7436 removerecordregions(startloc);
7441 if (subtype != VSNORMAL) { /* skip to end of alternative */
7444 unsigned char c = *p++;
7447 else if (c == CTLBACKQ) {
7449 argbackq = argbackq->next;
7450 } else if (c == CTLVAR) {
7451 if ((*p++ & VSTYPE) != VSNORMAL)
7453 } else if (c == CTLENDVAR) {
7463 * Add a file name to the list.
7466 addfname(const char *name)
7470 sp = stzalloc(sizeof(*sp));
7471 sp->text = sstrdup(name);
7473 exparg.lastp = &sp->next;
7476 /* Avoid glob() (and thus, stat() et al) for words like "echo" */
7478 hasmeta(const char *p)
7480 static const char chars[] ALIGN1 = {
7481 '*', '?', '[', '\\', CTLQUOTEMARK, CTLESC, 0
7485 p = strpbrk(p, chars);
7488 switch ((unsigned char)*p) {
7492 if ((unsigned char)*p == CTLQUOTEMARK)
7494 if ((unsigned char)*p == CTLESC)
7496 if (*p == '\0') /* huh? */
7507 if (!strchr(p + 1, ']')) {
7508 /* It's not a properly closed [] pattern,
7509 * but other metas may follow. Continue checking.
7510 * my[file* _is_ globbed by bash
7511 * and matches filenames like "my[file1".
7527 /* If we want to use glob() from libc... */
7528 #if !ENABLE_ASH_INTERNAL_GLOB
7530 /* Add the result of glob() to the list */
7532 addglob(const glob_t *pglob)
7534 char **p = pglob->gl_pathv;
7541 expandmeta(struct strlist *str /*, int flag*/)
7543 /* TODO - EXP_REDIR */
7553 if (!hasmeta(str->text))
7557 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7558 // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match
7559 // GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?)
7561 // glibc 2.24.90 glob(GLOB_NOMAGIC) does not remove backslashes used for escaping:
7562 // if you pass it "file\?", it returns "file\?", not "file?", if no match.
7563 // Which means you need to unescape the string, right? Not so fast:
7564 // if there _is_ a file named "file\?" (with backslash), it is returned
7565 // as "file\?" too (whichever pattern you used to find it, say, "file*").
7566 // You DON'T KNOW by looking at the result whether you need to unescape it.
7568 // Worse, globbing of "file\?" in a directory with two files, "file?" and "file\?",
7569 // returns "file\?" - which is WRONG: "file\?" pattern matches "file?" file.
7570 // Without GLOB_NOMAGIC, this works correctly ("file?" is returned as a match).
7571 // With GLOB_NOMAGIC | GLOB_NOCHECK, this also works correctly.
7572 // i = glob(p, GLOB_NOMAGIC | GLOB_NOCHECK, NULL, &pglob);
7573 // i = glob(p, GLOB_NOMAGIC, NULL, &pglob);
7574 i = glob(p, 0, NULL, &pglob);
7575 //bb_error_msg("glob('%s'):%d '%s'...", p, i, pglob.gl_pathv ? pglob.gl_pathv[0] : "-");
7580 #if 0 // glibc 2.24.90 bug? Patterns like "*/file", when match, don't set GLOB_MAGCHAR
7581 /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */
7582 if (!(pglob.gl_flags & GLOB_MAGCHAR))
7594 *exparg.lastp = str;
7595 rmescapes(str->text, 0, NULL);
7596 exparg.lastp = &str->next;
7598 default: /* GLOB_NOSPACE */
7601 ash_msg_and_raise_error(bb_msg_memory_exhausted);
7608 /* ENABLE_ASH_INTERNAL_GLOB: Homegrown globbing code. (dash also has both, uses homegrown one.) */
7611 * Do metacharacter (i.e. *, ?, [...]) expansion.
7613 typedef struct exp_t {
7618 expmeta(exp_t *exp, char *name, unsigned name_len, unsigned expdir_len)
7620 #define expdir exp->dir
7621 #define expdir_max exp->dir_max
7622 char *enddir = expdir + expdir_len;
7637 for (p = name; esc = 0, *p; p += esc + 1) {
7638 if (*p == '*' || *p == '?')
7640 else if (*p == '[') {
7647 if (*q == '/' || *q == '\0')
7655 if (*p == '\\' && p[1])
7657 if (p[esc] == '/') {
7660 start = p + esc + 1;
7664 if (metaflag == 0) { /* we've reached the end of the file name */
7669 if (*p == '\\' && p[1])
7673 if (lstat(expdir, &statb) == 0)
7681 if (*p == '\\' && p[1])
7684 } while (p < start);
7688 expdir_len = enddir - cp;
7694 if (*endname == 0) {
7701 name_len -= endname - name;
7708 while (!pending_int && (dp = readdir(dirp)) != NULL) {
7709 if (dp->d_name[0] == '.' && !matchdot)
7711 if (pmatch(start, dp->d_name)) {
7713 strcpy(enddir, dp->d_name);
7719 p = stpcpy(enddir, dp->d_name);
7722 offset = p - expdir + 1;
7723 len = offset + name_len + NAME_MAX;
7724 if (len > expdir_max) {
7726 expdir = ckrealloc(expdir, len);
7730 expmeta(exp, endname, name_len, offset);
7731 enddir = expdir + expdir_len;
7737 endname[-esc - 1] = esc ? '\\' : '/';
7742 static struct strlist *
7743 msort(struct strlist *list, int len)
7745 struct strlist *p, *q = NULL;
7746 struct strlist **lpp;
7754 for (n = half; --n >= 0;) {
7758 q->next = NULL; /* terminate first half of list */
7759 q = msort(list, half); /* sort first half of list */
7760 p = msort(p, len - half); /* sort second half */
7763 #if ENABLE_LOCALE_SUPPORT
7764 if (strcoll(p->text, q->text) < 0)
7766 if (strcmp(p->text, q->text) < 0)
7790 * Sort the results of file name expansion. It calculates the number of
7791 * strings to sort and then calls msort (short for merge sort) to do the
7794 static struct strlist *
7795 expsort(struct strlist *str)
7801 for (sp = str; sp; sp = sp->next)
7803 return msort(str, len);
7807 expandmeta(struct strlist *str /*, int flag*/)
7809 /* TODO - EXP_REDIR */
7813 struct strlist **savelastp;
7820 if (!hasmeta(str->text))
7822 savelastp = exparg.lastp;
7825 p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
7827 exp.dir_max = len + PATH_MAX;
7828 exp.dir = ckmalloc(exp.dir_max);
7830 expmeta(&exp, p, len, 0);
7835 if (exparg.lastp == savelastp) {
7840 *exparg.lastp = str;
7841 rmescapes(str->text, 0, NULL);
7842 exparg.lastp = &str->next;
7844 *exparg.lastp = NULL;
7845 *savelastp = sp = expsort(*savelastp);
7846 while (sp->next != NULL)
7848 exparg.lastp = &sp->next;
7853 #endif /* ENABLE_ASH_INTERNAL_GLOB */
7856 * Perform variable substitution and command substitution on an argument,
7857 * placing the resulting list of arguments in arglist. If EXP_FULL is true,
7858 * perform splitting and file name expansion. When arglist is NULL, perform
7859 * here document expansion.
7862 expandarg(union node *arg, struct arglist *arglist, int flag)
7867 argbackq = arg->narg.backquote;
7868 STARTSTACKSTR(expdest);
7869 TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag));
7870 argstr(arg->narg.text, flag);
7871 p = _STPUTC('\0', expdest);
7873 if (arglist == NULL) {
7874 /* here document expanded */
7877 p = grabstackstr(p);
7878 TRACE(("expandarg: p:'%s'\n", p));
7879 exparg.lastp = &exparg.list;
7883 if (flag & EXP_FULL) {
7884 ifsbreakup(p, &exparg);
7885 *exparg.lastp = NULL;
7886 exparg.lastp = &exparg.list;
7887 expandmeta(exparg.list /*, flag*/);
7889 sp = stzalloc(sizeof(*sp));
7892 exparg.lastp = &sp->next;
7894 *exparg.lastp = NULL;
7896 *arglist->lastp = exparg.list;
7897 arglist->lastp = exparg.lastp;
7905 * Expand shell variables and backquotes inside a here document.
7908 expandhere(union node *arg, int fd)
7910 expandarg(arg, (struct arglist *)NULL, EXP_QUOTED);
7911 full_write(fd, stackblock(), expdest - (char *)stackblock());
7915 * Returns true if the pattern matches the string.
7918 patmatch(char *pattern, const char *string)
7920 char *p = preglob(pattern, 0);
7921 int r = pmatch(p, string);
7922 //bb_error_msg("!fnmatch(pattern:'%s',str:'%s',0):%d", p, string, r);
7927 * See if a pattern matches in a case statement.
7930 casematch(union node *pattern, char *val)
7932 struct stackmark smark;
7935 setstackmark(&smark);
7936 argbackq = pattern->narg.backquote;
7937 STARTSTACKSTR(expdest);
7938 argstr(pattern->narg.text, EXP_TILDE | EXP_CASE);
7939 STACKSTRNUL(expdest);
7941 result = patmatch(stackblock(), val);
7942 popstackmark(&smark);
7947 /* ============ find_command */
7951 int (*builtin)(int, char **) FAST_FUNC;
7952 /* unsigned flags; */
7954 #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1)
7955 /* "regular" builtins always take precedence over commands,
7956 * regardless of PATH=....%builtin... position */
7957 #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2)
7958 #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4)
7961 smallint cmdtype; /* CMDxxx */
7964 /* index >= 0 for commands without path (slashes) */
7965 /* (TODO: what exactly does the value mean? PATH position?) */
7966 /* index == -1 for commands with slashes */
7967 /* index == (-2 - applet_no) for NOFORK applets */
7968 const struct builtincmd *cmd;
7969 struct funcnode *func;
7972 /* values of cmdtype */
7973 #define CMDUNKNOWN -1 /* no entry in table for command */
7974 #define CMDNORMAL 0 /* command is an executable program */
7975 #define CMDFUNCTION 1 /* command is a shell function */
7976 #define CMDBUILTIN 2 /* command is a shell builtin */
7978 /* action to find_command() */
7979 #define DO_ERR 0x01 /* prints errors */
7980 #define DO_ABS 0x02 /* checks absolute paths */
7981 #define DO_NOFUNC 0x04 /* don't return shell functions, for command */
7982 #define DO_ALTPATH 0x08 /* using alternate path */
7983 #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */
7985 static void find_command(char *, struct cmdentry *, int, const char *);
7988 /* ============ Hashing commands */
7991 * When commands are first encountered, they are entered in a hash table.
7992 * This ensures that a full path search will not have to be done for them
7993 * on each invocation.
7995 * We should investigate converting to a linear search, even though that
7996 * would make the command name "hash" a misnomer.
8000 struct tblentry *next; /* next entry in hash chain */
8001 union param param; /* definition of builtin function */
8002 smallint cmdtype; /* CMDxxx */
8003 char rehash; /* if set, cd done since entry created */
8004 char cmdname[1]; /* name of command */
8007 static struct tblentry **cmdtable;
8008 #define INIT_G_cmdtable() do { \
8009 cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \
8012 static int builtinloc = -1; /* index in path of %builtin, or -1 */
8016 tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, char **envp)
8018 #if ENABLE_FEATURE_SH_STANDALONE
8019 if (applet_no >= 0) {
8020 if (APPLET_IS_NOEXEC(applet_no)) {
8024 popredir(/*drop:*/ 1);
8025 run_noexec_applet_and_exit(applet_no, cmd, argv);
8027 /* re-exec ourselves with the new arguments */
8028 execve(bb_busybox_exec_path, argv, envp);
8029 /* If they called chroot or otherwise made the binary no longer
8030 * executable, fall through */
8037 execve(cmd, argv, envp);
8038 } while (errno == EINTR);
8040 execve(cmd, argv, envp);
8042 if (cmd != bb_busybox_exec_path && errno == ENOEXEC) {
8043 /* Run "cmd" as a shell script:
8044 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
8045 * "If the execve() function fails with ENOEXEC, the shell
8046 * shall execute a command equivalent to having a shell invoked
8047 * with the command name as its first operand,
8048 * with any remaining arguments passed to the new shell"
8050 * That is, do not use $SHELL, user's shell, or /bin/sh;
8051 * just call ourselves.
8053 * Note that bash reads ~80 chars of the file, and if it sees
8054 * a zero byte before it sees newline, it doesn't try to
8055 * interpret it, but fails with "cannot execute binary file"
8056 * message and exit code 126. For one, this prevents attempts
8057 * to interpret foreign ELF binaries as shell scripts.
8059 argv[0] = (char*) cmd;
8060 cmd = bb_busybox_exec_path;
8061 /* NB: this is only possible because all callers of shellexec()
8062 * ensure that the argv[-1] slot exists!
8065 argv[0] = (char*) "ash";
8071 * Exec a program. Never returns. If you change this routine, you may
8072 * have to change the find_command routine as well.
8073 * argv[-1] must exist and be writable! See tryexec() for why.
8075 static void shellexec(char *prog, char **argv, const char *path, int idx) NORETURN;
8076 static void shellexec(char *prog, char **argv, const char *path, int idx)
8082 int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */
8084 envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL);
8085 if (strchr(prog, '/') != NULL
8086 #if ENABLE_FEATURE_SH_STANDALONE
8087 || (applet_no = find_applet_by_name(prog)) >= 0
8090 tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp);
8091 if (applet_no >= 0) {
8092 /* We tried execing ourself, but it didn't work.
8093 * Maybe /proc/self/exe doesn't exist?
8102 while ((cmdname = path_advance(&path, prog)) != NULL) {
8103 if (--idx < 0 && pathopt == NULL) {
8104 tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp);
8105 if (errno != ENOENT && errno != ENOTDIR)
8112 /* Map to POSIX errors */
8124 exitstatus = exerrno;
8125 TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n",
8126 prog, e, suppress_int));
8127 ash_msg_and_raise(EXEXIT, "%s: %s", prog, errmsg(e, "not found"));
8132 printentry(struct tblentry *cmdp)
8138 idx = cmdp->param.index;
8141 name = path_advance(&path, cmdp->cmdname);
8143 } while (--idx >= 0);
8144 out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr));
8148 * Clear out command entries. The argument specifies the first entry in
8149 * PATH which has changed.
8152 clearcmdentry(int firstchange)
8154 struct tblentry **tblp;
8155 struct tblentry **pp;
8156 struct tblentry *cmdp;
8159 for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) {
8161 while ((cmdp = *pp) != NULL) {
8162 if ((cmdp->cmdtype == CMDNORMAL &&
8163 cmdp->param.index >= firstchange)
8164 || (cmdp->cmdtype == CMDBUILTIN &&
8165 builtinloc >= firstchange)
8178 * Locate a command in the command hash table. If "add" is nonzero,
8179 * add the command to the table if it is not already present. The
8180 * variable "lastcmdentry" is set to point to the address of the link
8181 * pointing to the entry, so that delete_cmd_entry can delete the
8184 * Interrupts must be off if called with add != 0.
8186 static struct tblentry **lastcmdentry;
8188 static struct tblentry *
8189 cmdlookup(const char *name, int add)
8191 unsigned int hashval;
8193 struct tblentry *cmdp;
8194 struct tblentry **pp;
8197 hashval = (unsigned char)*p << 4;
8199 hashval += (unsigned char)*p++;
8201 pp = &cmdtable[hashval % CMDTABLESIZE];
8202 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8203 if (strcmp(cmdp->cmdname, name) == 0)
8207 if (add && cmdp == NULL) {
8208 cmdp = *pp = ckzalloc(sizeof(struct tblentry)
8210 /* + 1 - already done because
8211 * tblentry::cmdname is char[1] */);
8212 /*cmdp->next = NULL; - ckzalloc did it */
8213 cmdp->cmdtype = CMDUNKNOWN;
8214 strcpy(cmdp->cmdname, name);
8221 * Delete the command entry returned on the last lookup.
8224 delete_cmd_entry(void)
8226 struct tblentry *cmdp;
8229 cmdp = *lastcmdentry;
8230 *lastcmdentry = cmdp->next;
8231 if (cmdp->cmdtype == CMDFUNCTION)
8232 freefunc(cmdp->param.func);
8238 * Add a new command entry, replacing any existing command entry for
8239 * the same name - except special builtins.
8242 addcmdentry(char *name, struct cmdentry *entry)
8244 struct tblentry *cmdp;
8246 cmdp = cmdlookup(name, 1);
8247 if (cmdp->cmdtype == CMDFUNCTION) {
8248 freefunc(cmdp->param.func);
8250 cmdp->cmdtype = entry->cmdtype;
8251 cmdp->param = entry->u;
8255 static int FAST_FUNC
8256 hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8258 struct tblentry **pp;
8259 struct tblentry *cmdp;
8261 struct cmdentry entry;
8264 if (nextopt("r") != '\0') {
8269 if (*argptr == NULL) {
8270 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8271 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8272 if (cmdp->cmdtype == CMDNORMAL)
8280 while ((name = *argptr) != NULL) {
8281 cmdp = cmdlookup(name, 0);
8283 && (cmdp->cmdtype == CMDNORMAL
8284 || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0))
8288 find_command(name, &entry, DO_ERR, pathval());
8289 if (entry.cmdtype == CMDUNKNOWN)
8297 * Called when a cd is done. Marks all commands so the next time they
8298 * are executed they will be rehashed.
8303 struct tblentry **pp;
8304 struct tblentry *cmdp;
8306 for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) {
8307 for (cmdp = *pp; cmdp; cmdp = cmdp->next) {
8308 if (cmdp->cmdtype == CMDNORMAL
8309 || (cmdp->cmdtype == CMDBUILTIN
8310 && !IS_BUILTIN_REGULAR(cmdp->param.cmd)
8320 * Fix command hash table when PATH changed.
8321 * Called before PATH is changed. The argument is the new value of PATH;
8322 * pathval() still returns the old value at this point.
8323 * Called with interrupts off.
8325 static void FAST_FUNC
8326 changepath(const char *new)
8334 firstchange = 9999; /* assume no change */
8340 if ((*old == '\0' && *new == ':')
8341 || (*old == ':' && *new == '\0')
8345 old = new; /* ignore subsequent differences */
8349 if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin"))
8356 if (builtinloc < 0 && idx_bltin >= 0)
8357 builtinloc = idx_bltin; /* zap builtins */
8358 if (builtinloc >= 0 && idx_bltin < 0)
8360 clearcmdentry(firstchange);
8361 builtinloc = idx_bltin;
8397 typedef smallint token_id_t;
8399 /* Nth bit indicates if token marks the end of a list */
8402 /* 0 */ | (1u << TEOF)
8403 /* 1 */ | (0u << TNL)
8404 /* 2 */ | (0u << TREDIR)
8405 /* 3 */ | (0u << TWORD)
8406 /* 4 */ | (0u << TSEMI)
8407 /* 5 */ | (0u << TBACKGND)
8408 /* 6 */ | (0u << TAND)
8409 /* 7 */ | (0u << TOR)
8410 /* 8 */ | (0u << TPIPE)
8411 /* 9 */ | (0u << TLP)
8412 /* 10 */ | (1u << TRP)
8413 /* 11 */ | (1u << TENDCASE)
8414 /* 12 */ | (1u << TENDBQUOTE)
8415 /* 13 */ | (0u << TNOT)
8416 /* 14 */ | (0u << TCASE)
8417 /* 15 */ | (1u << TDO)
8418 /* 16 */ | (1u << TDONE)
8419 /* 17 */ | (1u << TELIF)
8420 /* 18 */ | (1u << TELSE)
8421 /* 19 */ | (1u << TESAC)
8422 /* 20 */ | (1u << TFI)
8423 /* 21 */ | (0u << TFOR)
8425 /* 22 */ | (0u << TFUNCTION)
8427 /* 23 */ | (0u << TIF)
8428 /* 24 */ | (0u << TIN)
8429 /* 25 */ | (1u << TTHEN)
8430 /* 26 */ | (0u << TUNTIL)
8431 /* 27 */ | (0u << TWHILE)
8432 /* 28 */ | (0u << TBEGIN)
8433 /* 29 */ | (1u << TEND)
8434 , /* thus far 29 bits used */
8437 static const char *const tokname_array[] = {
8451 #define KWDOFFSET 13
8452 /* the following are keywords */
8474 /* Wrapper around strcmp for qsort/bsearch/... */
8476 pstrcmp(const void *a, const void *b)
8478 return strcmp((char*)a, *(char**)b);
8481 static const char *const *
8482 findkwd(const char *s)
8484 return bsearch(s, tokname_array + KWDOFFSET,
8485 ARRAY_SIZE(tokname_array) - KWDOFFSET,
8486 sizeof(tokname_array[0]), pstrcmp);
8490 * Locate and print what a word is...
8493 describe_command(char *command, const char *path, int describe_command_verbose)
8495 struct cmdentry entry;
8496 #if ENABLE_ASH_ALIAS
8497 const struct alias *ap;
8500 path = path ? path : pathval();
8502 if (describe_command_verbose) {
8506 /* First look at the keywords */
8507 if (findkwd(command)) {
8508 out1str(describe_command_verbose ? " is a shell keyword" : command);
8512 #if ENABLE_ASH_ALIAS
8513 /* Then look at the aliases */
8514 ap = lookupalias(command, 0);
8516 if (!describe_command_verbose) {
8521 out1fmt(" is an alias for %s", ap->val);
8526 find_command(command, &entry, DO_ABS, path);
8528 switch (entry.cmdtype) {
8530 int j = entry.u.index;
8536 p = path_advance(&path, command);
8540 if (describe_command_verbose) {
8541 out1fmt(" is %s", p);
8549 if (describe_command_verbose) {
8550 /*out1str(" is a shell function");*/
8551 out1str(" is a function"); /* bash says this */
8558 if (describe_command_verbose) {
8559 out1fmt(" is a %sshell builtin",
8560 IS_BUILTIN_SPECIAL(entry.u.cmd) ?
8561 "special " : nullstr
8569 if (describe_command_verbose) {
8570 out1str(": not found\n");
8579 static int FAST_FUNC
8580 typecmd(int argc UNUSED_PARAM, char **argv)
8586 /* type -p ... ? (we don't bother checking for 'p') */
8587 if (argv[1] && argv[1][0] == '-') {
8592 err |= describe_command(argv[i++], NULL, verbose);
8597 #if ENABLE_ASH_CMDCMD
8598 /* Is it "command [-p] PROG ARGS" bltin, no other opts? Return ptr to "PROG" if yes */
8600 parse_command_args(char **argv, const char **path)
8613 if (c == '-' && !*cp) {
8621 *path = bb_default_path;
8624 /* run 'typecmd' for other options */
8633 static int FAST_FUNC
8634 commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
8642 const char *path = NULL;
8644 /* "command [-p] PROG ARGS" (that is, without -V or -v)
8645 * never reaches this function.
8648 while ((c = nextopt("pvV")) != '\0')
8650 verify |= VERIFY_VERBOSE;
8652 /*verify |= VERIFY_BRIEF*/;
8658 path = bb_default_path;
8660 /* Mimic bash: just "command -v" doesn't complain, it's a nop */
8662 if (/*verify && */ cmd)
8663 return describe_command(cmd, path, verify /* - VERIFY_BRIEF*/);
8670 /*static int funcblocksize; // size of structures in function */
8671 /*static int funcstringsize; // size of strings in node */
8672 static void *funcblock; /* block to allocate function from */
8673 static char *funcstring_end; /* end of block to allocate strings from */
8675 static const uint8_t nodesize[N_NUMBER] ALIGN1 = {
8676 [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)),
8677 [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)),
8678 [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)),
8679 [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)),
8680 [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)),
8681 [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)),
8682 [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)),
8683 [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)),
8684 [NIF ] = SHELL_ALIGN(sizeof(struct nif)),
8685 [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)),
8686 [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)),
8687 [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)),
8688 [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)),
8689 [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)),
8690 [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)),
8691 [NARG ] = SHELL_ALIGN(sizeof(struct narg)),
8692 [NTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8693 #if BASH_REDIR_OUTPUT
8694 [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)),
8696 [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)),
8697 [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)),
8698 [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)),
8699 [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)),
8700 [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8701 [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)),
8702 [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8703 [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)),
8704 [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)),
8707 static int calcsize(int funcblocksize, union node *n);
8710 sizenodelist(int funcblocksize, struct nodelist *lp)
8713 funcblocksize += SHELL_ALIGN(sizeof(struct nodelist));
8714 funcblocksize = calcsize(funcblocksize, lp->n);
8717 return funcblocksize;
8721 calcsize(int funcblocksize, union node *n)
8724 return funcblocksize;
8725 funcblocksize += nodesize[n->type];
8728 funcblocksize = calcsize(funcblocksize, n->ncmd.redirect);
8729 funcblocksize = calcsize(funcblocksize, n->ncmd.args);
8730 funcblocksize = calcsize(funcblocksize, n->ncmd.assign);
8733 funcblocksize = sizenodelist(funcblocksize, n->npipe.cmdlist);
8738 funcblocksize = calcsize(funcblocksize, n->nredir.redirect);
8739 funcblocksize = calcsize(funcblocksize, n->nredir.n);
8746 funcblocksize = calcsize(funcblocksize, n->nbinary.ch2);
8747 funcblocksize = calcsize(funcblocksize, n->nbinary.ch1);
8750 funcblocksize = calcsize(funcblocksize, n->nif.elsepart);
8751 funcblocksize = calcsize(funcblocksize, n->nif.ifpart);
8752 funcblocksize = calcsize(funcblocksize, n->nif.test);
8755 funcblocksize += SHELL_ALIGN(strlen(n->nfor.var) + 1); /* was funcstringsize += ... */
8756 funcblocksize = calcsize(funcblocksize, n->nfor.body);
8757 funcblocksize = calcsize(funcblocksize, n->nfor.args);
8760 funcblocksize = calcsize(funcblocksize, n->ncase.cases);
8761 funcblocksize = calcsize(funcblocksize, n->ncase.expr);
8764 funcblocksize = calcsize(funcblocksize, n->nclist.body);
8765 funcblocksize = calcsize(funcblocksize, n->nclist.pattern);
8766 funcblocksize = calcsize(funcblocksize, n->nclist.next);
8769 funcblocksize = calcsize(funcblocksize, n->ndefun.body);
8770 funcblocksize += SHELL_ALIGN(strlen(n->ndefun.text) + 1);
8773 funcblocksize = sizenodelist(funcblocksize, n->narg.backquote);
8774 funcblocksize += SHELL_ALIGN(strlen(n->narg.text) + 1); /* was funcstringsize += ... */
8775 funcblocksize = calcsize(funcblocksize, n->narg.next);
8778 #if BASH_REDIR_OUTPUT
8785 funcblocksize = calcsize(funcblocksize, n->nfile.fname);
8786 funcblocksize = calcsize(funcblocksize, n->nfile.next);
8790 funcblocksize = calcsize(funcblocksize, n->ndup.vname);
8791 funcblocksize = calcsize(funcblocksize, n->ndup.next);
8795 funcblocksize = calcsize(funcblocksize, n->nhere.doc);
8796 funcblocksize = calcsize(funcblocksize, n->nhere.next);
8799 funcblocksize = calcsize(funcblocksize, n->nnot.com);
8802 return funcblocksize;
8806 nodeckstrdup(char *s)
8808 funcstring_end -= SHELL_ALIGN(strlen(s) + 1);
8809 return strcpy(funcstring_end, s);
8812 static union node *copynode(union node *);
8814 static struct nodelist *
8815 copynodelist(struct nodelist *lp)
8817 struct nodelist *start;
8818 struct nodelist **lpp;
8823 funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist));
8824 (*lpp)->n = copynode(lp->n);
8826 lpp = &(*lpp)->next;
8833 copynode(union node *n)
8840 funcblock = (char *) funcblock + nodesize[n->type];
8844 new->ncmd.redirect = copynode(n->ncmd.redirect);
8845 new->ncmd.args = copynode(n->ncmd.args);
8846 new->ncmd.assign = copynode(n->ncmd.assign);
8847 new->ncmd.linno = n->ncmd.linno;
8850 new->npipe.cmdlist = copynodelist(n->npipe.cmdlist);
8851 new->npipe.pipe_backgnd = n->npipe.pipe_backgnd;
8856 new->nredir.redirect = copynode(n->nredir.redirect);
8857 new->nredir.n = copynode(n->nredir.n);
8858 new->nredir.linno = n->nredir.linno;
8865 new->nbinary.ch2 = copynode(n->nbinary.ch2);
8866 new->nbinary.ch1 = copynode(n->nbinary.ch1);
8869 new->nif.elsepart = copynode(n->nif.elsepart);
8870 new->nif.ifpart = copynode(n->nif.ifpart);
8871 new->nif.test = copynode(n->nif.test);
8874 new->nfor.var = nodeckstrdup(n->nfor.var);
8875 new->nfor.body = copynode(n->nfor.body);
8876 new->nfor.args = copynode(n->nfor.args);
8877 new->nfor.linno = n->nfor.linno;
8880 new->ncase.cases = copynode(n->ncase.cases);
8881 new->ncase.expr = copynode(n->ncase.expr);
8882 new->ncase.linno = n->ncase.linno;
8885 new->nclist.body = copynode(n->nclist.body);
8886 new->nclist.pattern = copynode(n->nclist.pattern);
8887 new->nclist.next = copynode(n->nclist.next);
8890 new->ndefun.body = copynode(n->ndefun.body);
8891 new->ndefun.text = nodeckstrdup(n->ndefun.text);
8892 new->ndefun.linno = n->ndefun.linno;
8895 new->narg.backquote = copynodelist(n->narg.backquote);
8896 new->narg.text = nodeckstrdup(n->narg.text);
8897 new->narg.next = copynode(n->narg.next);
8900 #if BASH_REDIR_OUTPUT
8907 new->nfile.fname = copynode(n->nfile.fname);
8908 new->nfile.fd = n->nfile.fd;
8909 new->nfile.next = copynode(n->nfile.next);
8913 new->ndup.vname = copynode(n->ndup.vname);
8914 new->ndup.dupfd = n->ndup.dupfd;
8915 new->ndup.fd = n->ndup.fd;
8916 new->ndup.next = copynode(n->ndup.next);
8920 new->nhere.doc = copynode(n->nhere.doc);
8921 new->nhere.fd = n->nhere.fd;
8922 new->nhere.next = copynode(n->nhere.next);
8925 new->nnot.com = copynode(n->nnot.com);
8928 new->type = n->type;
8933 * Make a copy of a parse tree.
8935 static struct funcnode *
8936 copyfunc(union node *n)
8941 /*funcstringsize = 0;*/
8942 blocksize = offsetof(struct funcnode, n) + calcsize(0, n);
8943 f = ckzalloc(blocksize /* + funcstringsize */);
8944 funcblock = (char *) f + offsetof(struct funcnode, n);
8945 funcstring_end = (char *) f + blocksize;
8947 /* f->count = 0; - ckzalloc did it */
8952 * Define a shell function.
8955 defun(union node *func)
8957 struct cmdentry entry;
8960 entry.cmdtype = CMDFUNCTION;
8961 entry.u.func = copyfunc(func);
8962 addcmdentry(func->ndefun.text, &entry);
8966 /* Reasons for skipping commands (see comment on breakcmd routine) */
8967 #define SKIPBREAK (1 << 0)
8968 #define SKIPCONT (1 << 1)
8969 #define SKIPFUNC (1 << 2)
8970 static smallint evalskip; /* set to SKIPxxx if we are skipping commands */
8971 static int skipcount; /* number of levels to skip */
8972 static int loopnest; /* current loop nesting level */
8973 static int funcline; /* starting line number of current function, or 0 if not in a function */
8975 /* Forward decl way out to parsing code - dotrap needs it */
8976 static int evalstring(char *s, int flags);
8978 /* Called to execute a trap.
8979 * Single callsite - at the end of evaltree().
8980 * If we return non-zero, evaltree raises EXEXIT exception.
8982 * Perhaps we should avoid entering new trap handlers
8983 * while we are executing a trap handler. [is it a TODO?]
8990 uint8_t last_status;
8995 last_status = exitstatus;
8999 TRACE(("dotrap entered\n"));
9000 for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) {
9012 /* non-trapped SIGINT is handled separately by raise_interrupt,
9013 * don't upset it by resetting gotsig[SIGINT-1] */
9014 if (sig == SIGINT && !p)
9017 TRACE(("sig %d is active, will run handler '%s'\n", sig, p));
9023 exitstatus = last_status;
9024 TRACE(("dotrap returns\n"));
9027 /* forward declarations - evaluation is fairly recursive business... */
9028 static int evalloop(union node *, int);
9029 static int evalfor(union node *, int);
9030 static int evalcase(union node *, int);
9031 static int evalsubshell(union node *, int);
9032 static void expredir(union node *);
9033 static int evalpipe(union node *, int);
9034 static int evalcommand(union node *, int);
9035 static int evalbltin(const struct builtincmd *, int, char **, int);
9036 static void prehash(union node *);
9039 * Evaluate a parse tree. The value is left in the global variable
9043 evaltree(union node *n, int flags)
9046 int (*evalfn)(union node *, int);
9050 TRACE(("evaltree(NULL) called\n"));
9053 TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags));
9060 out1fmt("Node type = %d\n", n->type);
9065 status = !evaltree(n->nnot.com, EV_TESTED);
9068 errlinno = lineno = n->nredir.linno;
9070 lineno -= funcline - 1;
9071 expredir(n->nredir.redirect);
9072 pushredir(n->nredir.redirect);
9073 status = redirectsafe(n->nredir.redirect, REDIR_PUSH);
9075 status = evaltree(n->nredir.n, flags & EV_TESTED);
9077 if (n->nredir.redirect)
9078 popredir(/*drop:*/ 0);
9081 evalfn = evalcommand;
9083 if (eflag && !(flags & EV_TESTED))
9095 evalfn = evalsubshell;
9108 #error NAND + 1 != NOR
9110 #if NOR + 1 != NSEMI
9111 #error NOR + 1 != NSEMI
9113 unsigned is_or = n->type - NAND;
9116 (flags | ((is_or >> 1) - 1)) & EV_TESTED
9118 if ((!status) == is_or || evalskip)
9124 status = evalfn(n, flags);
9128 status = evaltree(n->nif.test, EV_TESTED);
9135 if (n->nif.elsepart) {
9136 n = n->nif.elsepart;
9143 /* Not necessary. To test it:
9144 * "false; f() { qwerty; }; echo $?" should print 0.
9148 exitstatus = status;
9152 /* Order of checks below is important:
9153 * signal handlers trigger before exit caused by "set -e".
9157 if (checkexit & status)
9158 raise_exception(EXEXIT);
9159 if (flags & EV_EXIT)
9160 raise_exception(EXEXIT);
9162 TRACE(("leaving evaltree (no interrupts)\n"));
9169 int skip = evalskip;
9176 if (--skipcount <= 0) {
9187 evalloop(union node *n, int flags)
9198 i = evaltree(n->nbinary.ch1, EV_TESTED);
9200 if (skip == SKIPFUNC)
9204 if (n->type != NWHILE)
9208 status = evaltree(n->nbinary.ch2, flags);
9210 } while (!(skip & ~SKIPCONT));
9217 evalfor(union node *n, int flags)
9219 struct arglist arglist;
9222 struct stackmark smark;
9225 errlinno = lineno = n->ncase.linno;
9227 lineno -= funcline - 1;
9229 setstackmark(&smark);
9230 arglist.list = NULL;
9231 arglist.lastp = &arglist.list;
9232 for (argp = n->nfor.args; argp; argp = argp->narg.next) {
9233 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
9235 *arglist.lastp = NULL;
9239 for (sp = arglist.list; sp; sp = sp->next) {
9240 setvar0(n->nfor.var, sp->text);
9241 status = evaltree(n->nfor.body, flags);
9242 if (skiploop() & ~SKIPCONT)
9246 popstackmark(&smark);
9252 evalcase(union node *n, int flags)
9256 struct arglist arglist;
9257 struct stackmark smark;
9260 errlinno = lineno = n->ncase.linno;
9262 lineno -= funcline - 1;
9264 setstackmark(&smark);
9265 arglist.list = NULL;
9266 arglist.lastp = &arglist.list;
9267 expandarg(n->ncase.expr, &arglist, EXP_TILDE);
9268 for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
9269 for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
9270 if (casematch(patp, arglist.list->text)) {
9271 /* Ensure body is non-empty as otherwise
9272 * EV_EXIT may prevent us from setting the
9275 if (evalskip == 0 && cp->nclist.body) {
9276 status = evaltree(cp->nclist.body, flags);
9283 popstackmark(&smark);
9289 * Kick off a subshell to evaluate a tree.
9292 evalsubshell(union node *n, int flags)
9295 int backgnd = (n->type == NBACKGND); /* FORK_BG(1) if yes, else FORK_FG(0) */
9298 errlinno = lineno = n->nredir.linno;
9300 lineno -= funcline - 1;
9302 expredir(n->nredir.redirect);
9303 if (!backgnd && (flags & EV_EXIT) && !may_have_traps)
9306 if (backgnd == FORK_FG)
9308 jp = makejob(/*n,*/ 1);
9309 if (forkshell(jp, n, backgnd) == 0) {
9314 flags &= ~EV_TESTED;
9316 redirect(n->nredir.redirect, 0);
9317 evaltreenr(n->nredir.n, flags);
9322 if (backgnd == FORK_FG)
9323 status = waitforjob(jp);
9329 * Compute the names of the files in a redirection list.
9331 static void fixredir(union node *, const char *, int);
9333 expredir(union node *n)
9337 for (redir = n; redir; redir = redir->nfile.next) {
9341 fn.lastp = &fn.list;
9342 switch (redir->type) {
9346 #if BASH_REDIR_OUTPUT
9351 expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR);
9352 TRACE(("expredir expanded to '%s'\n", fn.list->text));
9353 #if BASH_REDIR_OUTPUT
9357 // By the design of stack allocator, the loop of this kind:
9358 // while true; do while true; do break; done </dev/null; done
9359 // will look like a memory leak: ash plans to free expfname's
9360 // of "/dev/null" as soon as it finishes running the loop
9361 // (in this case, never).
9362 // This "fix" is wrong:
9363 if (redir->nfile.expfname)
9364 stunalloc(redir->nfile.expfname);
9365 // It results in corrupted state of stacked allocations.
9367 redir->nfile.expfname = fn.list->text;
9370 case NTOFD: /* >& */
9371 if (redir->ndup.vname) {
9372 expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE);
9373 if (fn.list == NULL)
9374 ash_msg_and_raise_error("redir error");
9375 #if BASH_REDIR_OUTPUT
9376 //FIXME: we used expandarg with different args!
9377 if (!isdigit_str9(fn.list->text)) {
9378 /* >&file, not >&fd */
9379 if (redir->nfile.fd != 1) /* 123>&file - BAD */
9380 ash_msg_and_raise_error("redir error");
9382 goto store_expfname;
9385 fixredir(redir, fn.list->text, 1);
9393 * Evaluate a pipeline. All the processes in the pipeline are children
9394 * of the process creating the pipeline. (This differs from some versions
9395 * of the shell, which make the last process in a pipeline the parent
9399 evalpipe(union node *n, int flags)
9402 struct nodelist *lp;
9408 TRACE(("evalpipe(0x%lx) called\n", (long)n));
9410 for (lp = n->npipe.cmdlist; lp; lp = lp->next)
9414 if (n->npipe.pipe_backgnd == 0)
9416 jp = makejob(/*n,*/ pipelen);
9418 for (lp = n->npipe.cmdlist; lp; lp = lp->next) {
9422 if (pipe(pip) < 0) {
9424 ash_msg_and_raise_perror("can't create pipe");
9427 if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) {
9441 evaltreenr(lp->n, flags);
9448 /* Don't want to trigger debugging */
9452 if (n->npipe.pipe_backgnd == 0) {
9453 status = waitforjob(jp);
9454 TRACE(("evalpipe: job done exit status %d\n", status));
9462 * Controls whether the shell is interactive or not.
9465 setinteractive(int on)
9467 static smallint is_interactive;
9469 if (++on == is_interactive)
9471 is_interactive = on;
9475 #if !ENABLE_FEATURE_SH_EXTRA_QUIET
9476 if (is_interactive > 1) {
9477 /* Looks like they want an interactive shell */
9478 static smallint did_banner;
9481 /* note: ash and hush share this string */
9482 out1fmt("\n\n%s %s\n"
9483 IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n")
9486 "built-in shell (ash)"
9500 setinteractive(iflag);
9502 #if ENABLE_FEATURE_EDITING_VI
9504 line_input_state->flags |= VI_MODE;
9506 line_input_state->flags &= ~VI_MODE;
9508 viflag = 0; /* forcibly keep the option off */
9512 struct localvar_list {
9513 struct localvar_list *next;
9514 struct localvar *lv;
9517 static struct localvar_list *localvar_stack;
9520 * Called after a function returns.
9521 * Interrupts must be off.
9524 poplocalvars(int keep)
9526 struct localvar_list *ll;
9527 struct localvar *lvp, *next;
9531 ll = localvar_stack;
9532 localvar_stack = ll->next;
9537 while ((lvp = next) != NULL) {
9540 TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-"));
9542 int bits = VSTRFIXED;
9544 if (lvp->flags != VUNSET) {
9545 if (vp->var_text == lvp->text)
9547 else if (!(lvp->flags & (VTEXTFIXED|VSTACK)))
9548 free((char*)lvp->text);
9552 vp->flags |= (lvp->flags & bits);
9555 (VEXPORT|VREADONLY|VSTRFIXED|VUNSET)) == VUNSET)
9556 unsetvar(vp->var_text);
9557 } else if (vp == NULL) { /* $- saved */
9558 memcpy(optlist, lvp->text, sizeof(optlist));
9559 free((char*)lvp->text);
9561 } else if (lvp->flags == VUNSET) {
9562 vp->flags &= ~(VSTRFIXED|VREADONLY);
9563 unsetvar(vp->var_text);
9566 vp->var_func(var_end(lvp->text));
9567 if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0)
9568 free((char*)vp->var_text);
9569 vp->flags = lvp->flags;
9570 vp->var_text = lvp->text;
9578 * Create a new localvar environment.
9580 static struct localvar_list *
9583 struct localvar_list *ll;
9586 ll = ckzalloc(sizeof(*ll));
9587 /*ll->lv = NULL; - zalloc did it */
9588 ll->next = localvar_stack;
9589 localvar_stack = ll;
9596 unwindlocalvars(struct localvar_list *stop)
9598 while (localvar_stack != stop)
9603 evalfun(struct funcnode *func, int argc, char **argv, int flags)
9605 volatile struct shparam saveparam;
9606 struct jmploc *volatile savehandler;
9607 struct jmploc jmploc;
9611 saveparam = shellparam;
9612 savefuncline = funcline;
9613 savehandler = exception_handler;
9614 e = setjmp(jmploc.loc);
9619 exception_handler = &jmploc;
9620 shellparam.malloced = 0;
9622 funcline = func->n.ndefun.linno;
9624 shellparam.nparam = argc - 1;
9625 shellparam.p = argv + 1;
9626 #if ENABLE_ASH_GETOPTS
9627 shellparam.optind = 1;
9628 shellparam.optoff = -1;
9630 evaltree(func->n.ndefun.body, flags & EV_TESTED);
9633 funcline = savefuncline;
9635 freeparam(&shellparam);
9636 shellparam = saveparam;
9637 exception_handler = savehandler;
9639 evalskip &= ~SKIPFUNC;
9644 * Make a variable a local variable. When a variable is made local, it's
9645 * value and flags are saved in a localvar structure. The saved values
9646 * will be restored when the shell function returns. We handle the name
9647 * "-" as a special case: it makes changes to "set +-options" local
9648 * (options will be restored on return from the function).
9653 struct localvar *lvp;
9656 char *eq = strchr(name, '=');
9659 /* Cater for duplicate "local". Examples:
9660 * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x
9661 * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x
9663 lvp = localvar_stack->lv;
9665 if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) {
9669 * it's a duplicate "local VAR" declaration, do nothing
9676 lvp = ckzalloc(sizeof(*lvp));
9677 if (LONE_DASH(name)) {
9679 p = ckmalloc(sizeof(optlist));
9680 lvp->text = memcpy(p, optlist, sizeof(optlist));
9683 vpp = hashvar(name);
9684 vp = *findvar(vpp, name);
9686 /* variable did not exist yet */
9688 vp = setvareq(name, VSTRFIXED);
9690 vp = setvar(name, NULL, VSTRFIXED);
9691 lvp->flags = VUNSET;
9693 lvp->text = vp->var_text;
9694 lvp->flags = vp->flags;
9695 /* make sure neither "struct var" nor string gets freed
9696 * during (un)setting:
9698 vp->flags |= VSTRFIXED|VTEXTFIXED;
9702 /* "local VAR" unsets VAR: */
9703 setvar0(name, NULL);
9707 lvp->next = localvar_stack->lv;
9708 localvar_stack->lv = lvp;
9714 * The "local" command.
9716 static int FAST_FUNC
9717 localcmd(int argc UNUSED_PARAM, char **argv)
9721 if (!localvar_stack)
9722 ash_msg_and_raise_error("not in a function");
9725 while ((name = *argv++) != NULL) {
9731 static int FAST_FUNC
9732 falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9737 static int FAST_FUNC
9738 truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9743 static int FAST_FUNC
9744 execcmd(int argc UNUSED_PARAM, char **argv)
9747 while (nextopt("a:") != '\0')
9748 /* nextopt() sets optionarg to "-a ARGV0" */;
9754 iflag = 0; /* exit on error */
9757 /* We should set up signals for "exec CMD"
9758 * the same way as for "CMD" without "exec".
9759 * But optschanged->setinteractive->setsignal
9760 * still thought we are a root shell. Therefore, for example,
9761 * SIGQUIT is still set to IGN. Fix it:
9765 /*setsignal(SIGTERM); - unnecessary because of iflag=0 */
9766 /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */
9767 /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */
9771 argv[0] = optionarg;
9772 shellexec(prog, argv, pathval(), 0);
9779 * The return command.
9781 static int FAST_FUNC
9782 returncmd(int argc UNUSED_PARAM, char **argv)
9785 * If called outside a function, do what ksh does;
9786 * skip the rest of the file.
9788 evalskip = SKIPFUNC;
9789 return argv[1] ? number(argv[1]) : exitstatus;
9792 /* Forward declarations for builtintab[] */
9793 static int breakcmd(int, char **) FAST_FUNC;
9794 static int dotcmd(int, char **) FAST_FUNC;
9795 static int evalcmd(int, char **, int) FAST_FUNC;
9796 static int exitcmd(int, char **) FAST_FUNC;
9797 static int exportcmd(int, char **) FAST_FUNC;
9798 #if ENABLE_ASH_GETOPTS
9799 static int getoptscmd(int, char **) FAST_FUNC;
9802 static int helpcmd(int, char **) FAST_FUNC;
9805 static int historycmd(int, char **) FAST_FUNC;
9807 #if ENABLE_FEATURE_SH_MATH
9808 static int letcmd(int, char **) FAST_FUNC;
9810 static int readcmd(int, char **) FAST_FUNC;
9811 static int setcmd(int, char **) FAST_FUNC;
9812 static int shiftcmd(int, char **) FAST_FUNC;
9813 static int timescmd(int, char **) FAST_FUNC;
9814 static int trapcmd(int, char **) FAST_FUNC;
9815 static int umaskcmd(int, char **) FAST_FUNC;
9816 static int unsetcmd(int, char **) FAST_FUNC;
9817 static int ulimitcmd(int, char **) FAST_FUNC;
9819 #define BUILTIN_NOSPEC "0"
9820 #define BUILTIN_SPECIAL "1"
9821 #define BUILTIN_REGULAR "2"
9822 #define BUILTIN_SPEC_REG "3"
9823 #define BUILTIN_ASSIGN "4"
9824 #define BUILTIN_SPEC_ASSG "5"
9825 #define BUILTIN_REG_ASSG "6"
9826 #define BUILTIN_SPEC_REG_ASSG "7"
9828 /* Stubs for calling non-FAST_FUNC's */
9830 static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); }
9832 #if ENABLE_ASH_PRINTF
9833 static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); }
9835 #if ENABLE_ASH_TEST || BASH_TEST2
9836 static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); }
9839 /* Keep these in proper order since it is searched via bsearch() */
9840 static const struct builtincmd builtintab[] = {
9841 { BUILTIN_SPEC_REG "." , dotcmd },
9842 { BUILTIN_SPEC_REG ":" , truecmd },
9844 { BUILTIN_REGULAR "[" , testcmd },
9847 { BUILTIN_REGULAR "[[" , testcmd },
9849 #if ENABLE_ASH_ALIAS
9850 { BUILTIN_REG_ASSG "alias" , aliascmd },
9853 { BUILTIN_REGULAR "bg" , fg_bgcmd },
9855 { BUILTIN_SPEC_REG "break" , breakcmd },
9856 { BUILTIN_REGULAR "cd" , cdcmd },
9857 { BUILTIN_NOSPEC "chdir" , cdcmd },
9858 #if ENABLE_ASH_CMDCMD
9859 { BUILTIN_REGULAR "command" , commandcmd },
9861 { BUILTIN_SPEC_REG "continue", breakcmd },
9863 { BUILTIN_REGULAR "echo" , echocmd },
9865 { BUILTIN_SPEC_REG "eval" , NULL }, /*evalcmd() has a differing prototype*/
9866 { BUILTIN_SPEC_REG "exec" , execcmd },
9867 { BUILTIN_SPEC_REG "exit" , exitcmd },
9868 { BUILTIN_SPEC_REG_ASSG "export" , exportcmd },
9869 { BUILTIN_REGULAR "false" , falsecmd },
9871 { BUILTIN_REGULAR "fg" , fg_bgcmd },
9873 #if ENABLE_ASH_GETOPTS
9874 { BUILTIN_REGULAR "getopts" , getoptscmd },
9876 { BUILTIN_NOSPEC "hash" , hashcmd },
9878 { BUILTIN_NOSPEC "help" , helpcmd },
9881 { BUILTIN_NOSPEC "history" , historycmd },
9884 { BUILTIN_REGULAR "jobs" , jobscmd },
9885 { BUILTIN_REGULAR "kill" , killcmd },
9887 #if ENABLE_FEATURE_SH_MATH
9888 { BUILTIN_NOSPEC "let" , letcmd },
9890 { BUILTIN_SPEC_REG_ASSG "local" , localcmd },
9891 #if ENABLE_ASH_PRINTF
9892 { BUILTIN_REGULAR "printf" , printfcmd },
9894 { BUILTIN_NOSPEC "pwd" , pwdcmd },
9895 { BUILTIN_REGULAR "read" , readcmd },
9896 { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd },
9897 { BUILTIN_SPEC_REG "return" , returncmd },
9898 { BUILTIN_SPEC_REG "set" , setcmd },
9899 { BUILTIN_SPEC_REG "shift" , shiftcmd },
9901 { BUILTIN_SPEC_REG "source" , dotcmd },
9904 { BUILTIN_REGULAR "test" , testcmd },
9906 { BUILTIN_SPEC_REG "times" , timescmd },
9907 { BUILTIN_SPEC_REG "trap" , trapcmd },
9908 { BUILTIN_REGULAR "true" , truecmd },
9909 { BUILTIN_NOSPEC "type" , typecmd },
9910 { BUILTIN_NOSPEC "ulimit" , ulimitcmd },
9911 { BUILTIN_REGULAR "umask" , umaskcmd },
9912 #if ENABLE_ASH_ALIAS
9913 { BUILTIN_REGULAR "unalias" , unaliascmd },
9915 { BUILTIN_SPEC_REG "unset" , unsetcmd },
9916 { BUILTIN_REGULAR "wait" , waitcmd },
9919 /* Should match the above table! */
9920 #define COMMANDCMD (builtintab + \
9922 /* [ */ 1 * ENABLE_ASH_TEST + \
9923 /* [[ */ 1 * BASH_TEST2 + \
9924 /* alias */ 1 * ENABLE_ASH_ALIAS + \
9925 /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \
9926 /* break cd cddir */ 3)
9927 #define EVALCMD (COMMANDCMD + \
9928 /* command */ 1 * ENABLE_ASH_CMDCMD + \
9929 /* continue */ 1 + \
9930 /* echo */ 1 * ENABLE_ASH_ECHO + \
9932 #define EXECCMD (EVALCMD + \
9936 * Search the table of builtin commands.
9939 pstrcmp1(const void *a, const void *b)
9941 return strcmp((char*)a, *(char**)b + 1);
9943 static struct builtincmd *
9944 find_builtin(const char *name)
9946 struct builtincmd *bp;
9949 name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]),
9956 * Execute a simple command.
9958 static void unwindfiles(struct parsefile *stop);
9960 isassignment(const char *p)
9962 const char *q = endofname(p);
9967 static int FAST_FUNC
9968 bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
9970 /* Preserve exitstatus of a previous possible redirection
9971 * as POSIX mandates */
9972 return back_exitstatus;
9975 evalcommand(union node *cmd, int flags)
9977 static const struct builtincmd null_bltin = {
9978 "\0\0", bltincmd /* why three NULs? */
9980 struct localvar_list *localvar_stop;
9981 struct parsefile *file_stop;
9982 struct redirtab *redir_stop;
9983 struct stackmark smark;
9985 struct arglist arglist;
9986 struct arglist varlist;
9989 const struct strlist *sp;
9990 struct cmdentry cmdentry;
9997 smallint cmd_is_exec;
9999 errlinno = lineno = cmd->ncmd.linno;
10001 lineno -= funcline - 1;
10003 /* First expand the arguments. */
10004 TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
10005 setstackmark(&smark);
10006 localvar_stop = pushlocalvars();
10007 file_stop = g_parsefile;
10008 back_exitstatus = 0;
10010 cmdentry.cmdtype = CMDBUILTIN;
10011 cmdentry.u.cmd = &null_bltin;
10012 varlist.lastp = &varlist.list;
10013 *varlist.lastp = NULL;
10014 arglist.lastp = &arglist.list;
10015 *arglist.lastp = NULL;
10018 if (cmd->ncmd.args) {
10019 struct builtincmd *bcmd;
10020 smallint pseudovarflag;
10022 bcmd = find_builtin(cmd->ncmd.args->narg.text);
10023 pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd);
10025 for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) {
10026 struct strlist **spp;
10028 spp = arglist.lastp;
10029 if (pseudovarflag && isassignment(argp->narg.text))
10030 expandarg(argp, &arglist, EXP_VARTILDE);
10032 expandarg(argp, &arglist, EXP_FULL | EXP_TILDE);
10034 for (sp = *spp; sp; sp = sp->next)
10039 /* Reserve one extra spot at the front for shellexec. */
10040 nargv = stalloc(sizeof(char *) * (argc + 2));
10042 for (sp = arglist.list; sp; sp = sp->next) {
10043 TRACE(("evalcommand arg: %s\n", sp->text));
10044 *nargv++ = sp->text;
10049 if (iflag && funcline == 0 && argc > 0)
10050 lastarg = nargv[-1];
10052 expredir(cmd->ncmd.redirect);
10053 redir_stop = pushredir(cmd->ncmd.redirect);
10055 if (BASH_XTRACEFD && xflag) {
10056 /* NB: bash closes fd == $BASH_XTRACEFD when it is changed.
10057 * we do not emulate this. We only use its value.
10059 const char *xtracefd = lookupvar("BASH_XTRACEFD");
10060 if (xtracefd && is_number(xtracefd))
10061 preverrout_fd = atoi(xtracefd);
10064 status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2);
10066 path = vpath.var_text;
10067 for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) {
10068 struct strlist **spp;
10071 spp = varlist.lastp;
10072 expandarg(argp, &varlist, EXP_VARTILDE);
10074 mklocal((*spp)->text);
10077 * Modify the command lookup path, if a PATH= assignment
10081 if (varcmp(p, path) == 0)
10085 /* Print the command if xflag is set. */
10087 const char *pfx = "";
10089 fdprintf(preverrout_fd, "%s", expandstr(ps4val(), DQSYNTAX));
10093 char *varval = sp->text;
10094 char *eq = strchrnul(varval, '=');
10097 fdprintf(preverrout_fd, "%s%.*s%s",
10099 (int)(eq - varval), varval,
10100 maybe_single_quote(eq)
10108 fdprintf(preverrout_fd, "%s%s",
10110 /* always quote if matches reserved word: */
10112 ? single_quote(sp->text)
10113 : maybe_single_quote(sp->text)
10118 safe_write(preverrout_fd, "\n", 1);
10124 /* Now locate the command. */
10126 int cmd_flag = DO_ERR;
10127 #if ENABLE_ASH_CMDCMD
10128 const char *oldpath = path + 5;
10132 find_command(argv[0], &cmdentry, cmd_flag, path);
10133 if (cmdentry.cmdtype == CMDUNKNOWN) {
10134 flush_stdout_stderr();
10139 /* implement bltin and command here */
10140 if (cmdentry.cmdtype != CMDBUILTIN)
10143 spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd);
10144 if (cmdentry.u.cmd == EXECCMD)
10146 #if ENABLE_ASH_CMDCMD
10147 if (cmdentry.u.cmd == COMMANDCMD) {
10149 nargv = parse_command_args(argv, &path);
10152 /* It's "command [-p] PROG ARGS" (that is, no -Vv).
10153 * nargv => "PROG". path is updated if -p.
10155 argc -= nargv - argv;
10157 cmd_flag |= DO_NOFUNC;
10166 exitstatus = status;
10168 /* We have a redirection error. */
10170 raise_exception(EXERROR);
10175 /* Execute the command. */
10176 switch (cmdentry.cmdtype) {
10179 #if ENABLE_FEATURE_SH_STANDALONE \
10180 && ENABLE_FEATURE_SH_NOFORK \
10182 /* (1) BUG: if variables are set, we need to fork, or save/restore them
10183 * around run_nofork_applet() call.
10184 * (2) Should this check also be done in forkshell()?
10185 * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...)
10187 /* find_command() encodes applet_no as (-2 - applet_no) */
10188 int applet_no = (- cmdentry.u.index - 2);
10189 if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) {
10193 sv_environ = environ;
10194 environ = listvars(VEXPORT, VUNSET, varlist.list, /*end:*/ NULL);
10196 * Run <applet>_main().
10197 * Signals (^C) can't interrupt here.
10198 * Otherwise we can mangle stdio or malloc internal state.
10199 * This makes applets which can run for a long time
10200 * and/or wait for user input ineligible for NOFORK:
10201 * for example, "yes" or "rm" (rm -i waits for input).
10203 status = run_nofork_applet(applet_no, argv);
10204 environ = sv_environ;
10206 * Try enabling NOFORK for "yes" applet.
10207 * ^C _will_ stop it (write returns EINTR),
10208 * but this causes stdout FILE to be stuck
10209 * and needing clearerr(). What if other applets
10210 * also can get EINTRs? Do we need to switch
10211 * our signals to SA_RESTART?
10213 /*clearerr(stdout);*/
10218 /* Can we avoid forking? For example, very last command
10219 * in a script or a subshell does not need forking,
10220 * we can just exec it.
10222 if (!(flags & EV_EXIT) || may_have_traps) {
10223 /* No, forking off a child is necessary */
10226 jp = makejob(/*cmd,*/ 1);
10227 if (forkshell(jp, cmd, FORK_FG) != 0) {
10229 status = waitforjob(jp);
10231 TRACE(("forked child exited with %d\n", status));
10236 /* fall through to exec'ing external program */
10238 listsetvar(varlist.list, VEXPORT|VSTACK);
10239 shellexec(argv[0], argv, path, cmdentry.u.index);
10243 if (spclbltin > 0 || argc == 0) {
10245 if (cmd_is_exec && argc > 1)
10246 listsetvar(varlist.list, VEXPORT);
10249 /* Tight loop with builtins only:
10250 * "while kill -0 $child; do true; done"
10251 * will never exit even if $child died, unless we do this
10252 * to reap the zombie and make kill detect that it's gone: */
10253 dowait(DOWAIT_NONBLOCK, NULL);
10255 if (evalbltin(cmdentry.u.cmd, argc, argv, flags)) {
10256 if (exception_type == EXERROR && spclbltin <= 0) {
10261 longjmp(exception_handler->loc, 1);
10266 /* See above for the rationale */
10267 dowait(DOWAIT_NONBLOCK, NULL);
10268 if (evalfun(cmdentry.u.func, argc, argv, flags))
10271 status = exitstatus;
10276 if (cmd->ncmd.redirect)
10277 popredir(/*drop:*/ cmd_is_exec);
10278 unwindredir(redir_stop);
10279 unwindfiles(file_stop);
10280 unwindlocalvars(localvar_stop);
10282 /* dsl: I think this is intended to be used to support
10283 * '_' in 'vi' command mode during line editing...
10284 * However I implemented that within libedit itself.
10286 setvar0("_", lastarg);
10288 popstackmark(&smark);
10294 evalbltin(const struct builtincmd *cmd, int argc, char **argv, int flags)
10296 char *volatile savecmdname;
10297 struct jmploc *volatile savehandler;
10298 struct jmploc jmploc;
10302 savecmdname = commandname;
10303 savehandler = exception_handler;
10304 i = setjmp(jmploc.loc);
10307 exception_handler = &jmploc;
10308 commandname = argv[0];
10310 optptr = NULL; /* initialize nextopt */
10311 if (cmd == EVALCMD)
10312 status = evalcmd(argc, argv, flags);
10314 status = (*cmd->builtin)(argc, argv);
10315 flush_stdout_stderr();
10316 status |= ferror(stdout);
10317 exitstatus = status;
10320 commandname = savecmdname;
10321 exception_handler = savehandler;
10327 goodname(const char *p)
10329 return endofname(p)[0] == '\0';
10334 * Search for a command. This is called before we fork so that the
10335 * location of the command will be available in the parent as well as
10336 * the child. The check for "goodname" is an overly conservative
10337 * check that the name will not be subject to expansion.
10340 prehash(union node *n)
10342 struct cmdentry entry;
10344 if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text))
10345 find_command(n->ncmd.args->narg.text, &entry, 0, pathval());
10349 /* ============ Builtin commands
10351 * Builtin commands whose functions are closely tied to evaluation
10352 * are implemented here.
10356 * Handle break and continue commands. Break, continue, and return are
10357 * all handled by setting the evalskip flag. The evaluation routines
10358 * above all check this flag, and if it is set they start skipping
10359 * commands rather than executing them. The variable skipcount is
10360 * the number of loops to break/continue, or the number of function
10361 * levels to return. (The latter is always 1.) It should probably
10362 * be an error to break out of more loops than exist, but it isn't
10363 * in the standard shell so we don't make it one here.
10365 static int FAST_FUNC
10366 breakcmd(int argc UNUSED_PARAM, char **argv)
10368 int n = argv[1] ? number(argv[1]) : 1;
10371 ash_msg_and_raise_error(msg_illnum, argv[1]);
10375 evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK;
10383 * This implements the input routines used by the parser.
10387 INPUT_PUSH_FILE = 1,
10388 INPUT_NOFILE_OK = 2,
10391 static smallint checkkwd;
10392 /* values of checkkwd variable */
10393 #define CHKALIAS 0x1
10396 #define CHKEOFMARK 0x8
10399 * Push a string back onto the input at this current parsefile level.
10400 * We handle aliases this way.
10402 #if !ENABLE_ASH_ALIAS
10403 #define pushstring(s, ap) pushstring(s)
10406 pushstring(char *s, struct alias *ap)
10408 struct strpush *sp;
10413 if (g_parsefile->strpush) {
10414 sp = ckzalloc(sizeof(*sp));
10415 sp->prev = g_parsefile->strpush;
10417 sp = &(g_parsefile->basestrpush);
10419 g_parsefile->strpush = sp;
10420 sp->prev_string = g_parsefile->next_to_pgetc;
10421 sp->prev_left_in_line = g_parsefile->left_in_line;
10422 sp->unget = g_parsefile->unget;
10423 memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc));
10424 #if ENABLE_ASH_ALIAS
10427 ap->flag |= ALIASINUSE;
10431 g_parsefile->next_to_pgetc = s;
10432 g_parsefile->left_in_line = len;
10433 g_parsefile->unget = 0;
10440 struct strpush *sp = g_parsefile->strpush;
10443 #if ENABLE_ASH_ALIAS
10445 if (g_parsefile->next_to_pgetc[-1] == ' '
10446 || g_parsefile->next_to_pgetc[-1] == '\t'
10448 checkkwd |= CHKALIAS;
10450 if (sp->string != sp->ap->val) {
10453 sp->ap->flag &= ~ALIASINUSE;
10454 if (sp->ap->flag & ALIASDEAD) {
10455 unalias(sp->ap->name);
10459 g_parsefile->next_to_pgetc = sp->prev_string;
10460 g_parsefile->left_in_line = sp->prev_left_in_line;
10461 g_parsefile->unget = sp->unget;
10462 memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc));
10463 g_parsefile->strpush = sp->prev;
10464 if (sp != &(g_parsefile->basestrpush))
10473 char *buf = g_parsefile->buf;
10475 g_parsefile->next_to_pgetc = buf;
10476 #if ENABLE_FEATURE_EDITING
10478 if (!iflag || g_parsefile->pf_fd != STDIN_FILENO)
10479 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10481 # if ENABLE_ASH_IDLE_TIMEOUT
10484 const char *tmout_var = lookupvar("TMOUT");
10486 timeout = atoi(tmout_var) * 1000;
10491 line_input_state->timeout = timeout;
10493 # if ENABLE_FEATURE_TAB_COMPLETION
10494 line_input_state->path_lookup = pathval();
10496 reinit_unicode_for_ash();
10497 nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ);
10499 /* ^C pressed, "convert" to SIGINT */
10500 write(STDOUT_FILENO, "^C", 2);
10501 if (trap[SIGINT]) {
10507 exitstatus = 128 + SIGINT;
10513 /* Ctrl+D pressed */
10516 # if ENABLE_ASH_IDLE_TIMEOUT
10517 else if (errno == EAGAIN && timeout > 0) {
10518 puts("\007timed out waiting for input: auto-logout");
10525 nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1);
10528 #if 0 /* disabled: nonblock_immune_read() handles this problem */
10530 if (parsefile->fd == 0 && errno == EWOULDBLOCK) {
10531 int flags = fcntl(0, F_GETFL);
10532 if (flags >= 0 && (flags & O_NONBLOCK)) {
10533 flags &= ~O_NONBLOCK;
10534 if (fcntl(0, F_SETFL, flags) >= 0) {
10535 out2str("sh: turning off NDELAY mode\n");
10546 * Refill the input buffer and return the next input character:
10548 * 1) If a string was pushed back on the input, pop it;
10549 * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM)
10550 * or we are reading from a string so we can't refill the buffer,
10552 * 3) If there is more stuff in this buffer, use it else call read to fill it.
10553 * 4) Process input up to the next newline, deleting nul characters.
10555 //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__)
10556 #define pgetc_debug(...) ((void)0)
10557 static int pgetc(void);
10564 if (g_parsefile->strpush) {
10565 #if ENABLE_ASH_ALIAS
10566 if (g_parsefile->left_in_line == -1
10567 && g_parsefile->strpush->ap
10568 && g_parsefile->next_to_pgetc[-1] != ' '
10569 && g_parsefile->next_to_pgetc[-1] != '\t'
10571 pgetc_debug("preadbuffer PEOA");
10578 /* on both branches above g_parsefile->left_in_line < 0.
10579 * "pgetc" needs refilling.
10582 /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read",
10583 * pungetc() may increment it a few times.
10584 * Assuming it won't increment it to less than -90.
10586 if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) {
10587 pgetc_debug("preadbuffer PEOF1");
10588 /* even in failure keep left_in_line and next_to_pgetc
10589 * in lock step, for correct multi-layer pungetc.
10590 * left_in_line was decremented before preadbuffer(),
10591 * must inc next_to_pgetc: */
10592 g_parsefile->next_to_pgetc++;
10596 more = g_parsefile->left_in_buffer;
10598 flush_stdout_stderr();
10602 /* don't try reading again */
10603 g_parsefile->left_in_line = -99;
10604 pgetc_debug("preadbuffer PEOF2");
10605 g_parsefile->next_to_pgetc++;
10610 /* Find out where's the end of line.
10611 * Set g_parsefile->left_in_line
10612 * and g_parsefile->left_in_buffer acordingly.
10613 * NUL chars are deleted.
10615 q = g_parsefile->next_to_pgetc;
10623 memmove(q, q + 1, more);
10627 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10633 g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1;
10634 if (g_parsefile->left_in_line < 0)
10639 g_parsefile->left_in_buffer = more;
10644 out2str(g_parsefile->next_to_pgetc);
10648 pgetc_debug("preadbuffer at %d:%p'%s'",
10649 g_parsefile->left_in_line,
10650 g_parsefile->next_to_pgetc,
10651 g_parsefile->next_to_pgetc);
10652 return (unsigned char)*g_parsefile->next_to_pgetc++;
10658 g_parsefile->linno++;
10659 setprompt_if(doprompt, 2);
10664 g_parsefile->linno++;
10665 needprompt = doprompt;
10673 pgetc_debug("pgetc at %d:%p'%s'",
10674 g_parsefile->left_in_line,
10675 g_parsefile->next_to_pgetc,
10676 g_parsefile->next_to_pgetc);
10677 if (g_parsefile->unget)
10678 return g_parsefile->lastc[--g_parsefile->unget];
10680 if (--g_parsefile->left_in_line >= 0)
10681 c = (unsigned char)*g_parsefile->next_to_pgetc++;
10685 g_parsefile->lastc[1] = g_parsefile->lastc[0];
10686 g_parsefile->lastc[0] = c;
10691 #if ENABLE_ASH_ALIAS
10693 pgetc_without_PEOA(void)
10697 pgetc_debug("pgetc at %d:%p'%s'",
10698 g_parsefile->left_in_line,
10699 g_parsefile->next_to_pgetc,
10700 g_parsefile->next_to_pgetc);
10702 } while (c == PEOA);
10706 # define pgetc_without_PEOA() pgetc()
10710 * Undo a call to pgetc. Only two characters may be pushed back.
10711 * PEOF may be pushed back.
10716 g_parsefile->unget++;
10719 /* This one eats backslash+newline */
10725 while ((c = pgetc()) == '\\') {
10726 if (pgetc() != '\n') {
10739 uint8_t innerdq :1;
10740 uint8_t varpushed :1;
10741 uint8_t dblquote :1;
10742 int varnest; /* levels of variables expansion */
10743 int dqvarnest; /* levels of variables expansion within double quotes */
10744 int parenlevel; /* levels of parens in arithmetic */
10745 struct synstack *prev;
10746 struct synstack *next;
10750 synstack_push(struct synstack **stack, struct synstack *next, int syntax)
10752 memset(next, 0, sizeof(*next));
10753 next->syntax = syntax;
10754 next->next = *stack;
10755 (*stack)->prev = next;
10759 static ALWAYS_INLINE void
10760 synstack_pop(struct synstack **stack)
10762 *stack = (*stack)->next;
10766 * To handle the "." command, a stack of input files is used. Pushfile
10767 * adds a new entry to the stack and popfile restores the previous level.
10772 struct parsefile *pf;
10774 pf = ckzalloc(sizeof(*pf));
10775 pf->prev = g_parsefile;
10777 /*pf->strpush = NULL; - ckzalloc did it */
10778 /*pf->basestrpush.prev = NULL;*/
10786 struct parsefile *pf = g_parsefile;
10792 if (pf->pf_fd >= 0)
10795 while (pf->strpush)
10797 g_parsefile = pf->prev;
10803 unwindfiles(struct parsefile *stop)
10805 while (g_parsefile != stop)
10810 * Return to top level.
10815 unwindfiles(&basepf);
10819 * Close the file(s) that the shell is reading commands from. Called
10820 * after a fork is done.
10826 if (g_parsefile->pf_fd > 0) {
10827 close(g_parsefile->pf_fd);
10828 g_parsefile->pf_fd = 0;
10833 * Like setinputfile, but takes an open file descriptor. Call this with
10837 setinputfd(int fd, int push)
10841 g_parsefile->buf = NULL;
10843 g_parsefile->pf_fd = fd;
10844 if (g_parsefile->buf == NULL)
10845 g_parsefile->buf = ckmalloc(IBUFSIZ);
10846 g_parsefile->left_in_buffer = 0;
10847 g_parsefile->left_in_line = 0;
10848 g_parsefile->linno = 1;
10852 * Set the input to take input from a file. If push is set, push the
10853 * old input onto the stack first.
10856 setinputfile(const char *fname, int flags)
10861 fd = open(fname, O_RDONLY | O_CLOEXEC);
10863 if (flags & INPUT_NOFILE_OK)
10866 ash_msg_and_raise_perror("can't open '%s'", fname);
10870 else if (O_CLOEXEC == 0) /* old libc */
10871 close_on_exec_on(fd);
10873 setinputfd(fd, flags & INPUT_PUSH_FILE);
10880 * Like setinputfile, but takes input from a string.
10883 setinputstring(char *string)
10887 g_parsefile->next_to_pgetc = string;
10888 g_parsefile->left_in_line = strlen(string);
10889 g_parsefile->buf = NULL;
10890 g_parsefile->linno = 1;
10896 * Routines to check for mail.
10899 #if ENABLE_ASH_MAIL
10901 /* Hash of mtimes of mailboxes */
10902 static unsigned mailtime_hash;
10903 /* Set if MAIL or MAILPATH is changed. */
10904 static smallint mail_var_path_changed;
10907 * Print appropriate message(s) if mail has arrived.
10908 * If mail_var_path_changed is set,
10909 * then the value of MAIL has mail_var_path_changed,
10910 * so we just update the values.
10919 struct stackmark smark;
10922 setstackmark(&smark);
10923 mpath = mpathset() ? mpathval() : mailval();
10926 p = path_advance(&mpath, nullstr);
10931 for (q = p; *q; q++)
10937 q[-1] = '\0'; /* delete trailing '/' */
10938 if (stat(p, &statb) < 0) {
10941 /* Very simplistic "hash": just a sum of all mtimes */
10942 new_hash += (unsigned)statb.st_mtime;
10944 if (!mail_var_path_changed && mailtime_hash != new_hash) {
10945 if (mailtime_hash != 0)
10946 out2str("you have mail\n");
10947 mailtime_hash = new_hash;
10949 mail_var_path_changed = 0;
10950 popstackmark(&smark);
10953 static void FAST_FUNC
10954 changemail(const char *val UNUSED_PARAM)
10956 mail_var_path_changed = 1;
10959 #endif /* ASH_MAIL */
10962 /* ============ ??? */
10965 * Set the shell parameters.
10968 setparam(char **argv)
10974 for (nparam = 0; argv[nparam]; nparam++)
10976 ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap));
10978 *ap++ = ckstrdup(*argv++);
10981 freeparam(&shellparam);
10982 shellparam.malloced = 1;
10983 shellparam.nparam = nparam;
10984 shellparam.p = newparam;
10985 #if ENABLE_ASH_GETOPTS
10986 shellparam.optind = 1;
10987 shellparam.optoff = -1;
10992 * Process shell options. The global variable argptr contains a pointer
10993 * to the argument list; we advance it past the options.
10995 * SUSv3 section 2.8.1 "Consequences of Shell Errors" says:
10996 * For a non-interactive shell, an error condition encountered
10997 * by a special built-in ... shall cause the shell to write a diagnostic message
10998 * to standard error and exit as shown in the following table:
10999 * Error Special Built-In
11001 * Utility syntax error (option or operand error) Shall exit
11003 * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142)
11004 * we see that bash does not do that (set "finishes" with error code 1 instead,
11005 * and shell continues), and people rely on this behavior!
11007 * set -o barfoo 2>/dev/null
11010 * Oh well. Let's mimic that.
11013 plus_minus_o(char *name, int val)
11018 for (i = 0; i < NOPTS; i++) {
11019 if (strcmp(name, optnames(i)) == 0) {
11024 ash_msg("illegal option %co %s", val ? '-' : '+', name);
11027 for (i = 0; i < NOPTS; i++) {
11029 out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off");
11031 out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i));
11037 setoption(int flag, int val)
11041 for (i = 0; i < NOPTS; i++) {
11042 if (optletters(i) == flag) {
11047 ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag);
11051 options(int cmdline, int *login_sh)
11059 while ((p = *argptr) != NULL) {
11061 if (c != '-' && c != '+')
11064 val = 0; /* val = 0 if c == '+' */
11067 if (p[0] == '\0' || LONE_DASH(p)) {
11069 /* "-" means turn off -x and -v */
11072 /* "--" means reset params */
11073 else if (*argptr == NULL)
11076 break; /* "-" or "--" terminates options */
11079 /* first char was + or - */
11080 while ((c = *p++) != '\0') {
11081 /* bash 3.2 indeed handles -c CMD and +c CMD the same */
11082 if (c == 'c' && cmdline) {
11083 minusc = p; /* command is after shell args */
11084 } else if (c == 'o') {
11085 if (plus_minus_o(*argptr, val)) {
11086 /* it already printed err message */
11087 return 1; /* error */
11091 } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
11094 /* bash does not accept +-login, we also won't */
11095 } else if (cmdline && val && (c == '-')) { /* long options */
11096 if (strcmp(p, "login") == 0) {
11110 * The shift builtin command.
11112 static int FAST_FUNC
11113 shiftcmd(int argc UNUSED_PARAM, char **argv)
11120 n = number(argv[1]);
11121 if (n > shellparam.nparam)
11124 shellparam.nparam -= n;
11125 for (ap1 = shellparam.p; --n >= 0; ap1++) {
11126 if (shellparam.malloced)
11129 ap2 = shellparam.p;
11130 while ((*ap2++ = *ap1++) != NULL)
11132 #if ENABLE_ASH_GETOPTS
11133 shellparam.optind = 1;
11134 shellparam.optoff = -1;
11141 * POSIX requires that 'set' (but not export or readonly) output the
11142 * variables in lexicographic order - by the locale's collating order (sigh).
11143 * Maybe we could keep them in an ordered balanced binary tree
11144 * instead of hashed lists.
11145 * For now just roll 'em through qsort for printing...
11148 showvars(const char *sep_prefix, int on, int off)
11151 char **ep, **epend;
11153 ep = listvars(on, off, /*strlist:*/ NULL, &epend);
11154 qsort(ep, epend - ep, sizeof(char *), vpcmp);
11156 sep = *sep_prefix ? " " : sep_prefix;
11158 for (; ep < epend; ep++) {
11162 p = endofname(*ep);
11163 /* Used to have simple "p = strchrnul(*ep, '=')" here instead, but this
11164 * makes "export -p" to have output not suitable for "eval":
11166 * os.environ["test-test"]="test"
11167 * if os.fork() == 0:
11168 * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this
11169 * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ])
11173 q = single_quote(++p);
11174 out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q);
11180 * The set command builtin.
11182 static int FAST_FUNC
11183 setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
11188 return showvars(nullstr, 0, VUNSET);
11191 retval = options(/*cmdline:*/ 0, NULL);
11192 if (retval == 0) { /* if no parse error... */
11194 if (*argptr != NULL) {
11202 #if ENABLE_ASH_RANDOM_SUPPORT
11203 static void FAST_FUNC
11204 change_random(const char *value)
11208 if (value == NULL) {
11209 /* "get", generate */
11210 t = next_random(&random_gen);
11211 /* set without recursion */
11212 setvar(vrandom.var_text, utoa(t), VNOFUNC);
11213 vrandom.flags &= ~VNOFUNC;
11216 t = strtoul(value, NULL, 10);
11217 INIT_RANDOM_T(&random_gen, (t ? t : 1), t);
11222 #if ENABLE_ASH_GETOPTS
11224 getopts(char *optstr, char *optvar, char **optfirst)
11231 int ind = shellparam.optind;
11232 int off = shellparam.optoff;
11236 shellparam.optind = -1;
11237 optnext = optfirst + ind - 1;
11239 if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off)
11242 p = optnext[-1] + off;
11243 if (p == NULL || *p == '\0') {
11244 /* Current word is done, advance */
11246 if (p == NULL || *p != '-' || *++p == '\0') {
11248 unsetvar("OPTARG");
11254 if (LONE_DASH(p)) /* check for "--" */
11259 for (q = optstr; *q != c;) {
11261 /* OPTERR is a bashism */
11262 const char *cp = lookupvar("OPTERR");
11263 if ((cp && LONE_CHAR(cp, '0'))
11264 || (optstr[0] == ':')
11267 /*sbuf[1] = '\0'; - already is */
11268 setvar0("OPTARG", sbuf);
11270 fprintf(stderr, "Illegal option -%c\n", c);
11271 unsetvar("OPTARG");
11281 if (*p == '\0' && (p = *optnext) == NULL) {
11282 /* OPTERR is a bashism */
11283 const char *cp = lookupvar("OPTERR");
11284 if ((cp && LONE_CHAR(cp, '0'))
11285 || (optstr[0] == ':')
11288 /*sbuf[1] = '\0'; - already is */
11289 setvar0("OPTARG", sbuf);
11292 fprintf(stderr, "No arg for -%c option\n", c);
11293 unsetvar("OPTARG");
11301 setvar0("OPTARG", p);
11304 setvar0("OPTARG", nullstr);
11306 ind = optnext - optfirst + 1;
11307 setvar("OPTIND", itoa(ind), VNOFUNC);
11309 /*sbuf[1] = '\0'; - already is */
11310 setvar0(optvar, sbuf);
11312 shellparam.optoff = p ? p - *(optnext - 1) : -1;
11313 shellparam.optind = ind;
11319 * The getopts builtin. Shellparam.optnext points to the next argument
11320 * to be processed. Shellparam.optptr points to the next character to
11321 * be processed in the current argument. If shellparam.optnext is NULL,
11322 * then it's the first time getopts has been called.
11324 static int FAST_FUNC
11325 getoptscmd(int argc, char **argv)
11330 ash_msg_and_raise_error("usage: getopts optstring var [arg]");
11332 optbase = shellparam.p;
11333 if ((unsigned)shellparam.optind > shellparam.nparam + 1) {
11334 shellparam.optind = 1;
11335 shellparam.optoff = -1;
11338 optbase = &argv[3];
11339 if ((unsigned)shellparam.optind > argc - 2) {
11340 shellparam.optind = 1;
11341 shellparam.optoff = -1;
11345 return getopts(argv[1], argv[2], optbase);
11347 #endif /* ASH_GETOPTS */
11350 /* ============ Shell parser */
11353 struct heredoc *next; /* next here document in list */
11354 union node *here; /* redirection node */
11355 char *eofmark; /* string indicating end of input */
11356 smallint striptabs; /* if set, strip leading tabs */
11359 static smallint tokpushback; /* last token pushed back */
11360 static smallint quoteflag; /* set if (part of) last token was quoted */
11361 static token_id_t lasttoken; /* last token read (integer id Txxx) */
11362 static struct heredoc *heredoclist; /* list of here documents to read */
11363 static char *wordtext; /* text of last word returned by readtoken */
11364 static struct nodelist *backquotelist;
11365 static union node *redirnode;
11366 static struct heredoc *heredoc;
11368 static const char *
11369 tokname(char *buf, int tok)
11372 return tokname_array[tok];
11373 sprintf(buf, "\"%s\"", tokname_array[tok]);
11377 /* raise_error_unexpected_syntax:
11378 * Called when an unexpected token is read during the parse. The argument
11379 * is the token that is expected, or -1 if more than one type of token can
11380 * occur at this point.
11382 static void raise_error_unexpected_syntax(int) NORETURN;
11384 raise_error_unexpected_syntax(int token)
11390 l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken));
11392 sprintf(msg + l, " (expecting %s)", tokname(buf, token));
11393 raise_error_syntax(msg);
11397 /* parsing is heavily cross-recursive, need these forward decls */
11398 static union node *andor(void);
11399 static union node *pipeline(void);
11400 static union node *parse_command(void);
11401 static void parseheredoc(void);
11402 static int peektoken(void);
11403 static int readtoken(void);
11405 static union node *
11408 union node *n1, *n2, *n3;
11413 switch (peektoken()) {
11421 if (!n1 && (nlflag & 1))
11427 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11428 if (nlflag == 2 && ((1 << peektoken()) & tokendlist))
11434 if (tok == TBACKGND) {
11435 if (n2->type == NPIPE) {
11436 n2->npipe.pipe_backgnd = 1;
11438 if (n2->type != NREDIR) {
11439 n3 = stzalloc(sizeof(struct nredir));
11441 /*n3->nredir.redirect = NULL; - stzalloc did it */
11444 n2->type = NBACKGND;
11450 n3 = stzalloc(sizeof(struct nbinary));
11452 n3->nbinary.ch1 = n1;
11453 n3->nbinary.ch2 = n2;
11466 raise_error_unexpected_syntax(-1);
11473 static union node *
11476 union node *n1, *n2, *n3;
11484 } else if (t == TOR) {
11490 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11492 n3 = stzalloc(sizeof(struct nbinary));
11494 n3->nbinary.ch1 = n1;
11495 n3->nbinary.ch2 = n2;
11500 static union node *
11503 union node *n1, *n2, *pipenode;
11504 struct nodelist *lp, *prev;
11508 TRACE(("pipeline: entered\n"));
11509 if (readtoken() == TNOT) {
11511 checkkwd = CHKKWD | CHKALIAS;
11514 n1 = parse_command();
11515 if (readtoken() == TPIPE) {
11516 pipenode = stzalloc(sizeof(struct npipe));
11517 pipenode->type = NPIPE;
11518 /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */
11519 lp = stzalloc(sizeof(struct nodelist));
11520 pipenode->npipe.cmdlist = lp;
11524 lp = stzalloc(sizeof(struct nodelist));
11525 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11526 lp->n = parse_command();
11528 } while (readtoken() == TPIPE);
11534 n2 = stzalloc(sizeof(struct nnot));
11542 static union node *
11547 n = stzalloc(sizeof(struct narg));
11549 /*n->narg.next = NULL; - stzalloc did it */
11550 n->narg.text = wordtext;
11551 n->narg.backquote = backquotelist;
11556 fixredir(union node *n, const char *text, int err)
11560 TRACE(("Fix redir %s %d\n", text, err));
11562 n->ndup.vname = NULL;
11564 fd = bb_strtou(text, NULL, 10);
11565 if (!errno && fd >= 0)
11566 n->ndup.dupfd = fd;
11567 else if (LONE_DASH(text))
11568 n->ndup.dupfd = -1;
11571 raise_error_syntax("bad fd number");
11572 n->ndup.vname = makename();
11579 union node *n = redirnode;
11581 if (n->type == NHERE)
11582 checkkwd = CHKEOFMARK;
11583 if (readtoken() != TWORD)
11584 raise_error_unexpected_syntax(-1);
11585 if (n->type == NHERE) {
11586 struct heredoc *here = heredoc;
11589 if (quoteflag == 0)
11591 TRACE(("Here document %d\n", n->type));
11592 rmescapes(wordtext, 0, NULL);
11593 here->eofmark = wordtext;
11595 if (heredoclist == NULL)
11596 heredoclist = here;
11598 for (p = heredoclist; p->next; p = p->next)
11602 } else if (n->type == NTOFD || n->type == NFROMFD) {
11603 fixredir(n, wordtext, 0);
11605 n->nfile.fname = makename();
11609 static union node *
11612 union node *args, **app;
11613 union node *n = NULL;
11614 union node *vars, **vpp;
11615 union node **rpp, *redir;
11619 smallint double_brackets_flag = 0;
11621 IF_BASH_FUNCTION(smallint function_flag = 0;)
11630 savecheckkwd = CHKALIAS;
11631 savelinno = g_parsefile->linno;
11634 checkkwd = savecheckkwd;
11639 if (peektoken() != TWORD)
11640 raise_error_unexpected_syntax(TWORD);
11645 case TAND: /* "&&" */
11646 case TOR: /* "||" */
11647 if (!double_brackets_flag) {
11651 wordtext = (char *) (t == TAND ? "-a" : "-o");
11654 n = stzalloc(sizeof(struct narg));
11656 /*n->narg.next = NULL; - stzalloc did it */
11657 n->narg.text = wordtext;
11659 if (strcmp("[[", wordtext) == 0)
11660 double_brackets_flag = 1;
11661 else if (strcmp("]]", wordtext) == 0)
11662 double_brackets_flag = 0;
11664 n->narg.backquote = backquotelist;
11665 if (savecheckkwd && isassignment(wordtext)) {
11667 vpp = &n->narg.next;
11670 app = &n->narg.next;
11674 if (function_flag) {
11675 checkkwd = CHKNL | CHKKWD;
11676 switch (peektoken()) {
11689 if (strcmp("[[", wordtext) == 0)
11694 raise_error_unexpected_syntax(-1);
11700 *rpp = n = redirnode;
11701 rpp = &n->nfile.next;
11702 parsefname(); /* read name of redirection file */
11705 IF_BASH_FUNCTION(do_func:)
11706 if (args && app == &args->narg.next
11709 struct builtincmd *bcmd;
11712 /* We have a function */
11713 if (IF_BASH_FUNCTION(!function_flag &&) readtoken() != TRP)
11714 raise_error_unexpected_syntax(TRP);
11715 name = n->narg.text;
11716 if (!goodname(name)
11717 || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd))
11719 raise_error_syntax("bad function name");
11722 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11723 n->ndefun.text = n->narg.text;
11724 n->ndefun.linno = g_parsefile->linno;
11725 n->ndefun.body = parse_command();
11728 IF_BASH_FUNCTION(function_flag = 0;)
11739 n = stzalloc(sizeof(struct ncmd));
11742 n->ncmd.linno = savelinno;
11743 n->ncmd.args = args;
11744 n->ncmd.assign = vars;
11745 n->ncmd.redirect = redir;
11749 static union node *
11750 parse_command(void)
11752 union node *n1, *n2;
11753 union node *ap, **app;
11754 union node *cp, **cpp;
11755 union node *redir, **rpp;
11763 savelinno = g_parsefile->linno;
11765 switch (readtoken()) {
11767 raise_error_unexpected_syntax(-1);
11770 n1 = stzalloc(sizeof(struct nif));
11772 n1->nif.test = list(0);
11773 if (readtoken() != TTHEN)
11774 raise_error_unexpected_syntax(TTHEN);
11775 n1->nif.ifpart = list(0);
11777 while (readtoken() == TELIF) {
11778 n2->nif.elsepart = stzalloc(sizeof(struct nif));
11779 n2 = n2->nif.elsepart;
11781 n2->nif.test = list(0);
11782 if (readtoken() != TTHEN)
11783 raise_error_unexpected_syntax(TTHEN);
11784 n2->nif.ifpart = list(0);
11786 if (lasttoken == TELSE)
11787 n2->nif.elsepart = list(0);
11789 n2->nif.elsepart = NULL;
11797 n1 = stzalloc(sizeof(struct nbinary));
11798 n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL;
11799 n1->nbinary.ch1 = list(0);
11802 TRACE(("expecting DO got '%s' %s\n", tokname_array[got],
11803 got == TWORD ? wordtext : ""));
11804 raise_error_unexpected_syntax(TDO);
11806 n1->nbinary.ch2 = list(0);
11811 if (readtoken() != TWORD || quoteflag || !goodname(wordtext))
11812 raise_error_syntax("bad for loop variable");
11813 n1 = stzalloc(sizeof(struct nfor));
11815 n1->nfor.linno = savelinno;
11816 n1->nfor.var = wordtext;
11817 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11818 if (readtoken() == TIN) {
11820 while (readtoken() == TWORD) {
11821 n2 = stzalloc(sizeof(struct narg));
11823 /*n2->narg.next = NULL; - stzalloc did it */
11824 n2->narg.text = wordtext;
11825 n2->narg.backquote = backquotelist;
11827 app = &n2->narg.next;
11830 n1->nfor.args = ap;
11831 if (lasttoken != TNL && lasttoken != TSEMI)
11832 raise_error_unexpected_syntax(-1);
11834 n2 = stzalloc(sizeof(struct narg));
11836 /*n2->narg.next = NULL; - stzalloc did it */
11837 n2->narg.text = (char *)dolatstr;
11838 /*n2->narg.backquote = NULL;*/
11839 n1->nfor.args = n2;
11841 * Newline or semicolon here is optional (but note
11842 * that the original Bourne shell only allowed NL).
11844 if (lasttoken != TSEMI)
11847 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11848 if (readtoken() != TDO)
11849 raise_error_unexpected_syntax(TDO);
11850 n1->nfor.body = list(0);
11854 n1 = stzalloc(sizeof(struct ncase));
11856 n1->ncase.linno = savelinno;
11857 if (readtoken() != TWORD)
11858 raise_error_unexpected_syntax(TWORD);
11859 n1->ncase.expr = n2 = stzalloc(sizeof(struct narg));
11861 /*n2->narg.next = NULL; - stzalloc did it */
11862 n2->narg.text = wordtext;
11863 n2->narg.backquote = backquotelist;
11864 checkkwd = CHKNL | CHKKWD | CHKALIAS;
11865 if (readtoken() != TIN)
11866 raise_error_unexpected_syntax(TIN);
11867 cpp = &n1->ncase.cases;
11869 checkkwd = CHKNL | CHKKWD;
11871 while (t != TESAC) {
11872 if (lasttoken == TLP)
11874 *cpp = cp = stzalloc(sizeof(struct nclist));
11876 app = &cp->nclist.pattern;
11878 *app = ap = stzalloc(sizeof(struct narg));
11880 /*ap->narg.next = NULL; - stzalloc did it */
11881 ap->narg.text = wordtext;
11882 ap->narg.backquote = backquotelist;
11883 if (readtoken() != TPIPE)
11885 app = &ap->narg.next;
11888 //ap->narg.next = NULL;
11889 if (lasttoken != TRP)
11890 raise_error_unexpected_syntax(TRP);
11891 cp->nclist.body = list(2);
11893 cpp = &cp->nclist.next;
11895 checkkwd = CHKNL | CHKKWD;
11899 raise_error_unexpected_syntax(TENDCASE);
11906 n1 = stzalloc(sizeof(struct nredir));
11907 n1->type = NSUBSHELL;
11908 n1->nredir.linno = savelinno;
11909 n1->nredir.n = list(0);
11910 /*n1->nredir.redirect = NULL; - stzalloc did it */
11917 IF_BASH_FUNCTION(case TFUNCTION:)
11921 return simplecmd();
11924 if (readtoken() != t)
11925 raise_error_unexpected_syntax(t);
11928 /* Now check for redirection which may follow command */
11929 checkkwd = CHKKWD | CHKALIAS;
11931 while (readtoken() == TREDIR) {
11932 *rpp = n2 = redirnode;
11933 rpp = &n2->nfile.next;
11939 if (n1->type != NSUBSHELL) {
11940 n2 = stzalloc(sizeof(struct nredir));
11942 n2->nredir.linno = savelinno;
11946 n1->nredir.redirect = redir;
11951 #if BASH_DOLLAR_SQUOTE
11953 decode_dollar_squote(void)
11955 static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
11961 p = strchr(C_escapes, c);
11966 if ((unsigned char)(c - '0') <= 7) { /* \ooo */
11970 } while ((unsigned char)(c - '0') <= 7 && --cnt);
11972 } else if (c == 'x') { /* \xHH */
11976 } while (isxdigit(c) && --cnt);
11978 if (cnt == 3) { /* \x but next char is "bad" */
11982 } else { /* simple seq like \\ or \t */
11987 c = bb_process_escape_sequence((void*)&p);
11988 } else { /* unrecognized "\z": print both chars unless ' or " */
11989 if (c != '\'' && c != '"') {
11991 c |= 0x100; /* "please encode \, then me" */
11998 /* Used by expandstr to get here-doc like behaviour. */
11999 #define FAKEEOFMARK ((char*)(uintptr_t)1)
12001 static ALWAYS_INLINE int
12002 realeofmark(const char *eofmark)
12004 return eofmark && eofmark != FAKEEOFMARK;
12008 * If eofmark is NULL, read a word or a redirection symbol. If eofmark
12009 * is not NULL, read a here document. In the latter case, eofmark is the
12010 * word which marks the end of the document and striptabs is true if
12011 * leading tabs should be stripped from the document. The argument c
12012 * is the first character of the input token or document.
12014 * Because C does not have internal subroutines, I have simulated them
12015 * using goto's to implement the subroutine linkage. The following macros
12016 * will run code that appears at the end of readtoken1.
12018 #define CHECKEND() {goto checkend; checkend_return:;}
12019 #define PARSEREDIR() {goto parseredir; parseredir_return:;}
12020 #define PARSESUB() {goto parsesub; parsesub_return:;}
12021 #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;}
12022 #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;}
12023 #define PARSEARITH() {goto parsearith; parsearith_return:;}
12025 readtoken1(int c, int syntax, char *eofmark, int striptabs)
12027 /* NB: syntax parameter fits into smallint */
12028 /* c parameter is an unsigned char or PEOF or PEOA */
12031 struct nodelist *bqlist;
12034 smallint pssyntax; /* we are expanding a prompt string */
12035 IF_BASH_DOLLAR_SQUOTE(smallint bash_dollar_squote = 0;)
12037 struct synstack synbase = { };
12038 struct synstack *synstack = &synbase;
12040 #if ENABLE_ASH_EXPAND_PRMT
12041 pssyntax = (syntax == PSSYNTAX);
12045 pssyntax = 0; /* constant */
12047 synstack->syntax = syntax;
12049 if (syntax == DQSYNTAX)
12050 synstack->dblquote = 1;
12054 STARTSTACKSTR(out);
12056 /* For each line, until end of word */
12057 CHECKEND(); /* set c to PEOF if at end of here document */
12058 for (;;) { /* until end of line or end of word */
12059 CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */
12060 switch (SIT(c, synstack->syntax)) {
12061 case CNL: /* '\n' */
12062 if (synstack->syntax == BASESYNTAX
12063 && !synstack->varnest
12065 goto endword; /* exit outer loop */
12070 goto loop; /* continue outer loop */
12075 #if BASH_DOLLAR_SQUOTE
12076 if (c == '\\' && bash_dollar_squote) {
12077 c = decode_dollar_squote();
12079 /* skip $'\000', $'\x00' (like bash) */
12083 /* Unknown escape. Encode as '\z' */
12084 c = (unsigned char)c;
12085 if (eofmark == NULL || synstack->dblquote)
12086 USTPUTC(CTLESC, out);
12087 USTPUTC('\\', out);
12091 if (!eofmark || synstack->dblquote || synstack->varnest)
12092 USTPUTC(CTLESC, out);
12095 case CBACK: /* backslash */
12096 c = pgetc_without_PEOA();
12098 USTPUTC(CTLESC, out);
12099 USTPUTC('\\', out);
12101 } else if (c == '\n') {
12104 if (pssyntax && c == '$') {
12105 USTPUTC(CTLESC, out);
12106 USTPUTC('\\', out);
12108 /* Backslash is retained if we are in "str"
12109 * and next char isn't dquote-special.
12111 if (synstack->dblquote
12115 && (c != '"' || (eofmark != NULL && !synstack->varnest))
12116 && (c != '}' || !synstack->varnest)
12118 USTPUTC(CTLESC, out); /* protect '\' from glob */
12119 USTPUTC('\\', out);
12121 USTPUTC(CTLESC, out);
12127 synstack->syntax = SQSYNTAX;
12129 if (eofmark == NULL) {
12130 USTPUTC(CTLQUOTEMARK, out);
12134 synstack->syntax = DQSYNTAX;
12135 synstack->dblquote = 1;
12137 if (synstack->varnest)
12138 synstack->innerdq ^= 1;
12141 IF_BASH_DOLLAR_SQUOTE(bash_dollar_squote = 0;)
12142 if (eofmark != NULL && synstack->varnest == 0) {
12147 if (synstack->dqvarnest == 0) {
12148 synstack->syntax = BASESYNTAX;
12149 synstack->dblquote = 0;
12158 case CVAR: /* '$' */
12159 PARSESUB(); /* parse substitution */
12161 case CENDVAR: /* '}' */
12162 if (!synstack->innerdq && synstack->varnest > 0) {
12163 if (!--synstack->varnest && synstack->varpushed)
12164 synstack_pop(&synstack);
12165 else if (synstack->dqvarnest > 0)
12166 synstack->dqvarnest--;
12171 #if ENABLE_FEATURE_SH_MATH
12172 case CLP: /* '(' in arithmetic */
12173 synstack->parenlevel++;
12176 case CRP: /* ')' in arithmetic */
12177 if (synstack->parenlevel > 0) {
12178 synstack->parenlevel--;
12180 if (pgetc_eatbnl() == ')') {
12182 synstack_pop(&synstack);
12185 * unbalanced parens
12186 * (don't 2nd guess - no error)
12194 case CBQUOTE: /* '`' */
12195 if (checkkwd & CHKEOFMARK) {
12204 goto endword; /* exit outer loop */
12208 if (synstack->varnest == 0) {
12209 #if BASH_REDIR_OUTPUT
12211 //Can't call pgetc_eatbnl() here, this requires three-deep pungetc()
12212 if (pgetc() == '>')
12213 c = 0x100 + '>'; /* flag &> */
12217 goto endword; /* exit outer loop */
12219 IF_ASH_ALIAS(if (c != PEOA))
12226 #if ENABLE_FEATURE_SH_MATH
12227 if (synstack->syntax == ARISYNTAX)
12228 raise_error_syntax("missing '))'");
12230 if (synstack->syntax != BASESYNTAX && eofmark == NULL)
12231 raise_error_syntax("unterminated quoted string");
12232 if (synstack->varnest != 0) {
12234 raise_error_syntax("missing '}'");
12236 USTPUTC('\0', out);
12237 len = out - (char *)stackblock();
12238 out = stackblock();
12239 if (eofmark == NULL) {
12240 if ((c == '>' || c == '<' IF_BASH_REDIR_OUTPUT( || c == 0x100 + '>'))
12243 if (isdigit_str9(out)) {
12244 PARSEREDIR(); /* passed as params: out, c */
12245 lasttoken = TREDIR;
12248 /* else: non-number X seen, interpret it
12249 * as "NNNX>file" = "NNNX >file" */
12253 quoteflag = quotef;
12254 backquotelist = bqlist;
12255 grabstackblock(len);
12259 /* end of readtoken routine */
12262 * Check to see whether we are at the end of the here document. When this
12263 * is called, c is set to the first character of the next input line. If
12264 * we are at the end of the here document, this routine sets the c to PEOF.
12267 if (realeofmark(eofmark)) {
12271 #if ENABLE_ASH_ALIAS
12273 c = pgetc_without_PEOA();
12276 while (c == '\t') {
12277 c = pgetc_without_PEOA();
12281 markloc = out - (char *)stackblock();
12282 for (p = eofmark; STPUTC(c, out), *p; p++) {
12286 c = pgetc_without_PEOA();
12289 if (c == '\n' || c == PEOF) {
12291 g_parsefile->linno++;
12292 needprompt = doprompt;
12297 p = (char *)stackblock() + markloc + 1;
12298 len_here = out - p;
12301 len_here -= (c >= PEOF);
12307 str = alloca(len_here + 1);
12308 *(char *)mempcpy(str, p, len_here) = '\0';
12310 pushstring(str, NULL);
12315 STADJUST((char *)stackblock() + markloc - out, out);
12317 goto checkend_return;
12321 * Parse a redirection operator. The variable "out" points to a string
12322 * specifying the fd to be redirected. The variable "c" contains the
12323 * first character of the redirection operator.
12326 /* out is already checked to be a valid number or "" */
12327 int fd = (*out == '\0' ? -1 : atoi(out));
12330 np = stzalloc(sizeof(struct nfile));
12333 c = pgetc_eatbnl();
12335 np->type = NAPPEND;
12337 np->type = NCLOBBER;
12340 /* it also can be NTO2 (>&file), but we can't figure it out yet */
12346 #if BASH_REDIR_OUTPUT
12347 else if (c == 0x100 + '>') { /* this flags &> redirection */
12349 pgetc(); /* this is '>', no need to check */
12353 else { /* c == '<' */
12354 /*np->nfile.fd = 0; - stzalloc did it */
12355 c = pgetc_eatbnl();
12358 if (sizeof(struct nfile) != sizeof(struct nhere)) {
12359 np = stzalloc(sizeof(struct nhere));
12360 /*np->nfile.fd = 0; - stzalloc did it */
12363 heredoc = stzalloc(sizeof(struct heredoc));
12364 heredoc->here = np;
12365 c = pgetc_eatbnl();
12367 heredoc->striptabs = 1;
12369 /*heredoc->striptabs = 0; - stzalloc did it */
12375 np->type = NFROMFD;
12379 np->type = NFROMTO;
12391 goto parseredir_return;
12395 * Parse a substitution. At this point, we have read the dollar sign
12396 * and nothing else.
12399 /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise
12400 * (assuming ascii char codes, as the original implementation did) */
12401 #define is_special(c) \
12402 (((unsigned)(c) - 33 < 32) \
12403 && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1))
12405 unsigned char subtype;
12408 c = pgetc_eatbnl();
12409 if ((checkkwd & CHKEOFMARK)
12410 || c > 255 /* PEOA or PEOF */
12411 || (c != '(' && c != '{' && !is_name(c) && !is_special(c))
12413 #if BASH_DOLLAR_SQUOTE
12414 if (synstack->syntax != DQSYNTAX && c == '\'')
12415 bash_dollar_squote = 1;
12420 } else if (c == '(') {
12421 /* $(command) or $((arith)) */
12422 if (pgetc_eatbnl() == '(') {
12423 #if ENABLE_FEATURE_SH_MATH
12426 raise_error_syntax("support for $((arith)) is disabled");
12433 /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */
12434 smalluint newsyn = synstack->syntax;
12436 USTPUTC(CTLVAR, out);
12437 typeloc = out - (char *)stackblock();
12439 subtype = VSNORMAL;
12441 c = pgetc_eatbnl();
12446 /* $[{[#]]NAME[}] */
12449 c = pgetc_eatbnl();
12450 } while (is_in_name(c));
12451 } else if (isdigit(c)) {
12452 /* $[{[#]]NUM[}] */
12455 c = pgetc_eatbnl();
12456 } while (isdigit(c));
12457 } else if (c != '}') {
12458 /* $[{[#]]<specialchar>[}] */
12461 c = pgetc_eatbnl();
12462 if (!subtype && cc == '#') {
12463 subtype = VSLENGTH;
12464 if (c == '_' || isalnum(c))
12467 c = pgetc_eatbnl();
12468 if (cc == '}' || c != '}') {
12476 if (!is_special(cc)) {
12477 if (subtype == VSLENGTH)
12486 if (c != '}' && subtype == VSLENGTH) {
12487 /* ${#VAR didn't end with } */
12491 if (subtype == 0) {
12492 static const char types[] ALIGN1 = "}-+?=";
12493 /* ${VAR...} but not $VAR or ${#VAR} */
12494 /* c == first char after VAR */
12499 c = pgetc_eatbnl();
12501 /* This check is only needed to not misinterpret
12502 * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD}
12505 if (!strchr(types, c)) {
12506 subtype = VSSUBSTR;
12508 break; /* "goto badsub" is bigger (!) */
12514 const char *p = strchr(types, c);
12517 subtype |= p - types + VSNORMAL;
12522 subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT);
12523 c = pgetc_eatbnl();
12529 newsyn = BASESYNTAX;
12531 #if BASH_PATTERN_SUBST
12533 /* ${v/[/]pattern/repl} */
12534 //TODO: encode pattern and repl separately.
12535 // Currently cases like: v=1;echo ${v/$((1/1))/ONE}
12536 // are broken (should print "ONE")
12537 subtype = VSREPLACE;
12538 newsyn = BASESYNTAX;
12539 c = pgetc_eatbnl();
12542 subtype++; /* VSREPLACEALL */
12551 if (newsyn == ARISYNTAX)
12554 if ((newsyn != synstack->syntax || synstack->innerdq)
12555 && subtype != VSNORMAL
12557 synstack_push(&synstack,
12558 synstack->prev ?: alloca(sizeof(*synstack)),
12561 synstack->varpushed = 1;
12562 synstack->dblquote = newsyn != BASESYNTAX;
12565 ((unsigned char *)stackblock())[typeloc] = subtype;
12566 if (subtype != VSNORMAL) {
12567 synstack->varnest++;
12568 if (synstack->dblquote)
12569 synstack->dqvarnest++;
12573 goto parsesub_return;
12577 * Called to parse command substitutions. Newstyle is set if the command
12578 * is enclosed inside $(...); nlpp is a pointer to the head of the linked
12579 * list of commands (passed by reference), and savelen is the number of
12580 * characters on the top of the stack which must be preserved.
12583 struct nodelist **nlpp;
12587 smallint saveprompt = 0;
12590 savelen = out - (char *)stackblock();
12593 * FIXME: this can allocate very large block on stack and SEGV.
12595 * echo "..<100kbytes>..`true` $(true) `true` ..."
12596 * allocates 100kb for every command subst. With about
12597 * a hundred command substitutions stack overflows.
12598 * With larger prepended string, SEGV happens sooner.
12600 str = alloca(savelen);
12601 memcpy(str, stackblock(), savelen);
12605 /* We must read until the closing backquote, giving special
12606 * treatment to some slashes, and then push the string and
12607 * reread it as input, interpreting it normally.
12613 STARTSTACKSTR(pout);
12617 setprompt_if(needprompt, 2);
12618 pc = pgetc_eatbnl();
12624 pc = pgetc(); /* or pgetc_eatbnl()? why (example)? */
12625 if (pc != '\\' && pc != '`' && pc != '$'
12626 && (!synstack->dblquote || pc != '"')
12628 STPUTC('\\', pout);
12630 if (pc <= 255 /* not PEOA or PEOF */) {
12636 IF_ASH_ALIAS(case PEOA:)
12637 raise_error_syntax("EOF in backquote substitution");
12649 STPUTC('\0', pout);
12650 psavelen = pout - (char *)stackblock();
12651 if (psavelen > 0) {
12652 pstr = grabstackstr(pout);
12653 setinputstring(pstr);
12658 nlpp = &(*nlpp)->next;
12659 *nlpp = stzalloc(sizeof(**nlpp));
12660 /* (*nlpp)->next = NULL; - stzalloc did it */
12663 saveprompt = doprompt;
12670 doprompt = saveprompt;
12671 else if (readtoken() != TRP)
12672 raise_error_unexpected_syntax(TRP);
12677 * Start reading from old file again, ignoring any pushed back
12678 * tokens left from the backquote parsing
12683 while (stackblocksize() <= savelen)
12685 STARTSTACKSTR(out);
12687 memcpy(out, str, savelen);
12688 STADJUST(savelen, out);
12690 USTPUTC(CTLBACKQ, out);
12692 goto parsebackq_oldreturn;
12693 goto parsebackq_newreturn;
12696 #if ENABLE_FEATURE_SH_MATH
12698 * Parse an arithmetic expansion (indicate start of one and set state)
12702 synstack_push(&synstack,
12703 synstack->prev ?: alloca(sizeof(*synstack)),
12705 synstack->dblquote = 1;
12706 USTPUTC(CTLARI, out);
12707 goto parsearith_return;
12710 } /* end of readtoken */
12713 * Read the next input token.
12714 * If the token is a word, we set backquotelist to the list of cmds in
12715 * backquotes. We set quoteflag to true if any part of the word was
12717 * If the token is TREDIR, then we set redirnode to a structure containing
12720 * [Change comment: here documents and internal procedures]
12721 * [Readtoken shouldn't have any arguments. Perhaps we should make the
12722 * word parsing code into a separate routine. In this case, readtoken
12723 * doesn't need to have any internal procedures, but parseword does.
12724 * We could also make parseoperator in essence the main routine, and
12725 * have parseword (readtoken1?) handle both words and redirection.]
12727 #define NEW_xxreadtoken
12728 #ifdef NEW_xxreadtoken
12729 /* singles must be first! */
12730 static const char xxreadtoken_chars[7] ALIGN1 = {
12731 '\n', '(', ')', /* singles */
12732 '&', '|', ';', /* doubles */
12736 #define xxreadtoken_singles 3
12737 #define xxreadtoken_doubles 3
12739 static const char xxreadtoken_tokens[] ALIGN1 = {
12740 TNL, TLP, TRP, /* only single occurrence allowed */
12741 TBACKGND, TPIPE, TSEMI, /* if single occurrence */
12742 TEOF, /* corresponds to trailing nul */
12743 TAND, TOR, TENDCASE /* if double occurrence */
12755 setprompt_if(needprompt, 2);
12756 for (;;) { /* until token or start of word found */
12757 c = pgetc_eatbnl();
12758 if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA))
12762 while ((c = pgetc()) != '\n' && c != PEOF)
12765 } else if (c == '\\') {
12766 break; /* return readtoken1(...) */
12770 p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1;
12776 p = strchr(xxreadtoken_chars, c);
12778 break; /* return readtoken1(...) */
12780 if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) {
12781 int cc = pgetc_eatbnl();
12782 if (cc == c) { /* double occurrence? */
12783 p += xxreadtoken_doubles + 1;
12786 #if BASH_REDIR_OUTPUT
12787 if (c == '&' && cc == '>') /* &> */
12788 break; /* return readtoken1(...) */
12793 lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars];
12798 return readtoken1(c, BASESYNTAX, (char *) NULL, 0);
12800 #else /* old xxreadtoken */
12801 #define RETURN(token) return lasttoken = token
12811 setprompt_if(needprompt, 2);
12812 for (;;) { /* until token or start of word found */
12813 c = pgetc_eatbnl();
12815 case ' ': case '\t':
12816 IF_ASH_ALIAS(case PEOA:)
12819 while ((c = pgetc()) != '\n' && c != PEOF)
12829 if (pgetc_eatbnl() == '&')
12834 if (pgetc_eatbnl() == '|')
12839 if (pgetc_eatbnl() == ';')
12850 return readtoken1(c, BASESYNTAX, (char *)NULL, 0);
12853 #endif /* old xxreadtoken */
12859 int kwd = checkkwd;
12861 smallint alreadyseen = tokpushback;
12864 #if ENABLE_ASH_ALIAS
12880 if (t != TWORD || quoteflag) {
12885 * check for keywords
12887 if (kwd & CHKKWD) {
12888 const char *const *pp;
12890 pp = findkwd(wordtext);
12892 lasttoken = t = pp - tokname_array;
12893 TRACE(("keyword '%s' recognized\n", tokname_array[t]));
12898 if (checkkwd & CHKALIAS) {
12899 #if ENABLE_ASH_ALIAS
12901 ap = lookupalias(wordtext, 1);
12904 pushstring(ap->val, ap);
12914 TRACE(("token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
12916 TRACE(("reread token '%s' %s\n", tokname_array[t], t == TWORD ? wordtext : ""));
12932 * Read and parse a command. Returns NODE_EOF on end of file.
12933 * (NULL is a valid parse tree indicating a blank line.)
12935 static union node *
12936 parsecmd(int interact)
12941 doprompt = interact;
12942 setprompt_if(doprompt, doprompt);
12948 * Input any here documents.
12953 struct heredoc *here;
12956 here = heredoclist;
12957 heredoclist = NULL;
12960 setprompt_if(needprompt, 2);
12961 readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX,
12962 here->eofmark, here->striptabs);
12963 n = stzalloc(sizeof(struct narg));
12964 n->narg.type = NARG;
12965 /*n->narg.next = NULL; - stzalloc did it */
12966 n->narg.text = wordtext;
12967 n->narg.backquote = backquotelist;
12968 here->here->nhere.doc = n;
12974 static const char *
12975 expandstr(const char *ps, int syntax_type)
12980 /* XXX Fix (char *) cast. */
12981 setinputstring((char *)ps);
12983 saveprompt = doprompt;
12986 /* readtoken1() might die horribly.
12987 * Try a prompt with syntactically wrong command:
12988 * PS1='$(date "+%H:%M:%S) > '
12991 volatile int saveint;
12992 struct jmploc *volatile savehandler = exception_handler;
12993 struct jmploc jmploc;
12995 if (setjmp(jmploc.loc) == 0) {
12996 exception_handler = &jmploc;
12997 readtoken1(pgetc(), syntax_type, FAKEEOFMARK, 0);
12999 exception_handler = savehandler;
13000 RESTORE_INT(saveint);
13003 doprompt = saveprompt;
13007 n.narg.type = NARG;
13008 n.narg.next = NULL;
13009 n.narg.text = wordtext;
13010 n.narg.backquote = backquotelist;
13012 expandarg(&n, NULL, EXP_QUOTED);
13013 return stackblock();
13019 return tokpushback && lasttoken == TEOF;
13023 * Execute a command or commands contained in a string.
13026 evalstring(char *s, int flags)
13028 struct jmploc *volatile savehandler;
13029 struct jmploc jmploc;
13033 struct stackmark smark;
13038 setstackmark(&smark);
13041 /* On exception inside execution loop, we must popfile().
13042 * Try interactively:
13044 * command eval "a=b" # throws "is read only" error
13045 * "command BLTIN" is not supposed to abort (even in non-interactive use).
13046 * But if we skip popfile(), we hit EOF in eval's string, and exit.
13048 savehandler = exception_handler;
13049 ex = setjmp(jmploc.loc);
13052 exception_handler = &jmploc;
13054 while ((n = parsecmd(0)) != NODE_EOF) {
13057 i = evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT));
13060 popstackmark(&smark);
13065 popstackmark(&smark);
13069 exception_handler = savehandler;
13071 longjmp(exception_handler->loc, ex);
13077 * The eval command.
13079 static int FAST_FUNC
13080 evalcmd(int argc UNUSED_PARAM, char **argv, int flags)
13089 STARTSTACKSTR(concat);
13091 concat = stack_putstr(p, concat);
13095 STPUTC(' ', concat);
13097 STPUTC('\0', concat);
13098 p = grabstackstr(concat);
13100 return evalstring(p, flags & EV_TESTED);
13106 * Read and execute commands.
13107 * "Top" is nonzero for the top level command loop;
13108 * it turns on prompting if the shell is interactive.
13114 struct stackmark smark;
13119 TRACE(("cmdloop(%d) called\n", top));
13123 setstackmark(&smark);
13126 showjobs(SHOW_CHANGED|SHOW_STDERR);
13129 if (iflag && top) {
13133 n = parsecmd(inter);
13135 if (DEBUG > 2 && debug && (n != NODE_EOF))
13138 if (n == NODE_EOF) {
13139 if (!top || numeof >= 50)
13141 if (!stoppedjobs()) {
13144 out2str("\nUse \"exit\" to leave shell.\n");
13147 } else if (nflag == 0) {
13150 /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */
13153 i = evaltree(n, 0);
13157 popstackmark(&smark);
13161 evalskip &= ~SKIPFUNC;
13169 * Take commands from a file. To be compatible we should do a path
13170 * search for the file, which is necessary to find sub-commands.
13173 find_dot_file(char *name)
13176 const char *path = pathval();
13179 /* don't try this for absolute or relative paths */
13180 if (strchr(name, '/'))
13183 while ((fullname = path_advance(&path, name)) != NULL) {
13184 if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) {
13186 * Don't bother freeing here, since it will
13187 * be freed by the caller.
13191 if (fullname != name)
13192 stunalloc(fullname);
13194 /* not found in PATH */
13196 #if ENABLE_ASH_BASH_SOURCE_CURDIR
13199 ash_msg_and_raise_error("%s: not found", name);
13204 static int FAST_FUNC
13205 dotcmd(int argc_ UNUSED_PARAM, char **argv_ UNUSED_PARAM)
13207 /* "false; . empty_file; echo $?" should print 0, not 1: */
13211 char *args_need_save;
13212 volatile struct shparam saveparam;
13215 // struct strlist *sp;
13216 // for (sp = cmdenviron; sp; sp = sp->next)
13217 // setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
13219 nextopt(nullstr); /* handle possible "--" */
13223 /* bash says: "bash: .: filename argument required" */
13224 return 2; /* bash compat */
13227 /* This aborts if file isn't found, which is POSIXly correct.
13228 * bash returns exitcode 1 instead.
13230 fullname = find_dot_file(argv[0]);
13232 args_need_save = argv[0];
13233 if (args_need_save) { /* ". FILE ARGS", and ARGS are not empty */
13235 saveparam = shellparam;
13236 shellparam.malloced = 0;
13240 shellparam.nparam = argc;
13241 shellparam.p = argv;
13244 /* This aborts if file can't be opened, which is POSIXly correct.
13245 * bash returns exitcode 1 instead.
13247 setinputfile(fullname, INPUT_PUSH_FILE);
13248 commandname = fullname;
13249 status = cmdloop(0);
13252 if (args_need_save) {
13253 freeparam(&shellparam);
13254 shellparam = saveparam;
13260 static int FAST_FUNC
13261 exitcmd(int argc UNUSED_PARAM, char **argv)
13266 exitstatus = number(argv[1]);
13267 raise_exception(EXEXIT);
13272 * Read a file containing shell functions.
13275 readcmdfile(char *name)
13277 setinputfile(name, INPUT_PUSH_FILE);
13283 /* ============ find_command inplementation */
13286 * Resolve a command name. If you change this routine, you may have to
13287 * change the shellexec routine as well.
13290 find_command(char *name, struct cmdentry *entry, int act, const char *path)
13292 struct tblentry *cmdp;
13299 struct builtincmd *bcmd;
13301 /* If name contains a slash, don't use PATH or hash table */
13302 if (strchr(name, '/') != NULL) {
13303 entry->u.index = -1;
13304 if (act & DO_ABS) {
13305 while (stat(name, &statb) < 0) {
13307 if (errno == EINTR)
13310 entry->cmdtype = CMDUNKNOWN;
13314 entry->cmdtype = CMDNORMAL;
13318 /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */
13320 updatetbl = (path == pathval());
13323 if (strstr(path, "%builtin") != NULL)
13324 act |= DO_ALTBLTIN;
13327 /* If name is in the table, check answer will be ok */
13328 cmdp = cmdlookup(name, 0);
13329 if (cmdp != NULL) {
13332 switch (cmdp->cmdtype) {
13350 } else if (cmdp->rehash == 0)
13351 /* if not invalidated by cd, we're done */
13355 /* If %builtin not in path, check for builtin next */
13356 bcmd = find_builtin(name);
13358 if (IS_BUILTIN_REGULAR(bcmd))
13359 goto builtin_success;
13360 if (act & DO_ALTPATH) {
13361 if (!(act & DO_ALTBLTIN))
13362 goto builtin_success;
13363 } else if (builtinloc <= 0) {
13364 goto builtin_success;
13368 #if ENABLE_FEATURE_SH_STANDALONE
13370 int applet_no = find_applet_by_name(name);
13371 if (applet_no >= 0) {
13372 entry->cmdtype = CMDNORMAL;
13373 entry->u.index = -2 - applet_no;
13379 /* We have to search path. */
13380 prev = -1; /* where to start */
13381 if (cmdp && cmdp->rehash) { /* doing a rehash */
13382 if (cmdp->cmdtype == CMDBUILTIN)
13385 prev = cmdp->param.index;
13391 while ((fullname = path_advance(&path, name)) != NULL) {
13392 stunalloc(fullname);
13393 /* NB: code below will still use fullname
13394 * despite it being "unallocated" */
13397 if (prefix(pathopt, "builtin")) {
13399 goto builtin_success;
13402 if ((act & DO_NOFUNC)
13403 || !prefix(pathopt, "func")
13404 ) { /* ignore unimplemented options */
13408 /* if rehash, don't redo absolute path names */
13409 if (fullname[0] == '/' && idx <= prev) {
13412 TRACE(("searchexec \"%s\": no change\n", name));
13415 while (stat(fullname, &statb) < 0) {
13417 if (errno == EINTR)
13420 if (errno != ENOENT && errno != ENOTDIR)
13424 e = EACCES; /* if we fail, this will be the error */
13425 if (!S_ISREG(statb.st_mode))
13427 if (pathopt) { /* this is a %func directory */
13428 stalloc(strlen(fullname) + 1);
13429 /* NB: stalloc will return space pointed by fullname
13430 * (because we don't have any intervening allocations
13431 * between stunalloc above and this stalloc) */
13432 readcmdfile(fullname);
13433 cmdp = cmdlookup(name, 0);
13434 if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION)
13435 ash_msg_and_raise_error("%s not defined in %s", name, fullname);
13436 stunalloc(fullname);
13439 TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname));
13441 entry->cmdtype = CMDNORMAL;
13442 entry->u.index = idx;
13446 cmdp = cmdlookup(name, 1);
13447 cmdp->cmdtype = CMDNORMAL;
13448 cmdp->param.index = idx;
13453 /* We failed. If there was an entry for this command, delete it */
13454 if (cmdp && updatetbl)
13455 delete_cmd_entry();
13456 if (act & DO_ERR) {
13457 #if ENABLE_ASH_BASH_NOT_FOUND_HOOK
13458 struct tblentry *hookp = cmdlookup("command_not_found_handle", 0);
13459 if (hookp && hookp->cmdtype == CMDFUNCTION) {
13461 argv[0] = (char*) "command_not_found_handle";
13464 evalfun(hookp->param.func, 2, argv, 0);
13465 entry->cmdtype = CMDUNKNOWN;
13469 ash_msg("%s: %s", name, errmsg(e, "not found"));
13471 entry->cmdtype = CMDUNKNOWN;
13476 entry->cmdtype = CMDBUILTIN;
13477 entry->u.cmd = bcmd;
13481 cmdp = cmdlookup(name, 1);
13482 cmdp->cmdtype = CMDBUILTIN;
13483 cmdp->param.cmd = bcmd;
13487 entry->cmdtype = cmdp->cmdtype;
13488 entry->u = cmdp->param;
13493 * The trap builtin.
13495 static int FAST_FUNC
13496 trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13500 int signo, exitcode;
13505 for (signo = 0; signo < NSIG; signo++) {
13506 char *tr = trap_ptr[signo];
13508 /* note: bash adds "SIG", but only if invoked
13509 * as "bash". If called as "sh", or if set -o posix,
13510 * then it prints short signal names.
13511 * We are printing short names: */
13512 out1fmt("trap -- %s %s\n",
13514 get_signame(signo));
13515 /* trap_ptr != trap only if we are in special-cased `trap` code.
13516 * In this case, we will exit very soon, no need to free(). */
13517 /* if (trap_ptr != trap && tp[0]) */
13522 if (trap_ptr != trap) {
13530 /* Why the second check?
13531 * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..."
13532 * In this case, NUM is signal no, not an action.
13535 if (ap[1] && !is_number(ap[0]))
13540 signo = get_signum(*ap);
13542 /* Mimic bash message exactly */
13543 ash_msg("%s: invalid signal specification", *ap);
13549 if (LONE_DASH(action))
13552 if (action[0]) /* not NULL and not "" and not "-" */
13553 may_have_traps = 1;
13554 action = ckstrdup(action);
13558 trap[signo] = action;
13569 /* ============ Builtins */
13571 #if ENABLE_ASH_HELP
13572 static int FAST_FUNC
13573 helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13579 "Built-in commands:\n"
13580 "------------------\n");
13581 for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) {
13582 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '),
13583 builtintab[i].name + 1);
13589 # if ENABLE_FEATURE_SH_STANDALONE
13591 const char *a = applet_names;
13593 col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a);
13598 while (*a++ != '\0')
13603 newline_and_flush(stdout);
13604 return EXIT_SUCCESS;
13609 static int FAST_FUNC
13610 historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13612 show_history(line_input_state);
13613 return EXIT_SUCCESS;
13618 * The export and readonly commands.
13620 static int FAST_FUNC
13621 exportcmd(int argc UNUSED_PARAM, char **argv)
13631 /* "readonly" in bash accepts, but ignores -n.
13632 * We do the same: it saves a conditional in nextopt's param.
13635 while ((opt = nextopt("np")) != '\0') {
13637 flag_off = VEXPORT;
13640 if (argv[0][0] == 'r') {
13642 flag_off = 0; /* readonly ignores -n */
13644 flag_off = ~flag_off;
13646 /*if (opt_p_not_specified) - bash doesn't check this. Try "export -p NAME" */
13652 p = strchr(name, '=');
13656 vp = *findvar(hashvar(name), name);
13658 vp->flags = ((vp->flags | flag) & flag_off);
13662 setvar(name, p, (flag & flag_off));
13663 } while ((name = *++aptr) != NULL);
13668 /* No arguments. Show the list of exported or readonly vars.
13671 showvars(argv[0], flag, 0);
13676 * Delete a function if it exists.
13679 unsetfunc(const char *name)
13681 struct tblentry *cmdp;
13683 cmdp = cmdlookup(name, 0);
13684 if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION)
13685 delete_cmd_entry();
13689 * The unset builtin command. We unset the function before we unset the
13690 * variable to allow a function to be unset when there is a readonly variable
13691 * with the same name.
13693 static int FAST_FUNC
13694 unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13700 while ((i = nextopt("vf")) != 0) {
13704 for (ap = argptr; *ap; ap++) {
13715 static const unsigned char timescmd_str[] ALIGN1 = {
13716 ' ', offsetof(struct tms, tms_utime),
13717 '\n', offsetof(struct tms, tms_stime),
13718 ' ', offsetof(struct tms, tms_cutime),
13719 '\n', offsetof(struct tms, tms_cstime),
13722 static int FAST_FUNC
13723 timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13726 const unsigned char *p;
13729 clk_tck = bb_clk_tck();
13734 unsigned sec, frac;
13736 t = *(clock_t *)(((char *) &buf) + p[1]);
13738 frac = t % clk_tck;
13739 out1fmt("%um%u.%03us%c",
13740 sec / 60, sec % 60,
13741 (frac * 1000) / clk_tck,
13749 #if ENABLE_FEATURE_SH_MATH
13751 * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell.
13752 * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
13754 * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru>
13756 static int FAST_FUNC
13757 letcmd(int argc UNUSED_PARAM, char **argv)
13763 ash_msg_and_raise_error("expression expected");
13765 i = ash_arith(*argv);
13773 * The read builtin. Options:
13774 * -r Do not interpret '\' specially
13775 * -s Turn off echo (tty only)
13776 * -n NCHARS Read NCHARS max
13777 * -p PROMPT Display PROMPT on stderr (if input is from tty)
13778 * -t SECONDS Timeout after SECONDS (tty or pipe only)
13779 * -u FD Read from given FD instead of fd 0
13780 * -d DELIM End on DELIM char, not newline
13781 * This uses unbuffered input, which may be avoidable in some cases.
13782 * TODO: bash also has:
13783 * -a ARRAY Read into array[0],[1],etc
13784 * -e Use line editing (tty only)
13786 static int FAST_FUNC
13787 readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13789 struct builtin_read_params params;
13793 memset(¶ms, 0, sizeof(params));
13795 while ((i = nextopt("p:u:rt:n:sd:")) != '\0') {
13798 params.opt_p = optionarg;
13801 params.opt_n = optionarg;
13804 params.read_flags |= BUILTIN_READ_SILENT;
13807 params.opt_t = optionarg;
13810 params.read_flags |= BUILTIN_READ_RAW;
13813 params.opt_u = optionarg;
13817 params.opt_d = optionarg;
13825 params.argv = argptr;
13826 params.setvar = setvar0;
13827 params.ifs = bltinlookup("IFS"); /* can be NULL */
13829 /* "read -s" needs to save/restore termios, can't allow ^C
13830 * to jump out of it.
13834 r = shell_builtin_read(¶ms);
13837 if ((uintptr_t)r == 1 && errno == EINTR) {
13838 /* To get SIGCHLD: sleep 1 & read x; echo $x
13839 * Correct behavior is to not exit "read"
13841 if (pending_sig == 0)
13845 if ((uintptr_t)r > 1)
13846 ash_msg_and_raise_error(r);
13848 return (uintptr_t)r;
13851 static int FAST_FUNC
13852 umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
13854 static const char permuser[3] ALIGN1 = "ogu";
13857 int symbolic_mode = 0;
13859 while (nextopt("S") != '\0') {
13868 if (*argptr == NULL) {
13869 if (symbolic_mode) {
13870 char buf[sizeof(",u=rwx,g=rwx,o=rwx")];
13877 *p++ = permuser[i];
13879 /* mask is 0..0uuugggooo. i=2 selects uuu bits */
13880 if (!(mask & 0400)) *p++ = 'r';
13881 if (!(mask & 0200)) *p++ = 'w';
13882 if (!(mask & 0100)) *p++ = 'x';
13890 out1fmt("%04o\n", mask);
13893 char *modestr = *argptr;
13894 /* numeric umasks are taken as-is */
13895 /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
13896 if (!isdigit(modestr[0]))
13898 mask = bb_parse_mode(modestr, mask);
13899 if ((unsigned)mask > 0777) {
13900 ash_msg_and_raise_error("illegal mode: %s", modestr);
13902 if (!isdigit(modestr[0]))
13909 static int FAST_FUNC
13910 ulimitcmd(int argc UNUSED_PARAM, char **argv)
13912 return shell_builtin_ulimit(argv);
13915 /* ============ main() and helpers */
13918 * Called to exit the shell.
13927 #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
13928 save_history(line_input_state);
13930 status = exitstatus;
13931 TRACE(("pid %d, exitshell(%d)\n", getpid(), status));
13932 if (setjmp(loc.loc)) {
13933 if (exception_type == EXEXIT)
13934 status = exitstatus;
13937 exception_handler = &loc;
13943 /*free(p); - we'll exit soon */
13946 /* dash wraps setjobctl(0) in "if (setjmp(loc.loc) == 0) {...}".
13947 * our setjobctl(0) does not panic if tcsetpgrp fails inside it.
13950 flush_stdout_stderr();
13955 /* Don't inline: conserve stack of caller from having our locals too */
13956 static NOINLINE void
13959 /* we will never free this */
13960 basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ);
13963 sigmode[SIGCHLD - 1] = S_DFL; /* ensure we install handler even if it is SIG_IGNed */
13964 setsignal(SIGCHLD);
13966 /* bash re-enables SIGHUP which is SIG_IGNed on entry.
13967 * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$"
13969 signal(SIGHUP, SIG_DFL);
13976 for (envp = environ; envp && *envp; envp++) {
13978 * p = endofname(*envp);
13979 * if (p != *envp && *p == '=') {
13980 * here to weed out badly-named variables, but this breaks
13981 * scenarios where people do want them passed to children:
13983 * os.environ["test-test"]="test"
13984 * if os.fork() == 0:
13985 * os.execv("ash", [ 'ash', '-c', 'eval $(export -p); echo OK' ]) # fixes this
13986 * os.execv("ash", [ 'ash', '-c', 'env | grep test-test' ]) # breaks this
13988 if (strchr(*envp, '=')) {
13989 setvareq(*envp, VEXPORT|VTEXTFIXED);
13993 setvareq((char*)defifsvar, VTEXTFIXED);
13994 setvareq((char*)defoptindvar, VTEXTFIXED);
13996 setvar0("PPID", utoa(getppid()));
13998 p = lookupvar("SHLVL");
13999 setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT);
14001 #if BASH_HOSTNAME_VAR
14002 if (!lookupvar("HOSTNAME")) {
14003 struct utsname uts;
14005 setvar0("HOSTNAME", uts.nodename);
14008 p = lookupvar("PWD");
14010 struct stat st1, st2;
14011 if (p[0] != '/' || stat(p, &st1) || stat(".", &st2)
14012 || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino
14022 //usage:#define ash_trivial_usage
14023 //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS] / -s [ARGS]]"
14024 //usage:#define ash_full_usage "\n\n"
14025 //usage: "Unix shell interpreter"
14028 * Process the shell command line arguments.
14031 procargs(char **argv)
14034 const char *xminusc;
14039 #if NUM_SCRIPTS > 0
14043 login_sh = xargv[0] && xargv[0][0] == '-';
14045 /* if (xargv[0]) - mmm, this is always true! */
14048 for (i = 0; i < NOPTS; i++)
14050 if (options(/*cmdline:*/ 1, &login_sh)) {
14051 /* it already printed err message */
14052 raise_exception(EXERROR);
14056 if (*xargv == NULL) {
14058 ash_msg_and_raise_error(bb_msg_requires_arg, "-c");
14061 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
14065 for (i = 0; i < NOPTS; i++)
14066 if (optlist[i] == 2)
14071 /* POSIX 1003.2: first arg after "-c CMD" is $0, remainder $1... */
14076 } else if (!sflag) {
14077 setinputfile(*xargv, 0);
14080 commandname = arg0;
14083 shellparam.p = xargv;
14084 #if ENABLE_ASH_GETOPTS
14085 shellparam.optind = 1;
14086 shellparam.optoff = -1;
14088 /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */
14090 shellparam.nparam++;
14099 * Read /etc/profile, ~/.profile, $ENV.
14102 read_profile(const char *name)
14104 name = expandstr(name, DQSYNTAX);
14105 if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0)
14112 * This routine is called when an error or an interrupt occurs in an
14113 * interactive shell and control is returned to the main command loop.
14114 * (In dash, this function is auto-generated by build machinery).
14123 /* from expand.c: */
14126 /* from input.c: */
14127 g_parsefile->left_in_buffer = 0;
14128 g_parsefile->left_in_line = 0; /* clear input buffer */
14131 /* from redir.c: */
14135 unwindlocalvars(NULL);
14139 static short profile_buf[16384];
14140 extern int etext();
14144 * Main routine. We initialize things, parse the arguments, execute
14145 * profiles if we're a login shell, and then call cmdloop to execute
14146 * commands. The setjmp call sets up the location to jump to when an
14147 * exception occurs. When an exception occurs the variable "state"
14148 * is used to figure out how far we had gotten.
14150 int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
14151 int ash_main(int argc UNUSED_PARAM, char **argv)
14152 /* note: 'argc' is used only if embedded scripts are enabled */
14154 volatile smallint state;
14155 struct jmploc jmploc;
14156 struct stackmark smark;
14159 /* Initialize global data */
14163 #if ENABLE_ASH_ALIAS
14169 monitor(4, etext, profile_buf, sizeof(profile_buf), 50);
14172 #if ENABLE_FEATURE_EDITING
14173 line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP);
14176 if (setjmp(jmploc.loc)) {
14182 e = exception_type;
14184 if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) {
14188 newline_and_flush(stderr);
14191 popstackmark(&smark);
14192 FORCE_INT_ON; /* enable interrupts */
14201 exception_handler = &jmploc;
14202 rootpid = getpid();
14205 setstackmark(&smark);
14207 #if NUM_SCRIPTS > 0
14209 /* Non-NULL minusc tells procargs that an embedded script is being run */
14210 minusc = get_script_content(-argc - 1);
14212 login_sh = procargs(argv);
14214 TRACE(("Shell args: "));
14215 trace_puts_args(argv);
14222 read_profile("/etc/profile");
14225 hp = lookupvar("HOME");
14227 read_profile("$HOME/.profile");
14233 getuid() == geteuid() && getgid() == getegid() &&
14237 const char *shinit = lookupvar("ENV");
14238 if (shinit != NULL && *shinit != '\0')
14239 read_profile(shinit);
14241 popstackmark(&smark);
14245 /* evalstring pushes parsefile stack.
14246 * Ensure we don't falsely claim that 0 (stdin)
14247 * is one of stacked source fds.
14248 * Testcase: ash -c 'exec 1>&0' must not complain. */
14249 // if (!sflag) g_parsefile->pf_fd = -1;
14250 // ^^ not necessary since now we special-case fd 0
14251 // in save_fd_on_redirect()
14252 evalstring(minusc, sflag ? 0 : EV_EXIT);
14255 if (sflag || minusc == NULL) {
14256 #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY
14258 const char *hp = lookupvar("HISTFILE");
14260 hp = lookupvar("HOME");
14263 hp = concat_path_file(hp, ".ash_history");
14264 setvar0("HISTFILE", hp);
14267 hp = lookupvar("HISTFILE");
14271 line_input_state->hist_file = hp;
14272 # if ENABLE_FEATURE_SH_HISTFILESIZE
14273 hp = lookupvar("HISTFILESIZE");
14274 line_input_state->max_history = size_from_HISTFILESIZE(hp);
14278 state4: /* XXX ??? - why isn't this before the "if" statement */
14286 extern void _mcleanup(void);
14290 TRACE(("End of main reached\n"));
14297 * Copyright (c) 1989, 1991, 1993, 1994
14298 * The Regents of the University of California. All rights reserved.
14300 * This code is derived from software contributed to Berkeley by
14301 * Kenneth Almquist.
14303 * Redistribution and use in source and binary forms, with or without
14304 * modification, are permitted provided that the following conditions
14306 * 1. Redistributions of source code must retain the above copyright
14307 * notice, this list of conditions and the following disclaimer.
14308 * 2. Redistributions in binary form must reproduce the above copyright
14309 * notice, this list of conditions and the following disclaimer in the
14310 * documentation and/or other materials provided with the distribution.
14311 * 3. Neither the name of the University nor the names of its contributors
14312 * may be used to endorse or promote products derived from this software
14313 * without specific prior written permission.
14315 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND
14316 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14317 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14318 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
14319 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14320 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14321 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14322 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14323 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
14324 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF