Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Kenneth Almquist. |
| 7 | * |
| 8 | * Original BSD copyright notice is retained at the end of this file. |
| 9 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 10 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 14 | * was re-ported from NetBSD and debianized. |
| 15 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 16 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 19 | /* |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 20 | * The following should be set to reflect the type of system you have: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 21 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 22 | * define SYSV if you are running under System V. |
| 23 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 24 | * define DEBUG=2 to compile in and turn on debugging. |
| 25 | * |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 26 | * When debugging is on (DEBUG is 1 and "set -o debug" was executed), |
| 27 | * debugging info will be written to ./trace and a quit signal |
| 28 | * will generate a core dump. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 29 | */ |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 30 | #define DEBUG 0 |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 31 | /* Tweak debug output verbosity here */ |
| 32 | #define DEBUG_TIME 0 |
| 33 | #define DEBUG_PID 1 |
| 34 | #define DEBUG_SIG 1 |
| 35 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 36 | #define PROFILE 0 |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 38 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 40 | #include <setjmp.h> |
| 41 | #include <fnmatch.h> |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 42 | #include <glob.h> |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 43 | #include <sys/times.h> |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 44 | #include <sys/utsname.h> /* for setting $HOSTNAME */ |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 45 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 46 | #include "busybox.h" /* for applet_names */ |
| 47 | #include "unicode.h" |
| 48 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 49 | #include "shell_common.h" |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 50 | #if ENABLE_SH_MATH_SUPPORT |
| 51 | # include "math.h" |
| 52 | #endif |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 53 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 54 | # include "random.h" |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 55 | #else |
| 56 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 57 | #endif |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 58 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 59 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 60 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 61 | /* STANDALONE does not make sense, and won't compile */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 62 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 63 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 64 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 65 | # undef IF_NOT_FEATURE_SH_STANDALONE |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 66 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 67 | # define IF_FEATURE_SH_STANDALONE(...) |
| 68 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 71 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 72 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 73 | #endif |
| 74 | |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 75 | #if !BB_MMU |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 76 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 79 | //config:config ASH |
| 80 | //config: bool "ash" |
| 81 | //config: default y |
| 82 | //config: depends on !NOMMU |
| 83 | //config: help |
| 84 | //config: Tha 'ash' shell adds about 60k in the default configuration and is |
| 85 | //config: the most complete and most pedantically correct shell included with |
| 86 | //config: busybox. This shell is actually a derivative of the Debian 'dash' |
| 87 | //config: shell (by Herbert Xu), which was created by porting the 'ash' shell |
| 88 | //config: (written by Kenneth Almquist) from NetBSD. |
| 89 | //config: |
| 90 | //config:config ASH_BASH_COMPAT |
| 91 | //config: bool "bash-compatible extensions" |
| 92 | //config: default y |
| 93 | //config: depends on ASH |
| 94 | //config: help |
| 95 | //config: Enable bash-compatible extensions. |
| 96 | //config: |
Denys Vlasenko | 046341e | 2011-02-04 17:53:59 +0100 | [diff] [blame] | 97 | //config:config ASH_IDLE_TIMEOUT |
| 98 | //config: bool "Idle timeout variable" |
| 99 | //config: default n |
| 100 | //config: depends on ASH |
| 101 | //config: help |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 102 | //config: Enables bash-like auto-logout after $TMOUT seconds of idle time. |
Denys Vlasenko | 046341e | 2011-02-04 17:53:59 +0100 | [diff] [blame] | 103 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 104 | //config:config ASH_JOB_CONTROL |
| 105 | //config: bool "Job control" |
| 106 | //config: default y |
| 107 | //config: depends on ASH |
| 108 | //config: help |
| 109 | //config: Enable job control in the ash shell. |
| 110 | //config: |
| 111 | //config:config ASH_ALIAS |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 112 | //config: bool "Alias support" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 113 | //config: default y |
| 114 | //config: depends on ASH |
| 115 | //config: help |
| 116 | //config: Enable alias support in the ash shell. |
| 117 | //config: |
| 118 | //config:config ASH_GETOPTS |
| 119 | //config: bool "Builtin getopt to parse positional parameters" |
| 120 | //config: default y |
| 121 | //config: depends on ASH |
| 122 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 123 | //config: Enable support for getopts builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 124 | //config: |
| 125 | //config:config ASH_BUILTIN_ECHO |
| 126 | //config: bool "Builtin version of 'echo'" |
| 127 | //config: default y |
| 128 | //config: depends on ASH |
| 129 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 130 | //config: Enable support for echo builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 131 | //config: |
| 132 | //config:config ASH_BUILTIN_PRINTF |
| 133 | //config: bool "Builtin version of 'printf'" |
| 134 | //config: default y |
| 135 | //config: depends on ASH |
| 136 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 137 | //config: Enable support for printf builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 138 | //config: |
| 139 | //config:config ASH_BUILTIN_TEST |
| 140 | //config: bool "Builtin version of 'test'" |
| 141 | //config: default y |
| 142 | //config: depends on ASH |
| 143 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 144 | //config: Enable support for test builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 145 | //config: |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 146 | //config:config ASH_HELP |
| 147 | //config: bool "help builtin" |
| 148 | //config: default y |
| 149 | //config: depends on ASH |
| 150 | //config: help |
| 151 | //config: Enable help builtin in ash. |
| 152 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 153 | //config:config ASH_CMDCMD |
| 154 | //config: bool "'command' command to override shell builtins" |
| 155 | //config: default y |
| 156 | //config: depends on ASH |
| 157 | //config: help |
| 158 | //config: Enable support for the ash 'command' builtin, which allows |
| 159 | //config: you to run the specified command with the specified arguments, |
| 160 | //config: even when there is an ash builtin command with the same name. |
| 161 | //config: |
| 162 | //config:config ASH_MAIL |
| 163 | //config: bool "Check for new mail on interactive shells" |
| 164 | //config: default n |
| 165 | //config: depends on ASH |
| 166 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 167 | //config: Enable "check for new mail" function in the ash shell. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 168 | //config: |
| 169 | //config:config ASH_OPTIMIZE_FOR_SIZE |
| 170 | //config: bool "Optimize for size instead of speed" |
| 171 | //config: default y |
| 172 | //config: depends on ASH |
| 173 | //config: help |
| 174 | //config: Compile ash for reduced size at the price of speed. |
| 175 | //config: |
| 176 | //config:config ASH_RANDOM_SUPPORT |
| 177 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 178 | //config: default y |
| 179 | //config: depends on ASH |
| 180 | //config: help |
| 181 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 182 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 183 | //config: You can reset the generator by using a specified start value. |
| 184 | //config: After "unset RANDOM" the generator will switch off and this |
| 185 | //config: variable will no longer have special treatment. |
| 186 | //config: |
| 187 | //config:config ASH_EXPAND_PRMT |
| 188 | //config: bool "Expand prompt string" |
| 189 | //config: default y |
| 190 | //config: depends on ASH |
| 191 | //config: help |
| 192 | //config: "PS#" may contain volatile content, such as backquote commands. |
| 193 | //config: This option recreates the prompt string from the environment |
| 194 | //config: variable each time it is displayed. |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 195 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 196 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 197 | //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) |
| 198 | //applet:IF_FEATURE_SH_IS_ASH(APPLET_ODDNAME(sh, ash, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 199 | //applet:IF_FEATURE_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, BB_DIR_BIN, BB_SUID_DROP, bash)) |
| 200 | |
| 201 | //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o |
| 202 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o |
| 203 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 204 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 205 | /* ============ Hash table sizes. Configurable. */ |
| 206 | |
| 207 | #define VTABSIZE 39 |
| 208 | #define ATABSIZE 39 |
| 209 | #define CMDTABLESIZE 31 /* should be prime */ |
| 210 | |
| 211 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 212 | /* ============ Shell options */ |
| 213 | |
| 214 | static const char *const optletters_optnames[] = { |
| 215 | "e" "errexit", |
| 216 | "f" "noglob", |
| 217 | "I" "ignoreeof", |
| 218 | "i" "interactive", |
| 219 | "m" "monitor", |
| 220 | "n" "noexec", |
| 221 | "s" "stdin", |
| 222 | "x" "xtrace", |
| 223 | "v" "verbose", |
| 224 | "C" "noclobber", |
| 225 | "a" "allexport", |
| 226 | "b" "notify", |
| 227 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 228 | "\0" "vi" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 229 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 230 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 231 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 232 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 233 | ,"\0" "nolog" |
| 234 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 235 | #endif |
| 236 | }; |
| 237 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 238 | #define optletters(n) optletters_optnames[n][0] |
| 239 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 240 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 241 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 242 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 243 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 244 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 245 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 246 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 247 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 248 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 249 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 250 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 251 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 252 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 253 | * exception handlers, the user should save the value of handler on entry |
| 254 | * to an inner scope, set handler to point to a jmploc structure for the |
| 255 | * inner scope, and restore handler on exit from the scope. |
| 256 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 257 | struct jmploc { |
| 258 | jmp_buf loc; |
| 259 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 260 | |
| 261 | struct globals_misc { |
| 262 | /* pid of main shell */ |
| 263 | int rootpid; |
| 264 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 265 | int shlvl; |
| 266 | #define rootshell (!shlvl) |
| 267 | char *minusc; /* argument to -c option */ |
| 268 | |
| 269 | char *curdir; // = nullstr; /* current working directory */ |
| 270 | char *physdir; // = nullstr; /* physical working directory */ |
| 271 | |
| 272 | char *arg0; /* value of $0 */ |
| 273 | |
| 274 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 275 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 276 | volatile int suppress_int; /* counter */ |
| 277 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 278 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 279 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 280 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 281 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 282 | #define EXINT 0 /* SIGINT received */ |
| 283 | #define EXERROR 1 /* a generic error */ |
| 284 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 285 | #define EXEXEC 3 /* command execution failed */ |
| 286 | #define EXEXIT 4 /* exit the shell */ |
| 287 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 289 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 290 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 291 | |
| 292 | char optlist[NOPTS]; |
| 293 | #define eflag optlist[0] |
| 294 | #define fflag optlist[1] |
| 295 | #define Iflag optlist[2] |
| 296 | #define iflag optlist[3] |
| 297 | #define mflag optlist[4] |
| 298 | #define nflag optlist[5] |
| 299 | #define sflag optlist[6] |
| 300 | #define xflag optlist[7] |
| 301 | #define vflag optlist[8] |
| 302 | #define Cflag optlist[9] |
| 303 | #define aflag optlist[10] |
| 304 | #define bflag optlist[11] |
| 305 | #define uflag optlist[12] |
| 306 | #define viflag optlist[13] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 307 | #if ENABLE_ASH_BASH_COMPAT |
| 308 | # define pipefail optlist[14] |
| 309 | #else |
| 310 | # define pipefail 0 |
| 311 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 312 | #if DEBUG |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 313 | # define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT] |
| 314 | # define debug optlist[15 + ENABLE_ASH_BASH_COMPAT] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 315 | #endif |
| 316 | |
| 317 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 318 | /* |
| 319 | * Sigmode records the current value of the signal handlers for the various |
| 320 | * modes. A value of zero means that the current handler is not known. |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 321 | * S_HARD_IGN indicates that the signal was ignored on entry to the shell. |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 322 | */ |
| 323 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 324 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 325 | #define S_CATCH 2 /* signal is caught */ |
| 326 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 327 | #define S_HARD_IGN 4 /* signal is ignored permanently */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 328 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 329 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 330 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 331 | uint8_t may_have_traps; /* 0: definitely no traps are set, 1: some traps may be set */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 332 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 333 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 334 | |
| 335 | /* Rarely referenced stuff */ |
| 336 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 337 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 338 | #endif |
| 339 | pid_t backgndpid; /* pid of last background process */ |
| 340 | smallint job_warning; /* user was warned about stopped jobs (can be 2, 1 or 0). */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 341 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 342 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 343 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 344 | #define rootpid (G_misc.rootpid ) |
| 345 | #define shlvl (G_misc.shlvl ) |
| 346 | #define minusc (G_misc.minusc ) |
| 347 | #define curdir (G_misc.curdir ) |
| 348 | #define physdir (G_misc.physdir ) |
| 349 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 350 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 351 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 352 | #define suppress_int (G_misc.suppress_int ) |
| 353 | #define pending_int (G_misc.pending_int ) |
| 354 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 355 | #define isloginsh (G_misc.isloginsh ) |
| 356 | #define nullstr (G_misc.nullstr ) |
| 357 | #define optlist (G_misc.optlist ) |
| 358 | #define sigmode (G_misc.sigmode ) |
| 359 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 360 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 361 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 362 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 363 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 364 | #define backgndpid (G_misc.backgndpid ) |
| 365 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 366 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 367 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 368 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 369 | curdir = nullstr; \ |
| 370 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 371 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 372 | } while (0) |
| 373 | |
| 374 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 375 | /* ============ DEBUG */ |
| 376 | #if DEBUG |
| 377 | static void trace_printf(const char *fmt, ...); |
| 378 | static void trace_vprintf(const char *fmt, va_list va); |
| 379 | # define TRACE(param) trace_printf param |
| 380 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 381 | # define close(fd) do { \ |
| 382 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 383 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 384 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 385 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 386 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 387 | #else |
| 388 | # define TRACE(param) |
| 389 | # define TRACEV(param) |
| 390 | #endif |
| 391 | |
| 392 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 393 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 394 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 395 | |
Denys Vlasenko | 1961aea | 2013-02-26 00:36:53 +0100 | [diff] [blame] | 396 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 397 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 398 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 399 | static int isdigit_str9(const char *str) |
| 400 | { |
| 401 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 402 | while (--maxlen && isdigit(*str)) |
| 403 | str++; |
| 404 | return (*str == '\0'); |
| 405 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 406 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 407 | static const char *var_end(const char *var) |
| 408 | { |
| 409 | while (*var) |
| 410 | if (*var++ == '=') |
| 411 | break; |
| 412 | return var; |
| 413 | } |
| 414 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 415 | |
| 416 | /* ============ Interrupts / exceptions */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 417 | |
| 418 | static void exitshell(void) NORETURN; |
| 419 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 420 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 421 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 422 | * over a period of time. This is similar to SIGHOLD or to sigblock, but |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 423 | * much more efficient and portable. (But hacking the kernel is so much |
| 424 | * more fun than worrying about efficiency and portability. :-)) |
| 425 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 426 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 427 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 428 | xbarrier(); \ |
| 429 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 430 | |
| 431 | /* |
| 432 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 433 | * just do a longjmp to the exception handler. The type of exception is |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 434 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 435 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 436 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 437 | static void |
| 438 | raise_exception(int e) |
| 439 | { |
| 440 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 441 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 442 | abort(); |
| 443 | #endif |
| 444 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 445 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 446 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 447 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 448 | #if DEBUG |
| 449 | #define raise_exception(e) do { \ |
| 450 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 451 | raise_exception(e); \ |
| 452 | } while (0) |
| 453 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 457 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 458 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 459 | * are held using the INT_OFF macro. (The test for iflag is just |
| 460 | * defensive programming.) |
| 461 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 462 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 463 | static void |
| 464 | raise_interrupt(void) |
| 465 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 466 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 467 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 468 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 469 | /* Signal is not automatically unmasked after it is raised, |
| 470 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 471 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 472 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 473 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 474 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 475 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 476 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 477 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 478 | signal(SIGINT, SIG_DFL); |
| 479 | raise(SIGINT); |
| 480 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 481 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 482 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 483 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 484 | /* NOTREACHED */ |
| 485 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 486 | #if DEBUG |
| 487 | #define raise_interrupt() do { \ |
| 488 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 489 | raise_interrupt(); \ |
| 490 | } while (0) |
| 491 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 492 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 493 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 494 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 495 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 496 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 497 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 498 | raise_interrupt(); |
| 499 | } |
| 500 | } |
| 501 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 502 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 503 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 504 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 505 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 506 | suppress_int = 0; |
| 507 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 508 | raise_interrupt(); |
| 509 | } |
| 510 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 511 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 512 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 513 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 514 | #define RESTORE_INT(v) do { \ |
| 515 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 516 | suppress_int = (v); \ |
| 517 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 518 | raise_interrupt(); \ |
| 519 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 520 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 521 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 522 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 523 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 524 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 525 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 526 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 527 | INT_OFF; |
| 528 | fputs(p, file); |
| 529 | INT_ON; |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | flush_stdout_stderr(void) |
| 534 | { |
| 535 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 536 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 537 | INT_ON; |
| 538 | } |
| 539 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 540 | /* Was called outcslow(c,FILE*), but c was always '\n' */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 541 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 542 | newline_and_flush(FILE *dest) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 543 | { |
| 544 | INT_OFF; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 545 | putc('\n', dest); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 546 | fflush(dest); |
| 547 | INT_ON; |
| 548 | } |
| 549 | |
| 550 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 551 | static int |
| 552 | out1fmt(const char *fmt, ...) |
| 553 | { |
| 554 | va_list ap; |
| 555 | int r; |
| 556 | |
| 557 | INT_OFF; |
| 558 | va_start(ap, fmt); |
| 559 | r = vprintf(fmt, ap); |
| 560 | va_end(ap); |
| 561 | INT_ON; |
| 562 | return r; |
| 563 | } |
| 564 | |
| 565 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 566 | static int |
| 567 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 568 | { |
| 569 | va_list ap; |
| 570 | int ret; |
| 571 | |
| 572 | va_start(ap, fmt); |
| 573 | INT_OFF; |
| 574 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 575 | va_end(ap); |
| 576 | INT_ON; |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static void |
| 581 | out1str(const char *p) |
| 582 | { |
| 583 | outstr(p, stdout); |
| 584 | } |
| 585 | |
| 586 | static void |
| 587 | out2str(const char *p) |
| 588 | { |
| 589 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 590 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 594 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 595 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 596 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 597 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 598 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 599 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 600 | #define CTLENDVAR ((unsigned char)'\203') |
| 601 | #define CTLBACKQ ((unsigned char)'\204') |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 602 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 603 | #define CTLENDARI ((unsigned char)'\207') |
| 604 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 605 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 606 | |
| 607 | /* variable substitution byte (follows CTLVAR) */ |
| 608 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 609 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 610 | |
| 611 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 612 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 613 | #define VSMINUS 0x2 /* ${var-text} */ |
| 614 | #define VSPLUS 0x3 /* ${var+text} */ |
| 615 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 616 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 617 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 618 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 619 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 620 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 621 | #define VSLENGTH 0xa /* ${#var} */ |
| 622 | #if ENABLE_ASH_BASH_COMPAT |
| 623 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 624 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 625 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 626 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 627 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 628 | static const char dolatstr[] ALIGN1 = { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 629 | CTLQUOTEMARK, CTLVAR, VSNORMAL, '@', '=', CTLQUOTEMARK, '\0' |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 630 | }; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 631 | #define DOLATSTRLEN 6 |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 632 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 633 | #define NCMD 0 |
| 634 | #define NPIPE 1 |
| 635 | #define NREDIR 2 |
| 636 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 637 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 638 | #define NAND 5 |
| 639 | #define NOR 6 |
| 640 | #define NSEMI 7 |
| 641 | #define NIF 8 |
| 642 | #define NWHILE 9 |
| 643 | #define NUNTIL 10 |
| 644 | #define NFOR 11 |
| 645 | #define NCASE 12 |
| 646 | #define NCLIST 13 |
| 647 | #define NDEFUN 14 |
| 648 | #define NARG 15 |
| 649 | #define NTO 16 |
| 650 | #if ENABLE_ASH_BASH_COMPAT |
| 651 | #define NTO2 17 |
| 652 | #endif |
| 653 | #define NCLOBBER 18 |
| 654 | #define NFROM 19 |
| 655 | #define NFROMTO 20 |
| 656 | #define NAPPEND 21 |
| 657 | #define NTOFD 22 |
| 658 | #define NFROMFD 23 |
| 659 | #define NHERE 24 |
| 660 | #define NXHERE 25 |
| 661 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 662 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 663 | |
| 664 | union node; |
| 665 | |
| 666 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 667 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 668 | union node *assign; |
| 669 | union node *args; |
| 670 | union node *redirect; |
| 671 | }; |
| 672 | |
| 673 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 674 | smallint type; |
| 675 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 676 | struct nodelist *cmdlist; |
| 677 | }; |
| 678 | |
| 679 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 680 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 681 | union node *n; |
| 682 | union node *redirect; |
| 683 | }; |
| 684 | |
| 685 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 686 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 687 | union node *ch1; |
| 688 | union node *ch2; |
| 689 | }; |
| 690 | |
| 691 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 692 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 693 | union node *test; |
| 694 | union node *ifpart; |
| 695 | union node *elsepart; |
| 696 | }; |
| 697 | |
| 698 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 699 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 700 | union node *args; |
| 701 | union node *body; |
| 702 | char *var; |
| 703 | }; |
| 704 | |
| 705 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 706 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 707 | union node *expr; |
| 708 | union node *cases; |
| 709 | }; |
| 710 | |
| 711 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 712 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 713 | union node *next; |
| 714 | union node *pattern; |
| 715 | union node *body; |
| 716 | }; |
| 717 | |
| 718 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 719 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 720 | union node *next; |
| 721 | char *text; |
| 722 | struct nodelist *backquote; |
| 723 | }; |
| 724 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 725 | /* nfile and ndup layout must match! |
| 726 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 727 | * that it is actually NTO2 (>&file), and change its type. |
| 728 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 729 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 730 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 731 | union node *next; |
| 732 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 733 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 734 | union node *fname; |
| 735 | char *expfname; |
| 736 | }; |
| 737 | |
| 738 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 739 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 740 | union node *next; |
| 741 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 742 | int dupfd; |
| 743 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 744 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 745 | }; |
| 746 | |
| 747 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 748 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 749 | union node *next; |
| 750 | int fd; |
| 751 | union node *doc; |
| 752 | }; |
| 753 | |
| 754 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 755 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 756 | union node *com; |
| 757 | }; |
| 758 | |
| 759 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 760 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 761 | struct ncmd ncmd; |
| 762 | struct npipe npipe; |
| 763 | struct nredir nredir; |
| 764 | struct nbinary nbinary; |
| 765 | struct nif nif; |
| 766 | struct nfor nfor; |
| 767 | struct ncase ncase; |
| 768 | struct nclist nclist; |
| 769 | struct narg narg; |
| 770 | struct nfile nfile; |
| 771 | struct ndup ndup; |
| 772 | struct nhere nhere; |
| 773 | struct nnot nnot; |
| 774 | }; |
| 775 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 776 | /* |
| 777 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 778 | * It must be distinct from NULL. |
| 779 | */ |
| 780 | #define NODE_EOF ((union node *) -1L) |
| 781 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 782 | struct nodelist { |
| 783 | struct nodelist *next; |
| 784 | union node *n; |
| 785 | }; |
| 786 | |
| 787 | struct funcnode { |
| 788 | int count; |
| 789 | union node n; |
| 790 | }; |
| 791 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 792 | /* |
| 793 | * Free a parse tree. |
| 794 | */ |
| 795 | static void |
| 796 | freefunc(struct funcnode *f) |
| 797 | { |
| 798 | if (f && --f->count < 0) |
| 799 | free(f); |
| 800 | } |
| 801 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 802 | |
| 803 | /* ============ Debugging output */ |
| 804 | |
| 805 | #if DEBUG |
| 806 | |
| 807 | static FILE *tracefile; |
| 808 | |
| 809 | static void |
| 810 | trace_printf(const char *fmt, ...) |
| 811 | { |
| 812 | va_list va; |
| 813 | |
| 814 | if (debug != 1) |
| 815 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 816 | if (DEBUG_TIME) |
| 817 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 818 | if (DEBUG_PID) |
| 819 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 820 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 821 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 822 | va_start(va, fmt); |
| 823 | vfprintf(tracefile, fmt, va); |
| 824 | va_end(va); |
| 825 | } |
| 826 | |
| 827 | static void |
| 828 | trace_vprintf(const char *fmt, va_list va) |
| 829 | { |
| 830 | if (debug != 1) |
| 831 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 832 | if (DEBUG_TIME) |
| 833 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 834 | if (DEBUG_PID) |
| 835 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 836 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 837 | fprintf(tracefile, "pending s:%d i:%d(supp:%d) ", pending_sig, pending_int, suppress_int); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 838 | vfprintf(tracefile, fmt, va); |
| 839 | } |
| 840 | |
| 841 | static void |
| 842 | trace_puts(const char *s) |
| 843 | { |
| 844 | if (debug != 1) |
| 845 | return; |
| 846 | fputs(s, tracefile); |
| 847 | } |
| 848 | |
| 849 | static void |
| 850 | trace_puts_quoted(char *s) |
| 851 | { |
| 852 | char *p; |
| 853 | char c; |
| 854 | |
| 855 | if (debug != 1) |
| 856 | return; |
| 857 | putc('"', tracefile); |
| 858 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 859 | switch ((unsigned char)*p) { |
| 860 | case '\n': c = 'n'; goto backslash; |
| 861 | case '\t': c = 't'; goto backslash; |
| 862 | case '\r': c = 'r'; goto backslash; |
| 863 | case '\"': c = '\"'; goto backslash; |
| 864 | case '\\': c = '\\'; goto backslash; |
| 865 | case CTLESC: c = 'e'; goto backslash; |
| 866 | case CTLVAR: c = 'v'; goto backslash; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 867 | case CTLBACKQ: c = 'q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 868 | backslash: |
| 869 | putc('\\', tracefile); |
| 870 | putc(c, tracefile); |
| 871 | break; |
| 872 | default: |
| 873 | if (*p >= ' ' && *p <= '~') |
| 874 | putc(*p, tracefile); |
| 875 | else { |
| 876 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 877 | putc((*p >> 6) & 03, tracefile); |
| 878 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 879 | putc(*p & 07, tracefile); |
| 880 | } |
| 881 | break; |
| 882 | } |
| 883 | } |
| 884 | putc('"', tracefile); |
| 885 | } |
| 886 | |
| 887 | static void |
| 888 | trace_puts_args(char **ap) |
| 889 | { |
| 890 | if (debug != 1) |
| 891 | return; |
| 892 | if (!*ap) |
| 893 | return; |
| 894 | while (1) { |
| 895 | trace_puts_quoted(*ap); |
| 896 | if (!*++ap) { |
| 897 | putc('\n', tracefile); |
| 898 | break; |
| 899 | } |
| 900 | putc(' ', tracefile); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | static void |
| 905 | opentrace(void) |
| 906 | { |
| 907 | char s[100]; |
| 908 | #ifdef O_APPEND |
| 909 | int flags; |
| 910 | #endif |
| 911 | |
| 912 | if (debug != 1) { |
| 913 | if (tracefile) |
| 914 | fflush(tracefile); |
| 915 | /* leave open because libedit might be using it */ |
| 916 | return; |
| 917 | } |
| 918 | strcpy(s, "./trace"); |
| 919 | if (tracefile) { |
| 920 | if (!freopen(s, "a", tracefile)) { |
| 921 | fprintf(stderr, "Can't re-open %s\n", s); |
| 922 | debug = 0; |
| 923 | return; |
| 924 | } |
| 925 | } else { |
| 926 | tracefile = fopen(s, "a"); |
| 927 | if (tracefile == NULL) { |
| 928 | fprintf(stderr, "Can't open %s\n", s); |
| 929 | debug = 0; |
| 930 | return; |
| 931 | } |
| 932 | } |
| 933 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 934 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 935 | if (flags >= 0) |
| 936 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 937 | #endif |
| 938 | setlinebuf(tracefile); |
| 939 | fputs("\nTracing started.\n", tracefile); |
| 940 | } |
| 941 | |
| 942 | static void |
| 943 | indent(int amount, char *pfx, FILE *fp) |
| 944 | { |
| 945 | int i; |
| 946 | |
| 947 | for (i = 0; i < amount; i++) { |
| 948 | if (pfx && i == amount - 1) |
| 949 | fputs(pfx, fp); |
| 950 | putc('\t', fp); |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* little circular references here... */ |
| 955 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 956 | |
| 957 | static void |
| 958 | sharg(union node *arg, FILE *fp) |
| 959 | { |
| 960 | char *p; |
| 961 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 962 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 963 | |
| 964 | if (arg->type != NARG) { |
| 965 | out1fmt("<node type %d>\n", arg->type); |
| 966 | abort(); |
| 967 | } |
| 968 | bqlist = arg->narg.backquote; |
| 969 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 970 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 971 | case CTLESC: |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 972 | p++; |
| 973 | putc(*p, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 974 | break; |
| 975 | case CTLVAR: |
| 976 | putc('$', fp); |
| 977 | putc('{', fp); |
| 978 | subtype = *++p; |
| 979 | if (subtype == VSLENGTH) |
| 980 | putc('#', fp); |
| 981 | |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 982 | while (*p != '=') { |
| 983 | putc(*p, fp); |
| 984 | p++; |
| 985 | } |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 986 | |
| 987 | if (subtype & VSNUL) |
| 988 | putc(':', fp); |
| 989 | |
| 990 | switch (subtype & VSTYPE) { |
| 991 | case VSNORMAL: |
| 992 | putc('}', fp); |
| 993 | break; |
| 994 | case VSMINUS: |
| 995 | putc('-', fp); |
| 996 | break; |
| 997 | case VSPLUS: |
| 998 | putc('+', fp); |
| 999 | break; |
| 1000 | case VSQUESTION: |
| 1001 | putc('?', fp); |
| 1002 | break; |
| 1003 | case VSASSIGN: |
| 1004 | putc('=', fp); |
| 1005 | break; |
| 1006 | case VSTRIMLEFT: |
| 1007 | putc('#', fp); |
| 1008 | break; |
| 1009 | case VSTRIMLEFTMAX: |
| 1010 | putc('#', fp); |
| 1011 | putc('#', fp); |
| 1012 | break; |
| 1013 | case VSTRIMRIGHT: |
| 1014 | putc('%', fp); |
| 1015 | break; |
| 1016 | case VSTRIMRIGHTMAX: |
| 1017 | putc('%', fp); |
| 1018 | putc('%', fp); |
| 1019 | break; |
| 1020 | case VSLENGTH: |
| 1021 | break; |
| 1022 | default: |
| 1023 | out1fmt("<subtype %d>", subtype); |
| 1024 | } |
| 1025 | break; |
| 1026 | case CTLENDVAR: |
| 1027 | putc('}', fp); |
| 1028 | break; |
| 1029 | case CTLBACKQ: |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1030 | putc('$', fp); |
| 1031 | putc('(', fp); |
| 1032 | shtree(bqlist->n, -1, NULL, fp); |
| 1033 | putc(')', fp); |
| 1034 | break; |
| 1035 | default: |
| 1036 | putc(*p, fp); |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1042 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1043 | shcmd(union node *cmd, FILE *fp) |
| 1044 | { |
| 1045 | union node *np; |
| 1046 | int first; |
| 1047 | const char *s; |
| 1048 | int dftfd; |
| 1049 | |
| 1050 | first = 1; |
| 1051 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1052 | if (!first) |
| 1053 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1054 | sharg(np, fp); |
| 1055 | first = 0; |
| 1056 | } |
| 1057 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1058 | if (!first) |
| 1059 | putc(' ', fp); |
| 1060 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1061 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1062 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1063 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1064 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1065 | #if ENABLE_ASH_BASH_COMPAT |
| 1066 | case NTO2: |
| 1067 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1068 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1069 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1070 | case NFROMFD: s = "<&"; break; |
| 1071 | case NFROMTO: s = "<>"; break; |
| 1072 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1073 | } |
| 1074 | if (np->nfile.fd != dftfd) |
| 1075 | fprintf(fp, "%d", np->nfile.fd); |
| 1076 | fputs(s, fp); |
| 1077 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1078 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1079 | } else { |
| 1080 | sharg(np->nfile.fname, fp); |
| 1081 | } |
| 1082 | first = 0; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | static void |
| 1087 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1088 | { |
| 1089 | struct nodelist *lp; |
| 1090 | const char *s; |
| 1091 | |
| 1092 | if (n == NULL) |
| 1093 | return; |
| 1094 | |
| 1095 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1096 | |
| 1097 | if (n == NODE_EOF) { |
| 1098 | fputs("<EOF>", fp); |
| 1099 | return; |
| 1100 | } |
| 1101 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1102 | switch (n->type) { |
| 1103 | case NSEMI: |
| 1104 | s = "; "; |
| 1105 | goto binop; |
| 1106 | case NAND: |
| 1107 | s = " && "; |
| 1108 | goto binop; |
| 1109 | case NOR: |
| 1110 | s = " || "; |
| 1111 | binop: |
| 1112 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1113 | /* if (ind < 0) */ |
| 1114 | fputs(s, fp); |
| 1115 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1116 | break; |
| 1117 | case NCMD: |
| 1118 | shcmd(n, fp); |
| 1119 | if (ind >= 0) |
| 1120 | putc('\n', fp); |
| 1121 | break; |
| 1122 | case NPIPE: |
| 1123 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1124 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1125 | if (lp->next) |
| 1126 | fputs(" | ", fp); |
| 1127 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1128 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1129 | fputs(" &", fp); |
| 1130 | if (ind >= 0) |
| 1131 | putc('\n', fp); |
| 1132 | break; |
| 1133 | default: |
| 1134 | fprintf(fp, "<node type %d>", n->type); |
| 1135 | if (ind >= 0) |
| 1136 | putc('\n', fp); |
| 1137 | break; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | static void |
| 1142 | showtree(union node *n) |
| 1143 | { |
| 1144 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1145 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1148 | #endif /* DEBUG */ |
| 1149 | |
| 1150 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1151 | /* ============ Parser data */ |
| 1152 | |
| 1153 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1154 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1155 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1156 | struct strlist { |
| 1157 | struct strlist *next; |
| 1158 | char *text; |
| 1159 | }; |
| 1160 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1161 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1162 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1163 | struct strpush { |
| 1164 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1165 | char *prev_string; |
| 1166 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1167 | #if ENABLE_ASH_ALIAS |
| 1168 | struct alias *ap; /* if push was associated with an alias */ |
| 1169 | #endif |
| 1170 | char *string; /* remember the string since it may change */ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 1171 | |
| 1172 | /* Remember last two characters for pungetc. */ |
| 1173 | int lastc[2]; |
| 1174 | |
| 1175 | /* Number of outstanding calls to pungetc. */ |
| 1176 | int unget; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1177 | }; |
| 1178 | |
| 1179 | struct parsefile { |
| 1180 | struct parsefile *prev; /* preceding file on stack */ |
| 1181 | int linno; /* current line */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1182 | int pf_fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1183 | int left_in_line; /* number of chars left in this line */ |
| 1184 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1185 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1186 | char *buf; /* input buffer */ |
| 1187 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1188 | struct strpush basestrpush; /* so pushing one is fast */ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 1189 | |
| 1190 | /* Remember last two characters for pungetc. */ |
| 1191 | int lastc[2]; |
| 1192 | |
| 1193 | /* Number of outstanding calls to pungetc. */ |
| 1194 | int unget; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1195 | }; |
| 1196 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1197 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1198 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1199 | static int startlinno; /* line # where last token started */ |
| 1200 | static char *commandname; /* currently executing command */ |
| 1201 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1202 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1203 | |
| 1204 | |
| 1205 | /* ============ Message printing */ |
| 1206 | |
| 1207 | static void |
| 1208 | ash_vmsg(const char *msg, va_list ap) |
| 1209 | { |
| 1210 | fprintf(stderr, "%s: ", arg0); |
| 1211 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1212 | if (strcmp(arg0, commandname)) |
| 1213 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1214 | if (!iflag || g_parsefile->pf_fd > 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1215 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1216 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1217 | vfprintf(stderr, msg, ap); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 1218 | newline_and_flush(stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1219 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* |
| 1222 | * Exverror is called to raise the error exception. If the second argument |
| 1223 | * is not NULL then error prints an error message using printf style |
| 1224 | * formatting. It then raises the error exception. |
| 1225 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1226 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1227 | static void |
| 1228 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1229 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1230 | #if DEBUG |
| 1231 | if (msg) { |
| 1232 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1233 | TRACEV((msg, ap)); |
| 1234 | TRACE(("\") pid=%d\n", getpid())); |
| 1235 | } else |
| 1236 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1237 | if (msg) |
| 1238 | #endif |
| 1239 | ash_vmsg(msg, ap); |
| 1240 | |
| 1241 | flush_stdout_stderr(); |
| 1242 | raise_exception(cond); |
| 1243 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1244 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1245 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1246 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1247 | static void |
| 1248 | ash_msg_and_raise_error(const char *msg, ...) |
| 1249 | { |
| 1250 | va_list ap; |
| 1251 | |
| 1252 | va_start(ap, msg); |
| 1253 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1254 | /* NOTREACHED */ |
| 1255 | va_end(ap); |
| 1256 | } |
| 1257 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1258 | static void raise_error_syntax(const char *) NORETURN; |
| 1259 | static void |
| 1260 | raise_error_syntax(const char *msg) |
| 1261 | { |
| 1262 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1263 | /* NOTREACHED */ |
| 1264 | } |
| 1265 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1266 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1267 | static void |
| 1268 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1269 | { |
| 1270 | va_list ap; |
| 1271 | |
| 1272 | va_start(ap, msg); |
| 1273 | ash_vmsg_and_raise(cond, msg, ap); |
| 1274 | /* NOTREACHED */ |
| 1275 | va_end(ap); |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * error/warning routines for external builtins |
| 1280 | */ |
| 1281 | static void |
| 1282 | ash_msg(const char *fmt, ...) |
| 1283 | { |
| 1284 | va_list ap; |
| 1285 | |
| 1286 | va_start(ap, fmt); |
| 1287 | ash_vmsg(fmt, ap); |
| 1288 | va_end(ap); |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Return a string describing an error. The returned string may be a |
| 1293 | * pointer to a static buffer that will be overwritten on the next call. |
| 1294 | * Action describes the operation that got the error. |
| 1295 | */ |
| 1296 | static const char * |
| 1297 | errmsg(int e, const char *em) |
| 1298 | { |
| 1299 | if (e == ENOENT || e == ENOTDIR) { |
| 1300 | return em; |
| 1301 | } |
| 1302 | return strerror(e); |
| 1303 | } |
| 1304 | |
| 1305 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1306 | /* ============ Memory allocation */ |
| 1307 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1308 | #if 0 |
| 1309 | /* I consider these wrappers nearly useless: |
| 1310 | * ok, they return you to nearest exception handler, but |
| 1311 | * how much memory do you leak in the process, making |
| 1312 | * memory starvation worse? |
| 1313 | */ |
| 1314 | static void * |
| 1315 | ckrealloc(void * p, size_t nbytes) |
| 1316 | { |
| 1317 | p = realloc(p, nbytes); |
| 1318 | if (!p) |
| 1319 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1320 | return p; |
| 1321 | } |
| 1322 | |
| 1323 | static void * |
| 1324 | ckmalloc(size_t nbytes) |
| 1325 | { |
| 1326 | return ckrealloc(NULL, nbytes); |
| 1327 | } |
| 1328 | |
| 1329 | static void * |
| 1330 | ckzalloc(size_t nbytes) |
| 1331 | { |
| 1332 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1333 | } |
| 1334 | |
| 1335 | static char * |
| 1336 | ckstrdup(const char *s) |
| 1337 | { |
| 1338 | char *p = strdup(s); |
| 1339 | if (!p) |
| 1340 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1341 | return p; |
| 1342 | } |
| 1343 | #else |
| 1344 | /* Using bbox equivalents. They exit if out of memory */ |
| 1345 | # define ckrealloc xrealloc |
| 1346 | # define ckmalloc xmalloc |
| 1347 | # define ckzalloc xzalloc |
| 1348 | # define ckstrdup xstrdup |
| 1349 | #endif |
| 1350 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1351 | /* |
| 1352 | * It appears that grabstackstr() will barf with such alignments |
| 1353 | * because stalloc() will return a string allocated in a new stackblock. |
| 1354 | */ |
| 1355 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1356 | enum { |
| 1357 | /* Most machines require the value returned from malloc to be aligned |
| 1358 | * in some way. The following macro will get this right |
| 1359 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1360 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1361 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1362 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1363 | }; |
| 1364 | |
| 1365 | struct stack_block { |
| 1366 | struct stack_block *prev; |
| 1367 | char space[MINSIZE]; |
| 1368 | }; |
| 1369 | |
| 1370 | struct stackmark { |
| 1371 | struct stack_block *stackp; |
| 1372 | char *stacknxt; |
| 1373 | size_t stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1374 | }; |
| 1375 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1376 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1377 | struct globals_memstack { |
| 1378 | struct stack_block *g_stackp; // = &stackbase; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1379 | char *g_stacknxt; // = stackbase.space; |
| 1380 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1381 | size_t g_stacknleft; // = MINSIZE; |
| 1382 | int herefd; // = -1; |
| 1383 | struct stack_block stackbase; |
| 1384 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1385 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1386 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1387 | #define g_stackp (G_memstack.g_stackp ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1388 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1389 | #define sstrend (G_memstack.sstrend ) |
| 1390 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1391 | #define herefd (G_memstack.herefd ) |
| 1392 | #define stackbase (G_memstack.stackbase ) |
| 1393 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1394 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1395 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1396 | g_stackp = &stackbase; \ |
| 1397 | g_stacknxt = stackbase.space; \ |
| 1398 | g_stacknleft = MINSIZE; \ |
| 1399 | sstrend = stackbase.space + MINSIZE; \ |
| 1400 | herefd = -1; \ |
| 1401 | } while (0) |
| 1402 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1403 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1404 | #define stackblock() ((void *)g_stacknxt) |
| 1405 | #define stackblocksize() g_stacknleft |
| 1406 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1407 | /* |
| 1408 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1409 | * to make this more efficient, and also to avoid all sorts of exception |
| 1410 | * handling code to handle interrupts in the middle of a parse. |
| 1411 | * |
| 1412 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1413 | * well. |
| 1414 | */ |
| 1415 | static void * |
| 1416 | stalloc(size_t nbytes) |
| 1417 | { |
| 1418 | char *p; |
| 1419 | size_t aligned; |
| 1420 | |
| 1421 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1422 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1423 | size_t len; |
| 1424 | size_t blocksize; |
| 1425 | struct stack_block *sp; |
| 1426 | |
| 1427 | blocksize = aligned; |
| 1428 | if (blocksize < MINSIZE) |
| 1429 | blocksize = MINSIZE; |
| 1430 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1431 | if (len < blocksize) |
| 1432 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1433 | INT_OFF; |
| 1434 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1435 | sp->prev = g_stackp; |
| 1436 | g_stacknxt = sp->space; |
| 1437 | g_stacknleft = blocksize; |
| 1438 | sstrend = g_stacknxt + blocksize; |
| 1439 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1440 | INT_ON; |
| 1441 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1442 | p = g_stacknxt; |
| 1443 | g_stacknxt += aligned; |
| 1444 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1445 | return p; |
| 1446 | } |
| 1447 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1448 | static void * |
| 1449 | stzalloc(size_t nbytes) |
| 1450 | { |
| 1451 | return memset(stalloc(nbytes), 0, nbytes); |
| 1452 | } |
| 1453 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1454 | static void |
| 1455 | stunalloc(void *p) |
| 1456 | { |
| 1457 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1458 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1459 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1460 | abort(); |
| 1461 | } |
| 1462 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1463 | g_stacknleft += g_stacknxt - (char *)p; |
| 1464 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1467 | /* |
| 1468 | * Like strdup but works with the ash stack. |
| 1469 | */ |
| 1470 | static char * |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 1471 | sstrdup(const char *p) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1472 | { |
| 1473 | size_t len = strlen(p) + 1; |
| 1474 | return memcpy(stalloc(len), p, len); |
| 1475 | } |
| 1476 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1477 | static void |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1478 | grabstackblock(size_t len) |
| 1479 | { |
| 1480 | len = SHELL_ALIGN(len); |
| 1481 | g_stacknxt += len; |
| 1482 | g_stacknleft -= len; |
| 1483 | } |
| 1484 | |
| 1485 | static void |
| 1486 | pushstackmark(struct stackmark *mark, size_t len) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1487 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1488 | mark->stackp = g_stackp; |
| 1489 | mark->stacknxt = g_stacknxt; |
| 1490 | mark->stacknleft = g_stacknleft; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 1491 | grabstackblock(len); |
| 1492 | } |
| 1493 | |
| 1494 | static void |
| 1495 | setstackmark(struct stackmark *mark) |
| 1496 | { |
| 1497 | pushstackmark(mark, g_stacknxt == g_stackp->space && g_stackp != &stackbase); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static void |
| 1501 | popstackmark(struct stackmark *mark) |
| 1502 | { |
| 1503 | struct stack_block *sp; |
| 1504 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1505 | if (!mark->stackp) |
| 1506 | return; |
| 1507 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1508 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1509 | while (g_stackp != mark->stackp) { |
| 1510 | sp = g_stackp; |
| 1511 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1512 | free(sp); |
| 1513 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1514 | g_stacknxt = mark->stacknxt; |
| 1515 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1516 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1517 | INT_ON; |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * When the parser reads in a string, it wants to stick the string on the |
| 1522 | * stack and only adjust the stack pointer when it knows how big the |
| 1523 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1524 | * of space on top of the stack and stackblocklen returns the length of |
| 1525 | * this block. Growstackblock will grow this space by at least one byte, |
| 1526 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1527 | * part of the block that has been used. |
| 1528 | */ |
| 1529 | static void |
| 1530 | growstackblock(void) |
| 1531 | { |
| 1532 | size_t newlen; |
| 1533 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1534 | newlen = g_stacknleft * 2; |
| 1535 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1536 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1537 | if (newlen < 128) |
| 1538 | newlen += 128; |
| 1539 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1540 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1541 | struct stack_block *sp; |
| 1542 | struct stack_block *prevstackp; |
| 1543 | size_t grosslen; |
| 1544 | |
| 1545 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1546 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1547 | prevstackp = sp->prev; |
| 1548 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1549 | sp = ckrealloc(sp, grosslen); |
| 1550 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1551 | g_stackp = sp; |
| 1552 | g_stacknxt = sp->space; |
| 1553 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1554 | sstrend = sp->space + newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1555 | INT_ON; |
| 1556 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1557 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1558 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1559 | char *p = stalloc(newlen); |
| 1560 | |
| 1561 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1562 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1563 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1567 | /* |
| 1568 | * The following routines are somewhat easier to use than the above. |
| 1569 | * The user declares a variable of type STACKSTR, which may be declared |
| 1570 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1571 | * the user uses the macro STPUTC to add characters to the string. In |
| 1572 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1573 | * grown as necessary. When the user is done, she can just leave the |
| 1574 | * string there and refer to it using stackblock(). Or she can allocate |
| 1575 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1576 | * someone else to use the stack temporarily and then continue to grow |
| 1577 | * the string, the user should use grabstack to allocate the space, and |
| 1578 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1579 | * |
| 1580 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1581 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1582 | * is space for at least one character. |
| 1583 | */ |
| 1584 | static void * |
| 1585 | growstackstr(void) |
| 1586 | { |
| 1587 | size_t len = stackblocksize(); |
| 1588 | if (herefd >= 0 && len >= 1024) { |
| 1589 | full_write(herefd, stackblock(), len); |
| 1590 | return stackblock(); |
| 1591 | } |
| 1592 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1593 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * Called from CHECKSTRSPACE. |
| 1598 | */ |
| 1599 | static char * |
| 1600 | makestrspace(size_t newlen, char *p) |
| 1601 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1602 | size_t len = p - g_stacknxt; |
Denys Vlasenko | 53d6e03 | 2016-09-30 11:24:12 +0200 | [diff] [blame] | 1603 | size_t size; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1604 | |
| 1605 | for (;;) { |
| 1606 | size_t nleft; |
| 1607 | |
| 1608 | size = stackblocksize(); |
| 1609 | nleft = size - len; |
| 1610 | if (nleft >= newlen) |
| 1611 | break; |
| 1612 | growstackblock(); |
| 1613 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1614 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | static char * |
| 1618 | stack_nputstr(const char *s, size_t n, char *p) |
| 1619 | { |
| 1620 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1621 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1622 | return p; |
| 1623 | } |
| 1624 | |
| 1625 | static char * |
| 1626 | stack_putstr(const char *s, char *p) |
| 1627 | { |
| 1628 | return stack_nputstr(s, strlen(s), p); |
| 1629 | } |
| 1630 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1631 | static char * |
| 1632 | _STPUTC(int c, char *p) |
| 1633 | { |
| 1634 | if (p == sstrend) |
| 1635 | p = growstackstr(); |
| 1636 | *p++ = c; |
| 1637 | return p; |
| 1638 | } |
| 1639 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1640 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1641 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1642 | #define CHECKSTRSPACE(n, p) do { \ |
| 1643 | char *q = (p); \ |
| 1644 | size_t l = (n); \ |
| 1645 | size_t m = sstrend - q; \ |
| 1646 | if (l > m) \ |
| 1647 | (p) = makestrspace(l, q); \ |
| 1648 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1649 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1650 | #define STACKSTRNUL(p) do { \ |
| 1651 | if ((p) == sstrend) \ |
| 1652 | (p) = growstackstr(); \ |
| 1653 | *(p) = '\0'; \ |
| 1654 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1655 | #define STUNPUTC(p) (--(p)) |
| 1656 | #define STTOPC(p) ((p)[-1]) |
| 1657 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1658 | |
| 1659 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1660 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1661 | #define stackstrend() ((void *)sstrend) |
| 1662 | |
| 1663 | |
| 1664 | /* ============ String helpers */ |
| 1665 | |
| 1666 | /* |
| 1667 | * prefix -- see if pfx is a prefix of string. |
| 1668 | */ |
| 1669 | static char * |
| 1670 | prefix(const char *string, const char *pfx) |
| 1671 | { |
| 1672 | while (*pfx) { |
| 1673 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1674 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1675 | } |
| 1676 | return (char *) string; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Check for a valid number. This should be elsewhere. |
| 1681 | */ |
| 1682 | static int |
| 1683 | is_number(const char *p) |
| 1684 | { |
| 1685 | do { |
| 1686 | if (!isdigit(*p)) |
| 1687 | return 0; |
| 1688 | } while (*++p != '\0'); |
| 1689 | return 1; |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | * Convert a string of digits to an integer, printing an error message on |
| 1694 | * failure. |
| 1695 | */ |
| 1696 | static int |
| 1697 | number(const char *s) |
| 1698 | { |
| 1699 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1700 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1701 | return atoi(s); |
| 1702 | } |
| 1703 | |
| 1704 | /* |
| 1705 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1706 | * The return string is allocated on the stack. |
| 1707 | */ |
| 1708 | static char * |
| 1709 | single_quote(const char *s) |
| 1710 | { |
| 1711 | char *p; |
| 1712 | |
| 1713 | STARTSTACKSTR(p); |
| 1714 | |
| 1715 | do { |
| 1716 | char *q; |
| 1717 | size_t len; |
| 1718 | |
| 1719 | len = strchrnul(s, '\'') - s; |
| 1720 | |
| 1721 | q = p = makestrspace(len + 3, p); |
| 1722 | |
| 1723 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1724 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1725 | *q++ = '\''; |
| 1726 | s += len; |
| 1727 | |
| 1728 | STADJUST(q - p, p); |
| 1729 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1730 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1731 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1732 | len = 0; |
| 1733 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1734 | |
| 1735 | q = p = makestrspace(len + 3, p); |
| 1736 | |
| 1737 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1738 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1739 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1740 | |
| 1741 | STADJUST(q - p, p); |
| 1742 | } while (*s); |
| 1743 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1744 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1745 | |
| 1746 | return stackblock(); |
| 1747 | } |
| 1748 | |
| 1749 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1750 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1751 | |
| 1752 | static char **argptr; /* argument list for builtin commands */ |
| 1753 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1754 | static char *optptr; /* used by nextopt */ |
| 1755 | |
| 1756 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1757 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1758 | * The library getopt must have the BSD extension static variable |
| 1759 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1760 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1761 | * Standard option processing (a la getopt) for builtin routines. |
| 1762 | * The only argument that is passed to nextopt is the option string; |
| 1763 | * the other arguments are unnecessary. It returns the character, |
| 1764 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1765 | */ |
| 1766 | static int |
| 1767 | nextopt(const char *optstring) |
| 1768 | { |
| 1769 | char *p; |
| 1770 | const char *q; |
| 1771 | char c; |
| 1772 | |
| 1773 | p = optptr; |
| 1774 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1775 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1776 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1777 | if (p == NULL) |
| 1778 | return '\0'; |
| 1779 | if (*p != '-') |
| 1780 | return '\0'; |
| 1781 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1782 | return '\0'; |
| 1783 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1784 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1785 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1786 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1787 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1788 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1789 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1790 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1791 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1792 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1793 | if (*++q == ':') |
| 1794 | q++; |
| 1795 | } |
| 1796 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1797 | if (*p == '\0') { |
| 1798 | p = *argptr++; |
| 1799 | if (p == NULL) |
| 1800 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1801 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1802 | optionarg = p; |
| 1803 | p = NULL; |
| 1804 | } |
| 1805 | optptr = p; |
| 1806 | return c; |
| 1807 | } |
| 1808 | |
| 1809 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1810 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1811 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1812 | /* |
| 1813 | * The parsefile structure pointed to by the global variable parsefile |
| 1814 | * contains information about the current file being read. |
| 1815 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1816 | struct shparam { |
| 1817 | int nparam; /* # of positional parameters (without $0) */ |
| 1818 | #if ENABLE_ASH_GETOPTS |
| 1819 | int optind; /* next parameter to be processed by getopts */ |
| 1820 | int optoff; /* used by getopts */ |
| 1821 | #endif |
| 1822 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1823 | char **p; /* parameter list */ |
| 1824 | }; |
| 1825 | |
| 1826 | /* |
| 1827 | * Free the list of positional parameters. |
| 1828 | */ |
| 1829 | static void |
| 1830 | freeparam(volatile struct shparam *param) |
| 1831 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1832 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1833 | char **ap, **ap1; |
| 1834 | ap = ap1 = param->p; |
| 1835 | while (*ap) |
| 1836 | free(*ap++); |
| 1837 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1842 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1843 | #endif |
| 1844 | |
| 1845 | struct var { |
| 1846 | struct var *next; /* next entry in hash list */ |
| 1847 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1848 | const char *var_text; /* name=value */ |
| 1849 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1850 | /* the variable gets set/unset */ |
| 1851 | }; |
| 1852 | |
| 1853 | struct localvar { |
| 1854 | struct localvar *next; /* next local variable in list */ |
| 1855 | struct var *vp; /* the variable that was made local */ |
| 1856 | int flags; /* saved flags */ |
| 1857 | const char *text; /* saved text */ |
| 1858 | }; |
| 1859 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1860 | /* flags */ |
| 1861 | #define VEXPORT 0x01 /* variable is exported */ |
| 1862 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1863 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1864 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1865 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1866 | #define VUNSET 0x20 /* the variable is not set */ |
| 1867 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1868 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1869 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1870 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1871 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1872 | #else |
| 1873 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1874 | #endif |
| 1875 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1876 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1877 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1878 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1879 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1880 | change_lc_all(const char *value) |
| 1881 | { |
| 1882 | if (value && *value != '\0') |
| 1883 | setlocale(LC_ALL, value); |
| 1884 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1885 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1886 | change_lc_ctype(const char *value) |
| 1887 | { |
| 1888 | if (value && *value != '\0') |
| 1889 | setlocale(LC_CTYPE, value); |
| 1890 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1891 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1892 | #if ENABLE_ASH_MAIL |
| 1893 | static void chkmail(void); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 1894 | static void changemail(const char *var_value) FAST_FUNC; |
| 1895 | #else |
| 1896 | # define chkmail() ((void)0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1897 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1898 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1899 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1900 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1901 | #endif |
| 1902 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1903 | static const struct { |
| 1904 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1905 | const char *var_text; |
| 1906 | void (*var_func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1907 | } varinit_data[] = { |
Denys Vlasenko | 566a313 | 2012-07-07 21:40:35 +0200 | [diff] [blame] | 1908 | /* |
| 1909 | * Note: VEXPORT would not work correctly here for NOFORK applets: |
| 1910 | * some environment strings may be constant. |
| 1911 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1912 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1913 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1914 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 1915 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1916 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1917 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1918 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1919 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1920 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1921 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1922 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1923 | #endif |
| 1924 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1925 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1926 | #endif |
| 1927 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1928 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 1929 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1930 | #endif |
| 1931 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1932 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1933 | #endif |
| 1934 | }; |
| 1935 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1936 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1937 | |
| 1938 | struct globals_var { |
| 1939 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1940 | struct redirtab *redirlist; |
| 1941 | int g_nullredirs; |
| 1942 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1943 | struct var *vartab[VTABSIZE]; |
| 1944 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1945 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1946 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1947 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1948 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1949 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1950 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1951 | #define preverrout_fd (G_var.preverrout_fd) |
| 1952 | #define vartab (G_var.vartab ) |
| 1953 | #define varinit (G_var.varinit ) |
| 1954 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1955 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1956 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1957 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1958 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1959 | varinit[i].flags = varinit_data[i].flags; \ |
| 1960 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 1961 | varinit[i].var_func = varinit_data[i].var_func; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1962 | } \ |
| 1963 | } while (0) |
| 1964 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1965 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1966 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1967 | # define vmail (&vifs)[1] |
| 1968 | # define vmpath (&vmail)[1] |
| 1969 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1970 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1971 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1972 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1973 | #define vps1 (&vpath)[1] |
| 1974 | #define vps2 (&vps1)[1] |
| 1975 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1976 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1977 | # define voptind (&vps4)[1] |
| 1978 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1979 | # define vrandom (&voptind)[1] |
| 1980 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1981 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1982 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1983 | # define vrandom (&vps4)[1] |
| 1984 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1985 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1986 | |
| 1987 | /* |
| 1988 | * The following macros access the values of the above variables. |
| 1989 | * They have to skip over the name. They return the null string |
| 1990 | * for unset variables. |
| 1991 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1992 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1993 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1994 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1995 | # define mailval() (vmail.var_text + 5) |
| 1996 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1997 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1998 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1999 | #define pathval() (vpath.var_text + 5) |
| 2000 | #define ps1val() (vps1.var_text + 4) |
| 2001 | #define ps2val() (vps2.var_text + 4) |
| 2002 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2003 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2004 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2005 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2006 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2007 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2008 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2009 | getoptsreset(const char *value) |
| 2010 | { |
| 2011 | shellparam.optind = number(value); |
| 2012 | shellparam.optoff = -1; |
| 2013 | } |
| 2014 | #endif |
| 2015 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2016 | /* |
| 2017 | * Compares two strings up to the first = or '\0'. The first |
| 2018 | * string must be terminated by '='; the second may be terminated by |
| 2019 | * either '=' or '\0'. |
| 2020 | */ |
| 2021 | static int |
| 2022 | varcmp(const char *p, const char *q) |
| 2023 | { |
| 2024 | int c, d; |
| 2025 | |
| 2026 | while ((c = *p) == (d = *q)) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2027 | if (c == '\0' || c == '=') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2028 | goto out; |
| 2029 | p++; |
| 2030 | q++; |
| 2031 | } |
| 2032 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2033 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2034 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2035 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2036 | out: |
| 2037 | return c - d; |
| 2038 | } |
| 2039 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2040 | /* |
| 2041 | * Find the appropriate entry in the hash table from the name. |
| 2042 | */ |
| 2043 | static struct var ** |
| 2044 | hashvar(const char *p) |
| 2045 | { |
| 2046 | unsigned hashval; |
| 2047 | |
| 2048 | hashval = ((unsigned char) *p) << 4; |
| 2049 | while (*p && *p != '=') |
| 2050 | hashval += (unsigned char) *p++; |
| 2051 | return &vartab[hashval % VTABSIZE]; |
| 2052 | } |
| 2053 | |
| 2054 | static int |
| 2055 | vpcmp(const void *a, const void *b) |
| 2056 | { |
| 2057 | return varcmp(*(const char **)a, *(const char **)b); |
| 2058 | } |
| 2059 | |
| 2060 | /* |
| 2061 | * This routine initializes the builtin variables. |
| 2062 | */ |
| 2063 | static void |
| 2064 | initvar(void) |
| 2065 | { |
| 2066 | struct var *vp; |
| 2067 | struct var *end; |
| 2068 | struct var **vpp; |
| 2069 | |
| 2070 | /* |
| 2071 | * PS1 depends on uid |
| 2072 | */ |
| 2073 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2074 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2075 | #else |
| 2076 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2077 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2078 | #endif |
| 2079 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2080 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2081 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2082 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2083 | vp->next = *vpp; |
| 2084 | *vpp = vp; |
| 2085 | } while (++vp < end); |
| 2086 | } |
| 2087 | |
| 2088 | static struct var ** |
| 2089 | findvar(struct var **vpp, const char *name) |
| 2090 | { |
| 2091 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2092 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2093 | break; |
| 2094 | } |
| 2095 | } |
| 2096 | return vpp; |
| 2097 | } |
| 2098 | |
| 2099 | /* |
| 2100 | * Find the value of a variable. Returns NULL if not set. |
| 2101 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2102 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2103 | lookupvar(const char *name) |
| 2104 | { |
| 2105 | struct var *v; |
| 2106 | |
| 2107 | v = *findvar(hashvar(name), name); |
| 2108 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2109 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2110 | /* |
| 2111 | * Dynamic variables are implemented roughly the same way they are |
| 2112 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2113 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2114 | * lookup will no longer happen at that point. -- PFM. |
| 2115 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2116 | if (v->flags & VDYNAMIC) |
| 2117 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2118 | #endif |
| 2119 | if (!(v->flags & VUNSET)) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2120 | return var_end(v->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2121 | } |
| 2122 | return NULL; |
| 2123 | } |
| 2124 | |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 2125 | static void reinit_unicode_for_ash(void) |
| 2126 | { |
| 2127 | /* Unicode support should be activated even if LANG is set |
| 2128 | * _during_ shell execution, not only if it was set when |
| 2129 | * shell was started. Therefore, re-check LANG every time: |
| 2130 | */ |
| 2131 | if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
| 2132 | || ENABLE_UNICODE_USING_LOCALE |
| 2133 | ) { |
| 2134 | const char *s = lookupvar("LC_ALL"); |
| 2135 | if (!s) s = lookupvar("LC_CTYPE"); |
| 2136 | if (!s) s = lookupvar("LANG"); |
| 2137 | reinit_unicode(s); |
| 2138 | } |
| 2139 | } |
| 2140 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2141 | /* |
| 2142 | * Search the environment of a builtin command. |
| 2143 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2144 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2145 | bltinlookup(const char *name) |
| 2146 | { |
| 2147 | struct strlist *sp; |
| 2148 | |
| 2149 | for (sp = cmdenviron; sp; sp = sp->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2150 | if (varcmp(sp->text, name) == 0) |
| 2151 | return var_end(sp->text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2152 | } |
| 2153 | return lookupvar(name); |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * Same as setvar except that the variable and value are passed in |
| 2158 | * the first argument as name=value. Since the first argument will |
| 2159 | * be actually stored in the table, it should not be a string that |
| 2160 | * will go away. |
| 2161 | * Called with interrupts off. |
| 2162 | */ |
| 2163 | static void |
| 2164 | setvareq(char *s, int flags) |
| 2165 | { |
| 2166 | struct var *vp, **vpp; |
| 2167 | |
| 2168 | vpp = hashvar(s); |
| 2169 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2170 | vp = *findvar(vpp, s); |
| 2171 | if (vp) { |
| 2172 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2173 | const char *n; |
| 2174 | |
| 2175 | if (flags & VNOSAVE) |
| 2176 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2177 | n = vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2178 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2179 | } |
| 2180 | |
| 2181 | if (flags & VNOSET) |
| 2182 | return; |
| 2183 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2184 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2185 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2186 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2187 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2188 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2189 | |
| 2190 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2191 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2192 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2193 | if (flags & VNOSET) |
| 2194 | return; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2195 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2196 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2197 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2198 | *vpp = vp; |
| 2199 | } |
| 2200 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2201 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2202 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2203 | vp->flags = flags; |
| 2204 | } |
| 2205 | |
| 2206 | /* |
| 2207 | * Set the value of a variable. The flags argument is ored with the |
| 2208 | * flags of the variable. If val is NULL, the variable is unset. |
| 2209 | */ |
| 2210 | static void |
| 2211 | setvar(const char *name, const char *val, int flags) |
| 2212 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2213 | const char *q; |
| 2214 | char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2215 | char *nameeq; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2216 | size_t namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2217 | size_t vallen; |
| 2218 | |
| 2219 | q = endofname(name); |
| 2220 | p = strchrnul(q, '='); |
| 2221 | namelen = p - name; |
| 2222 | if (!namelen || p != q) |
| 2223 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2224 | vallen = 0; |
| 2225 | if (val == NULL) { |
| 2226 | flags |= VUNSET; |
| 2227 | } else { |
| 2228 | vallen = strlen(val); |
| 2229 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2230 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2231 | INT_OFF; |
| 2232 | nameeq = ckmalloc(namelen + vallen + 2); |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2233 | p = memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2234 | if (val) { |
| 2235 | *p++ = '='; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2236 | p = memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2237 | } |
| 2238 | *p = '\0'; |
| 2239 | setvareq(nameeq, flags | VNOSAVE); |
| 2240 | INT_ON; |
| 2241 | } |
| 2242 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2243 | static void FAST_FUNC |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2244 | setvar0(const char *name, const char *val) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2245 | { |
| 2246 | setvar(name, val, 0); |
| 2247 | } |
| 2248 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2249 | #if ENABLE_ASH_GETOPTS |
| 2250 | /* |
| 2251 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2252 | */ |
| 2253 | static int |
| 2254 | setvarsafe(const char *name, const char *val, int flags) |
| 2255 | { |
| 2256 | int err; |
| 2257 | volatile int saveint; |
| 2258 | struct jmploc *volatile savehandler = exception_handler; |
| 2259 | struct jmploc jmploc; |
| 2260 | |
| 2261 | SAVE_INT(saveint); |
| 2262 | if (setjmp(jmploc.loc)) |
| 2263 | err = 1; |
| 2264 | else { |
| 2265 | exception_handler = &jmploc; |
| 2266 | setvar(name, val, flags); |
| 2267 | err = 0; |
| 2268 | } |
| 2269 | exception_handler = savehandler; |
| 2270 | RESTORE_INT(saveint); |
| 2271 | return err; |
| 2272 | } |
| 2273 | #endif |
| 2274 | |
| 2275 | /* |
| 2276 | * Unset the specified variable. |
| 2277 | */ |
| 2278 | static int |
| 2279 | unsetvar(const char *s) |
| 2280 | { |
| 2281 | struct var **vpp; |
| 2282 | struct var *vp; |
| 2283 | int retval; |
| 2284 | |
| 2285 | vpp = findvar(hashvar(s), s); |
| 2286 | vp = *vpp; |
| 2287 | retval = 2; |
| 2288 | if (vp) { |
| 2289 | int flags = vp->flags; |
| 2290 | |
| 2291 | retval = 1; |
| 2292 | if (flags & VREADONLY) |
| 2293 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2294 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2295 | vp->flags &= ~VDYNAMIC; |
| 2296 | #endif |
| 2297 | if (flags & VUNSET) |
| 2298 | goto ok; |
| 2299 | if ((flags & VSTRFIXED) == 0) { |
| 2300 | INT_OFF; |
| 2301 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2302 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2303 | *vpp = vp->next; |
| 2304 | free(vp); |
| 2305 | INT_ON; |
| 2306 | } else { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 2307 | setvar0(s, NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2308 | vp->flags &= ~VEXPORT; |
| 2309 | } |
| 2310 | ok: |
| 2311 | retval = 0; |
| 2312 | } |
| 2313 | out: |
| 2314 | return retval; |
| 2315 | } |
| 2316 | |
| 2317 | /* |
| 2318 | * Process a linked list of variable assignments. |
| 2319 | */ |
| 2320 | static void |
| 2321 | listsetvar(struct strlist *list_set_var, int flags) |
| 2322 | { |
| 2323 | struct strlist *lp = list_set_var; |
| 2324 | |
| 2325 | if (!lp) |
| 2326 | return; |
| 2327 | INT_OFF; |
| 2328 | do { |
| 2329 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2330 | lp = lp->next; |
| 2331 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2332 | INT_ON; |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Generate a list of variables satisfying the given conditions. |
| 2337 | */ |
| 2338 | static char ** |
| 2339 | listvars(int on, int off, char ***end) |
| 2340 | { |
| 2341 | struct var **vpp; |
| 2342 | struct var *vp; |
| 2343 | char **ep; |
| 2344 | int mask; |
| 2345 | |
| 2346 | STARTSTACKSTR(ep); |
| 2347 | vpp = vartab; |
| 2348 | mask = on | off; |
| 2349 | do { |
| 2350 | for (vp = *vpp; vp; vp = vp->next) { |
| 2351 | if ((vp->flags & mask) == on) { |
| 2352 | if (ep == stackstrend()) |
| 2353 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2354 | *ep++ = (char*)vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | } while (++vpp < vartab + VTABSIZE); |
| 2358 | if (ep == stackstrend()) |
| 2359 | ep = growstackstr(); |
| 2360 | if (end) |
| 2361 | *end = ep; |
| 2362 | *ep++ = NULL; |
| 2363 | return grabstackstr(ep); |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | /* ============ Path search helper |
| 2368 | * |
| 2369 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2370 | * of the path before the first call; path_advance will update |
| 2371 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2372 | * the possible path expansions in sequence. If an option (indicated by |
| 2373 | * a percent sign) appears in the path entry then the global variable |
| 2374 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2375 | * NULL. |
| 2376 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2377 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2378 | |
| 2379 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2380 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2381 | { |
| 2382 | const char *p; |
| 2383 | char *q; |
| 2384 | const char *start; |
| 2385 | size_t len; |
| 2386 | |
| 2387 | if (*path == NULL) |
| 2388 | return NULL; |
| 2389 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2390 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2391 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2392 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2393 | while (stackblocksize() < len) |
| 2394 | growstackblock(); |
| 2395 | q = stackblock(); |
| 2396 | if (p != start) { |
| 2397 | memcpy(q, start, p - start); |
| 2398 | q += p - start; |
| 2399 | *q++ = '/'; |
| 2400 | } |
| 2401 | strcpy(q, name); |
| 2402 | pathopt = NULL; |
| 2403 | if (*p == '%') { |
| 2404 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2405 | while (*p && *p != ':') |
| 2406 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2407 | } |
| 2408 | if (*p == ':') |
| 2409 | *path = p + 1; |
| 2410 | else |
| 2411 | *path = NULL; |
| 2412 | return stalloc(len); |
| 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | /* ============ Prompt */ |
| 2417 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2418 | static smallint doprompt; /* if set, prompt the user */ |
| 2419 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2420 | |
| 2421 | #if ENABLE_FEATURE_EDITING |
| 2422 | static line_input_t *line_input_state; |
| 2423 | static const char *cmdedit_prompt; |
| 2424 | static void |
| 2425 | putprompt(const char *s) |
| 2426 | { |
| 2427 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2428 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2429 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2430 | return; |
| 2431 | } |
| 2432 | cmdedit_prompt = s; |
| 2433 | } |
| 2434 | #else |
| 2435 | static void |
| 2436 | putprompt(const char *s) |
| 2437 | { |
| 2438 | out2str(s); |
| 2439 | } |
| 2440 | #endif |
| 2441 | |
| 2442 | #if ENABLE_ASH_EXPAND_PRMT |
| 2443 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2444 | static const char *expandstr(const char *ps); |
| 2445 | #else |
| 2446 | #define expandstr(s) s |
| 2447 | #endif |
| 2448 | |
| 2449 | static void |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2450 | setprompt_if(smallint do_set, int whichprompt) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2451 | { |
| 2452 | const char *prompt; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2453 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
| 2454 | |
| 2455 | if (!do_set) |
| 2456 | return; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2457 | |
| 2458 | needprompt = 0; |
| 2459 | |
| 2460 | switch (whichprompt) { |
| 2461 | case 1: |
| 2462 | prompt = ps1val(); |
| 2463 | break; |
| 2464 | case 2: |
| 2465 | prompt = ps2val(); |
| 2466 | break; |
| 2467 | default: /* 0 */ |
| 2468 | prompt = nullstr; |
| 2469 | } |
| 2470 | #if ENABLE_ASH_EXPAND_PRMT |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 2471 | pushstackmark(&smark, stackblocksize()); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2472 | #endif |
| 2473 | putprompt(expandstr(prompt)); |
| 2474 | #if ENABLE_ASH_EXPAND_PRMT |
| 2475 | popstackmark(&smark); |
| 2476 | #endif |
| 2477 | } |
| 2478 | |
| 2479 | |
| 2480 | /* ============ The cd and pwd commands */ |
| 2481 | |
| 2482 | #define CD_PHYSICAL 1 |
| 2483 | #define CD_PRINT 2 |
| 2484 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2485 | static int |
| 2486 | cdopt(void) |
| 2487 | { |
| 2488 | int flags = 0; |
| 2489 | int i, j; |
| 2490 | |
| 2491 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2492 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2493 | if (i != j) { |
| 2494 | flags ^= CD_PHYSICAL; |
| 2495 | j = i; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | return flags; |
| 2500 | } |
| 2501 | |
| 2502 | /* |
| 2503 | * Update curdir (the name of the current directory) in response to a |
| 2504 | * cd command. |
| 2505 | */ |
| 2506 | static const char * |
| 2507 | updatepwd(const char *dir) |
| 2508 | { |
| 2509 | char *new; |
| 2510 | char *p; |
| 2511 | char *cdcomppath; |
| 2512 | const char *lim; |
| 2513 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 2514 | cdcomppath = sstrdup(dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2515 | STARTSTACKSTR(new); |
| 2516 | if (*dir != '/') { |
| 2517 | if (curdir == nullstr) |
| 2518 | return 0; |
| 2519 | new = stack_putstr(curdir, new); |
| 2520 | } |
| 2521 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2522 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2523 | if (*dir != '/') { |
| 2524 | if (new[-1] != '/') |
| 2525 | USTPUTC('/', new); |
| 2526 | if (new > lim && *lim == '/') |
| 2527 | lim++; |
| 2528 | } else { |
| 2529 | USTPUTC('/', new); |
| 2530 | cdcomppath++; |
| 2531 | if (dir[1] == '/' && dir[2] != '/') { |
| 2532 | USTPUTC('/', new); |
| 2533 | cdcomppath++; |
| 2534 | lim++; |
| 2535 | } |
| 2536 | } |
| 2537 | p = strtok(cdcomppath, "/"); |
| 2538 | while (p) { |
| 2539 | switch (*p) { |
| 2540 | case '.': |
| 2541 | if (p[1] == '.' && p[2] == '\0') { |
| 2542 | while (new > lim) { |
| 2543 | STUNPUTC(new); |
| 2544 | if (new[-1] == '/') |
| 2545 | break; |
| 2546 | } |
| 2547 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2548 | } |
| 2549 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2550 | break; |
| 2551 | /* fall through */ |
| 2552 | default: |
| 2553 | new = stack_putstr(p, new); |
| 2554 | USTPUTC('/', new); |
| 2555 | } |
Denys Vlasenko | 00da72b | 2015-10-23 18:43:16 +0200 | [diff] [blame] | 2556 | p = strtok(NULL, "/"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2557 | } |
| 2558 | if (new > lim) |
| 2559 | STUNPUTC(new); |
| 2560 | *new = 0; |
| 2561 | return stackblock(); |
| 2562 | } |
| 2563 | |
| 2564 | /* |
| 2565 | * Find out what the current directory is. If we already know the current |
| 2566 | * directory, this routine returns immediately. |
| 2567 | */ |
| 2568 | static char * |
| 2569 | getpwd(void) |
| 2570 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2571 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2572 | return dir ? dir : nullstr; |
| 2573 | } |
| 2574 | |
| 2575 | static void |
| 2576 | setpwd(const char *val, int setold) |
| 2577 | { |
| 2578 | char *oldcur, *dir; |
| 2579 | |
| 2580 | oldcur = dir = curdir; |
| 2581 | |
| 2582 | if (setold) { |
| 2583 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2584 | } |
| 2585 | INT_OFF; |
| 2586 | if (physdir != nullstr) { |
| 2587 | if (physdir != oldcur) |
| 2588 | free(physdir); |
| 2589 | physdir = nullstr; |
| 2590 | } |
| 2591 | if (oldcur == val || !val) { |
| 2592 | char *s = getpwd(); |
| 2593 | physdir = s; |
| 2594 | if (!val) |
| 2595 | dir = s; |
| 2596 | } else |
| 2597 | dir = ckstrdup(val); |
| 2598 | if (oldcur != dir && oldcur != nullstr) { |
| 2599 | free(oldcur); |
| 2600 | } |
| 2601 | curdir = dir; |
| 2602 | INT_ON; |
| 2603 | setvar("PWD", dir, VEXPORT); |
| 2604 | } |
| 2605 | |
| 2606 | static void hashcd(void); |
| 2607 | |
| 2608 | /* |
| 2609 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2610 | * know that the current directory has changed. |
| 2611 | */ |
| 2612 | static int |
| 2613 | docd(const char *dest, int flags) |
| 2614 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2615 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2616 | int err; |
| 2617 | |
| 2618 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2619 | |
| 2620 | INT_OFF; |
| 2621 | if (!(flags & CD_PHYSICAL)) { |
| 2622 | dir = updatepwd(dest); |
| 2623 | if (dir) |
| 2624 | dest = dir; |
| 2625 | } |
| 2626 | err = chdir(dest); |
| 2627 | if (err) |
| 2628 | goto out; |
| 2629 | setpwd(dir, 1); |
| 2630 | hashcd(); |
| 2631 | out: |
| 2632 | INT_ON; |
| 2633 | return err; |
| 2634 | } |
| 2635 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2636 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2637 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2638 | { |
| 2639 | const char *dest; |
| 2640 | const char *path; |
| 2641 | const char *p; |
| 2642 | char c; |
| 2643 | struct stat statb; |
| 2644 | int flags; |
| 2645 | |
| 2646 | flags = cdopt(); |
| 2647 | dest = *argptr; |
| 2648 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2649 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2650 | else if (LONE_DASH(dest)) { |
| 2651 | dest = bltinlookup("OLDPWD"); |
| 2652 | flags |= CD_PRINT; |
| 2653 | } |
| 2654 | if (!dest) |
| 2655 | dest = nullstr; |
| 2656 | if (*dest == '/') |
| 2657 | goto step7; |
| 2658 | if (*dest == '.') { |
| 2659 | c = dest[1]; |
| 2660 | dotdot: |
| 2661 | switch (c) { |
| 2662 | case '\0': |
| 2663 | case '/': |
| 2664 | goto step6; |
| 2665 | case '.': |
| 2666 | c = dest[2]; |
| 2667 | if (c != '.') |
| 2668 | goto dotdot; |
| 2669 | } |
| 2670 | } |
| 2671 | if (!*dest) |
| 2672 | dest = "."; |
| 2673 | path = bltinlookup("CDPATH"); |
| 2674 | if (!path) { |
| 2675 | step6: |
| 2676 | step7: |
| 2677 | p = dest; |
| 2678 | goto docd; |
| 2679 | } |
| 2680 | do { |
| 2681 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2682 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2683 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2684 | if (c && c != ':') |
| 2685 | flags |= CD_PRINT; |
| 2686 | docd: |
| 2687 | if (!docd(p, flags)) |
| 2688 | goto out; |
| 2689 | break; |
| 2690 | } |
| 2691 | } while (path); |
| 2692 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2693 | /* NOTREACHED */ |
| 2694 | out: |
| 2695 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2696 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2697 | return 0; |
| 2698 | } |
| 2699 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2700 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2701 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2702 | { |
| 2703 | int flags; |
| 2704 | const char *dir = curdir; |
| 2705 | |
| 2706 | flags = cdopt(); |
| 2707 | if (flags) { |
| 2708 | if (physdir == nullstr) |
| 2709 | setpwd(dir, 0); |
| 2710 | dir = physdir; |
| 2711 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2712 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2713 | return 0; |
| 2714 | } |
| 2715 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2716 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2717 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2718 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2719 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2720 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2721 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2722 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2723 | #define CWORD 0 /* character is nothing special */ |
| 2724 | #define CNL 1 /* newline character */ |
| 2725 | #define CBACK 2 /* a backslash character */ |
| 2726 | #define CSQUOTE 3 /* single quote */ |
| 2727 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2728 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2729 | #define CBQUOTE 6 /* backwards single quote */ |
| 2730 | #define CVAR 7 /* a dollar sign */ |
| 2731 | #define CENDVAR 8 /* a '}' character */ |
| 2732 | #define CLP 9 /* a left paren in arithmetic */ |
| 2733 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2734 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2735 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2736 | #define CSPCL 13 /* these terminate a word */ |
| 2737 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2738 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2739 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2740 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2741 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2742 | #endif |
| 2743 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2744 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2745 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2746 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2747 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8) | (d << 12)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2748 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2749 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2750 | #endif |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 2751 | static const uint16_t S_I_T[] ALIGN2 = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2752 | #if ENABLE_ASH_ALIAS |
| 2753 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2754 | #endif |
| 2755 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2756 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2757 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2758 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2759 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2760 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2761 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 2762 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 2763 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 2764 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 2765 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2766 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2767 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 2768 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 2769 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 2770 | #endif |
| 2771 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2772 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2773 | /* Constants below must match table above */ |
| 2774 | enum { |
| 2775 | #if ENABLE_ASH_ALIAS |
| 2776 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 2777 | #endif |
| 2778 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 2779 | CNL_CNL_CNL_CNL , /* 2 */ |
| 2780 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 2781 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 2782 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 2783 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 2784 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 2785 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 2786 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 2787 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 2788 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 2789 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 2790 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 2791 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 2792 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2793 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2794 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 2795 | * caller must ensure proper cast on it if c is *char_ptr! |
| 2796 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2797 | /* Values for syntax param */ |
| 2798 | #define BASESYNTAX 0 /* not in quotes */ |
| 2799 | #define DQSYNTAX 1 /* in double quotes */ |
| 2800 | #define SQSYNTAX 2 /* in single quotes */ |
| 2801 | #define ARISYNTAX 3 /* in arithmetic */ |
| 2802 | #define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2803 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2804 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2805 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2806 | static int |
| 2807 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2808 | { |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 2809 | /* Used to also have '/' in this string: "\t\n !\"$&'()*-/:;<=>?[\\]`|}~" */ |
| 2810 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-:;<=>?[\\]`|}~"; |
| 2811 | /* |
| 2812 | * This causes '/' to be prepended with CTLESC in dquoted string, |
| 2813 | * making "./file"* treated incorrectly because we feed |
| 2814 | * ".\/file*" string to glob(), confusing it (see expandmeta func). |
| 2815 | * The "homegrown" glob implementation is okay with that, |
| 2816 | * but glibc one isn't. With '/' always treated as CWORD, |
| 2817 | * both work fine. |
| 2818 | */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2819 | # if ENABLE_ASH_ALIAS |
| 2820 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2821 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 2822 | 7, 8, 3, 3,/*3,*/3, 1, 1, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2823 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2824 | 11, 3 /* "}~" */ |
| 2825 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2826 | # else |
| 2827 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2828 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 2829 | 6, 7, 2, 2,/*2,*/2, 0, 0, /* "()*-/:;<" */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2830 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2831 | 10, 2 /* "}~" */ |
| 2832 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2833 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2834 | const char *s; |
| 2835 | int indx; |
| 2836 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2837 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2838 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2839 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2840 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2841 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2842 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2843 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2844 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2845 | /* Cast is purely for paranoia here, |
| 2846 | * just in case someone passed signed char to us */ |
| 2847 | if ((unsigned char)c >= CTL_FIRST |
| 2848 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2849 | ) { |
| 2850 | return CCTL; |
| 2851 | } |
| 2852 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2853 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2854 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2855 | indx = syntax_index_table[s - spec_symbls]; |
| 2856 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2857 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2860 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2861 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 2862 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2863 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2864 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2874 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2875 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2877 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2879 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2881 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2884 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2897 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 2898 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 2899 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2900 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2901 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2902 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 2903 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 2904 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2905 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2906 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2907 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2910 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 2911 | /* "/" was CWORD_CCTL_CCTL_CWORD, see comment in SIT() function why this is changed: */ |
| 2912 | /* 47 "/" */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2913 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2924 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2925 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2926 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2927 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2928 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2929 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2930 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2931 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2932 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2935 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2957 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2958 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2959 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2962 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2968 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2969 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2972 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2973 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2974 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2975 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2985 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2986 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2987 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2988 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2989 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2990 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2991 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2992 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 2993 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2994 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2995 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2996 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2997 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2998 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2999 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 3000 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 3001 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 3002 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 3003 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 3004 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 3005 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 3006 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 3007 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 3008 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 3009 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 3010 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 3011 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 3012 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 3013 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 3014 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 3015 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 3016 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 3017 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 3018 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 3019 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 3020 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 3021 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 3022 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 3023 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 3024 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 3025 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 3026 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 3027 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 3028 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 3029 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 3030 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 3031 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 3032 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 3033 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 3034 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 3035 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 3036 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 3037 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 3038 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 3039 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 3040 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 3041 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 3042 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 3043 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 3044 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 3045 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 3046 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 3047 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 3048 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 3049 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 3050 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 3051 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 3052 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 3053 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 3054 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 3055 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 3056 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 3057 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 3058 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 3059 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 3060 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 3061 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 3062 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 3063 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 3064 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 3065 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 3066 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 3067 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 3068 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 3069 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 3070 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 3071 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 3072 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 3073 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 3074 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 3075 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 3076 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 3077 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 3078 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 3079 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 3080 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 3081 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 3082 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 3083 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 3084 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 3085 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 3086 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 3087 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 3088 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 3089 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 3090 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 3091 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 3092 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 3093 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 3094 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 3095 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3096 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 3097 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 3098 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 3099 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 3100 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 3101 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 3102 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 3103 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 3104 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 3105 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3106 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 3107 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 3108 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 3109 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 3110 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 3111 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 3112 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 3113 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 3114 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 3115 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 3116 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 3117 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 3118 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 3119 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 3120 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3121 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3122 | # if ENABLE_ASH_ALIAS |
| 3123 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 3124 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3125 | }; |
| 3126 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3127 | # define SIT(c, syntax) ((S_I_T[syntax_index_table[c]] >> ((syntax)*4)) & 0xf) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 3128 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3129 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3130 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3131 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3132 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3133 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3134 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3135 | |
| 3136 | #define ALIASINUSE 1 |
| 3137 | #define ALIASDEAD 2 |
| 3138 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3139 | struct alias { |
| 3140 | struct alias *next; |
| 3141 | char *name; |
| 3142 | char *val; |
| 3143 | int flag; |
| 3144 | }; |
| 3145 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3146 | |
| 3147 | static struct alias **atab; // [ATABSIZE]; |
| 3148 | #define INIT_G_alias() do { \ |
| 3149 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3150 | } while (0) |
| 3151 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3152 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3153 | static struct alias ** |
| 3154 | __lookupalias(const char *name) { |
| 3155 | unsigned int hashval; |
| 3156 | struct alias **app; |
| 3157 | const char *p; |
| 3158 | unsigned int ch; |
| 3159 | |
| 3160 | p = name; |
| 3161 | |
| 3162 | ch = (unsigned char)*p; |
| 3163 | hashval = ch << 4; |
| 3164 | while (ch) { |
| 3165 | hashval += ch; |
| 3166 | ch = (unsigned char)*++p; |
| 3167 | } |
| 3168 | app = &atab[hashval % ATABSIZE]; |
| 3169 | |
| 3170 | for (; *app; app = &(*app)->next) { |
| 3171 | if (strcmp(name, (*app)->name) == 0) { |
| 3172 | break; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | return app; |
| 3177 | } |
| 3178 | |
| 3179 | static struct alias * |
| 3180 | lookupalias(const char *name, int check) |
| 3181 | { |
| 3182 | struct alias *ap = *__lookupalias(name); |
| 3183 | |
| 3184 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3185 | return NULL; |
| 3186 | return ap; |
| 3187 | } |
| 3188 | |
| 3189 | static struct alias * |
| 3190 | freealias(struct alias *ap) |
| 3191 | { |
| 3192 | struct alias *next; |
| 3193 | |
| 3194 | if (ap->flag & ALIASINUSE) { |
| 3195 | ap->flag |= ALIASDEAD; |
| 3196 | return ap; |
| 3197 | } |
| 3198 | |
| 3199 | next = ap->next; |
| 3200 | free(ap->name); |
| 3201 | free(ap->val); |
| 3202 | free(ap); |
| 3203 | return next; |
| 3204 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3205 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3206 | static void |
| 3207 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3208 | { |
| 3209 | struct alias *ap, **app; |
| 3210 | |
| 3211 | app = __lookupalias(name); |
| 3212 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3213 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3214 | if (ap) { |
| 3215 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3216 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3217 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3218 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3219 | ap->flag &= ~ALIASDEAD; |
| 3220 | } else { |
| 3221 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3222 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3223 | ap->name = ckstrdup(name); |
| 3224 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3225 | /*ap->flag = 0; - ckzalloc did it */ |
| 3226 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3227 | *app = ap; |
| 3228 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3229 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3232 | static int |
| 3233 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3234 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3235 | struct alias **app; |
| 3236 | |
| 3237 | app = __lookupalias(name); |
| 3238 | |
| 3239 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3240 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3241 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3242 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3243 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3246 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3249 | static void |
| 3250 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3251 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3252 | struct alias *ap, **app; |
| 3253 | int i; |
| 3254 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3255 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3256 | for (i = 0; i < ATABSIZE; i++) { |
| 3257 | app = &atab[i]; |
| 3258 | for (ap = *app; ap; ap = *app) { |
| 3259 | *app = freealias(*app); |
| 3260 | if (ap == *app) { |
| 3261 | app = &ap->next; |
| 3262 | } |
| 3263 | } |
| 3264 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3265 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3268 | static void |
| 3269 | printalias(const struct alias *ap) |
| 3270 | { |
| 3271 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3272 | } |
| 3273 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3274 | /* |
| 3275 | * TODO - sort output |
| 3276 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3277 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3278 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3279 | { |
| 3280 | char *n, *v; |
| 3281 | int ret = 0; |
| 3282 | struct alias *ap; |
| 3283 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3284 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3285 | int i; |
| 3286 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3287 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3288 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3289 | printalias(ap); |
| 3290 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3291 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3292 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3293 | } |
| 3294 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3295 | v = strchr(n+1, '='); |
| 3296 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3297 | ap = *__lookupalias(n); |
| 3298 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3299 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3300 | ret = 1; |
| 3301 | } else |
| 3302 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3303 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3304 | *v++ = '\0'; |
| 3305 | setalias(n, v); |
| 3306 | } |
| 3307 | } |
| 3308 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3309 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3312 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3313 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3314 | { |
| 3315 | int i; |
| 3316 | |
| 3317 | while ((i = nextopt("a")) != '\0') { |
| 3318 | if (i == 'a') { |
| 3319 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3320 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3321 | } |
| 3322 | } |
| 3323 | for (i = 0; *argptr; argptr++) { |
| 3324 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3325 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3326 | i = 1; |
| 3327 | } |
| 3328 | } |
| 3329 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3330 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3331 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3332 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3333 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3334 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3335 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3336 | /* ============ jobs.c */ |
| 3337 | |
| 3338 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3339 | #define FORK_FG 0 |
| 3340 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3341 | #define FORK_NOJOB 2 |
| 3342 | |
| 3343 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3344 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3345 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3346 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3347 | #define SHOW_STDERR 0x08 /* print to stderr (else stdout) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3348 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3349 | /* |
| 3350 | * A job structure contains information about a job. A job is either a |
| 3351 | * single process or a set of processes contained in a pipeline. In the |
| 3352 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3353 | * array of pids. |
| 3354 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3355 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3356 | pid_t ps_pid; /* process id */ |
| 3357 | int ps_status; /* last process status from wait() */ |
| 3358 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3359 | }; |
| 3360 | |
| 3361 | struct job { |
| 3362 | struct procstat ps0; /* status of process */ |
| 3363 | struct procstat *ps; /* status or processes when more than one */ |
| 3364 | #if JOBS |
| 3365 | int stopstatus; /* status of a stopped job */ |
| 3366 | #endif |
| 3367 | uint32_t |
| 3368 | nprocs: 16, /* number of processes */ |
| 3369 | state: 8, |
| 3370 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3371 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3372 | #define JOBDONE 2 /* all procs are completed */ |
| 3373 | #if JOBS |
| 3374 | sigint: 1, /* job was killed by SIGINT */ |
| 3375 | jobctl: 1, /* job running under job control */ |
| 3376 | #endif |
| 3377 | waited: 1, /* true if this entry has been waited for */ |
| 3378 | used: 1, /* true if this entry is in used */ |
| 3379 | changed: 1; /* true if status has changed */ |
| 3380 | struct job *prev_job; /* previous job */ |
| 3381 | }; |
| 3382 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3383 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3384 | static int forkshell(struct job *, union node *, int); |
| 3385 | static int waitforjob(struct job *); |
| 3386 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3387 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3388 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3389 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3390 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3391 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3392 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3393 | #endif |
| 3394 | |
| 3395 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3396 | * Ignore a signal. |
| 3397 | */ |
| 3398 | static void |
| 3399 | ignoresig(int signo) |
| 3400 | { |
| 3401 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3402 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3403 | /* No, need to do it */ |
| 3404 | signal(signo, SIG_IGN); |
| 3405 | } |
| 3406 | sigmode[signo - 1] = S_HARD_IGN; |
| 3407 | } |
| 3408 | |
| 3409 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3410 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3411 | */ |
| 3412 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3413 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3414 | { |
| 3415 | gotsig[signo - 1] = 1; |
| 3416 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3417 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3418 | if (!suppress_int) { |
| 3419 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3420 | raise_interrupt(); /* does not return */ |
| 3421 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3422 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3423 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3424 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3429 | * Set the signal handler for the specified signal. The routine figures |
| 3430 | * out what it should be set to. |
| 3431 | */ |
| 3432 | static void |
| 3433 | setsignal(int signo) |
| 3434 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3435 | char *t; |
| 3436 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3437 | struct sigaction act; |
| 3438 | |
| 3439 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3440 | new_act = S_DFL; |
| 3441 | if (t != NULL) { /* trap for this sig is set */ |
| 3442 | new_act = S_CATCH; |
| 3443 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3444 | new_act = S_IGN; |
| 3445 | } |
| 3446 | |
| 3447 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3448 | switch (signo) { |
| 3449 | case SIGINT: |
| 3450 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3451 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3452 | break; |
| 3453 | case SIGQUIT: |
| 3454 | #if DEBUG |
| 3455 | if (debug) |
| 3456 | break; |
| 3457 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3458 | /* man bash: |
| 3459 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3460 | * commands run by bash have signal handlers |
| 3461 | * set to the values inherited by the shell |
| 3462 | * from its parent". */ |
| 3463 | new_act = S_IGN; |
| 3464 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3465 | case SIGTERM: |
| 3466 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3467 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3468 | break; |
| 3469 | #if JOBS |
| 3470 | case SIGTSTP: |
| 3471 | case SIGTTOU: |
| 3472 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3473 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3474 | break; |
| 3475 | #endif |
| 3476 | } |
| 3477 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3478 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3479 | //whereas we have to restore it to what shell got on entry |
| 3480 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3481 | |
| 3482 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3483 | cur_act = *t; |
| 3484 | if (cur_act == 0) { |
| 3485 | /* current setting is not yet known */ |
| 3486 | if (sigaction(signo, NULL, &act)) { |
| 3487 | /* pretend it worked; maybe we should give a warning, |
| 3488 | * but other shells don't. We don't alter sigmode, |
| 3489 | * so we retry every time. |
| 3490 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3491 | return; |
| 3492 | } |
| 3493 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3494 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3495 | if (mflag |
| 3496 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3497 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3498 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3499 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3500 | } |
| 3501 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3502 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3503 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3504 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3505 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3506 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3507 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3508 | act.sa_handler = signal_handler; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3509 | break; |
| 3510 | case S_IGN: |
| 3511 | act.sa_handler = SIG_IGN; |
| 3512 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3513 | } |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3514 | |
| 3515 | /* flags and mask matter only if !DFL and !IGN, but we do it |
| 3516 | * for all cases for more deterministic behavior: |
| 3517 | */ |
| 3518 | act.sa_flags = 0; |
| 3519 | sigfillset(&act.sa_mask); |
| 3520 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3521 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3522 | |
| 3523 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | /* mode flags for set_curjob */ |
| 3527 | #define CUR_DELETE 2 |
| 3528 | #define CUR_RUNNING 1 |
| 3529 | #define CUR_STOPPED 0 |
| 3530 | |
| 3531 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3532 | #define DOWAIT_NONBLOCK WNOHANG |
| 3533 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3534 | |
| 3535 | #if JOBS |
| 3536 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3537 | static int initialpgrp; //references:2 |
| 3538 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3539 | #endif |
| 3540 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3541 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3542 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3543 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3544 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3545 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3546 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3547 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3548 | |
| 3549 | static void |
| 3550 | set_curjob(struct job *jp, unsigned mode) |
| 3551 | { |
| 3552 | struct job *jp1; |
| 3553 | struct job **jpp, **curp; |
| 3554 | |
| 3555 | /* first remove from list */ |
| 3556 | jpp = curp = &curjob; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3557 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3558 | jp1 = *jpp; |
| 3559 | if (jp1 == jp) |
| 3560 | break; |
| 3561 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3562 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3563 | *jpp = jp1->prev_job; |
| 3564 | |
| 3565 | /* Then re-insert in correct position */ |
| 3566 | jpp = curp; |
| 3567 | switch (mode) { |
| 3568 | default: |
| 3569 | #if DEBUG |
| 3570 | abort(); |
| 3571 | #endif |
| 3572 | case CUR_DELETE: |
| 3573 | /* job being deleted */ |
| 3574 | break; |
| 3575 | case CUR_RUNNING: |
| 3576 | /* newly created job or backgrounded job, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 3577 | * put after all stopped jobs. |
| 3578 | */ |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3579 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3580 | jp1 = *jpp; |
| 3581 | #if JOBS |
| 3582 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3583 | #endif |
| 3584 | break; |
| 3585 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3586 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3587 | /* FALLTHROUGH */ |
| 3588 | #if JOBS |
| 3589 | case CUR_STOPPED: |
| 3590 | #endif |
| 3591 | /* newly stopped job - becomes curjob */ |
| 3592 | jp->prev_job = *jpp; |
| 3593 | *jpp = jp; |
| 3594 | break; |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | #if JOBS || DEBUG |
| 3599 | static int |
| 3600 | jobno(const struct job *jp) |
| 3601 | { |
| 3602 | return jp - jobtab + 1; |
| 3603 | } |
| 3604 | #endif |
| 3605 | |
| 3606 | /* |
| 3607 | * Convert a job name to a job structure. |
| 3608 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3609 | #if !JOBS |
| 3610 | #define getjob(name, getctl) getjob(name) |
| 3611 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3612 | static struct job * |
| 3613 | getjob(const char *name, int getctl) |
| 3614 | { |
| 3615 | struct job *jp; |
| 3616 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3617 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3618 | unsigned num; |
| 3619 | int c; |
| 3620 | const char *p; |
| 3621 | char *(*match)(const char *, const char *); |
| 3622 | |
| 3623 | jp = curjob; |
| 3624 | p = name; |
| 3625 | if (!p) |
| 3626 | goto currentjob; |
| 3627 | |
| 3628 | if (*p != '%') |
| 3629 | goto err; |
| 3630 | |
| 3631 | c = *++p; |
| 3632 | if (!c) |
| 3633 | goto currentjob; |
| 3634 | |
| 3635 | if (!p[1]) { |
| 3636 | if (c == '+' || c == '%') { |
| 3637 | currentjob: |
| 3638 | err_msg = "No current job"; |
| 3639 | goto check; |
| 3640 | } |
| 3641 | if (c == '-') { |
| 3642 | if (jp) |
| 3643 | jp = jp->prev_job; |
| 3644 | err_msg = "No previous job"; |
| 3645 | check: |
| 3646 | if (!jp) |
| 3647 | goto err; |
| 3648 | goto gotit; |
| 3649 | } |
| 3650 | } |
| 3651 | |
| 3652 | if (is_number(p)) { |
| 3653 | num = atoi(p); |
Denys Vlasenko | 46a45ce | 2016-09-29 01:10:08 +0200 | [diff] [blame] | 3654 | if (num > 0 && num <= njobs) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3655 | jp = jobtab + num - 1; |
| 3656 | if (jp->used) |
| 3657 | goto gotit; |
| 3658 | goto err; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | match = prefix; |
| 3663 | if (*p == '?') { |
| 3664 | match = strstr; |
| 3665 | p++; |
| 3666 | } |
| 3667 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3668 | found = NULL; |
| 3669 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3670 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3671 | if (found) |
| 3672 | goto err; |
| 3673 | found = jp; |
| 3674 | err_msg = "%s: ambiguous"; |
| 3675 | } |
| 3676 | jp = jp->prev_job; |
| 3677 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3678 | if (!found) |
| 3679 | goto err; |
| 3680 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3681 | |
| 3682 | gotit: |
| 3683 | #if JOBS |
| 3684 | err_msg = "job %s not created under job control"; |
| 3685 | if (getctl && jp->jobctl == 0) |
| 3686 | goto err; |
| 3687 | #endif |
| 3688 | return jp; |
| 3689 | err: |
| 3690 | ash_msg_and_raise_error(err_msg, name); |
| 3691 | } |
| 3692 | |
| 3693 | /* |
| 3694 | * Mark a job structure as unused. |
| 3695 | */ |
| 3696 | static void |
| 3697 | freejob(struct job *jp) |
| 3698 | { |
| 3699 | struct procstat *ps; |
| 3700 | int i; |
| 3701 | |
| 3702 | INT_OFF; |
| 3703 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3704 | if (ps->ps_cmd != nullstr) |
| 3705 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3706 | } |
| 3707 | if (jp->ps != &jp->ps0) |
| 3708 | free(jp->ps); |
| 3709 | jp->used = 0; |
| 3710 | set_curjob(jp, CUR_DELETE); |
| 3711 | INT_ON; |
| 3712 | } |
| 3713 | |
| 3714 | #if JOBS |
| 3715 | static void |
| 3716 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3717 | { |
| 3718 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3719 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | /* |
| 3723 | * Turn job control on and off. |
| 3724 | * |
| 3725 | * Note: This code assumes that the third arg to ioctl is a character |
| 3726 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3727 | * System V doesn't have job control yet, this isn't a problem now. |
| 3728 | * |
| 3729 | * Called with interrupts off. |
| 3730 | */ |
| 3731 | static void |
| 3732 | setjobctl(int on) |
| 3733 | { |
| 3734 | int fd; |
| 3735 | int pgrp; |
| 3736 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3737 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3738 | return; |
| 3739 | if (on) { |
| 3740 | int ofd; |
| 3741 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3742 | if (fd < 0) { |
| 3743 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3744 | * That sometimes helps to acquire controlling tty. |
| 3745 | * Obviously, a workaround for bugs when someone |
| 3746 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3747 | fd = 2; |
| 3748 | while (!isatty(fd)) |
| 3749 | if (--fd < 0) |
| 3750 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3751 | } |
| 3752 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3753 | if (ofd >= 0) |
| 3754 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3755 | if (fd < 0) |
| 3756 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3757 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3758 | close_on_exec_on(fd); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3759 | while (1) { /* while we are in the background */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3760 | pgrp = tcgetpgrp(fd); |
| 3761 | if (pgrp < 0) { |
| 3762 | out: |
| 3763 | ash_msg("can't access tty; job control turned off"); |
| 3764 | mflag = on = 0; |
| 3765 | goto close; |
| 3766 | } |
| 3767 | if (pgrp == getpgrp()) |
| 3768 | break; |
| 3769 | killpg(0, SIGTTIN); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3770 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3771 | initialpgrp = pgrp; |
| 3772 | |
| 3773 | setsignal(SIGTSTP); |
| 3774 | setsignal(SIGTTOU); |
| 3775 | setsignal(SIGTTIN); |
| 3776 | pgrp = rootpid; |
| 3777 | setpgid(0, pgrp); |
| 3778 | xtcsetpgrp(fd, pgrp); |
| 3779 | } else { |
| 3780 | /* turning job control off */ |
| 3781 | fd = ttyfd; |
| 3782 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3783 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3784 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3785 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3786 | setpgid(0, pgrp); |
| 3787 | setsignal(SIGTSTP); |
| 3788 | setsignal(SIGTTOU); |
| 3789 | setsignal(SIGTTIN); |
| 3790 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3791 | if (fd >= 0) |
| 3792 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3793 | fd = -1; |
| 3794 | } |
| 3795 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3796 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3799 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3800 | killcmd(int argc, char **argv) |
| 3801 | { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3802 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 3803 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3804 | do { |
| 3805 | if (argv[i][0] == '%') { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 3806 | /* |
| 3807 | * "kill %N" - job kill |
| 3808 | * Converting to pgrp / pid kill |
| 3809 | */ |
| 3810 | struct job *jp; |
| 3811 | char *dst; |
| 3812 | int j, n; |
| 3813 | |
| 3814 | jp = getjob(argv[i], 0); |
| 3815 | /* |
| 3816 | * In jobs started under job control, we signal |
| 3817 | * entire process group by kill -PGRP_ID. |
| 3818 | * This happens, f.e., in interactive shell. |
| 3819 | * |
| 3820 | * Otherwise, we signal each child via |
| 3821 | * kill PID1 PID2 PID3. |
| 3822 | * Testcases: |
| 3823 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 3824 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 3825 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 3826 | */ |
| 3827 | n = jp->nprocs; /* can't be 0 (I hope) */ |
| 3828 | if (jp->jobctl) |
| 3829 | n = 1; |
| 3830 | dst = alloca(n * sizeof(int)*4); |
| 3831 | argv[i] = dst; |
| 3832 | for (j = 0; j < n; j++) { |
| 3833 | struct procstat *ps = &jp->ps[j]; |
| 3834 | /* Skip non-running and not-stopped members |
| 3835 | * (i.e. dead members) of the job |
| 3836 | */ |
| 3837 | if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status)) |
| 3838 | continue; |
| 3839 | /* |
| 3840 | * kill_main has matching code to expect |
| 3841 | * leading space. Needed to not confuse |
| 3842 | * negative pids with "kill -SIGNAL_NO" syntax |
| 3843 | */ |
| 3844 | dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid); |
| 3845 | } |
| 3846 | *dst = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3847 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3848 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3849 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3850 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3854 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3855 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3856 | struct procstat *ps; |
| 3857 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3858 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3859 | psend = jp->ps + jp->nprocs; |
| 3860 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3861 | printf(" | %s", ps->ps_cmd); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3862 | newline_and_flush(stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3863 | flush_stdout_stderr(); |
| 3864 | } |
| 3865 | |
| 3866 | |
| 3867 | static int |
| 3868 | restartjob(struct job *jp, int mode) |
| 3869 | { |
| 3870 | struct procstat *ps; |
| 3871 | int i; |
| 3872 | int status; |
| 3873 | pid_t pgid; |
| 3874 | |
| 3875 | INT_OFF; |
| 3876 | if (jp->state == JOBDONE) |
| 3877 | goto out; |
| 3878 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3879 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3880 | if (mode == FORK_FG) |
| 3881 | xtcsetpgrp(ttyfd, pgid); |
| 3882 | killpg(pgid, SIGCONT); |
| 3883 | ps = jp->ps; |
| 3884 | i = jp->nprocs; |
| 3885 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3886 | if (WIFSTOPPED(ps->ps_status)) { |
| 3887 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3888 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3889 | ps++; |
| 3890 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3891 | out: |
| 3892 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3893 | INT_ON; |
| 3894 | return status; |
| 3895 | } |
| 3896 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3897 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3898 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3899 | { |
| 3900 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3901 | int mode; |
| 3902 | int retval; |
| 3903 | |
| 3904 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3905 | nextopt(nullstr); |
| 3906 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3907 | do { |
| 3908 | jp = getjob(*argv, 1); |
| 3909 | if (mode == FORK_BG) { |
| 3910 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3911 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3912 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3913 | out1str(jp->ps[0].ps_cmd); |
| 3914 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3915 | retval = restartjob(jp, mode); |
| 3916 | } while (*argv && *++argv); |
| 3917 | return retval; |
| 3918 | } |
| 3919 | #endif |
| 3920 | |
| 3921 | static int |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3922 | sprint_status48(char *s, int status, int sigonly) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3923 | { |
| 3924 | int col; |
| 3925 | int st; |
| 3926 | |
| 3927 | col = 0; |
| 3928 | if (!WIFEXITED(status)) { |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 3929 | if (JOBS && WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3930 | st = WSTOPSIG(status); |
| 3931 | else |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3932 | st = WTERMSIG(status); |
| 3933 | if (sigonly) { |
| 3934 | if (st == SIGINT || st == SIGPIPE) |
| 3935 | goto out; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 3936 | if (JOBS && WIFSTOPPED(status)) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3937 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3938 | } |
| 3939 | st &= 0x7f; |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 3940 | //TODO: use bbox's get_signame? strsignal adds ~600 bytes to text+rodata |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3941 | col = fmtstr(s, 32, strsignal(st)); |
| 3942 | if (WCOREDUMP(status)) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3943 | strcpy(s + col, " (core dumped)"); |
| 3944 | col += sizeof(" (core dumped)")-1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } else if (!sigonly) { |
| 3947 | st = WEXITSTATUS(status); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 3948 | col = fmtstr(s, 16, (st ? "Done(%d)" : "Done"), st); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3949 | } |
| 3950 | out: |
| 3951 | return col; |
| 3952 | } |
| 3953 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3954 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3955 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3956 | { |
| 3957 | int pid; |
| 3958 | int status; |
| 3959 | struct job *jp; |
| 3960 | struct job *thisjob; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3961 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3962 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3963 | |
| 3964 | /* Do a wait system call. If job control is compiled in, we accept |
| 3965 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3966 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3967 | if (doing_jobctl) |
| 3968 | wait_flags |= WUNTRACED; |
| 3969 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3970 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3971 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3972 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3973 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3974 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3975 | INT_OFF; |
| 3976 | thisjob = NULL; |
| 3977 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 3978 | int jobstate; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3979 | struct procstat *ps; |
| 3980 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3981 | if (jp->state == JOBDONE) |
| 3982 | continue; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 3983 | jobstate = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3984 | ps = jp->ps; |
| 3985 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3986 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3987 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3988 | TRACE(("Job %d: changing status of proc %d " |
| 3989 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3990 | jobno(jp), pid, ps->ps_status, status)); |
| 3991 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3992 | thisjob = jp; |
| 3993 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3994 | if (ps->ps_status == -1) |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 3995 | jobstate = JOBRUNNING; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3996 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 3997 | if (jobstate == JOBRUNNING) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3998 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3999 | if (WIFSTOPPED(ps->ps_status)) { |
| 4000 | jp->stopstatus = ps->ps_status; |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4001 | jobstate = JOBSTOPPED; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4002 | } |
| 4003 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4004 | } while (++ps < psend); |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4005 | if (!thisjob) |
| 4006 | continue; |
| 4007 | |
| 4008 | /* Found the job where one of its processes changed its state. |
| 4009 | * Is there at least one live and running process in this job? */ |
| 4010 | if (jobstate != JOBRUNNING) { |
| 4011 | /* No. All live processes in the job are stopped |
| 4012 | * (JOBSTOPPED) or there are no live processes (JOBDONE) |
| 4013 | */ |
| 4014 | thisjob->changed = 1; |
| 4015 | if (thisjob->state != jobstate) { |
| 4016 | TRACE(("Job %d: changing state from %d to %d\n", |
| 4017 | jobno(thisjob), thisjob->state, jobstate)); |
| 4018 | thisjob->state = jobstate; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4019 | #if JOBS |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4020 | if (jobstate == JOBSTOPPED) |
| 4021 | set_curjob(thisjob, CUR_STOPPED); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4022 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4023 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4024 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4025 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4026 | } |
Denys Vlasenko | 4700fb5 | 2015-10-09 15:40:13 +0200 | [diff] [blame] | 4027 | /* The process wasn't found in job list */ |
| 4028 | if (JOBS && !WIFSTOPPED(status)) |
| 4029 | jobless--; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4030 | out: |
| 4031 | INT_ON; |
| 4032 | |
| 4033 | if (thisjob && thisjob == job) { |
| 4034 | char s[48 + 1]; |
| 4035 | int len; |
| 4036 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4037 | len = sprint_status48(s, status, 1); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4038 | if (len) { |
| 4039 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4040 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4041 | out2str(s); |
| 4042 | } |
| 4043 | } |
| 4044 | return pid; |
| 4045 | } |
| 4046 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4047 | static int |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4048 | blocking_wait_with_raise_on_sig(void) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4049 | { |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4050 | pid_t pid = dowait(DOWAIT_BLOCK, NULL); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4051 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4052 | raise_exception(EXSIG); |
| 4053 | return pid; |
| 4054 | } |
| 4055 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4056 | #if JOBS |
| 4057 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4058 | showjob(struct job *jp, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4059 | { |
| 4060 | struct procstat *ps; |
| 4061 | struct procstat *psend; |
| 4062 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4063 | int indent_col; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4064 | char s[16 + 16 + 48]; |
| 4065 | FILE *out = (mode & SHOW_STDERR ? stderr : stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4066 | |
| 4067 | ps = jp->ps; |
| 4068 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4069 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4070 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4071 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4072 | return; |
| 4073 | } |
| 4074 | |
| 4075 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4076 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4077 | |
| 4078 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4079 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4080 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4081 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4082 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4083 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4084 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4085 | |
| 4086 | psend = ps + jp->nprocs; |
| 4087 | |
| 4088 | if (jp->state == JOBRUNNING) { |
| 4089 | strcpy(s + col, "Running"); |
| 4090 | col += sizeof("Running") - 1; |
| 4091 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4092 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4093 | if (jp->state == JOBSTOPPED) |
| 4094 | status = jp->stopstatus; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4095 | col += sprint_status48(s + col, status, 0); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4096 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4097 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4098 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4099 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4100 | * or prints several "PID | <cmdN>" lines, |
| 4101 | * depending on SHOW_PIDS bit. |
| 4102 | * We do not print status of individual processes |
| 4103 | * between PID and <cmdN>. bash does it, but not very well: |
| 4104 | * first line shows overall job status, not process status, |
| 4105 | * making it impossible to know 1st process status. |
| 4106 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4107 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4108 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4109 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4110 | s[0] = '\0'; |
| 4111 | col = 33; |
| 4112 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4113 | col = fmtstr(s, 48, "\n%*c%d ", indent_col, ' ', ps->ps_pid) - 1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4114 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4115 | fprintf(out, "%s%*c%s%s", |
| 4116 | s, |
| 4117 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4118 | ps == jp->ps ? "" : "| ", |
| 4119 | ps->ps_cmd |
| 4120 | ); |
| 4121 | } while (++ps != psend); |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4122 | newline_and_flush(out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4123 | |
| 4124 | jp->changed = 0; |
| 4125 | |
| 4126 | if (jp->state == JOBDONE) { |
| 4127 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4128 | freejob(jp); |
| 4129 | } |
| 4130 | } |
| 4131 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4132 | /* |
| 4133 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4134 | * statuses have changed since the last call to showjobs. |
| 4135 | */ |
| 4136 | static void |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4137 | showjobs(int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4138 | { |
| 4139 | struct job *jp; |
| 4140 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4141 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4142 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4143 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4144 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4145 | continue; |
| 4146 | |
| 4147 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4148 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4149 | showjob(jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4150 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4151 | } |
| 4152 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4153 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4154 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4155 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4156 | { |
| 4157 | int mode, m; |
| 4158 | |
| 4159 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4160 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4161 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4162 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4163 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4164 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
| 4167 | argv = argptr; |
| 4168 | if (*argv) { |
| 4169 | do |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4170 | showjob(getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4171 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4172 | } else { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 4173 | showjobs(mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4174 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4175 | |
| 4176 | return 0; |
| 4177 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4178 | #endif /* JOBS */ |
| 4179 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4180 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4181 | static int |
| 4182 | getstatus(struct job *job) |
| 4183 | { |
| 4184 | int status; |
| 4185 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4186 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4187 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4188 | /* Fetch last member's status */ |
| 4189 | ps = job->ps + job->nprocs - 1; |
| 4190 | status = ps->ps_status; |
| 4191 | if (pipefail) { |
| 4192 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4193 | while (status == 0 && --ps >= job->ps) |
| 4194 | status = ps->ps_status; |
| 4195 | } |
| 4196 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4197 | retval = WEXITSTATUS(status); |
| 4198 | if (!WIFEXITED(status)) { |
| 4199 | #if JOBS |
| 4200 | retval = WSTOPSIG(status); |
| 4201 | if (!WIFSTOPPED(status)) |
| 4202 | #endif |
| 4203 | { |
| 4204 | /* XXX: limits number of signals */ |
| 4205 | retval = WTERMSIG(status); |
| 4206 | #if JOBS |
| 4207 | if (retval == SIGINT) |
| 4208 | job->sigint = 1; |
| 4209 | #endif |
| 4210 | } |
| 4211 | retval += 128; |
| 4212 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4213 | TRACE(("getstatus: job %d, nproc %d, status 0x%x, retval 0x%x\n", |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4214 | jobno(job), job->nprocs, status, retval)); |
| 4215 | return retval; |
| 4216 | } |
| 4217 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4218 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4219 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4220 | { |
| 4221 | struct job *job; |
| 4222 | int retval; |
| 4223 | struct job *jp; |
| 4224 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4225 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4226 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4227 | |
| 4228 | nextopt(nullstr); |
| 4229 | retval = 0; |
| 4230 | |
| 4231 | argv = argptr; |
| 4232 | if (!*argv) { |
| 4233 | /* wait for all jobs */ |
| 4234 | for (;;) { |
| 4235 | jp = curjob; |
| 4236 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4237 | if (!jp) /* no running procs */ |
| 4238 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4239 | if (jp->state == JOBRUNNING) |
| 4240 | break; |
| 4241 | jp->waited = 1; |
| 4242 | jp = jp->prev_job; |
| 4243 | } |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4244 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4245 | /* man bash: |
| 4246 | * "When bash is waiting for an asynchronous command via |
| 4247 | * the wait builtin, the reception of a signal for which a trap |
| 4248 | * has been set will cause the wait builtin to return immediately |
| 4249 | * with an exit status greater than 128, immediately after which |
| 4250 | * the trap is executed." |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4251 | * |
| 4252 | * blocking_wait_with_raise_on_sig raises signal handlers |
| 4253 | * if it gets no pid (pid < 0). However, |
| 4254 | * if child sends us a signal *and immediately exits*, |
| 4255 | * blocking_wait_with_raise_on_sig gets pid > 0 |
| 4256 | * and does not handle pending_sig. Check this case: */ |
| 4257 | if (pending_sig) |
| 4258 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | retval = 127; |
| 4263 | do { |
| 4264 | if (**argv != '%') { |
| 4265 | pid_t pid = number(*argv); |
| 4266 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4267 | while (1) { |
| 4268 | if (!job) |
| 4269 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4270 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4271 | break; |
| 4272 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4273 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4274 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4275 | job = getjob(*argv, 0); |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4276 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4277 | /* loop until process terminated or stopped */ |
| 4278 | while (job->state == JOBRUNNING) |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4279 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4280 | job->waited = 1; |
| 4281 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4282 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4283 | } while (*++argv); |
| 4284 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4285 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4286 | return retval; |
| 4287 | } |
| 4288 | |
| 4289 | static struct job * |
| 4290 | growjobtab(void) |
| 4291 | { |
| 4292 | size_t len; |
| 4293 | ptrdiff_t offset; |
| 4294 | struct job *jp, *jq; |
| 4295 | |
| 4296 | len = njobs * sizeof(*jp); |
| 4297 | jq = jobtab; |
| 4298 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4299 | |
| 4300 | offset = (char *)jp - (char *)jq; |
| 4301 | if (offset) { |
| 4302 | /* Relocate pointers */ |
| 4303 | size_t l = len; |
| 4304 | |
| 4305 | jq = (struct job *)((char *)jq + l); |
| 4306 | while (l) { |
| 4307 | l -= sizeof(*jp); |
| 4308 | jq--; |
| 4309 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4310 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4311 | if (joff(jp)->ps == &jq->ps0) |
| 4312 | jmove(joff(jp)->ps); |
| 4313 | if (joff(jp)->prev_job) |
| 4314 | jmove(joff(jp)->prev_job); |
| 4315 | } |
| 4316 | if (curjob) |
| 4317 | jmove(curjob); |
| 4318 | #undef joff |
| 4319 | #undef jmove |
| 4320 | } |
| 4321 | |
| 4322 | njobs += 4; |
| 4323 | jobtab = jp; |
| 4324 | jp = (struct job *)((char *)jp + len); |
| 4325 | jq = jp + 3; |
| 4326 | do { |
| 4327 | jq->used = 0; |
| 4328 | } while (--jq >= jp); |
| 4329 | return jp; |
| 4330 | } |
| 4331 | |
| 4332 | /* |
| 4333 | * Return a new job structure. |
| 4334 | * Called with interrupts off. |
| 4335 | */ |
| 4336 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4337 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4338 | { |
| 4339 | int i; |
| 4340 | struct job *jp; |
| 4341 | |
| 4342 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4343 | if (--i < 0) { |
| 4344 | jp = growjobtab(); |
| 4345 | break; |
| 4346 | } |
| 4347 | if (jp->used == 0) |
| 4348 | break; |
| 4349 | if (jp->state != JOBDONE || !jp->waited) |
| 4350 | continue; |
| 4351 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4352 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4353 | continue; |
| 4354 | #endif |
| 4355 | freejob(jp); |
| 4356 | break; |
| 4357 | } |
| 4358 | memset(jp, 0, sizeof(*jp)); |
| 4359 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4360 | /* jp->jobctl is a bitfield. |
| 4361 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4362 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4363 | jp->jobctl = 1; |
| 4364 | #endif |
| 4365 | jp->prev_job = curjob; |
| 4366 | curjob = jp; |
| 4367 | jp->used = 1; |
| 4368 | jp->ps = &jp->ps0; |
| 4369 | if (nprocs > 1) { |
| 4370 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4371 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4372 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4373 | jobno(jp))); |
| 4374 | return jp; |
| 4375 | } |
| 4376 | |
| 4377 | #if JOBS |
| 4378 | /* |
| 4379 | * Return a string identifying a command (to be printed by the |
| 4380 | * jobs command). |
| 4381 | */ |
| 4382 | static char *cmdnextc; |
| 4383 | |
| 4384 | static void |
| 4385 | cmdputs(const char *s) |
| 4386 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4387 | static const char vstype[VSTYPE + 1][3] = { |
| 4388 | "", "}", "-", "+", "?", "=", |
| 4389 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4390 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4391 | }; |
| 4392 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4393 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4394 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4395 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4396 | unsigned char c; |
| 4397 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4398 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4399 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4400 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4401 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4402 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4403 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4404 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4405 | switch (c) { |
| 4406 | case CTLESC: |
| 4407 | c = *p++; |
| 4408 | break; |
| 4409 | case CTLVAR: |
| 4410 | subtype = *p++; |
| 4411 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4412 | str = "${#"; |
| 4413 | else |
| 4414 | str = "${"; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 4415 | goto dostr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4416 | case CTLENDVAR: |
| 4417 | str = "\"}" + !(quoted & 1); |
| 4418 | quoted >>= 1; |
| 4419 | subtype = 0; |
| 4420 | goto dostr; |
| 4421 | case CTLBACKQ: |
| 4422 | str = "$(...)"; |
| 4423 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4424 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4425 | case CTLARI: |
| 4426 | str = "$(("; |
| 4427 | goto dostr; |
| 4428 | case CTLENDARI: |
| 4429 | str = "))"; |
| 4430 | goto dostr; |
| 4431 | #endif |
| 4432 | case CTLQUOTEMARK: |
| 4433 | quoted ^= 1; |
| 4434 | c = '"'; |
| 4435 | break; |
| 4436 | case '=': |
| 4437 | if (subtype == 0) |
| 4438 | break; |
| 4439 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4440 | quoted <<= 1; |
| 4441 | str = vstype[subtype & VSTYPE]; |
| 4442 | if (subtype & VSNUL) |
| 4443 | c = ':'; |
| 4444 | else |
| 4445 | goto checkstr; |
| 4446 | break; |
| 4447 | case '\'': |
| 4448 | case '\\': |
| 4449 | case '"': |
| 4450 | case '$': |
| 4451 | /* These can only happen inside quotes */ |
| 4452 | cc[0] = c; |
| 4453 | str = cc; |
| 4454 | c = '\\'; |
| 4455 | break; |
| 4456 | default: |
| 4457 | break; |
| 4458 | } |
| 4459 | USTPUTC(c, nextc); |
| 4460 | checkstr: |
| 4461 | if (!str) |
| 4462 | continue; |
| 4463 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4464 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4465 | USTPUTC(c, nextc); |
| 4466 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4467 | } /* while *p++ not NUL */ |
| 4468 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4469 | if (quoted & 1) { |
| 4470 | USTPUTC('"', nextc); |
| 4471 | } |
| 4472 | *nextc = 0; |
| 4473 | cmdnextc = nextc; |
| 4474 | } |
| 4475 | |
| 4476 | /* cmdtxt() and cmdlist() call each other */ |
| 4477 | static void cmdtxt(union node *n); |
| 4478 | |
| 4479 | static void |
| 4480 | cmdlist(union node *np, int sep) |
| 4481 | { |
| 4482 | for (; np; np = np->narg.next) { |
| 4483 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4484 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4485 | cmdtxt(np); |
| 4486 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4487 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | static void |
| 4492 | cmdtxt(union node *n) |
| 4493 | { |
| 4494 | union node *np; |
| 4495 | struct nodelist *lp; |
| 4496 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4497 | |
| 4498 | if (!n) |
| 4499 | return; |
| 4500 | switch (n->type) { |
| 4501 | default: |
| 4502 | #if DEBUG |
| 4503 | abort(); |
| 4504 | #endif |
| 4505 | case NPIPE: |
| 4506 | lp = n->npipe.cmdlist; |
| 4507 | for (;;) { |
| 4508 | cmdtxt(lp->n); |
| 4509 | lp = lp->next; |
| 4510 | if (!lp) |
| 4511 | break; |
| 4512 | cmdputs(" | "); |
| 4513 | } |
| 4514 | break; |
| 4515 | case NSEMI: |
| 4516 | p = "; "; |
| 4517 | goto binop; |
| 4518 | case NAND: |
| 4519 | p = " && "; |
| 4520 | goto binop; |
| 4521 | case NOR: |
| 4522 | p = " || "; |
| 4523 | binop: |
| 4524 | cmdtxt(n->nbinary.ch1); |
| 4525 | cmdputs(p); |
| 4526 | n = n->nbinary.ch2; |
| 4527 | goto donode; |
| 4528 | case NREDIR: |
| 4529 | case NBACKGND: |
| 4530 | n = n->nredir.n; |
| 4531 | goto donode; |
| 4532 | case NNOT: |
| 4533 | cmdputs("!"); |
| 4534 | n = n->nnot.com; |
| 4535 | donode: |
| 4536 | cmdtxt(n); |
| 4537 | break; |
| 4538 | case NIF: |
| 4539 | cmdputs("if "); |
| 4540 | cmdtxt(n->nif.test); |
| 4541 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4542 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4543 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4544 | cmdputs("; else "); |
| 4545 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4546 | } else { |
| 4547 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4548 | } |
| 4549 | p = "; fi"; |
| 4550 | goto dotail; |
| 4551 | case NSUBSHELL: |
| 4552 | cmdputs("("); |
| 4553 | n = n->nredir.n; |
| 4554 | p = ")"; |
| 4555 | goto dotail; |
| 4556 | case NWHILE: |
| 4557 | p = "while "; |
| 4558 | goto until; |
| 4559 | case NUNTIL: |
| 4560 | p = "until "; |
| 4561 | until: |
| 4562 | cmdputs(p); |
| 4563 | cmdtxt(n->nbinary.ch1); |
| 4564 | n = n->nbinary.ch2; |
| 4565 | p = "; done"; |
| 4566 | dodo: |
| 4567 | cmdputs("; do "); |
| 4568 | dotail: |
| 4569 | cmdtxt(n); |
| 4570 | goto dotail2; |
| 4571 | case NFOR: |
| 4572 | cmdputs("for "); |
| 4573 | cmdputs(n->nfor.var); |
| 4574 | cmdputs(" in "); |
| 4575 | cmdlist(n->nfor.args, 1); |
| 4576 | n = n->nfor.body; |
| 4577 | p = "; done"; |
| 4578 | goto dodo; |
| 4579 | case NDEFUN: |
| 4580 | cmdputs(n->narg.text); |
| 4581 | p = "() { ... }"; |
| 4582 | goto dotail2; |
| 4583 | case NCMD: |
| 4584 | cmdlist(n->ncmd.args, 1); |
| 4585 | cmdlist(n->ncmd.redirect, 0); |
| 4586 | break; |
| 4587 | case NARG: |
| 4588 | p = n->narg.text; |
| 4589 | dotail2: |
| 4590 | cmdputs(p); |
| 4591 | break; |
| 4592 | case NHERE: |
| 4593 | case NXHERE: |
| 4594 | p = "<<..."; |
| 4595 | goto dotail2; |
| 4596 | case NCASE: |
| 4597 | cmdputs("case "); |
| 4598 | cmdputs(n->ncase.expr->narg.text); |
| 4599 | cmdputs(" in "); |
| 4600 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4601 | cmdtxt(np->nclist.pattern); |
| 4602 | cmdputs(") "); |
| 4603 | cmdtxt(np->nclist.body); |
| 4604 | cmdputs(";; "); |
| 4605 | } |
| 4606 | p = "esac"; |
| 4607 | goto dotail2; |
| 4608 | case NTO: |
| 4609 | p = ">"; |
| 4610 | goto redir; |
| 4611 | case NCLOBBER: |
| 4612 | p = ">|"; |
| 4613 | goto redir; |
| 4614 | case NAPPEND: |
| 4615 | p = ">>"; |
| 4616 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4617 | #if ENABLE_ASH_BASH_COMPAT |
| 4618 | case NTO2: |
| 4619 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4620 | case NTOFD: |
| 4621 | p = ">&"; |
| 4622 | goto redir; |
| 4623 | case NFROM: |
| 4624 | p = "<"; |
| 4625 | goto redir; |
| 4626 | case NFROMFD: |
| 4627 | p = "<&"; |
| 4628 | goto redir; |
| 4629 | case NFROMTO: |
| 4630 | p = "<>"; |
| 4631 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4632 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4633 | cmdputs(p); |
| 4634 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4635 | cmdputs(utoa(n->ndup.dupfd)); |
| 4636 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4637 | } |
| 4638 | n = n->nfile.fname; |
| 4639 | goto donode; |
| 4640 | } |
| 4641 | } |
| 4642 | |
| 4643 | static char * |
| 4644 | commandtext(union node *n) |
| 4645 | { |
| 4646 | char *name; |
| 4647 | |
| 4648 | STARTSTACKSTR(cmdnextc); |
| 4649 | cmdtxt(n); |
| 4650 | name = stackblock(); |
| 4651 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4652 | name, cmdnextc, cmdnextc)); |
| 4653 | return ckstrdup(name); |
| 4654 | } |
| 4655 | #endif /* JOBS */ |
| 4656 | |
| 4657 | /* |
| 4658 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4659 | * own process group. Jp is a job structure that the job is to be added to. |
| 4660 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4661 | * be NULL. The mode parameter can be one of the following: |
| 4662 | * FORK_FG - Fork off a foreground process. |
| 4663 | * FORK_BG - Fork off a background process. |
| 4664 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4665 | * process group even if job control is on. |
| 4666 | * |
| 4667 | * When job control is turned off, background processes have their standard |
| 4668 | * input redirected to /dev/null (except for the second and later processes |
| 4669 | * in a pipeline). |
| 4670 | * |
| 4671 | * Called with interrupts off. |
| 4672 | */ |
| 4673 | /* |
| 4674 | * Clear traps on a fork. |
| 4675 | */ |
| 4676 | static void |
| 4677 | clear_traps(void) |
| 4678 | { |
| 4679 | char **tp; |
| 4680 | |
| 4681 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4682 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4683 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4684 | if (trap_ptr == trap) |
| 4685 | free(*tp); |
| 4686 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4687 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4688 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4689 | setsignal(tp - trap); |
| 4690 | INT_ON; |
| 4691 | } |
| 4692 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 4693 | may_have_traps = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4694 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4695 | |
| 4696 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4697 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4698 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4699 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4700 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4701 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4702 | { |
| 4703 | int oldlvl; |
| 4704 | |
| 4705 | TRACE(("Child shell %d\n", getpid())); |
| 4706 | oldlvl = shlvl; |
| 4707 | shlvl++; |
| 4708 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4709 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4710 | * set to the values inherited by the shell from its parent". |
| 4711 | * Do we do it correctly? */ |
| 4712 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4713 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4714 | |
| 4715 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4716 | && n && n->type == NCMD /* is it single cmd? */ |
| 4717 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4718 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4719 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4720 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4721 | ) { |
| 4722 | TRACE(("Trap hack\n")); |
| 4723 | /* Awful hack for `trap` or $(trap). |
| 4724 | * |
| 4725 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4726 | * contains an example where "trap" is executed in a subshell: |
| 4727 | * |
| 4728 | * save_traps=$(trap) |
| 4729 | * ... |
| 4730 | * eval "$save_traps" |
| 4731 | * |
| 4732 | * Standard does not say that "trap" in subshell shall print |
| 4733 | * parent shell's traps. It only says that its output |
| 4734 | * must have suitable form, but then, in the above example |
| 4735 | * (which is not supposed to be normative), it implies that. |
| 4736 | * |
| 4737 | * bash (and probably other shell) does implement it |
| 4738 | * (traps are reset to defaults, but "trap" still shows them), |
| 4739 | * but as a result, "trap" logic is hopelessly messed up: |
| 4740 | * |
| 4741 | * # trap |
| 4742 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4743 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4744 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4745 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4746 | * trap -- 'echo Ho' SIGWINCH |
| 4747 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4748 | * trap -- 'echo Ho' SIGWINCH |
| 4749 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4750 | * trap -- 'echo Ho' SIGWINCH |
| 4751 | * |
| 4752 | * The rules when to forget and when to not forget traps |
| 4753 | * get really complex and nonsensical. |
| 4754 | * |
| 4755 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4756 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4757 | /* Save trap handler strings for trap builtin to print */ |
Ron Yorston | d840c5d | 2015-07-19 23:05:20 +0200 | [diff] [blame] | 4758 | trap_ptr = xmemdup(trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4759 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4760 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4761 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4762 | #if JOBS |
| 4763 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4764 | doing_jobctl = 0; |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4765 | if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4766 | pid_t pgrp; |
| 4767 | |
| 4768 | if (jp->nprocs == 0) |
| 4769 | pgrp = getpid(); |
| 4770 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4771 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4772 | /* this can fail because we are doing it in the parent also */ |
| 4773 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4774 | if (mode == FORK_FG) |
| 4775 | xtcsetpgrp(ttyfd, pgrp); |
| 4776 | setsignal(SIGTSTP); |
| 4777 | setsignal(SIGTTOU); |
| 4778 | } else |
| 4779 | #endif |
| 4780 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4781 | /* man bash: "When job control is not in effect, |
| 4782 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4783 | ignoresig(SIGINT); |
| 4784 | ignoresig(SIGQUIT); |
| 4785 | if (jp->nprocs == 0) { |
| 4786 | close(0); |
| 4787 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4788 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4789 | } |
| 4790 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4791 | if (oldlvl == 0) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4792 | if (iflag) { /* why if iflag only? */ |
| 4793 | setsignal(SIGINT); |
| 4794 | setsignal(SIGTERM); |
| 4795 | } |
| 4796 | /* man bash: |
| 4797 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4798 | * commands run by bash have signal handlers |
| 4799 | * set to the values inherited by the shell |
| 4800 | * from its parent". |
| 4801 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4802 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4803 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4804 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4805 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4806 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4807 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4808 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4809 | /* "jobs": we do not want to clear job list for it, |
| 4810 | * instead we remove only _its_ own_ job from job list. |
| 4811 | * This makes "jobs .... | cat" more useful. |
| 4812 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4813 | freejob(curjob); |
| 4814 | return; |
| 4815 | } |
| 4816 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4817 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4818 | freejob(jp); |
| 4819 | jobless = 0; |
| 4820 | } |
| 4821 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4822 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4823 | #if !JOBS |
| 4824 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4825 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4826 | static void |
| 4827 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4828 | { |
| 4829 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4830 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4831 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4832 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4833 | jobless++; |
| 4834 | return; |
| 4835 | } |
| 4836 | #if JOBS |
| 4837 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4838 | int pgrp; |
| 4839 | |
| 4840 | if (jp->nprocs == 0) |
| 4841 | pgrp = pid; |
| 4842 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4843 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4844 | /* This can fail because we are doing it in the child also */ |
| 4845 | setpgid(pid, pgrp); |
| 4846 | } |
| 4847 | #endif |
| 4848 | if (mode == FORK_BG) { |
| 4849 | backgndpid = pid; /* set $! */ |
| 4850 | set_curjob(jp, CUR_RUNNING); |
| 4851 | } |
| 4852 | if (jp) { |
| 4853 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4854 | ps->ps_pid = pid; |
| 4855 | ps->ps_status = -1; |
| 4856 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4857 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4858 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4859 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4860 | #endif |
| 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | static int |
| 4865 | forkshell(struct job *jp, union node *n, int mode) |
| 4866 | { |
| 4867 | int pid; |
| 4868 | |
| 4869 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4870 | pid = fork(); |
| 4871 | if (pid < 0) { |
| 4872 | TRACE(("Fork failed, errno=%d", errno)); |
| 4873 | if (jp) |
| 4874 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4875 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4876 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4877 | if (pid == 0) { |
| 4878 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4879 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4880 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4881 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4882 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4883 | return pid; |
| 4884 | } |
| 4885 | |
| 4886 | /* |
| 4887 | * Wait for job to finish. |
| 4888 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4889 | * Under job control we have the problem that while a child process |
| 4890 | * is running interrupts generated by the user are sent to the child |
| 4891 | * but not to the shell. This means that an infinite loop started by |
| 4892 | * an interactive user may be hard to kill. With job control turned off, |
| 4893 | * an interactive user may place an interactive program inside a loop. |
| 4894 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4895 | * these interrupts to also abort the loop. The approach we take here |
| 4896 | * is to have the shell ignore interrupt signals while waiting for a |
| 4897 | * foreground process to terminate, and then send itself an interrupt |
| 4898 | * signal if the child process was terminated by an interrupt signal. |
| 4899 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4900 | * exit on interrupt; unless these processes terminate themselves by |
| 4901 | * sending a signal to themselves (instead of calling exit) they will |
| 4902 | * confuse this approach. |
| 4903 | * |
| 4904 | * Called with interrupts off. |
| 4905 | */ |
| 4906 | static int |
| 4907 | waitforjob(struct job *jp) |
| 4908 | { |
| 4909 | int st; |
| 4910 | |
| 4911 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4912 | |
| 4913 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4914 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4915 | /* In non-interactive shells, we _can_ get |
| 4916 | * a keyboard signal here and be EINTRed, |
| 4917 | * but we just loop back, waiting for command to complete. |
| 4918 | * |
| 4919 | * man bash: |
| 4920 | * "If bash is waiting for a command to complete and receives |
| 4921 | * a signal for which a trap has been set, the trap |
| 4922 | * will not be executed until the command completes." |
| 4923 | * |
| 4924 | * Reality is that even if trap is not set, bash |
| 4925 | * will not act on the signal until command completes. |
| 4926 | * Try this. sleep5intoff.c: |
| 4927 | * #include <signal.h> |
| 4928 | * #include <unistd.h> |
| 4929 | * int main() { |
| 4930 | * sigset_t set; |
| 4931 | * sigemptyset(&set); |
| 4932 | * sigaddset(&set, SIGINT); |
| 4933 | * sigaddset(&set, SIGQUIT); |
| 4934 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4935 | * sleep(5); |
| 4936 | * return 0; |
| 4937 | * } |
| 4938 | * $ bash -c './sleep5intoff; echo hi' |
| 4939 | * ^C^C^C^C <--- pressing ^C once a second |
| 4940 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4941 | * $ bash -c './sleep5intoff; echo hi' |
| 4942 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4943 | * $ _ |
| 4944 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4945 | dowait(DOWAIT_BLOCK, jp); |
| 4946 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4947 | INT_ON; |
| 4948 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4949 | st = getstatus(jp); |
| 4950 | #if JOBS |
| 4951 | if (jp->jobctl) { |
| 4952 | xtcsetpgrp(ttyfd, rootpid); |
| 4953 | /* |
| 4954 | * This is truly gross. |
| 4955 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4956 | * caused us (the shell) to no longer be in the controlling |
| 4957 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4958 | * intuit from the subprocess exit status whether a SIGINT |
| 4959 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4960 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4961 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4962 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4963 | } |
| 4964 | if (jp->state == JOBDONE) |
| 4965 | #endif |
| 4966 | freejob(jp); |
| 4967 | return st; |
| 4968 | } |
| 4969 | |
| 4970 | /* |
| 4971 | * return 1 if there are stopped jobs, otherwise 0 |
| 4972 | */ |
| 4973 | static int |
| 4974 | stoppedjobs(void) |
| 4975 | { |
| 4976 | struct job *jp; |
| 4977 | int retval; |
| 4978 | |
| 4979 | retval = 0; |
| 4980 | if (job_warning) |
| 4981 | goto out; |
| 4982 | jp = curjob; |
| 4983 | if (jp && jp->state == JOBSTOPPED) { |
| 4984 | out2str("You have stopped jobs.\n"); |
| 4985 | job_warning = 2; |
| 4986 | retval++; |
| 4987 | } |
| 4988 | out: |
| 4989 | return retval; |
| 4990 | } |
| 4991 | |
| 4992 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4993 | /* ============ redir.c |
| 4994 | * |
| 4995 | * Code for dealing with input/output redirection. |
| 4996 | */ |
| 4997 | |
Denys Vlasenko | 8d0e0cd | 2011-01-25 23:21:46 +0100 | [diff] [blame] | 4998 | #undef EMPTY |
| 4999 | #undef CLOSED |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5000 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5001 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5002 | |
| 5003 | /* |
| 5004 | * Open a file in noclobber mode. |
| 5005 | * The code was copied from bash. |
| 5006 | */ |
| 5007 | static int |
| 5008 | noclobberopen(const char *fname) |
| 5009 | { |
| 5010 | int r, fd; |
| 5011 | struct stat finfo, finfo2; |
| 5012 | |
| 5013 | /* |
| 5014 | * If the file exists and is a regular file, return an error |
| 5015 | * immediately. |
| 5016 | */ |
| 5017 | r = stat(fname, &finfo); |
| 5018 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 5019 | errno = EEXIST; |
| 5020 | return -1; |
| 5021 | } |
| 5022 | |
| 5023 | /* |
| 5024 | * If the file was not present (r != 0), make sure we open it |
| 5025 | * exclusively so that if it is created before we open it, our open |
| 5026 | * will fail. Make sure that we do not truncate an existing file. |
| 5027 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 5028 | * file was not a regular file, we leave O_EXCL off. |
| 5029 | */ |
| 5030 | if (r != 0) |
| 5031 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 5032 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 5033 | |
| 5034 | /* If the open failed, return the file descriptor right away. */ |
| 5035 | if (fd < 0) |
| 5036 | return fd; |
| 5037 | |
| 5038 | /* |
| 5039 | * OK, the open succeeded, but the file may have been changed from a |
| 5040 | * non-regular file to a regular file between the stat and the open. |
| 5041 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 5042 | * did not exist and is symlinked to an existing file between the stat |
| 5043 | * and open. |
| 5044 | */ |
| 5045 | |
| 5046 | /* |
| 5047 | * If we can open it and fstat the file descriptor, and neither check |
| 5048 | * revealed that it was a regular file, and the file has not been |
| 5049 | * replaced, return the file descriptor. |
| 5050 | */ |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 5051 | if (fstat(fd, &finfo2) == 0 |
| 5052 | && !S_ISREG(finfo2.st_mode) |
| 5053 | && finfo.st_dev == finfo2.st_dev |
| 5054 | && finfo.st_ino == finfo2.st_ino |
| 5055 | ) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5056 | return fd; |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 5057 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5058 | |
| 5059 | /* The file has been replaced. badness. */ |
| 5060 | close(fd); |
| 5061 | errno = EEXIST; |
| 5062 | return -1; |
| 5063 | } |
| 5064 | |
| 5065 | /* |
| 5066 | * Handle here documents. Normally we fork off a process to write the |
| 5067 | * data to a pipe. If the document is short, we can stuff the data in |
| 5068 | * the pipe without forking. |
| 5069 | */ |
| 5070 | /* openhere needs this forward reference */ |
| 5071 | static void expandhere(union node *arg, int fd); |
| 5072 | static int |
| 5073 | openhere(union node *redir) |
| 5074 | { |
| 5075 | int pip[2]; |
| 5076 | size_t len = 0; |
| 5077 | |
| 5078 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5079 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5080 | if (redir->type == NHERE) { |
| 5081 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5082 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5083 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 5084 | goto out; |
| 5085 | } |
| 5086 | } |
| 5087 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5088 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5089 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5090 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5091 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5092 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5093 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5094 | signal(SIGPIPE, SIG_DFL); |
| 5095 | if (redir->type == NHERE) |
| 5096 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5097 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5098 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5099 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5100 | } |
| 5101 | out: |
| 5102 | close(pip[1]); |
| 5103 | return pip[0]; |
| 5104 | } |
| 5105 | |
| 5106 | static int |
| 5107 | openredirect(union node *redir) |
| 5108 | { |
| 5109 | char *fname; |
| 5110 | int f; |
| 5111 | |
| 5112 | switch (redir->nfile.type) { |
Denys Vlasenko | 557482c | 2016-09-25 21:24:04 +0200 | [diff] [blame] | 5113 | /* Can't happen, our single caller does this itself */ |
| 5114 | // case NTOFD: |
| 5115 | // case NFROMFD: |
| 5116 | // return -1; |
| 5117 | case NHERE: |
| 5118 | case NXHERE: |
| 5119 | return openhere(redir); |
| 5120 | } |
| 5121 | |
| 5122 | /* For N[X]HERE, reading redir->nfile.expfname would touch beyond |
| 5123 | * allocated space. Do it only when we know it is safe. |
| 5124 | */ |
| 5125 | fname = redir->nfile.expfname; |
| 5126 | |
| 5127 | switch (redir->nfile.type) { |
| 5128 | default: |
| 5129 | #if DEBUG |
| 5130 | abort(); |
| 5131 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5132 | case NFROM: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5133 | f = open(fname, O_RDONLY); |
| 5134 | if (f < 0) |
| 5135 | goto eopen; |
| 5136 | break; |
| 5137 | case NFROMTO: |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5138 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5139 | if (f < 0) |
| 5140 | goto ecreate; |
| 5141 | break; |
| 5142 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5143 | #if ENABLE_ASH_BASH_COMPAT |
| 5144 | case NTO2: |
| 5145 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5146 | /* Take care of noclobber mode. */ |
| 5147 | if (Cflag) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5148 | f = noclobberopen(fname); |
| 5149 | if (f < 0) |
| 5150 | goto ecreate; |
| 5151 | break; |
| 5152 | } |
| 5153 | /* FALLTHROUGH */ |
| 5154 | case NCLOBBER: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5155 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5156 | if (f < 0) |
| 5157 | goto ecreate; |
| 5158 | break; |
| 5159 | case NAPPEND: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5160 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5161 | if (f < 0) |
| 5162 | goto ecreate; |
| 5163 | break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | return f; |
| 5167 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5168 | ash_msg_and_raise_error("can't create %s: %s", fname, errmsg(errno, "nonexistent directory")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5169 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5170 | ash_msg_and_raise_error("can't open %s: %s", fname, errmsg(errno, "no such file")); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | /* |
| 5174 | * Copy a file descriptor to be >= to. Returns -1 |
| 5175 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5176 | * file descriptors left. |
| 5177 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5178 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5179 | * old code was doing close(to) prior to copyfd() to achieve the same */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5180 | enum { |
| 5181 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5182 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5183 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5184 | static int |
| 5185 | copyfd(int from, int to) |
| 5186 | { |
| 5187 | int newfd; |
| 5188 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5189 | if (to & COPYFD_EXACT) { |
| 5190 | to &= ~COPYFD_EXACT; |
| 5191 | /*if (from != to)*/ |
| 5192 | newfd = dup2(from, to); |
| 5193 | } else { |
| 5194 | newfd = fcntl(from, F_DUPFD, to); |
| 5195 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5196 | if (newfd < 0) { |
| 5197 | if (errno == EMFILE) |
| 5198 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5199 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5200 | ash_msg_and_raise_error("%d: %m", from); |
| 5201 | } |
| 5202 | return newfd; |
| 5203 | } |
| 5204 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5205 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5206 | struct two_fd_t { |
| 5207 | int orig, copy; |
| 5208 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5209 | struct redirtab { |
| 5210 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5211 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5212 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5213 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5214 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5215 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5216 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5217 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5218 | { |
| 5219 | int i; |
| 5220 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5221 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5222 | return 0; |
| 5223 | |
| 5224 | for (i = 0; i < rp->pair_count; i++) { |
| 5225 | if (rp->two_fd[i].orig == fd) { |
| 5226 | /* already remembered */ |
| 5227 | return 0; |
| 5228 | } |
| 5229 | } |
| 5230 | return 1; |
| 5231 | } |
| 5232 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5233 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5234 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5235 | { |
| 5236 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5237 | struct parsefile *pf; |
| 5238 | |
| 5239 | if (fd == -1) |
| 5240 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5241 | /* Check open scripts' fds */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5242 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5243 | while (pf) { |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5244 | /* We skip pf_fd == 0 case because of the following case: |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5245 | * $ ash # running ash interactively |
| 5246 | * $ . ./script.sh |
| 5247 | * and in script.sh: "exec 9>&0". |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5248 | * Even though top-level pf_fd _is_ 0, |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5249 | * it's still ok to use it: "read" builtin uses it, |
| 5250 | * why should we cripple "exec" builtin? |
| 5251 | */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5252 | if (pf->pf_fd > 0 && fd == pf->pf_fd) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5253 | return 1; |
| 5254 | } |
| 5255 | pf = pf->prev; |
| 5256 | } |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5257 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5258 | if (!rp) |
| 5259 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5260 | /* Check saved fds of redirects */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5261 | fd |= COPYFD_RESTORE; |
| 5262 | for (i = 0; i < rp->pair_count; i++) { |
| 5263 | if (rp->two_fd[i].copy == fd) { |
| 5264 | return 1; |
| 5265 | } |
| 5266 | } |
| 5267 | return 0; |
| 5268 | } |
| 5269 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5270 | /* |
| 5271 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5272 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5273 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5274 | */ |
| 5275 | /* flags passed to redirect */ |
| 5276 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5277 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5278 | static void |
| 5279 | redirect(union node *redir, int flags) |
| 5280 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5281 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5282 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5283 | int i; |
| 5284 | int fd; |
| 5285 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5286 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5287 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5288 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5289 | if (!redir) { |
| 5290 | return; |
| 5291 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5292 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5293 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5294 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5295 | INT_OFF; |
| 5296 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5297 | union node *tmp = redir; |
| 5298 | do { |
| 5299 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5300 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5301 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5302 | sv_pos++; |
| 5303 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5304 | tmp = tmp->nfile.next; |
| 5305 | } while (tmp); |
| 5306 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5307 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5308 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5309 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5310 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5311 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5312 | while (sv_pos > 0) { |
| 5313 | sv_pos--; |
| 5314 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5315 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5316 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5317 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5318 | do { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5319 | int right_fd = -1; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5320 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5321 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5322 | right_fd = redir->ndup.dupfd; |
| 5323 | //bb_error_msg("doing %d > %d", fd, right_fd); |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5324 | /* redirect from/to same file descriptor? */ |
| 5325 | if (right_fd == fd) |
| 5326 | continue; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5327 | /* "echo >&10" and 10 is a fd opened to a sh script? */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5328 | if (is_hidden_fd(sv, right_fd)) { |
| 5329 | errno = EBADF; /* as if it is closed */ |
| 5330 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5331 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5332 | newfd = -1; |
| 5333 | } else { |
| 5334 | newfd = openredirect(redir); /* always >= 0 */ |
| 5335 | if (fd == newfd) { |
| 5336 | /* Descriptor wasn't open before redirect. |
| 5337 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5338 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5339 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5340 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5341 | continue; |
| 5342 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5343 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5344 | #if ENABLE_ASH_BASH_COMPAT |
| 5345 | redirect_more: |
| 5346 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5347 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5348 | /* Copy old descriptor */ |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5349 | /* Careful to not accidentally "save" |
| 5350 | * to the same fd as right side fd in N>&M */ |
| 5351 | int minfd = right_fd < 10 ? 10 : right_fd + 1; |
| 5352 | i = fcntl(fd, F_DUPFD, minfd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5353 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5354 | * are closed in popredir() in the child, preventing them from leaking |
| 5355 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5356 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5357 | if (i == -1) { |
| 5358 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5359 | if (i != EBADF) { |
| 5360 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5361 | if (newfd >= 0) |
| 5362 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5363 | errno = i; |
| 5364 | ash_msg_and_raise_error("%d: %m", fd); |
| 5365 | /* NOTREACHED */ |
| 5366 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5367 | /* EBADF: it is not open - good, remember to close it */ |
| 5368 | remember_to_close: |
| 5369 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5370 | } else { /* fd is open, save its copy */ |
| 5371 | /* "exec fd>&-" should not close fds |
| 5372 | * which point to script file(s). |
| 5373 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5374 | if (is_hidden_fd(sv, fd)) |
| 5375 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5376 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5377 | if (fd == 2) |
| 5378 | copied_fd2 = i; |
| 5379 | sv->two_fd[sv_pos].orig = fd; |
| 5380 | sv->two_fd[sv_pos].copy = i; |
| 5381 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5382 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5383 | if (newfd < 0) { |
| 5384 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5385 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5386 | /* Don't want to trigger debugging */ |
| 5387 | if (fd != -1) |
| 5388 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5389 | } else { |
| 5390 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5391 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5392 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5393 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5394 | #if ENABLE_ASH_BASH_COMPAT |
| 5395 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5396 | #endif |
| 5397 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5398 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5399 | #if ENABLE_ASH_BASH_COMPAT |
| 5400 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5401 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5402 | newfd = 1; |
| 5403 | fd = 2; |
| 5404 | goto redirect_more; |
| 5405 | } |
| 5406 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5407 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5408 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5409 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5410 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5411 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
| 5414 | /* |
| 5415 | * Undo the effects of the last redirection. |
| 5416 | */ |
| 5417 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5418 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5419 | { |
| 5420 | struct redirtab *rp; |
| 5421 | int i; |
| 5422 | |
Ron Yorston | 95650a8 | 2015-10-30 19:07:37 +0000 | [diff] [blame] | 5423 | if (--g_nullredirs >= 0 || redirlist == NULL) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5424 | return; |
| 5425 | INT_OFF; |
| 5426 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5427 | for (i = 0; i < rp->pair_count; i++) { |
| 5428 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5429 | int copy = rp->two_fd[i].copy; |
| 5430 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5431 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5432 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5433 | continue; |
| 5434 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5435 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5436 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5437 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5438 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5439 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5440 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5441 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5442 | } |
| 5443 | } |
| 5444 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5445 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5446 | free(rp); |
| 5447 | INT_ON; |
| 5448 | } |
| 5449 | |
| 5450 | /* |
| 5451 | * Undo all redirections. Called on error or interrupt. |
| 5452 | */ |
| 5453 | |
| 5454 | /* |
| 5455 | * Discard all saved file descriptors. |
| 5456 | */ |
| 5457 | static void |
| 5458 | clearredir(int drop) |
| 5459 | { |
| 5460 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5461 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5462 | if (!redirlist) |
| 5463 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5464 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5465 | } |
| 5466 | } |
| 5467 | |
| 5468 | static int |
| 5469 | redirectsafe(union node *redir, int flags) |
| 5470 | { |
| 5471 | int err; |
| 5472 | volatile int saveint; |
| 5473 | struct jmploc *volatile savehandler = exception_handler; |
| 5474 | struct jmploc jmploc; |
| 5475 | |
| 5476 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5477 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5478 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5479 | if (!err) { |
| 5480 | exception_handler = &jmploc; |
| 5481 | redirect(redir, flags); |
| 5482 | } |
| 5483 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5484 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5485 | longjmp(exception_handler->loc, 1); |
| 5486 | RESTORE_INT(saveint); |
| 5487 | return err; |
| 5488 | } |
| 5489 | |
| 5490 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5491 | /* ============ Routines to expand arguments to commands |
| 5492 | * |
| 5493 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5494 | */ |
| 5495 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5496 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5497 | static arith_t |
| 5498 | ash_arith(const char *s) |
| 5499 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5500 | arith_state_t math_state; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5501 | arith_t result; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5502 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5503 | math_state.lookupvar = lookupvar; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 5504 | math_state.setvar = setvar0; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5505 | //math_state.endofname = endofname; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5506 | |
| 5507 | INT_OFF; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5508 | result = arith(&math_state, s); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5509 | if (math_state.errmsg) |
| 5510 | ash_msg_and_raise_error(math_state.errmsg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5511 | INT_ON; |
| 5512 | |
| 5513 | return result; |
| 5514 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5515 | #endif |
| 5516 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5517 | /* |
| 5518 | * expandarg flags |
| 5519 | */ |
| 5520 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5521 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5522 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5523 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5524 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5525 | #define EXP_QPAT 0x20 /* pattern in quoted parameter expansion */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5526 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5527 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
Ron Yorston | 3df47f9 | 2015-05-18 09:53:26 +0200 | [diff] [blame] | 5528 | #define EXP_QUOTED 0x100 /* expand word in double quotes */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5529 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5530 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5531 | */ |
| 5532 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5533 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5534 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5535 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 5536 | #define RMESCAPE_SLASH 0x20 /* Stop globbing after slash */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5537 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5538 | /* Add CTLESC when necessary. */ |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5539 | #define QUOTES_ESC (EXP_FULL | EXP_CASE | EXP_QPAT | EXP_REDIR) |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5540 | /* Do not skip NUL characters. */ |
| 5541 | #define QUOTES_KEEPNUL EXP_TILDE |
| 5542 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5543 | /* |
| 5544 | * Structure specifying which parts of the string should be searched |
| 5545 | * for IFS characters. |
| 5546 | */ |
| 5547 | struct ifsregion { |
| 5548 | struct ifsregion *next; /* next region in list */ |
| 5549 | int begoff; /* offset of start of region */ |
| 5550 | int endoff; /* offset of end of region */ |
| 5551 | int nulonly; /* search for nul bytes only */ |
| 5552 | }; |
| 5553 | |
| 5554 | struct arglist { |
| 5555 | struct strlist *list; |
| 5556 | struct strlist **lastp; |
| 5557 | }; |
| 5558 | |
| 5559 | /* output of current string */ |
| 5560 | static char *expdest; |
| 5561 | /* list of back quote expressions */ |
| 5562 | static struct nodelist *argbackq; |
| 5563 | /* first struct in list of ifs regions */ |
| 5564 | static struct ifsregion ifsfirst; |
| 5565 | /* last struct in list */ |
| 5566 | static struct ifsregion *ifslastp; |
| 5567 | /* holds expanded arg list */ |
| 5568 | static struct arglist exparg; |
| 5569 | |
| 5570 | /* |
| 5571 | * Our own itoa(). |
| 5572 | */ |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5573 | #if !ENABLE_SH_MATH_SUPPORT |
| 5574 | /* cvtnum() is used even if math support is off (to prepare $? values and such) */ |
| 5575 | typedef long arith_t; |
| 5576 | # define ARITH_FMT "%ld" |
| 5577 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5578 | static int |
| 5579 | cvtnum(arith_t num) |
| 5580 | { |
| 5581 | int len; |
| 5582 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 5583 | expdest = makestrspace(sizeof(arith_t)*3 + 2, expdest); |
| 5584 | len = fmtstr(expdest, sizeof(arith_t)*3 + 2, ARITH_FMT, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5585 | STADJUST(len, expdest); |
| 5586 | return len; |
| 5587 | } |
| 5588 | |
| 5589 | static size_t |
| 5590 | esclen(const char *start, const char *p) |
| 5591 | { |
| 5592 | size_t esc = 0; |
| 5593 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5594 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5595 | esc++; |
| 5596 | } |
| 5597 | return esc; |
| 5598 | } |
| 5599 | |
| 5600 | /* |
| 5601 | * Remove any CTLESC characters from a string. |
| 5602 | */ |
| 5603 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5604 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5605 | { |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 5606 | static const char qchars[] ALIGN1 = { |
| 5607 | IF_ASH_BASH_COMPAT('/',) CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5608 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5609 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5610 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5611 | unsigned protect_against_glob; |
| 5612 | unsigned globbing; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 5613 | IF_ASH_BASH_COMPAT(unsigned slash = flag & RMESCAPE_SLASH;) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5614 | |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 5615 | p = strpbrk(str, qchars IF_ASH_BASH_COMPAT(+ !slash)); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5616 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5617 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5618 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5619 | q = p; |
| 5620 | r = str; |
| 5621 | if (flag & RMESCAPE_ALLOC) { |
| 5622 | size_t len = p - str; |
| 5623 | size_t fulllen = len + strlen(p) + 1; |
| 5624 | |
| 5625 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5626 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5627 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5628 | /* p and str may be invalidated by makestrspace */ |
| 5629 | str = (char *)stackblock() + strloc; |
| 5630 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5631 | } else if (flag & RMESCAPE_HEAP) { |
| 5632 | r = ckmalloc(fulllen); |
| 5633 | } else { |
| 5634 | r = stalloc(fulllen); |
| 5635 | } |
| 5636 | q = r; |
| 5637 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5638 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5639 | } |
| 5640 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5641 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5642 | inquotes = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5643 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5644 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5645 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5646 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5647 | // Note: both inquotes and protect_against_glob only affect whether |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5648 | inquotes = ~inquotes; |
| 5649 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5650 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5651 | continue; |
| 5652 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5653 | if ((unsigned char)*p == CTLESC) { |
| 5654 | p++; |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 5655 | #if DEBUG |
| 5656 | if (*p == '\0') |
| 5657 | ash_msg_and_raise_error("CTLESC at EOL (shouldn't happen)"); |
| 5658 | #endif |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5659 | if (protect_against_glob) { |
| 5660 | *q++ = '\\'; |
| 5661 | } |
| 5662 | } else if (*p == '\\' && !inquotes) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5663 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5664 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5665 | goto copy; |
| 5666 | } |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 5667 | #if ENABLE_ASH_BASH_COMPAT |
| 5668 | else if (*p == '/' && slash) { |
| 5669 | /* stop handling globbing and mark location of slash */ |
| 5670 | globbing = slash = 0; |
| 5671 | *p = CTLESC; |
| 5672 | } |
| 5673 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5674 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5675 | copy: |
| 5676 | *q++ = *p++; |
| 5677 | } |
| 5678 | *q = '\0'; |
| 5679 | if (flag & RMESCAPE_GROW) { |
| 5680 | expdest = r; |
| 5681 | STADJUST(q - r + 1, expdest); |
| 5682 | } |
| 5683 | return r; |
| 5684 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5685 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5686 | |
| 5687 | /* |
| 5688 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5689 | * |
| 5690 | * Returns an stalloced string. |
| 5691 | */ |
| 5692 | static char * |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5693 | preglob(const char *pattern, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5694 | { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5695 | return rmescapes((char *)pattern, flag | RMESCAPE_GLOB); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | /* |
| 5699 | * Put a string on the stack. |
| 5700 | */ |
| 5701 | static void |
| 5702 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5703 | { |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5704 | char *q; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5705 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5706 | if (!len) |
| 5707 | return; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5708 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5709 | q = makestrspace((quotes & QUOTES_ESC) ? len * 2 : len, expdest); |
| 5710 | |
| 5711 | do { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5712 | unsigned char c = *p++; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5713 | if (c) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5714 | int n = SIT(c, syntax); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 5715 | if ((quotes & QUOTES_ESC) |
| 5716 | && ((n == CCTL) |
| 5717 | || (((quotes & EXP_FULL) || syntax != BASESYNTAX) |
| 5718 | && n == CBACK) |
| 5719 | ) |
| 5720 | ) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5721 | USTPUTC(CTLESC, q); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 5722 | } |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5723 | } else if (!(quotes & QUOTES_KEEPNUL)) |
| 5724 | continue; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5725 | USTPUTC(c, q); |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5726 | } while (--len); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5727 | |
| 5728 | expdest = q; |
| 5729 | } |
| 5730 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5731 | static size_t |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5732 | strtodest(const char *p, int syntax, int quotes) |
| 5733 | { |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5734 | size_t len = strlen(p); |
| 5735 | memtodest(p, len, syntax, quotes); |
| 5736 | return len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | /* |
| 5740 | * Record the fact that we have to scan this region of the |
| 5741 | * string for IFS characters. |
| 5742 | */ |
| 5743 | static void |
| 5744 | recordregion(int start, int end, int nulonly) |
| 5745 | { |
| 5746 | struct ifsregion *ifsp; |
| 5747 | |
| 5748 | if (ifslastp == NULL) { |
| 5749 | ifsp = &ifsfirst; |
| 5750 | } else { |
| 5751 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5752 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5753 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5754 | ifslastp->next = ifsp; |
| 5755 | INT_ON; |
| 5756 | } |
| 5757 | ifslastp = ifsp; |
| 5758 | ifslastp->begoff = start; |
| 5759 | ifslastp->endoff = end; |
| 5760 | ifslastp->nulonly = nulonly; |
| 5761 | } |
| 5762 | |
| 5763 | static void |
| 5764 | removerecordregions(int endoff) |
| 5765 | { |
| 5766 | if (ifslastp == NULL) |
| 5767 | return; |
| 5768 | |
| 5769 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5770 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5771 | struct ifsregion *ifsp; |
| 5772 | INT_OFF; |
| 5773 | ifsp = ifsfirst.next->next; |
| 5774 | free(ifsfirst.next); |
| 5775 | ifsfirst.next = ifsp; |
| 5776 | INT_ON; |
| 5777 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5778 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5779 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5780 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5781 | ifslastp = &ifsfirst; |
| 5782 | ifsfirst.endoff = endoff; |
| 5783 | } |
| 5784 | return; |
| 5785 | } |
| 5786 | |
| 5787 | ifslastp = &ifsfirst; |
| 5788 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5789 | ifslastp = ifslastp->next; |
| 5790 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5791 | struct ifsregion *ifsp; |
| 5792 | INT_OFF; |
| 5793 | ifsp = ifslastp->next->next; |
| 5794 | free(ifslastp->next); |
| 5795 | ifslastp->next = ifsp; |
| 5796 | INT_ON; |
| 5797 | } |
| 5798 | if (ifslastp->endoff > endoff) |
| 5799 | ifslastp->endoff = endoff; |
| 5800 | } |
| 5801 | |
| 5802 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5803 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5804 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5805 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5806 | char *name; |
| 5807 | struct passwd *pw; |
| 5808 | const char *home; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 5809 | int quotes = flags & QUOTES_ESC; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5810 | |
| 5811 | name = p + 1; |
| 5812 | |
| 5813 | while ((c = *++p) != '\0') { |
| 5814 | switch (c) { |
| 5815 | case CTLESC: |
| 5816 | return startp; |
| 5817 | case CTLQUOTEMARK: |
| 5818 | return startp; |
| 5819 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5820 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5821 | goto done; |
| 5822 | break; |
| 5823 | case '/': |
| 5824 | case CTLENDVAR: |
| 5825 | goto done; |
| 5826 | } |
| 5827 | } |
| 5828 | done: |
| 5829 | *p = '\0'; |
| 5830 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 5831 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5832 | } else { |
| 5833 | pw = getpwnam(name); |
| 5834 | if (pw == NULL) |
| 5835 | goto lose; |
| 5836 | home = pw->pw_dir; |
| 5837 | } |
| 5838 | if (!home || !*home) |
| 5839 | goto lose; |
| 5840 | *p = c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5841 | strtodest(home, SQSYNTAX, quotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5842 | return p; |
| 5843 | lose: |
| 5844 | *p = c; |
| 5845 | return startp; |
| 5846 | } |
| 5847 | |
| 5848 | /* |
| 5849 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5850 | * want to save its output in a block obtained from malloc. Otherwise |
| 5851 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5852 | * Should be called with interrupts off. |
| 5853 | */ |
| 5854 | struct backcmd { /* result of evalbackcmd */ |
| 5855 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5856 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5857 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5858 | struct job *jp; /* job structure for command */ |
| 5859 | }; |
| 5860 | |
| 5861 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5862 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5863 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 5864 | static int evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5865 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5866 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5867 | evalbackcmd(union node *n, struct backcmd *result) |
| 5868 | { |
| 5869 | int saveherefd; |
| 5870 | |
| 5871 | result->fd = -1; |
| 5872 | result->buf = NULL; |
| 5873 | result->nleft = 0; |
| 5874 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5875 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5876 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5877 | |
| 5878 | saveherefd = herefd; |
| 5879 | herefd = -1; |
| 5880 | |
| 5881 | { |
| 5882 | int pip[2]; |
| 5883 | struct job *jp; |
| 5884 | |
| 5885 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5886 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5887 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5888 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5889 | FORCE_INT_ON; |
| 5890 | close(pip[0]); |
| 5891 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5892 | /*close(1);*/ |
| 5893 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5894 | close(pip[1]); |
| 5895 | } |
| 5896 | eflag = 0; |
| 5897 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5898 | /* NOTREACHED */ |
| 5899 | } |
| 5900 | close(pip[1]); |
| 5901 | result->fd = pip[0]; |
| 5902 | result->jp = jp; |
| 5903 | } |
| 5904 | herefd = saveherefd; |
| 5905 | out: |
| 5906 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5907 | result->fd, result->buf, result->nleft, result->jp)); |
| 5908 | } |
| 5909 | |
| 5910 | /* |
| 5911 | * Expand stuff in backwards quotes. |
| 5912 | */ |
| 5913 | static void |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5914 | expbackq(union node *cmd, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5915 | { |
| 5916 | struct backcmd in; |
| 5917 | int i; |
| 5918 | char buf[128]; |
| 5919 | char *p; |
| 5920 | char *dest; |
| 5921 | int startloc; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5922 | int syntax = flag & EXP_QUOTED ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5923 | struct stackmark smark; |
| 5924 | |
| 5925 | INT_OFF; |
Denys Vlasenko | 60ca834 | 2016-09-30 11:21:21 +0200 | [diff] [blame] | 5926 | startloc = expdest - (char *)stackblock(); |
| 5927 | pushstackmark(&smark, startloc); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5928 | evalbackcmd(cmd, &in); |
| 5929 | popstackmark(&smark); |
| 5930 | |
| 5931 | p = in.buf; |
| 5932 | i = in.nleft; |
| 5933 | if (i == 0) |
| 5934 | goto read; |
| 5935 | for (;;) { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5936 | memtodest(p, i, syntax, flag & QUOTES_ESC); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5937 | read: |
| 5938 | if (in.fd < 0) |
| 5939 | break; |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 5940 | i = nonblock_immune_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5941 | TRACE(("expbackq: read returns %d\n", i)); |
| 5942 | if (i <= 0) |
| 5943 | break; |
| 5944 | p = buf; |
| 5945 | } |
| 5946 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5947 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5948 | if (in.fd >= 0) { |
| 5949 | close(in.fd); |
| 5950 | back_exitstatus = waitforjob(in.jp); |
| 5951 | } |
| 5952 | INT_ON; |
| 5953 | |
| 5954 | /* Eat all trailing newlines */ |
| 5955 | dest = expdest; |
| 5956 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5957 | STUNPUTC(dest); |
| 5958 | expdest = dest; |
| 5959 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5960 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5961 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5962 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 5963 | (int)((dest - (char *)stackblock()) - startloc), |
| 5964 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5965 | stackblock() + startloc)); |
| 5966 | } |
| 5967 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5968 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5969 | /* |
| 5970 | * Expand arithmetic expression. Backup to start of expression, |
| 5971 | * evaluate, place result in (backed up) result, adjust string position. |
| 5972 | */ |
| 5973 | static void |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 5974 | expari(int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5975 | { |
| 5976 | char *p, *start; |
| 5977 | int begoff; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5978 | int len; |
| 5979 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5980 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5981 | |
| 5982 | /* |
| 5983 | * This routine is slightly over-complicated for |
| 5984 | * efficiency. Next we scan backwards looking for the |
| 5985 | * start of arithmetic. |
| 5986 | */ |
| 5987 | start = stackblock(); |
| 5988 | p = expdest - 1; |
| 5989 | *p = '\0'; |
| 5990 | p--; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 5991 | while (1) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5992 | int esc; |
| 5993 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5994 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5995 | p--; |
| 5996 | #if DEBUG |
| 5997 | if (p < start) { |
| 5998 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5999 | } |
| 6000 | #endif |
| 6001 | } |
| 6002 | |
| 6003 | esc = esclen(start, p); |
| 6004 | if (!(esc % 2)) { |
| 6005 | break; |
| 6006 | } |
| 6007 | |
| 6008 | p -= esc + 1; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 6009 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6010 | |
| 6011 | begoff = p - start; |
| 6012 | |
| 6013 | removerecordregions(begoff); |
| 6014 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6015 | expdest = p; |
| 6016 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6017 | if (flag & QUOTES_ESC) |
| 6018 | rmescapes(p + 1, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6019 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6020 | len = cvtnum(ash_arith(p + 1)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6021 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6022 | if (!(flag & EXP_QUOTED)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6023 | recordregion(begoff, begoff + len, 0); |
| 6024 | } |
| 6025 | #endif |
| 6026 | |
| 6027 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6028 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6029 | |
| 6030 | /* |
| 6031 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 6032 | * characters to allow for further processing. Otherwise treat |
| 6033 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6034 | * |
| 6035 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 6036 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 6037 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6038 | */ |
| 6039 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6040 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6041 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6042 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6043 | '=', |
| 6044 | ':', |
| 6045 | CTLQUOTEMARK, |
| 6046 | CTLENDVAR, |
| 6047 | CTLESC, |
| 6048 | CTLVAR, |
| 6049 | CTLBACKQ, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6050 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6051 | CTLENDARI, |
| 6052 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6053 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6054 | }; |
| 6055 | const char *reject = spclchars; |
Ron Yorston | 3df47f9 | 2015-05-18 09:53:26 +0200 | [diff] [blame] | 6056 | int breakall = (flags & (EXP_WORD | EXP_QUOTED)) == EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6057 | int inquotes; |
| 6058 | size_t length; |
| 6059 | int startloc; |
| 6060 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6061 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6062 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6063 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6064 | reject++; |
| 6065 | } |
| 6066 | inquotes = 0; |
| 6067 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6068 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6069 | char *q; |
| 6070 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6071 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6072 | tilde: |
| 6073 | q = p; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6074 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6075 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6076 | } |
| 6077 | start: |
| 6078 | startloc = expdest - (char *)stackblock(); |
| 6079 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6080 | unsigned char c; |
| 6081 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6082 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6083 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6084 | if (c) { |
| 6085 | if (!(c & 0x80) |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 6086 | IF_SH_MATH_SUPPORT(|| c == CTLENDARI) |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6087 | ) { |
| 6088 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 6089 | length++; |
| 6090 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6091 | } |
| 6092 | if (length > 0) { |
| 6093 | int newloc; |
| 6094 | expdest = stack_nputstr(p, length, expdest); |
| 6095 | newloc = expdest - (char *)stackblock(); |
| 6096 | if (breakall && !inquotes && newloc > startloc) { |
| 6097 | recordregion(startloc, newloc, 0); |
| 6098 | } |
| 6099 | startloc = newloc; |
| 6100 | } |
| 6101 | p += length + 1; |
| 6102 | length = 0; |
| 6103 | |
| 6104 | switch (c) { |
| 6105 | case '\0': |
| 6106 | goto breakloop; |
| 6107 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6108 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6109 | p--; |
| 6110 | continue; |
| 6111 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6112 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6113 | reject++; |
| 6114 | /* fall through */ |
| 6115 | case ':': |
| 6116 | /* |
| 6117 | * sort of a hack - expand tildes in variable |
| 6118 | * assignments (after the first '=' and after ':'s). |
| 6119 | */ |
| 6120 | if (*--p == '~') { |
| 6121 | goto tilde; |
| 6122 | } |
| 6123 | continue; |
| 6124 | } |
| 6125 | |
| 6126 | switch (c) { |
| 6127 | case CTLENDVAR: /* ??? */ |
| 6128 | goto breakloop; |
| 6129 | case CTLQUOTEMARK: |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6130 | inquotes ^= EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6131 | /* "$@" syntax adherence hack */ |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6132 | if (inquotes && !memcmp(p, dolatstr + 1, DOLATSTRLEN - 1)) { |
| 6133 | p = evalvar(p + 1, flags | inquotes, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6134 | goto start; |
| 6135 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6136 | addquote: |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6137 | if (flags & QUOTES_ESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6138 | p--; |
| 6139 | length++; |
| 6140 | startloc++; |
| 6141 | } |
| 6142 | break; |
| 6143 | case CTLESC: |
| 6144 | startloc++; |
| 6145 | length++; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6146 | |
| 6147 | /* |
| 6148 | * Quoted parameter expansion pattern: remove quote |
| 6149 | * unless inside inner quotes or we have a literal |
| 6150 | * backslash. |
| 6151 | */ |
| 6152 | if (((flags | inquotes) & (EXP_QPAT | EXP_QUOTED)) == |
| 6153 | EXP_QPAT && *p != '\\') |
| 6154 | break; |
| 6155 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6156 | goto addquote; |
| 6157 | case CTLVAR: |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6158 | TRACE(("argstr: evalvar('%s')\n", p)); |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6159 | p = evalvar(p, flags | inquotes, var_str_list); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6160 | TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock())); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6161 | goto start; |
| 6162 | case CTLBACKQ: |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6163 | expbackq(argbackq->n, flags | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6164 | argbackq = argbackq->next; |
| 6165 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6166 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6167 | case CTLENDARI: |
| 6168 | p--; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6169 | expari(flags | inquotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6170 | goto start; |
| 6171 | #endif |
| 6172 | } |
| 6173 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 6174 | breakloop: ; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6178 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6179 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6180 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6181 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6182 | char c; |
| 6183 | |
| 6184 | loc = startp; |
| 6185 | loc2 = rmesc; |
| 6186 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6187 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6188 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6189 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6190 | c = *loc2; |
| 6191 | if (zero) { |
| 6192 | *loc2 = '\0'; |
| 6193 | s = rmesc; |
| 6194 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6195 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6196 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6197 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6198 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6199 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6200 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6201 | loc++; |
| 6202 | loc++; |
| 6203 | loc2++; |
| 6204 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6205 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6209 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6210 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6211 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6212 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6213 | int try2optimize = match_at_start; |
| 6214 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6215 | int esc = 0; |
| 6216 | char *loc; |
| 6217 | char *loc2; |
| 6218 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6219 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6220 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6221 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6222 | * Logic: |
| 6223 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6224 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6225 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6226 | */ |
| 6227 | /* TODO: document in what other circumstances we are called. */ |
| 6228 | |
| 6229 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6230 | int match; |
| 6231 | char c = *loc2; |
| 6232 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6233 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6234 | *loc2 = '\0'; |
| 6235 | s = rmesc; |
| 6236 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6237 | match = pmatch(pattern, s); |
| 6238 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6239 | *loc2 = c; |
| 6240 | if (match) |
| 6241 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6242 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6243 | if (try2optimize) { |
| 6244 | /* Maybe we can optimize this: |
| 6245 | * if pattern ends with unescaped *, we can avoid checking |
| 6246 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6247 | * it wont match truncated "raw_value_of_" strings too. |
| 6248 | */ |
| 6249 | unsigned plen = strlen(pattern); |
| 6250 | /* Does it end with "*"? */ |
| 6251 | if (plen != 0 && pattern[--plen] == '*') { |
| 6252 | /* "xxxx*" is not escaped */ |
| 6253 | /* "xxx\*" is escaped */ |
| 6254 | /* "xx\\*" is not escaped */ |
| 6255 | /* "x\\\*" is escaped */ |
| 6256 | int slashes = 0; |
| 6257 | while (plen != 0 && pattern[--plen] == '\\') |
| 6258 | slashes++; |
| 6259 | if (!(slashes & 1)) |
| 6260 | break; /* ends with unescaped "*" */ |
| 6261 | } |
| 6262 | try2optimize = 0; |
| 6263 | } |
| 6264 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6265 | loc--; |
| 6266 | if (quotes) { |
| 6267 | if (--esc < 0) { |
| 6268 | esc = esclen(startp, loc); |
| 6269 | } |
| 6270 | if (esc % 2) { |
| 6271 | esc--; |
| 6272 | loc--; |
| 6273 | } |
| 6274 | } |
| 6275 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6276 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6279 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6280 | static void |
| 6281 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6282 | { |
| 6283 | const char *msg; |
| 6284 | const char *tail; |
| 6285 | |
| 6286 | tail = nullstr; |
| 6287 | msg = "parameter not set"; |
| 6288 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6289 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6290 | if (varflags & VSNUL) |
| 6291 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6292 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6293 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6294 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6295 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6296 | ash_msg_and_raise_error("%.*s: %s%s", (int)(end - var - 1), var, msg, tail); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | static const char * |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6300 | subevalvar(char *p, char *varname, int strloc, int subtype, |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6301 | int startloc, int varflags, int flag, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6302 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6303 | struct nodelist *saveargbackq = argbackq; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6304 | int quotes = flag & QUOTES_ESC; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6305 | char *startp; |
| 6306 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6307 | char *rmesc, *rmescend; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6308 | char *str; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6309 | IF_ASH_BASH_COMPAT(char *repl = NULL;) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6310 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6311 | int saveherefd = herefd; |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 6312 | int amount, resetloc; |
| 6313 | IF_ASH_BASH_COMPAT(int workloc;) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6314 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6315 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6316 | |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6317 | //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)", |
| 6318 | // p, varname, strloc, subtype, startloc, varflags, quotes); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6319 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6320 | herefd = -1; |
Ron Yorston | eb6b48b | 2015-05-18 09:51:35 +0200 | [diff] [blame] | 6321 | argstr(p, EXP_TILDE | (subtype != VSASSIGN && subtype != VSQUESTION ? |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6322 | (flag & (EXP_QUOTED | EXP_QPAT) ? EXP_QPAT : EXP_CASE) : 0), |
| 6323 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6324 | STPUTC('\0', expdest); |
| 6325 | herefd = saveherefd; |
| 6326 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6327 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6328 | |
| 6329 | switch (subtype) { |
| 6330 | case VSASSIGN: |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 6331 | setvar0(varname, startp); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6332 | amount = startp - expdest; |
| 6333 | STADJUST(amount, expdest); |
| 6334 | return startp; |
| 6335 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6336 | case VSQUESTION: |
| 6337 | varunset(p, varname, startp, varflags); |
| 6338 | /* NOTREACHED */ |
| 6339 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6340 | #if ENABLE_ASH_BASH_COMPAT |
| 6341 | case VSSUBSTR: |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 6342 | //TODO: support more general format ${v:EXPR:EXPR}, |
| 6343 | // where EXPR follows $(()) rules |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6344 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6345 | /* Read POS in ${var:POS:LEN} */ |
| 6346 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6347 | len = str - startp - 1; |
| 6348 | |
| 6349 | /* *loc != '\0', guaranteed by parser */ |
| 6350 | if (quotes) { |
| 6351 | char *ptr; |
| 6352 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6353 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6354 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6355 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6356 | len--; |
| 6357 | ptr++; |
| 6358 | } |
| 6359 | } |
| 6360 | } |
| 6361 | orig_len = len; |
| 6362 | |
| 6363 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6364 | /* ${var::LEN} */ |
| 6365 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6366 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6367 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6368 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6369 | while (*loc && *loc != ':') { |
| 6370 | /* TODO? |
| 6371 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6372 | if (!isdigit(*loc)) |
| 6373 | ash_msg_and_raise_error(msg_illnum, str); |
| 6374 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6375 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6376 | } |
| 6377 | if (*loc++ == ':') { |
| 6378 | len = number(loc); |
| 6379 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6380 | } |
Denys Vlasenko | 08a5dab | 2014-11-17 20:27:18 +0100 | [diff] [blame] | 6381 | if (pos < 0) { |
| 6382 | /* ${VAR:$((-n)):l} starts n chars from the end */ |
| 6383 | pos = orig_len + pos; |
| 6384 | } |
| 6385 | if ((unsigned)pos >= orig_len) { |
| 6386 | /* apart from obvious ${VAR:999999:l}, |
| 6387 | * covers ${VAR:$((-9999999)):l} - result is "" |
| 6388 | * (bash-compat) |
| 6389 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6390 | pos = 0; |
| 6391 | len = 0; |
| 6392 | } |
| 6393 | if (len > (orig_len - pos)) |
| 6394 | len = orig_len - pos; |
| 6395 | |
| 6396 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6397 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6398 | str++; |
| 6399 | } |
| 6400 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6401 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6402 | *loc++ = *str++; |
| 6403 | *loc++ = *str++; |
| 6404 | } |
| 6405 | *loc = '\0'; |
| 6406 | amount = loc - expdest; |
| 6407 | STADJUST(amount, expdest); |
| 6408 | return loc; |
| 6409 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6410 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6411 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6412 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6413 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6414 | /* We'll comeback here if we grow the stack while handling |
| 6415 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6416 | * stack will need rebasing, and we'll need to remove our work |
| 6417 | * areas each time |
| 6418 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6419 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6420 | |
| 6421 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6422 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6423 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6424 | |
| 6425 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6426 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6427 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6428 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6429 | if (rmesc != startp) { |
| 6430 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6431 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6432 | } |
| 6433 | } |
| 6434 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6435 | str = (char *)stackblock() + strloc; |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6436 | /* |
| 6437 | * Example: v='a\bc'; echo ${v/\\b/_\\_\z_} |
| 6438 | * The result is a_\_z_c (not a\_\_z_c)! |
| 6439 | * |
| 6440 | * The search pattern and replace string treat backslashes differently! |
| 6441 | * RMESCAPE_SLASH causes preglob to work differently on the pattern |
| 6442 | * and string. It's only used on the first call. |
| 6443 | */ |
| 6444 | preglob(str, IF_ASH_BASH_COMPAT( |
| 6445 | (subtype == VSREPLACE || subtype == VSREPLACEALL) && !repl ? |
| 6446 | RMESCAPE_SLASH :) 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6447 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6448 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 6449 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6450 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6451 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6452 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6453 | if (!repl) { |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 6454 | repl = strchr(str, CTLESC); |
| 6455 | if (repl) |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6456 | *repl++ = '\0'; |
| 6457 | else |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6458 | repl = nullstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6459 | } |
Ron Yorston | 417622c | 2015-05-18 09:59:14 +0200 | [diff] [blame] | 6460 | //bb_error_msg("str:'%s' repl:'%s'", str, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6461 | |
| 6462 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6463 | if (str[0] == '\0') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6464 | return NULL; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6465 | |
| 6466 | len = 0; |
| 6467 | idx = startp; |
| 6468 | end = str - 1; |
| 6469 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6470 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6471 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6472 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6473 | if (!loc) { |
| 6474 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6475 | char *restart_detect = stackblock(); |
| 6476 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6477 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6478 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6479 | idx++; |
| 6480 | len++; |
| 6481 | STPUTC(*idx, expdest); |
| 6482 | } |
| 6483 | if (stackblock() != restart_detect) |
| 6484 | goto restart; |
| 6485 | idx++; |
| 6486 | len++; |
| 6487 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6488 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6489 | if (idx >= end) |
| 6490 | break; |
| 6491 | if (str[0] == '*') { |
| 6492 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6493 | * it would never match "ong_string" etc, no point in trying. |
| 6494 | */ |
| 6495 | goto skip_matching; |
| 6496 | } |
| 6497 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
| 6500 | if (subtype == VSREPLACEALL) { |
| 6501 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6502 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6503 | idx++; |
| 6504 | idx++; |
| 6505 | rmesc++; |
| 6506 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6507 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6508 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6509 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6510 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6511 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6512 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6513 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6514 | if (quotes && *loc == '\\') { |
| 6515 | STPUTC(CTLESC, expdest); |
| 6516 | len++; |
| 6517 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6518 | STPUTC(*loc, expdest); |
| 6519 | if (stackblock() != restart_detect) |
| 6520 | goto restart; |
| 6521 | len++; |
| 6522 | } |
| 6523 | |
| 6524 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6525 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6526 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6527 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6528 | STPUTC(*idx, expdest); |
| 6529 | if (stackblock() != restart_detect) |
| 6530 | goto restart; |
| 6531 | len++; |
| 6532 | idx++; |
| 6533 | } |
| 6534 | break; |
| 6535 | } |
| 6536 | } |
| 6537 | |
| 6538 | /* We've put the replaced text into a buffer at workloc, now |
| 6539 | * move it to the right place and adjust the stack. |
| 6540 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6541 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6542 | startp = (char *)stackblock() + startloc; |
| 6543 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6544 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6545 | amount = expdest - (startp + len); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6546 | STADJUST(-amount, expdest); |
| 6547 | return startp; |
| 6548 | } |
| 6549 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6550 | |
| 6551 | subtype -= VSTRIMRIGHT; |
| 6552 | #if DEBUG |
| 6553 | if (subtype < 0 || subtype > 7) |
| 6554 | abort(); |
| 6555 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6556 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6557 | zero = subtype >> 1; |
| 6558 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6559 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6560 | |
| 6561 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6562 | if (loc) { |
| 6563 | if (zero) { |
| 6564 | memmove(startp, loc, str - loc); |
| 6565 | loc = startp + (str - loc) - 1; |
| 6566 | } |
| 6567 | *loc = '\0'; |
| 6568 | amount = loc - expdest; |
| 6569 | STADJUST(amount, expdest); |
| 6570 | } |
| 6571 | return loc; |
| 6572 | } |
| 6573 | |
| 6574 | /* |
| 6575 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6576 | * name parameter (examples): |
| 6577 | * ash -c 'echo $1' name:'1=' |
| 6578 | * ash -c 'echo $qwe' name:'qwe=' |
| 6579 | * ash -c 'echo $$' name:'$=' |
| 6580 | * ash -c 'echo ${$}' name:'$=' |
| 6581 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6582 | * ash -c 'echo ${#$}' name:'$=' |
| 6583 | * note: examples with bad shell syntax: |
| 6584 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6585 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6586 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6587 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6588 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6589 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6590 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6591 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6592 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6593 | ssize_t len = 0; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6594 | int sep; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6595 | int quoted = flags & EXP_QUOTED; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6596 | int subtype = varflags & VSTYPE; |
| 6597 | int discard = subtype == VSPLUS || subtype == VSLENGTH; |
| 6598 | int quotes = (discard ? 0 : (flags & QUOTES_ESC)) | QUOTES_KEEPNUL; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6599 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6600 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6601 | sep = quoted ? ((flags & EXP_FULL) << CHAR_BIT) : 0; |
| 6602 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6603 | switch (*name) { |
| 6604 | case '$': |
| 6605 | num = rootpid; |
| 6606 | goto numvar; |
| 6607 | case '?': |
| 6608 | num = exitstatus; |
| 6609 | goto numvar; |
| 6610 | case '#': |
| 6611 | num = shellparam.nparam; |
| 6612 | goto numvar; |
| 6613 | case '!': |
| 6614 | num = backgndpid; |
| 6615 | if (num == 0) |
| 6616 | return -1; |
| 6617 | numvar: |
| 6618 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6619 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6620 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6621 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6622 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6623 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6624 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6625 | len++; |
| 6626 | } |
| 6627 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6628 | check_1char_name: |
| 6629 | #if 0 |
| 6630 | /* handles cases similar to ${#$1} */ |
| 6631 | if (name[2] != '\0') |
| 6632 | raise_error_syntax("bad substitution"); |
| 6633 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6634 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6635 | case '@': { |
| 6636 | char **ap; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6637 | char sepc; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6638 | |
| 6639 | if (quoted && (flags & EXP_FULL)) { |
| 6640 | /* note: this is not meant as PEOF value */ |
| 6641 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6642 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6643 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6644 | /* fall through */ |
| 6645 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6646 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6647 | param: |
| 6648 | ap = shellparam.p; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6649 | sepc = sep; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6650 | if (!ap) |
| 6651 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6652 | while ((p = *ap++) != NULL) { |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6653 | len += strtodest(p, syntax, quotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6654 | |
| 6655 | if (*ap && sep) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6656 | len++; |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6657 | memtodest(&sepc, 1, syntax, quotes); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6658 | } |
| 6659 | } |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6660 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6661 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6662 | case '0': |
| 6663 | case '1': |
| 6664 | case '2': |
| 6665 | case '3': |
| 6666 | case '4': |
| 6667 | case '5': |
| 6668 | case '6': |
| 6669 | case '7': |
| 6670 | case '8': |
| 6671 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6672 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6673 | if (num < 0 || num > shellparam.nparam) |
| 6674 | return -1; |
| 6675 | p = num ? shellparam.p[num - 1] : arg0; |
| 6676 | goto value; |
| 6677 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6678 | /* NB: name has form "VAR=..." */ |
| 6679 | |
| 6680 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6681 | * which should be considered before we check variables. */ |
| 6682 | if (var_str_list) { |
| 6683 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6684 | p = NULL; |
| 6685 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6686 | char *str, *eq; |
| 6687 | str = var_str_list->text; |
| 6688 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6689 | if (!eq) /* stop at first non-assignment */ |
| 6690 | break; |
| 6691 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6692 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6693 | && strncmp(str, name, name_len) == 0 |
| 6694 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6695 | p = eq; |
| 6696 | /* goto value; - WRONG! */ |
| 6697 | /* think "A=1 A=2 B=$A" */ |
| 6698 | } |
| 6699 | var_str_list = var_str_list->next; |
| 6700 | } while (var_str_list); |
| 6701 | if (p) |
| 6702 | goto value; |
| 6703 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6704 | p = lookupvar(name); |
| 6705 | value: |
| 6706 | if (!p) |
| 6707 | return -1; |
| 6708 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6709 | len = strtodest(p, syntax, quotes); |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 6710 | #if ENABLE_UNICODE_SUPPORT |
| 6711 | if (subtype == VSLENGTH && len > 0) { |
| 6712 | reinit_unicode_for_ash(); |
| 6713 | if (unicode_status == UNICODE_ON) { |
Ron Yorston | 3e3bfb8 | 2016-03-18 11:29:19 +0000 | [diff] [blame] | 6714 | STADJUST(-len, expdest); |
| 6715 | discard = 0; |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 6716 | len = unicode_strlen(p); |
| 6717 | } |
| 6718 | } |
| 6719 | #endif |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6720 | break; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
Ron Yorston | d68d1fb | 2015-05-18 09:49:28 +0200 | [diff] [blame] | 6723 | if (discard) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6724 | STADJUST(-len, expdest); |
| 6725 | return len; |
| 6726 | } |
| 6727 | |
| 6728 | /* |
| 6729 | * Expand a variable, and return a pointer to the next character in the |
| 6730 | * input string. |
| 6731 | */ |
| 6732 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6733 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6734 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6735 | char varflags; |
| 6736 | char subtype; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6737 | int quoted; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6738 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6739 | char *var; |
| 6740 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6741 | int startloc; |
| 6742 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6743 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6744 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6745 | subtype = varflags & VSTYPE; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6746 | quoted = flags & EXP_QUOTED; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6747 | var = p; |
| 6748 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6749 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 6750 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6751 | |
| 6752 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6753 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6754 | if (varflags & VSNUL) |
| 6755 | varlen--; |
| 6756 | |
| 6757 | if (subtype == VSPLUS) { |
| 6758 | varlen = -1 - varlen; |
| 6759 | goto vsplus; |
| 6760 | } |
| 6761 | |
| 6762 | if (subtype == VSMINUS) { |
| 6763 | vsplus: |
| 6764 | if (varlen < 0) { |
| 6765 | argstr( |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6766 | p, |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6767 | flags | EXP_TILDE | EXP_WORD, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6768 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6769 | ); |
| 6770 | goto end; |
| 6771 | } |
| 6772 | if (easy) |
| 6773 | goto record; |
| 6774 | goto end; |
| 6775 | } |
| 6776 | |
| 6777 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6778 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6779 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6780 | subtype, startloc, varflags, |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6781 | /* quotes: */ flags & ~QUOTES_ESC, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6782 | var_str_list) |
| 6783 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6784 | varflags &= ~VSNUL; |
| 6785 | /* |
| 6786 | * Remove any recorded regions beyond |
| 6787 | * start of variable |
| 6788 | */ |
| 6789 | removerecordregions(startloc); |
| 6790 | goto again; |
| 6791 | } |
| 6792 | goto end; |
| 6793 | } |
| 6794 | if (easy) |
| 6795 | goto record; |
| 6796 | goto end; |
| 6797 | } |
| 6798 | |
| 6799 | if (varlen < 0 && uflag) |
| 6800 | varunset(p, var, 0, 0); |
| 6801 | |
| 6802 | if (subtype == VSLENGTH) { |
Denys Vlasenko | c76236f | 2014-12-29 00:04:18 +0100 | [diff] [blame] | 6803 | cvtnum(varlen > 0 ? varlen : 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6804 | goto record; |
| 6805 | } |
| 6806 | |
| 6807 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6808 | if (easy) |
| 6809 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6810 | goto end; |
| 6811 | } |
| 6812 | |
| 6813 | #if DEBUG |
| 6814 | switch (subtype) { |
| 6815 | case VSTRIMLEFT: |
| 6816 | case VSTRIMLEFTMAX: |
| 6817 | case VSTRIMRIGHT: |
| 6818 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6819 | #if ENABLE_ASH_BASH_COMPAT |
| 6820 | case VSSUBSTR: |
| 6821 | case VSREPLACE: |
| 6822 | case VSREPLACEALL: |
| 6823 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6824 | break; |
| 6825 | default: |
| 6826 | abort(); |
| 6827 | } |
| 6828 | #endif |
| 6829 | |
| 6830 | if (varlen >= 0) { |
| 6831 | /* |
| 6832 | * Terminate the string and start recording the pattern |
| 6833 | * right after it |
| 6834 | */ |
| 6835 | STPUTC('\0', expdest); |
| 6836 | patloc = expdest - (char *)stackblock(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6837 | if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype, |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6838 | startloc, varflags, flags, var_str_list)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6839 | int amount = expdest - ( |
| 6840 | (char *)stackblock() + patloc - 1 |
| 6841 | ); |
| 6842 | STADJUST(-amount, expdest); |
| 6843 | } |
| 6844 | /* Remove any recorded regions beyond start of variable */ |
| 6845 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6846 | record: |
| 6847 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6848 | } |
| 6849 | |
| 6850 | end: |
| 6851 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6852 | int nesting = 1; |
| 6853 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6854 | unsigned char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6855 | if (c == CTLESC) |
| 6856 | p++; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 6857 | else if (c == CTLBACKQ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6858 | if (varlen >= 0) |
| 6859 | argbackq = argbackq->next; |
| 6860 | } else if (c == CTLVAR) { |
| 6861 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6862 | nesting++; |
| 6863 | } else if (c == CTLENDVAR) { |
| 6864 | if (--nesting == 0) |
| 6865 | break; |
| 6866 | } |
| 6867 | } |
| 6868 | } |
| 6869 | return p; |
| 6870 | } |
| 6871 | |
| 6872 | /* |
| 6873 | * Break the argument string into pieces based upon IFS and add the |
| 6874 | * strings to the argument list. The regions of the string to be |
| 6875 | * searched for IFS characters have been stored by recordregion. |
| 6876 | */ |
| 6877 | static void |
| 6878 | ifsbreakup(char *string, struct arglist *arglist) |
| 6879 | { |
| 6880 | struct ifsregion *ifsp; |
| 6881 | struct strlist *sp; |
| 6882 | char *start; |
| 6883 | char *p; |
| 6884 | char *q; |
| 6885 | const char *ifs, *realifs; |
| 6886 | int ifsspc; |
| 6887 | int nulonly; |
| 6888 | |
| 6889 | start = string; |
| 6890 | if (ifslastp != NULL) { |
| 6891 | ifsspc = 0; |
| 6892 | nulonly = 0; |
| 6893 | realifs = ifsset() ? ifsval() : defifs; |
| 6894 | ifsp = &ifsfirst; |
| 6895 | do { |
| 6896 | p = string + ifsp->begoff; |
| 6897 | nulonly = ifsp->nulonly; |
| 6898 | ifs = nulonly ? nullstr : realifs; |
| 6899 | ifsspc = 0; |
| 6900 | while (p < string + ifsp->endoff) { |
| 6901 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6902 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6903 | p++; |
| 6904 | if (!strchr(ifs, *p)) { |
| 6905 | p++; |
| 6906 | continue; |
| 6907 | } |
| 6908 | if (!nulonly) |
| 6909 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6910 | /* Ignore IFS whitespace at start */ |
| 6911 | if (q == start && ifsspc) { |
| 6912 | p++; |
| 6913 | start = p; |
| 6914 | continue; |
| 6915 | } |
| 6916 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6917 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6918 | sp->text = start; |
| 6919 | *arglist->lastp = sp; |
| 6920 | arglist->lastp = &sp->next; |
| 6921 | p++; |
| 6922 | if (!nulonly) { |
| 6923 | for (;;) { |
| 6924 | if (p >= string + ifsp->endoff) { |
| 6925 | break; |
| 6926 | } |
| 6927 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6928 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6929 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6930 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6931 | p = q; |
| 6932 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6933 | } |
| 6934 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6935 | if (ifsspc) { |
| 6936 | p++; |
| 6937 | ifsspc = 0; |
| 6938 | } else { |
| 6939 | p = q; |
| 6940 | break; |
| 6941 | } |
| 6942 | } else |
| 6943 | p++; |
| 6944 | } |
| 6945 | } |
| 6946 | start = p; |
| 6947 | } /* while */ |
| 6948 | ifsp = ifsp->next; |
| 6949 | } while (ifsp != NULL); |
| 6950 | if (nulonly) |
| 6951 | goto add; |
| 6952 | } |
| 6953 | |
| 6954 | if (!*start) |
| 6955 | return; |
| 6956 | |
| 6957 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6958 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6959 | sp->text = start; |
| 6960 | *arglist->lastp = sp; |
| 6961 | arglist->lastp = &sp->next; |
| 6962 | } |
| 6963 | |
| 6964 | static void |
| 6965 | ifsfree(void) |
| 6966 | { |
| 6967 | struct ifsregion *p; |
| 6968 | |
| 6969 | INT_OFF; |
| 6970 | p = ifsfirst.next; |
| 6971 | do { |
| 6972 | struct ifsregion *ifsp; |
| 6973 | ifsp = p->next; |
| 6974 | free(p); |
| 6975 | p = ifsp; |
| 6976 | } while (p); |
| 6977 | ifslastp = NULL; |
| 6978 | ifsfirst.next = NULL; |
| 6979 | INT_ON; |
| 6980 | } |
| 6981 | |
| 6982 | /* |
| 6983 | * Add a file name to the list. |
| 6984 | */ |
| 6985 | static void |
| 6986 | addfname(const char *name) |
| 6987 | { |
| 6988 | struct strlist *sp; |
| 6989 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6990 | sp = stzalloc(sizeof(*sp)); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 6991 | sp->text = sstrdup(name); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6992 | *exparg.lastp = sp; |
| 6993 | exparg.lastp = &sp->next; |
| 6994 | } |
| 6995 | |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 6996 | /* If we want to use glob() from libc... */ |
| 6997 | #if 1 |
| 6998 | |
| 6999 | /* Add the result of glob() to the list */ |
| 7000 | static void |
| 7001 | addglob(const glob_t *pglob) |
| 7002 | { |
| 7003 | char **p = pglob->gl_pathv; |
| 7004 | |
| 7005 | do { |
| 7006 | addfname(*p); |
| 7007 | } while (*++p); |
| 7008 | } |
| 7009 | static void |
| 7010 | expandmeta(struct strlist *str /*, int flag*/) |
| 7011 | { |
| 7012 | /* TODO - EXP_REDIR */ |
| 7013 | |
| 7014 | while (str) { |
| 7015 | char *p; |
| 7016 | glob_t pglob; |
| 7017 | int i; |
| 7018 | |
| 7019 | if (fflag) |
| 7020 | goto nometa; |
| 7021 | INT_OFF; |
| 7022 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7023 | /* |
| 7024 | * GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match |
| 7025 | * TODO?: GLOB_NOCHECK: if no match, return unchanged pattern (sans \* escapes?) |
| 7026 | */ |
| 7027 | i = glob(p, GLOB_NOMAGIC, NULL, &pglob); |
| 7028 | if (p != str->text) |
| 7029 | free(p); |
| 7030 | switch (i) { |
| 7031 | case 0: |
| 7032 | /* GLOB_MAGCHAR is set if *?[ chars were seen (GNU) */ |
| 7033 | if (!(pglob.gl_flags & GLOB_MAGCHAR)) |
| 7034 | goto nometa2; |
| 7035 | addglob(&pglob); |
| 7036 | globfree(&pglob); |
| 7037 | INT_ON; |
| 7038 | break; |
| 7039 | case GLOB_NOMATCH: |
| 7040 | nometa2: |
| 7041 | globfree(&pglob); |
| 7042 | INT_ON; |
| 7043 | nometa: |
| 7044 | *exparg.lastp = str; |
| 7045 | rmescapes(str->text, 0); |
| 7046 | exparg.lastp = &str->next; |
| 7047 | break; |
| 7048 | default: /* GLOB_NOSPACE */ |
| 7049 | globfree(&pglob); |
| 7050 | INT_ON; |
| 7051 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 7052 | } |
| 7053 | str = str->next; |
| 7054 | } |
| 7055 | } |
| 7056 | |
| 7057 | #else |
| 7058 | /* Homegrown globbing code. (dash also has both, uses homegrown one.) */ |
| 7059 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7060 | /* |
| 7061 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 7062 | */ |
| 7063 | static void |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7064 | expmeta(char *expdir, char *enddir, char *name) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7065 | { |
| 7066 | char *p; |
| 7067 | const char *cp; |
| 7068 | char *start; |
| 7069 | char *endname; |
| 7070 | int metaflag; |
| 7071 | struct stat statb; |
| 7072 | DIR *dirp; |
| 7073 | struct dirent *dp; |
| 7074 | int atend; |
| 7075 | int matchdot; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7076 | int esc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7077 | |
| 7078 | metaflag = 0; |
| 7079 | start = name; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7080 | for (p = name; esc = 0, *p; p += esc + 1) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7081 | if (*p == '*' || *p == '?') |
| 7082 | metaflag = 1; |
| 7083 | else if (*p == '[') { |
| 7084 | char *q = p + 1; |
| 7085 | if (*q == '!') |
| 7086 | q++; |
| 7087 | for (;;) { |
| 7088 | if (*q == '\\') |
| 7089 | q++; |
| 7090 | if (*q == '/' || *q == '\0') |
| 7091 | break; |
| 7092 | if (*++q == ']') { |
| 7093 | metaflag = 1; |
| 7094 | break; |
| 7095 | } |
| 7096 | } |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7097 | } else { |
| 7098 | if (*p == '\\') |
| 7099 | esc++; |
| 7100 | if (p[esc] == '/') { |
| 7101 | if (metaflag) |
| 7102 | break; |
| 7103 | start = p + esc + 1; |
| 7104 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7105 | } |
| 7106 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7107 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 7108 | if (enddir != expdir) |
| 7109 | metaflag++; |
| 7110 | p = name; |
| 7111 | do { |
| 7112 | if (*p == '\\') |
| 7113 | p++; |
| 7114 | *enddir++ = *p; |
| 7115 | } while (*p++); |
| 7116 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 7117 | addfname(expdir); |
| 7118 | return; |
| 7119 | } |
| 7120 | endname = p; |
| 7121 | if (name < start) { |
| 7122 | p = name; |
| 7123 | do { |
| 7124 | if (*p == '\\') |
| 7125 | p++; |
| 7126 | *enddir++ = *p++; |
| 7127 | } while (p < start); |
| 7128 | } |
| 7129 | if (enddir == expdir) { |
| 7130 | cp = "."; |
| 7131 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 7132 | cp = "/"; |
| 7133 | } else { |
| 7134 | cp = expdir; |
| 7135 | enddir[-1] = '\0'; |
| 7136 | } |
| 7137 | dirp = opendir(cp); |
| 7138 | if (dirp == NULL) |
| 7139 | return; |
| 7140 | if (enddir != expdir) |
| 7141 | enddir[-1] = '/'; |
| 7142 | if (*endname == 0) { |
| 7143 | atend = 1; |
| 7144 | } else { |
| 7145 | atend = 0; |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7146 | *endname = '\0'; |
| 7147 | endname += esc + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7148 | } |
| 7149 | matchdot = 0; |
| 7150 | p = start; |
| 7151 | if (*p == '\\') |
| 7152 | p++; |
| 7153 | if (*p == '.') |
| 7154 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7155 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7156 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7157 | continue; |
| 7158 | if (pmatch(start, dp->d_name)) { |
| 7159 | if (atend) { |
| 7160 | strcpy(enddir, dp->d_name); |
| 7161 | addfname(expdir); |
| 7162 | } else { |
| 7163 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 7164 | continue; |
| 7165 | p[-1] = '/'; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7166 | expmeta(expdir, p, endname); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7167 | } |
| 7168 | } |
| 7169 | } |
| 7170 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7171 | if (!atend) |
Ron Yorston | ca25af9 | 2015-09-04 10:32:41 +0100 | [diff] [blame] | 7172 | endname[-esc - 1] = esc ? '\\' : '/'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7173 | } |
| 7174 | |
| 7175 | static struct strlist * |
| 7176 | msort(struct strlist *list, int len) |
| 7177 | { |
| 7178 | struct strlist *p, *q = NULL; |
| 7179 | struct strlist **lpp; |
| 7180 | int half; |
| 7181 | int n; |
| 7182 | |
| 7183 | if (len <= 1) |
| 7184 | return list; |
| 7185 | half = len >> 1; |
| 7186 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7187 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7188 | q = p; |
| 7189 | p = p->next; |
| 7190 | } |
| 7191 | q->next = NULL; /* terminate first half of list */ |
| 7192 | q = msort(list, half); /* sort first half of list */ |
| 7193 | p = msort(p, len - half); /* sort second half */ |
| 7194 | lpp = &list; |
| 7195 | for (;;) { |
| 7196 | #if ENABLE_LOCALE_SUPPORT |
| 7197 | if (strcoll(p->text, q->text) < 0) |
| 7198 | #else |
| 7199 | if (strcmp(p->text, q->text) < 0) |
| 7200 | #endif |
| 7201 | { |
| 7202 | *lpp = p; |
| 7203 | lpp = &p->next; |
| 7204 | p = *lpp; |
| 7205 | if (p == NULL) { |
| 7206 | *lpp = q; |
| 7207 | break; |
| 7208 | } |
| 7209 | } else { |
| 7210 | *lpp = q; |
| 7211 | lpp = &q->next; |
| 7212 | q = *lpp; |
| 7213 | if (q == NULL) { |
| 7214 | *lpp = p; |
| 7215 | break; |
| 7216 | } |
| 7217 | } |
| 7218 | } |
| 7219 | return list; |
| 7220 | } |
| 7221 | |
| 7222 | /* |
| 7223 | * Sort the results of file name expansion. It calculates the number of |
| 7224 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7225 | * work. |
| 7226 | */ |
| 7227 | static struct strlist * |
| 7228 | expsort(struct strlist *str) |
| 7229 | { |
| 7230 | int len; |
| 7231 | struct strlist *sp; |
| 7232 | |
| 7233 | len = 0; |
| 7234 | for (sp = str; sp; sp = sp->next) |
| 7235 | len++; |
| 7236 | return msort(str, len); |
| 7237 | } |
| 7238 | |
| 7239 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7240 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7241 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7242 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7243 | '*', '?', '[', 0 |
| 7244 | }; |
| 7245 | /* TODO - EXP_REDIR */ |
| 7246 | |
| 7247 | while (str) { |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7248 | char *expdir; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7249 | struct strlist **savelastp; |
| 7250 | struct strlist *sp; |
| 7251 | char *p; |
| 7252 | |
| 7253 | if (fflag) |
| 7254 | goto nometa; |
| 7255 | if (!strpbrk(str->text, metachars)) |
| 7256 | goto nometa; |
| 7257 | savelastp = exparg.lastp; |
| 7258 | |
| 7259 | INT_OFF; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7260 | p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7261 | { |
| 7262 | int i = strlen(str->text); |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7263 | //BUGGY estimation of how long expanded name can be |
| 7264 | expdir = ckmalloc(i < 2048 ? 2048 : i+1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7265 | } |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7266 | expmeta(expdir, expdir, p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7267 | free(expdir); |
| 7268 | if (p != str->text) |
| 7269 | free(p); |
| 7270 | INT_ON; |
| 7271 | if (exparg.lastp == savelastp) { |
| 7272 | /* |
| 7273 | * no matches |
| 7274 | */ |
| 7275 | nometa: |
| 7276 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7277 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7278 | exparg.lastp = &str->next; |
| 7279 | } else { |
| 7280 | *exparg.lastp = NULL; |
| 7281 | *savelastp = sp = expsort(*savelastp); |
| 7282 | while (sp->next != NULL) |
| 7283 | sp = sp->next; |
| 7284 | exparg.lastp = &sp->next; |
| 7285 | } |
| 7286 | str = str->next; |
| 7287 | } |
| 7288 | } |
Denys Vlasenko | b3f29b4 | 2016-09-21 16:25:58 +0200 | [diff] [blame] | 7289 | #endif /* our globbing code */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7290 | |
| 7291 | /* |
| 7292 | * Perform variable substitution and command substitution on an argument, |
| 7293 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7294 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7295 | * here document expansion. |
| 7296 | */ |
| 7297 | static void |
| 7298 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7299 | { |
| 7300 | struct strlist *sp; |
| 7301 | char *p; |
| 7302 | |
| 7303 | argbackq = arg->narg.backquote; |
| 7304 | STARTSTACKSTR(expdest); |
| 7305 | ifsfirst.next = NULL; |
| 7306 | ifslastp = NULL; |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7307 | TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag)); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7308 | argstr(arg->narg.text, flag, |
| 7309 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7310 | p = _STPUTC('\0', expdest); |
| 7311 | expdest = p - 1; |
| 7312 | if (arglist == NULL) { |
| 7313 | return; /* here document expanded */ |
| 7314 | } |
| 7315 | p = grabstackstr(p); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7316 | TRACE(("expandarg: p:'%s'\n", p)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7317 | exparg.lastp = &exparg.list; |
| 7318 | /* |
| 7319 | * TODO - EXP_REDIR |
| 7320 | */ |
| 7321 | if (flag & EXP_FULL) { |
| 7322 | ifsbreakup(p, &exparg); |
| 7323 | *exparg.lastp = NULL; |
| 7324 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7325 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7326 | } else { |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7327 | if (flag & EXP_REDIR) { /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7328 | rmescapes(p, 0); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7329 | TRACE(("expandarg: rmescapes:'%s'\n", p)); |
| 7330 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7331 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7332 | sp->text = p; |
| 7333 | *exparg.lastp = sp; |
| 7334 | exparg.lastp = &sp->next; |
| 7335 | } |
| 7336 | if (ifsfirst.next) |
| 7337 | ifsfree(); |
| 7338 | *exparg.lastp = NULL; |
| 7339 | if (exparg.list) { |
| 7340 | *arglist->lastp = exparg.list; |
| 7341 | arglist->lastp = exparg.lastp; |
| 7342 | } |
| 7343 | } |
| 7344 | |
| 7345 | /* |
| 7346 | * Expand shell variables and backquotes inside a here document. |
| 7347 | */ |
| 7348 | static void |
| 7349 | expandhere(union node *arg, int fd) |
| 7350 | { |
| 7351 | herefd = fd; |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7352 | expandarg(arg, (struct arglist *)NULL, EXP_QUOTED); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7353 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7354 | } |
| 7355 | |
| 7356 | /* |
| 7357 | * Returns true if the pattern matches the string. |
| 7358 | */ |
| 7359 | static int |
| 7360 | patmatch(char *pattern, const char *string) |
| 7361 | { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 7362 | return pmatch(preglob(pattern, 0), string); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| 7365 | /* |
| 7366 | * See if a pattern matches in a case statement. |
| 7367 | */ |
| 7368 | static int |
| 7369 | casematch(union node *pattern, char *val) |
| 7370 | { |
| 7371 | struct stackmark smark; |
| 7372 | int result; |
| 7373 | |
| 7374 | setstackmark(&smark); |
| 7375 | argbackq = pattern->narg.backquote; |
| 7376 | STARTSTACKSTR(expdest); |
| 7377 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7378 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7379 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7380 | STACKSTRNUL(expdest); |
| 7381 | result = patmatch(stackblock(), val); |
| 7382 | popstackmark(&smark); |
| 7383 | return result; |
| 7384 | } |
| 7385 | |
| 7386 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7387 | /* ============ find_command */ |
| 7388 | |
| 7389 | struct builtincmd { |
| 7390 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7391 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7392 | /* unsigned flags; */ |
| 7393 | }; |
| 7394 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7395 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7396 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7397 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7398 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7399 | |
| 7400 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7401 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7402 | union param { |
| 7403 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7404 | /* index >= 0 for commands without path (slashes) */ |
| 7405 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7406 | /* index == -1 for commands with slashes */ |
| 7407 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7408 | const struct builtincmd *cmd; |
| 7409 | struct funcnode *func; |
| 7410 | } u; |
| 7411 | }; |
| 7412 | /* values of cmdtype */ |
| 7413 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7414 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7415 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7416 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7417 | |
| 7418 | /* action to find_command() */ |
| 7419 | #define DO_ERR 0x01 /* prints errors */ |
| 7420 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7421 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7422 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7423 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7424 | |
| 7425 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7426 | |
| 7427 | |
| 7428 | /* ============ Hashing commands */ |
| 7429 | |
| 7430 | /* |
| 7431 | * When commands are first encountered, they are entered in a hash table. |
| 7432 | * This ensures that a full path search will not have to be done for them |
| 7433 | * on each invocation. |
| 7434 | * |
| 7435 | * We should investigate converting to a linear search, even though that |
| 7436 | * would make the command name "hash" a misnomer. |
| 7437 | */ |
| 7438 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7439 | struct tblentry { |
| 7440 | struct tblentry *next; /* next entry in hash chain */ |
| 7441 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7442 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7443 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7444 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7445 | }; |
| 7446 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7447 | static struct tblentry **cmdtable; |
| 7448 | #define INIT_G_cmdtable() do { \ |
| 7449 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7450 | } while (0) |
| 7451 | |
| 7452 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7453 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7454 | |
| 7455 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7456 | tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **envp) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7457 | { |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7458 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7459 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7460 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 7461 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7462 | while (*envp) |
| 7463 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7464 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7465 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7466 | /* re-exec ourselves with the new arguments */ |
| 7467 | execve(bb_busybox_exec_path, argv, envp); |
| 7468 | /* If they called chroot or otherwise made the binary no longer |
| 7469 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7470 | } |
| 7471 | #endif |
| 7472 | |
| 7473 | repeat: |
| 7474 | #ifdef SYSV |
| 7475 | do { |
| 7476 | execve(cmd, argv, envp); |
| 7477 | } while (errno == EINTR); |
| 7478 | #else |
| 7479 | execve(cmd, argv, envp); |
| 7480 | #endif |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7481 | if (cmd == (char*) bb_busybox_exec_path) { |
| 7482 | /* We already visited ENOEXEC branch below, don't do it again */ |
| 7483 | //TODO: try execve(initial_argv0_of_shell, argv, envp) before giving up? |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7484 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7485 | return; |
| 7486 | } |
| 7487 | if (errno == ENOEXEC) { |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7488 | /* Run "cmd" as a shell script: |
| 7489 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
| 7490 | * "If the execve() function fails with ENOEXEC, the shell |
| 7491 | * shall execute a command equivalent to having a shell invoked |
| 7492 | * with the command name as its first operand, |
| 7493 | * with any remaining arguments passed to the new shell" |
| 7494 | * |
| 7495 | * That is, do not use $SHELL, user's shell, or /bin/sh; |
| 7496 | * just call ourselves. |
Denys Vlasenko | 2bef526 | 2011-12-16 00:25:17 +0100 | [diff] [blame] | 7497 | * |
| 7498 | * Note that bash reads ~80 chars of the file, and if it sees |
| 7499 | * a zero byte before it sees newline, it doesn't try to |
| 7500 | * interpret it, but fails with "cannot execute binary file" |
Denys Vlasenko | cda6ea9 | 2011-12-16 00:44:36 +0100 | [diff] [blame] | 7501 | * message and exit code 126. For one, this prevents attempts |
| 7502 | * to interpret foreign ELF binaries as shell scripts. |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7503 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7504 | char **ap; |
| 7505 | char **new; |
| 7506 | |
| 7507 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7508 | continue; |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7509 | new = ckmalloc((ap - argv + 2) * sizeof(new[0])); |
| 7510 | new[0] = (char*) "ash"; |
| 7511 | new[1] = cmd; |
| 7512 | ap = new + 2; |
| 7513 | while ((*ap++ = *++argv) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7514 | continue; |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7515 | cmd = (char*) bb_busybox_exec_path; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7516 | argv = new; |
| 7517 | goto repeat; |
| 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | /* |
| 7522 | * Exec a program. Never returns. If you change this routine, you may |
| 7523 | * have to change the find_command routine as well. |
| 7524 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7525 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7526 | static void |
| 7527 | shellexec(char **argv, const char *path, int idx) |
| 7528 | { |
| 7529 | char *cmdname; |
| 7530 | int e; |
| 7531 | char **envp; |
| 7532 | int exerrno; |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 7533 | int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7534 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7535 | clearredir(/*drop:*/ 1); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 7536 | envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7537 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7538 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7539 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7540 | #endif |
| 7541 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7542 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 7543 | if (applet_no >= 0) { |
| 7544 | /* We tried execing ourself, but it didn't work. |
| 7545 | * Maybe /proc/self/exe doesn't exist? |
| 7546 | * Try $PATH search. |
| 7547 | */ |
| 7548 | goto try_PATH; |
| 7549 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7550 | e = errno; |
| 7551 | } else { |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 7552 | try_PATH: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7553 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7554 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7555 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7556 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7557 | if (errno != ENOENT && errno != ENOTDIR) |
| 7558 | e = errno; |
| 7559 | } |
| 7560 | stunalloc(cmdname); |
| 7561 | } |
| 7562 | } |
| 7563 | |
| 7564 | /* Map to POSIX errors */ |
| 7565 | switch (e) { |
| 7566 | case EACCES: |
| 7567 | exerrno = 126; |
| 7568 | break; |
| 7569 | case ENOENT: |
| 7570 | exerrno = 127; |
| 7571 | break; |
| 7572 | default: |
| 7573 | exerrno = 2; |
| 7574 | break; |
| 7575 | } |
| 7576 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7577 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7578 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7579 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7580 | /* NOTREACHED */ |
| 7581 | } |
| 7582 | |
| 7583 | static void |
| 7584 | printentry(struct tblentry *cmdp) |
| 7585 | { |
| 7586 | int idx; |
| 7587 | const char *path; |
| 7588 | char *name; |
| 7589 | |
| 7590 | idx = cmdp->param.index; |
| 7591 | path = pathval(); |
| 7592 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7593 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7594 | stunalloc(name); |
| 7595 | } while (--idx >= 0); |
| 7596 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7597 | } |
| 7598 | |
| 7599 | /* |
| 7600 | * Clear out command entries. The argument specifies the first entry in |
| 7601 | * PATH which has changed. |
| 7602 | */ |
| 7603 | static void |
| 7604 | clearcmdentry(int firstchange) |
| 7605 | { |
| 7606 | struct tblentry **tblp; |
| 7607 | struct tblentry **pp; |
| 7608 | struct tblentry *cmdp; |
| 7609 | |
| 7610 | INT_OFF; |
| 7611 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7612 | pp = tblp; |
| 7613 | while ((cmdp = *pp) != NULL) { |
| 7614 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7615 | cmdp->param.index >= firstchange) |
| 7616 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7617 | builtinloc >= firstchange) |
| 7618 | ) { |
| 7619 | *pp = cmdp->next; |
| 7620 | free(cmdp); |
| 7621 | } else { |
| 7622 | pp = &cmdp->next; |
| 7623 | } |
| 7624 | } |
| 7625 | } |
| 7626 | INT_ON; |
| 7627 | } |
| 7628 | |
| 7629 | /* |
| 7630 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7631 | * add the command to the table if it is not already present. The |
| 7632 | * variable "lastcmdentry" is set to point to the address of the link |
| 7633 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7634 | * entry. |
| 7635 | * |
| 7636 | * Interrupts must be off if called with add != 0. |
| 7637 | */ |
| 7638 | static struct tblentry **lastcmdentry; |
| 7639 | |
| 7640 | static struct tblentry * |
| 7641 | cmdlookup(const char *name, int add) |
| 7642 | { |
| 7643 | unsigned int hashval; |
| 7644 | const char *p; |
| 7645 | struct tblentry *cmdp; |
| 7646 | struct tblentry **pp; |
| 7647 | |
| 7648 | p = name; |
| 7649 | hashval = (unsigned char)*p << 4; |
| 7650 | while (*p) |
| 7651 | hashval += (unsigned char)*p++; |
| 7652 | hashval &= 0x7FFF; |
| 7653 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7654 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7655 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7656 | break; |
| 7657 | pp = &cmdp->next; |
| 7658 | } |
| 7659 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7660 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7661 | + strlen(name) |
| 7662 | /* + 1 - already done because |
| 7663 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7664 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7665 | cmdp->cmdtype = CMDUNKNOWN; |
| 7666 | strcpy(cmdp->cmdname, name); |
| 7667 | } |
| 7668 | lastcmdentry = pp; |
| 7669 | return cmdp; |
| 7670 | } |
| 7671 | |
| 7672 | /* |
| 7673 | * Delete the command entry returned on the last lookup. |
| 7674 | */ |
| 7675 | static void |
| 7676 | delete_cmd_entry(void) |
| 7677 | { |
| 7678 | struct tblentry *cmdp; |
| 7679 | |
| 7680 | INT_OFF; |
| 7681 | cmdp = *lastcmdentry; |
| 7682 | *lastcmdentry = cmdp->next; |
| 7683 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7684 | freefunc(cmdp->param.func); |
| 7685 | free(cmdp); |
| 7686 | INT_ON; |
| 7687 | } |
| 7688 | |
| 7689 | /* |
| 7690 | * Add a new command entry, replacing any existing command entry for |
| 7691 | * the same name - except special builtins. |
| 7692 | */ |
| 7693 | static void |
| 7694 | addcmdentry(char *name, struct cmdentry *entry) |
| 7695 | { |
| 7696 | struct tblentry *cmdp; |
| 7697 | |
| 7698 | cmdp = cmdlookup(name, 1); |
| 7699 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7700 | freefunc(cmdp->param.func); |
| 7701 | } |
| 7702 | cmdp->cmdtype = entry->cmdtype; |
| 7703 | cmdp->param = entry->u; |
| 7704 | cmdp->rehash = 0; |
| 7705 | } |
| 7706 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7707 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7708 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7709 | { |
| 7710 | struct tblentry **pp; |
| 7711 | struct tblentry *cmdp; |
| 7712 | int c; |
| 7713 | struct cmdentry entry; |
| 7714 | char *name; |
| 7715 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7716 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7717 | clearcmdentry(0); |
| 7718 | return 0; |
| 7719 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7720 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7721 | if (*argptr == NULL) { |
| 7722 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7723 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7724 | if (cmdp->cmdtype == CMDNORMAL) |
| 7725 | printentry(cmdp); |
| 7726 | } |
| 7727 | } |
| 7728 | return 0; |
| 7729 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7730 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7731 | c = 0; |
| 7732 | while ((name = *argptr) != NULL) { |
| 7733 | cmdp = cmdlookup(name, 0); |
| 7734 | if (cmdp != NULL |
| 7735 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7736 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7737 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7738 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7739 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7740 | find_command(name, &entry, DO_ERR, pathval()); |
| 7741 | if (entry.cmdtype == CMDUNKNOWN) |
| 7742 | c = 1; |
| 7743 | argptr++; |
| 7744 | } |
| 7745 | return c; |
| 7746 | } |
| 7747 | |
| 7748 | /* |
| 7749 | * Called when a cd is done. Marks all commands so the next time they |
| 7750 | * are executed they will be rehashed. |
| 7751 | */ |
| 7752 | static void |
| 7753 | hashcd(void) |
| 7754 | { |
| 7755 | struct tblentry **pp; |
| 7756 | struct tblentry *cmdp; |
| 7757 | |
| 7758 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7759 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7760 | if (cmdp->cmdtype == CMDNORMAL |
| 7761 | || (cmdp->cmdtype == CMDBUILTIN |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 7762 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7763 | && builtinloc > 0) |
| 7764 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7765 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7766 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7767 | } |
| 7768 | } |
| 7769 | } |
| 7770 | |
| 7771 | /* |
| 7772 | * Fix command hash table when PATH changed. |
| 7773 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7774 | * pathval() still returns the old value at this point. |
| 7775 | * Called with interrupts off. |
| 7776 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7777 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7778 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7779 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7780 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7781 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7782 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7783 | int idx_bltin; |
| 7784 | |
| 7785 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7786 | firstchange = 9999; /* assume no change */ |
| 7787 | idx = 0; |
| 7788 | idx_bltin = -1; |
| 7789 | for (;;) { |
| 7790 | if (*old != *new) { |
| 7791 | firstchange = idx; |
| 7792 | if ((*old == '\0' && *new == ':') |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7793 | || (*old == ':' && *new == '\0') |
| 7794 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7795 | firstchange++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7796 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7797 | old = new; /* ignore subsequent differences */ |
| 7798 | } |
| 7799 | if (*new == '\0') |
| 7800 | break; |
| 7801 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7802 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7803 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7804 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7805 | new++; |
| 7806 | old++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7807 | } |
| 7808 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7809 | builtinloc = idx_bltin; /* zap builtins */ |
| 7810 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7811 | firstchange = 0; |
| 7812 | clearcmdentry(firstchange); |
| 7813 | builtinloc = idx_bltin; |
| 7814 | } |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 7815 | enum { |
| 7816 | TEOF, |
| 7817 | TNL, |
| 7818 | TREDIR, |
| 7819 | TWORD, |
| 7820 | TSEMI, |
| 7821 | TBACKGND, |
| 7822 | TAND, |
| 7823 | TOR, |
| 7824 | TPIPE, |
| 7825 | TLP, |
| 7826 | TRP, |
| 7827 | TENDCASE, |
| 7828 | TENDBQUOTE, |
| 7829 | TNOT, |
| 7830 | TCASE, |
| 7831 | TDO, |
| 7832 | TDONE, |
| 7833 | TELIF, |
| 7834 | TELSE, |
| 7835 | TESAC, |
| 7836 | TFI, |
| 7837 | TFOR, |
| 7838 | #if ENABLE_ASH_BASH_COMPAT |
| 7839 | TFUNCTION, |
| 7840 | #endif |
| 7841 | TIF, |
| 7842 | TIN, |
| 7843 | TTHEN, |
| 7844 | TUNTIL, |
| 7845 | TWHILE, |
| 7846 | TBEGIN, |
| 7847 | TEND |
| 7848 | }; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7849 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7850 | |
| 7851 | /* first char is indicating which tokens mark the end of a list */ |
| 7852 | static const char *const tokname_array[] = { |
| 7853 | "\1end of file", |
| 7854 | "\0newline", |
| 7855 | "\0redirection", |
| 7856 | "\0word", |
| 7857 | "\0;", |
| 7858 | "\0&", |
| 7859 | "\0&&", |
| 7860 | "\0||", |
| 7861 | "\0|", |
| 7862 | "\0(", |
| 7863 | "\1)", |
| 7864 | "\1;;", |
| 7865 | "\1`", |
| 7866 | #define KWDOFFSET 13 |
| 7867 | /* the following are keywords */ |
| 7868 | "\0!", |
| 7869 | "\0case", |
| 7870 | "\1do", |
| 7871 | "\1done", |
| 7872 | "\1elif", |
| 7873 | "\1else", |
| 7874 | "\1esac", |
| 7875 | "\1fi", |
| 7876 | "\0for", |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 7877 | #if ENABLE_ASH_BASH_COMPAT |
| 7878 | "\0function", |
| 7879 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7880 | "\0if", |
| 7881 | "\0in", |
| 7882 | "\1then", |
| 7883 | "\0until", |
| 7884 | "\0while", |
| 7885 | "\0{", |
| 7886 | "\1}", |
| 7887 | }; |
| 7888 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7889 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7890 | static int |
| 7891 | pstrcmp(const void *a, const void *b) |
| 7892 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7893 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7894 | } |
| 7895 | |
| 7896 | static const char *const * |
| 7897 | findkwd(const char *s) |
| 7898 | { |
| 7899 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7900 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7901 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | /* |
| 7905 | * Locate and print what a word is... |
| 7906 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7907 | static int |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 7908 | describe_command(char *command, const char *path, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7909 | { |
| 7910 | struct cmdentry entry; |
| 7911 | struct tblentry *cmdp; |
| 7912 | #if ENABLE_ASH_ALIAS |
| 7913 | const struct alias *ap; |
| 7914 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 7915 | |
| 7916 | path = path ? path : pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7917 | |
| 7918 | if (describe_command_verbose) { |
| 7919 | out1str(command); |
| 7920 | } |
| 7921 | |
| 7922 | /* First look at the keywords */ |
| 7923 | if (findkwd(command)) { |
| 7924 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7925 | goto out; |
| 7926 | } |
| 7927 | |
| 7928 | #if ENABLE_ASH_ALIAS |
| 7929 | /* Then look at the aliases */ |
| 7930 | ap = lookupalias(command, 0); |
| 7931 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7932 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7933 | out1str("alias "); |
| 7934 | printalias(ap); |
| 7935 | return 0; |
| 7936 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7937 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7938 | goto out; |
| 7939 | } |
| 7940 | #endif |
| 7941 | /* Then check if it is a tracked alias */ |
| 7942 | cmdp = cmdlookup(command, 0); |
| 7943 | if (cmdp != NULL) { |
| 7944 | entry.cmdtype = cmdp->cmdtype; |
| 7945 | entry.u = cmdp->param; |
| 7946 | } else { |
| 7947 | /* Finally use brute force */ |
| 7948 | find_command(command, &entry, DO_ABS, path); |
| 7949 | } |
| 7950 | |
| 7951 | switch (entry.cmdtype) { |
| 7952 | case CMDNORMAL: { |
| 7953 | int j = entry.u.index; |
| 7954 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7955 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7956 | p = command; |
| 7957 | } else { |
| 7958 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7959 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7960 | stunalloc(p); |
| 7961 | } while (--j >= 0); |
| 7962 | } |
| 7963 | if (describe_command_verbose) { |
| 7964 | out1fmt(" is%s %s", |
| 7965 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7966 | ); |
| 7967 | } else { |
| 7968 | out1str(p); |
| 7969 | } |
| 7970 | break; |
| 7971 | } |
| 7972 | |
| 7973 | case CMDFUNCTION: |
| 7974 | if (describe_command_verbose) { |
| 7975 | out1str(" is a shell function"); |
| 7976 | } else { |
| 7977 | out1str(command); |
| 7978 | } |
| 7979 | break; |
| 7980 | |
| 7981 | case CMDBUILTIN: |
| 7982 | if (describe_command_verbose) { |
| 7983 | out1fmt(" is a %sshell builtin", |
| 7984 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7985 | "special " : nullstr |
| 7986 | ); |
| 7987 | } else { |
| 7988 | out1str(command); |
| 7989 | } |
| 7990 | break; |
| 7991 | |
| 7992 | default: |
| 7993 | if (describe_command_verbose) { |
| 7994 | out1str(": not found\n"); |
| 7995 | } |
| 7996 | return 127; |
| 7997 | } |
| 7998 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7999 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8000 | return 0; |
| 8001 | } |
| 8002 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8003 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8004 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8005 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8006 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8007 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8008 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8009 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8010 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 8011 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 8012 | i++; |
| 8013 | verbose = 0; |
| 8014 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8015 | while (argv[i]) { |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8016 | err |= describe_command(argv[i++], NULL, verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8017 | } |
| 8018 | return err; |
| 8019 | } |
| 8020 | |
| 8021 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8022 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8023 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8024 | { |
| 8025 | int c; |
| 8026 | enum { |
| 8027 | VERIFY_BRIEF = 1, |
| 8028 | VERIFY_VERBOSE = 2, |
| 8029 | } verify = 0; |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8030 | const char *path = NULL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8031 | |
| 8032 | while ((c = nextopt("pvV")) != '\0') |
| 8033 | if (c == 'V') |
| 8034 | verify |= VERIFY_VERBOSE; |
| 8035 | else if (c == 'v') |
| 8036 | verify |= VERIFY_BRIEF; |
| 8037 | #if DEBUG |
| 8038 | else if (c != 'p') |
| 8039 | abort(); |
| 8040 | #endif |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8041 | else |
| 8042 | path = bb_default_path; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 8043 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 8044 | if (verify && (*argptr != NULL)) { |
Ron Yorston | 3f22111 | 2015-08-03 13:47:33 +0100 | [diff] [blame] | 8045 | return describe_command(*argptr, path, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 8046 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8047 | |
| 8048 | return 0; |
| 8049 | } |
| 8050 | #endif |
| 8051 | |
| 8052 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8053 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8054 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8055 | static int funcblocksize; /* size of structures in function */ |
| 8056 | static int funcstringsize; /* size of strings in node */ |
| 8057 | static void *funcblock; /* block to allocate function from */ |
| 8058 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8059 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8060 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8061 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 8062 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8063 | |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 8064 | static const uint8_t nodesize[N_NUMBER] ALIGN1 = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8065 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 8066 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 8067 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8068 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8069 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 8070 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8071 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8072 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8073 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 8074 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8075 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 8076 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 8077 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 8078 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 8079 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8080 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8081 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8082 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8083 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8084 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8085 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8086 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8087 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8088 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8089 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8090 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8091 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8092 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8093 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8094 | }; |
| 8095 | |
| 8096 | static void calcsize(union node *n); |
| 8097 | |
| 8098 | static void |
| 8099 | sizenodelist(struct nodelist *lp) |
| 8100 | { |
| 8101 | while (lp) { |
| 8102 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 8103 | calcsize(lp->n); |
| 8104 | lp = lp->next; |
| 8105 | } |
| 8106 | } |
| 8107 | |
| 8108 | static void |
| 8109 | calcsize(union node *n) |
| 8110 | { |
| 8111 | if (n == NULL) |
| 8112 | return; |
| 8113 | funcblocksize += nodesize[n->type]; |
| 8114 | switch (n->type) { |
| 8115 | case NCMD: |
| 8116 | calcsize(n->ncmd.redirect); |
| 8117 | calcsize(n->ncmd.args); |
| 8118 | calcsize(n->ncmd.assign); |
| 8119 | break; |
| 8120 | case NPIPE: |
| 8121 | sizenodelist(n->npipe.cmdlist); |
| 8122 | break; |
| 8123 | case NREDIR: |
| 8124 | case NBACKGND: |
| 8125 | case NSUBSHELL: |
| 8126 | calcsize(n->nredir.redirect); |
| 8127 | calcsize(n->nredir.n); |
| 8128 | break; |
| 8129 | case NAND: |
| 8130 | case NOR: |
| 8131 | case NSEMI: |
| 8132 | case NWHILE: |
| 8133 | case NUNTIL: |
| 8134 | calcsize(n->nbinary.ch2); |
| 8135 | calcsize(n->nbinary.ch1); |
| 8136 | break; |
| 8137 | case NIF: |
| 8138 | calcsize(n->nif.elsepart); |
| 8139 | calcsize(n->nif.ifpart); |
| 8140 | calcsize(n->nif.test); |
| 8141 | break; |
| 8142 | case NFOR: |
| 8143 | funcstringsize += strlen(n->nfor.var) + 1; |
| 8144 | calcsize(n->nfor.body); |
| 8145 | calcsize(n->nfor.args); |
| 8146 | break; |
| 8147 | case NCASE: |
| 8148 | calcsize(n->ncase.cases); |
| 8149 | calcsize(n->ncase.expr); |
| 8150 | break; |
| 8151 | case NCLIST: |
| 8152 | calcsize(n->nclist.body); |
| 8153 | calcsize(n->nclist.pattern); |
| 8154 | calcsize(n->nclist.next); |
| 8155 | break; |
| 8156 | case NDEFUN: |
| 8157 | case NARG: |
| 8158 | sizenodelist(n->narg.backquote); |
| 8159 | funcstringsize += strlen(n->narg.text) + 1; |
| 8160 | calcsize(n->narg.next); |
| 8161 | break; |
| 8162 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8163 | #if ENABLE_ASH_BASH_COMPAT |
| 8164 | case NTO2: |
| 8165 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8166 | case NCLOBBER: |
| 8167 | case NFROM: |
| 8168 | case NFROMTO: |
| 8169 | case NAPPEND: |
| 8170 | calcsize(n->nfile.fname); |
| 8171 | calcsize(n->nfile.next); |
| 8172 | break; |
| 8173 | case NTOFD: |
| 8174 | case NFROMFD: |
| 8175 | calcsize(n->ndup.vname); |
| 8176 | calcsize(n->ndup.next); |
| 8177 | break; |
| 8178 | case NHERE: |
| 8179 | case NXHERE: |
| 8180 | calcsize(n->nhere.doc); |
| 8181 | calcsize(n->nhere.next); |
| 8182 | break; |
| 8183 | case NNOT: |
| 8184 | calcsize(n->nnot.com); |
| 8185 | break; |
| 8186 | }; |
| 8187 | } |
| 8188 | |
| 8189 | static char * |
| 8190 | nodeckstrdup(char *s) |
| 8191 | { |
| 8192 | char *rtn = funcstring; |
| 8193 | |
| 8194 | strcpy(funcstring, s); |
| 8195 | funcstring += strlen(s) + 1; |
| 8196 | return rtn; |
| 8197 | } |
| 8198 | |
| 8199 | static union node *copynode(union node *); |
| 8200 | |
| 8201 | static struct nodelist * |
| 8202 | copynodelist(struct nodelist *lp) |
| 8203 | { |
| 8204 | struct nodelist *start; |
| 8205 | struct nodelist **lpp; |
| 8206 | |
| 8207 | lpp = &start; |
| 8208 | while (lp) { |
| 8209 | *lpp = funcblock; |
| 8210 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 8211 | (*lpp)->n = copynode(lp->n); |
| 8212 | lp = lp->next; |
| 8213 | lpp = &(*lpp)->next; |
| 8214 | } |
| 8215 | *lpp = NULL; |
| 8216 | return start; |
| 8217 | } |
| 8218 | |
| 8219 | static union node * |
| 8220 | copynode(union node *n) |
| 8221 | { |
| 8222 | union node *new; |
| 8223 | |
| 8224 | if (n == NULL) |
| 8225 | return NULL; |
| 8226 | new = funcblock; |
| 8227 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8228 | |
| 8229 | switch (n->type) { |
| 8230 | case NCMD: |
| 8231 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8232 | new->ncmd.args = copynode(n->ncmd.args); |
| 8233 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8234 | break; |
| 8235 | case NPIPE: |
| 8236 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8237 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8238 | break; |
| 8239 | case NREDIR: |
| 8240 | case NBACKGND: |
| 8241 | case NSUBSHELL: |
| 8242 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8243 | new->nredir.n = copynode(n->nredir.n); |
| 8244 | break; |
| 8245 | case NAND: |
| 8246 | case NOR: |
| 8247 | case NSEMI: |
| 8248 | case NWHILE: |
| 8249 | case NUNTIL: |
| 8250 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8251 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8252 | break; |
| 8253 | case NIF: |
| 8254 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8255 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8256 | new->nif.test = copynode(n->nif.test); |
| 8257 | break; |
| 8258 | case NFOR: |
| 8259 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8260 | new->nfor.body = copynode(n->nfor.body); |
| 8261 | new->nfor.args = copynode(n->nfor.args); |
| 8262 | break; |
| 8263 | case NCASE: |
| 8264 | new->ncase.cases = copynode(n->ncase.cases); |
| 8265 | new->ncase.expr = copynode(n->ncase.expr); |
| 8266 | break; |
| 8267 | case NCLIST: |
| 8268 | new->nclist.body = copynode(n->nclist.body); |
| 8269 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8270 | new->nclist.next = copynode(n->nclist.next); |
| 8271 | break; |
| 8272 | case NDEFUN: |
| 8273 | case NARG: |
| 8274 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8275 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8276 | new->narg.next = copynode(n->narg.next); |
| 8277 | break; |
| 8278 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8279 | #if ENABLE_ASH_BASH_COMPAT |
| 8280 | case NTO2: |
| 8281 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8282 | case NCLOBBER: |
| 8283 | case NFROM: |
| 8284 | case NFROMTO: |
| 8285 | case NAPPEND: |
| 8286 | new->nfile.fname = copynode(n->nfile.fname); |
| 8287 | new->nfile.fd = n->nfile.fd; |
| 8288 | new->nfile.next = copynode(n->nfile.next); |
| 8289 | break; |
| 8290 | case NTOFD: |
| 8291 | case NFROMFD: |
| 8292 | new->ndup.vname = copynode(n->ndup.vname); |
| 8293 | new->ndup.dupfd = n->ndup.dupfd; |
| 8294 | new->ndup.fd = n->ndup.fd; |
| 8295 | new->ndup.next = copynode(n->ndup.next); |
| 8296 | break; |
| 8297 | case NHERE: |
| 8298 | case NXHERE: |
| 8299 | new->nhere.doc = copynode(n->nhere.doc); |
| 8300 | new->nhere.fd = n->nhere.fd; |
| 8301 | new->nhere.next = copynode(n->nhere.next); |
| 8302 | break; |
| 8303 | case NNOT: |
| 8304 | new->nnot.com = copynode(n->nnot.com); |
| 8305 | break; |
| 8306 | }; |
| 8307 | new->type = n->type; |
| 8308 | return new; |
| 8309 | } |
| 8310 | |
| 8311 | /* |
| 8312 | * Make a copy of a parse tree. |
| 8313 | */ |
| 8314 | static struct funcnode * |
| 8315 | copyfunc(union node *n) |
| 8316 | { |
| 8317 | struct funcnode *f; |
| 8318 | size_t blocksize; |
| 8319 | |
| 8320 | funcblocksize = offsetof(struct funcnode, n); |
| 8321 | funcstringsize = 0; |
| 8322 | calcsize(n); |
| 8323 | blocksize = funcblocksize; |
| 8324 | f = ckmalloc(blocksize + funcstringsize); |
| 8325 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8326 | funcstring = (char *) f + blocksize; |
| 8327 | copynode(n); |
| 8328 | f->count = 0; |
| 8329 | return f; |
| 8330 | } |
| 8331 | |
| 8332 | /* |
| 8333 | * Define a shell function. |
| 8334 | */ |
| 8335 | static void |
| 8336 | defun(char *name, union node *func) |
| 8337 | { |
| 8338 | struct cmdentry entry; |
| 8339 | |
| 8340 | INT_OFF; |
| 8341 | entry.cmdtype = CMDFUNCTION; |
| 8342 | entry.u.func = copyfunc(func); |
| 8343 | addcmdentry(name, &entry); |
| 8344 | INT_ON; |
| 8345 | } |
| 8346 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8347 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8348 | #define SKIPBREAK (1 << 0) |
| 8349 | #define SKIPCONT (1 << 1) |
| 8350 | #define SKIPFUNC (1 << 2) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8351 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8352 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8353 | static int skipcount; /* number of levels to skip */ |
| 8354 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8355 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8356 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8357 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8358 | static int evalstring(char *s, int mask); |
| 8359 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8360 | /* Called to execute a trap. |
| 8361 | * Single callsite - at the end of evaltree(). |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8362 | * If we return non-zero, evaltree raises EXEXIT exception. |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8363 | * |
| 8364 | * Perhaps we should avoid entering new trap handlers |
| 8365 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8366 | */ |
| 8367 | static int |
| 8368 | dotrap(void) |
| 8369 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8370 | uint8_t *g; |
| 8371 | int sig; |
| 8372 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8373 | |
| 8374 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8375 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8376 | xbarrier(); |
| 8377 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8378 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8379 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8380 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8381 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8382 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8383 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8384 | t = trap[sig]; |
| 8385 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8386 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8387 | if (sig == SIGINT && !t) |
| 8388 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8389 | |
| 8390 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8391 | *g = 0; |
| 8392 | if (!t) |
| 8393 | continue; |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 8394 | evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8395 | exitstatus = savestatus; |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 8396 | if (evalskip) { |
| 8397 | TRACE(("dotrap returns %d\n", evalskip)); |
| 8398 | return evalskip; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8399 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8400 | } |
| 8401 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8402 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8403 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8404 | } |
| 8405 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8406 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8407 | static int evalloop(union node *, int); |
| 8408 | static int evalfor(union node *, int); |
| 8409 | static int evalcase(union node *, int); |
| 8410 | static int evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8411 | static void expredir(union node *); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8412 | static int evalpipe(union node *, int); |
| 8413 | static int evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8414 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8415 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8416 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8417 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8418 | * Evaluate a parse tree. The value is left in the global variable |
| 8419 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8420 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8421 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8422 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8423 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8424 | struct jmploc *volatile savehandler = exception_handler; |
| 8425 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8426 | int checkexit = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8427 | int (*evalfn)(union node *, int); |
| 8428 | int status = 0; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8429 | int int_level; |
| 8430 | |
| 8431 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8432 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8433 | if (n == NULL) { |
| 8434 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8435 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8436 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8437 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8438 | |
| 8439 | exception_handler = &jmploc; |
| 8440 | { |
| 8441 | int err = setjmp(jmploc.loc); |
| 8442 | if (err) { |
| 8443 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8444 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8445 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8446 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8447 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8448 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8449 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8450 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8451 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8452 | exception_handler = savehandler; |
| 8453 | longjmp(exception_handler->loc, err); |
| 8454 | } |
| 8455 | } |
| 8456 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8457 | switch (n->type) { |
| 8458 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8459 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8460 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8461 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8462 | break; |
| 8463 | #endif |
| 8464 | case NNOT: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8465 | status = !evaltree(n->nnot.com, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8466 | goto setstatus; |
| 8467 | case NREDIR: |
| 8468 | expredir(n->nredir.redirect); |
| 8469 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8470 | if (!status) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8471 | status = evaltree(n->nredir.n, flags & EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8472 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8473 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8474 | goto setstatus; |
| 8475 | case NCMD: |
| 8476 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8477 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8478 | if (eflag && !(flags & EV_TESTED)) |
| 8479 | checkexit = ~0; |
| 8480 | goto calleval; |
| 8481 | case NFOR: |
| 8482 | evalfn = evalfor; |
| 8483 | goto calleval; |
| 8484 | case NWHILE: |
| 8485 | case NUNTIL: |
| 8486 | evalfn = evalloop; |
| 8487 | goto calleval; |
| 8488 | case NSUBSHELL: |
Rostislav Skudnov | 204c7fb | 2016-09-16 19:04:02 +0000 | [diff] [blame] | 8489 | evalfn = evalsubshell; |
| 8490 | goto checkexit; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8491 | case NBACKGND: |
| 8492 | evalfn = evalsubshell; |
| 8493 | goto calleval; |
| 8494 | case NPIPE: |
| 8495 | evalfn = evalpipe; |
| 8496 | goto checkexit; |
| 8497 | case NCASE: |
| 8498 | evalfn = evalcase; |
| 8499 | goto calleval; |
| 8500 | case NAND: |
| 8501 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8502 | case NSEMI: { |
| 8503 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8504 | #if NAND + 1 != NOR |
| 8505 | #error NAND + 1 != NOR |
| 8506 | #endif |
| 8507 | #if NOR + 1 != NSEMI |
| 8508 | #error NOR + 1 != NSEMI |
| 8509 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8510 | unsigned is_or = n->type - NAND; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8511 | status = evaltree( |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8512 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8513 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8514 | ); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8515 | if (!status == is_or || evalskip) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8516 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8517 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8518 | evaln: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8519 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8520 | calleval: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8521 | status = evalfn(n, flags); |
| 8522 | goto setstatus; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8523 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8524 | case NIF: |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8525 | status = evaltree(n->nif.test, EV_TESTED); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8526 | if (evalskip) |
| 8527 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8528 | if (!status) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8529 | n = n->nif.ifpart; |
| 8530 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8531 | } |
| 8532 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8533 | n = n->nif.elsepart; |
| 8534 | goto evaln; |
| 8535 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8536 | status = 0; |
| 8537 | goto setstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8538 | case NDEFUN: |
| 8539 | defun(n->narg.text, n->narg.next); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8540 | /* Not necessary. To test it: |
| 8541 | * "false; f() { qwerty; }; echo $?" should print 0. |
| 8542 | */ |
| 8543 | /* status = 0; */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8544 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8545 | exitstatus = status; |
| 8546 | break; |
| 8547 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8548 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8549 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8550 | exception_handler = savehandler; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8551 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8552 | out1: |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8553 | /* Order of checks below is important: |
| 8554 | * signal handlers trigger before exit caused by "set -e". |
| 8555 | */ |
| 8556 | if (pending_sig && dotrap()) |
| 8557 | goto exexit; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8558 | if (checkexit & status) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8559 | evalskip |= SKIPEVAL; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8560 | |
| 8561 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8562 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8563 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8564 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8565 | |
| 8566 | RESTORE_INT(int_level); |
| 8567 | TRACE(("leaving evaltree (no interrupts)\n")); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8568 | |
| 8569 | return exitstatus; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8570 | } |
| 8571 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8572 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8573 | static |
| 8574 | #endif |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8575 | int evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8576 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8577 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8578 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8579 | { |
| 8580 | int status; |
| 8581 | |
| 8582 | loopnest++; |
| 8583 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8584 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8585 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8586 | int i; |
| 8587 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8588 | i = evaltree(n->nbinary.ch1, EV_TESTED); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8589 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8590 | skipping: |
| 8591 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8592 | evalskip = 0; |
| 8593 | continue; |
| 8594 | } |
| 8595 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8596 | evalskip = 0; |
| 8597 | break; |
| 8598 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8599 | if (n->type != NWHILE) |
| 8600 | i = !i; |
| 8601 | if (i != 0) |
| 8602 | break; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8603 | status = evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8604 | if (evalskip) |
| 8605 | goto skipping; |
| 8606 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8607 | exitstatus = status; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8608 | loopnest--; |
| 8609 | |
| 8610 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8611 | } |
| 8612 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8613 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8614 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8615 | { |
| 8616 | struct arglist arglist; |
| 8617 | union node *argp; |
| 8618 | struct strlist *sp; |
| 8619 | struct stackmark smark; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8620 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8621 | |
| 8622 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8623 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8624 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8625 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 8626 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8627 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8628 | if (evalskip) |
| 8629 | goto out; |
| 8630 | } |
| 8631 | *arglist.lastp = NULL; |
| 8632 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8633 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8634 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8635 | for (sp = arglist.list; sp; sp = sp->next) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 8636 | setvar0(n->nfor.var, sp->text); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8637 | status = evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8638 | if (evalskip) { |
| 8639 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8640 | evalskip = 0; |
| 8641 | continue; |
| 8642 | } |
| 8643 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8644 | evalskip = 0; |
| 8645 | break; |
| 8646 | } |
| 8647 | } |
| 8648 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8649 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8650 | popstackmark(&smark); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8651 | |
| 8652 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8653 | } |
| 8654 | |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8655 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8656 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8657 | { |
| 8658 | union node *cp; |
| 8659 | union node *patp; |
| 8660 | struct arglist arglist; |
| 8661 | struct stackmark smark; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8662 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8663 | |
| 8664 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8665 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8666 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8667 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8668 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8669 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8670 | if (casematch(patp, arglist.list->text)) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8671 | /* Ensure body is non-empty as otherwise |
| 8672 | * EV_EXIT may prevent us from setting the |
| 8673 | * exit status. |
| 8674 | */ |
| 8675 | if (evalskip == 0 && cp->nclist.body) { |
| 8676 | status = evaltree(cp->nclist.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8677 | } |
| 8678 | goto out; |
| 8679 | } |
| 8680 | } |
| 8681 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8682 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8683 | popstackmark(&smark); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8684 | |
| 8685 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8686 | } |
| 8687 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8688 | /* |
| 8689 | * Kick off a subshell to evaluate a tree. |
| 8690 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8691 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8692 | evalsubshell(union node *n, int flags) |
| 8693 | { |
| 8694 | struct job *jp; |
| 8695 | int backgnd = (n->type == NBACKGND); |
| 8696 | int status; |
| 8697 | |
| 8698 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8699 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8700 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8701 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8702 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8703 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8704 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8705 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8706 | flags |= EV_EXIT; |
| 8707 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8708 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8709 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8710 | redirect(n->nredir.redirect, 0); |
| 8711 | evaltreenr(n->nredir.n, flags); |
| 8712 | /* never returns */ |
| 8713 | } |
| 8714 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8715 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8716 | status = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8717 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8718 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8721 | /* |
| 8722 | * Compute the names of the files in a redirection list. |
| 8723 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8724 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8725 | static void |
| 8726 | expredir(union node *n) |
| 8727 | { |
| 8728 | union node *redir; |
| 8729 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8730 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8731 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8732 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8733 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8734 | fn.lastp = &fn.list; |
| 8735 | switch (redir->type) { |
| 8736 | case NFROMTO: |
| 8737 | case NFROM: |
| 8738 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8739 | #if ENABLE_ASH_BASH_COMPAT |
| 8740 | case NTO2: |
| 8741 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8742 | case NCLOBBER: |
| 8743 | case NAPPEND: |
| 8744 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 8745 | TRACE(("expredir expanded to '%s'\n", fn.list->text)); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8746 | #if ENABLE_ASH_BASH_COMPAT |
| 8747 | store_expfname: |
| 8748 | #endif |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 8749 | #if 0 |
| 8750 | // By the design of stack allocator, the loop of this kind: |
| 8751 | // while true; do while true; do break; done </dev/null; done |
| 8752 | // will look like a memory leak: ash plans to free expfname's |
| 8753 | // of "/dev/null" as soon as it finishes running the loop |
| 8754 | // (in this case, never). |
| 8755 | // This "fix" is wrong: |
Jon Tollefson | 4ba6c5d | 2012-11-13 19:26:53 +0100 | [diff] [blame] | 8756 | if (redir->nfile.expfname) |
| 8757 | stunalloc(redir->nfile.expfname); |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 8758 | // It results in corrupted state of stacked allocations. |
| 8759 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8760 | redir->nfile.expfname = fn.list->text; |
| 8761 | break; |
| 8762 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8763 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8764 | if (redir->ndup.vname) { |
| 8765 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8766 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8767 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8768 | #if ENABLE_ASH_BASH_COMPAT |
| 8769 | //FIXME: we used expandarg with different args! |
| 8770 | if (!isdigit_str9(fn.list->text)) { |
| 8771 | /* >&file, not >&fd */ |
| 8772 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8773 | ash_msg_and_raise_error("redir error"); |
| 8774 | redir->type = NTO2; |
| 8775 | goto store_expfname; |
| 8776 | } |
| 8777 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8778 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8779 | } |
| 8780 | break; |
| 8781 | } |
| 8782 | } |
| 8783 | } |
| 8784 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8785 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8786 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8787 | * of the process creating the pipeline. (This differs from some versions |
| 8788 | * of the shell, which make the last process in a pipeline the parent |
| 8789 | * of all the rest.) |
| 8790 | */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8791 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8792 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8793 | { |
| 8794 | struct job *jp; |
| 8795 | struct nodelist *lp; |
| 8796 | int pipelen; |
| 8797 | int prevfd; |
| 8798 | int pip[2]; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8799 | int status = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8800 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8801 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8802 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8803 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8804 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8805 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8806 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8807 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8808 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8809 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8810 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8811 | pip[1] = -1; |
| 8812 | if (lp->next) { |
| 8813 | if (pipe(pip) < 0) { |
| 8814 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8815 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8816 | } |
| 8817 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8818 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8819 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8820 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8821 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8822 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8823 | if (prevfd > 0) { |
| 8824 | dup2(prevfd, 0); |
| 8825 | close(prevfd); |
| 8826 | } |
| 8827 | if (pip[1] > 1) { |
| 8828 | dup2(pip[1], 1); |
| 8829 | close(pip[1]); |
| 8830 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8831 | evaltreenr(lp->n, flags); |
| 8832 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8833 | } |
| 8834 | if (prevfd >= 0) |
| 8835 | close(prevfd); |
| 8836 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8837 | /* Don't want to trigger debugging */ |
| 8838 | if (pip[1] != -1) |
| 8839 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8840 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8841 | if (n->npipe.pipe_backgnd == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8842 | status = waitforjob(jp); |
| 8843 | TRACE(("evalpipe: job done exit status %d\n", status)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8844 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8845 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 8846 | |
| 8847 | return status; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8848 | } |
| 8849 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8850 | /* |
| 8851 | * Controls whether the shell is interactive or not. |
| 8852 | */ |
| 8853 | static void |
| 8854 | setinteractive(int on) |
| 8855 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8856 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8857 | |
| 8858 | if (++on == is_interactive) |
| 8859 | return; |
| 8860 | is_interactive = on; |
| 8861 | setsignal(SIGINT); |
| 8862 | setsignal(SIGQUIT); |
| 8863 | setsignal(SIGTERM); |
| 8864 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8865 | if (is_interactive > 1) { |
| 8866 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8867 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8868 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8869 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8870 | /* note: ash and hush share this string */ |
| 8871 | out1fmt("\n\n%s %s\n" |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 8872 | IF_ASH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 8873 | "\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8874 | bb_banner, |
| 8875 | "built-in shell (ash)" |
| 8876 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8877 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8878 | } |
| 8879 | } |
| 8880 | #endif |
| 8881 | } |
| 8882 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8883 | static void |
| 8884 | optschanged(void) |
| 8885 | { |
| 8886 | #if DEBUG |
| 8887 | opentrace(); |
| 8888 | #endif |
| 8889 | setinteractive(iflag); |
| 8890 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8891 | #if ENABLE_FEATURE_EDITING_VI |
| 8892 | if (viflag) |
| 8893 | line_input_state->flags |= VI_MODE; |
| 8894 | else |
| 8895 | line_input_state->flags &= ~VI_MODE; |
| 8896 | #else |
| 8897 | viflag = 0; /* forcibly keep the option off */ |
| 8898 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8899 | } |
| 8900 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8901 | static struct localvar *localvars; |
| 8902 | |
| 8903 | /* |
| 8904 | * Called after a function returns. |
| 8905 | * Interrupts must be off. |
| 8906 | */ |
| 8907 | static void |
| 8908 | poplocalvars(void) |
| 8909 | { |
| 8910 | struct localvar *lvp; |
| 8911 | struct var *vp; |
| 8912 | |
| 8913 | while ((lvp = localvars) != NULL) { |
| 8914 | localvars = lvp->next; |
| 8915 | vp = lvp->vp; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8916 | TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8917 | if (vp == NULL) { /* $- saved */ |
| 8918 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8919 | free((char*)lvp->text); |
| 8920 | optschanged(); |
| 8921 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8922 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8923 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8924 | if (vp->var_func) |
| 8925 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8926 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8927 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8928 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8929 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8930 | } |
| 8931 | free(lvp); |
| 8932 | } |
| 8933 | } |
| 8934 | |
| 8935 | static int |
| 8936 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8937 | { |
| 8938 | volatile struct shparam saveparam; |
| 8939 | struct localvar *volatile savelocalvars; |
| 8940 | struct jmploc *volatile savehandler; |
| 8941 | struct jmploc jmploc; |
| 8942 | int e; |
| 8943 | |
| 8944 | saveparam = shellparam; |
| 8945 | savelocalvars = localvars; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 8946 | savehandler = exception_handler; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8947 | e = setjmp(jmploc.loc); |
| 8948 | if (e) { |
| 8949 | goto funcdone; |
| 8950 | } |
| 8951 | INT_OFF; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8952 | exception_handler = &jmploc; |
| 8953 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8954 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8955 | func->count++; |
| 8956 | funcnest++; |
| 8957 | INT_ON; |
| 8958 | shellparam.nparam = argc - 1; |
| 8959 | shellparam.p = argv + 1; |
| 8960 | #if ENABLE_ASH_GETOPTS |
| 8961 | shellparam.optind = 1; |
| 8962 | shellparam.optoff = -1; |
| 8963 | #endif |
| 8964 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8965 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8966 | INT_OFF; |
| 8967 | funcnest--; |
| 8968 | freefunc(func); |
| 8969 | poplocalvars(); |
| 8970 | localvars = savelocalvars; |
| 8971 | freeparam(&shellparam); |
| 8972 | shellparam = saveparam; |
| 8973 | exception_handler = savehandler; |
| 8974 | INT_ON; |
| 8975 | evalskip &= ~SKIPFUNC; |
| 8976 | return e; |
| 8977 | } |
| 8978 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8979 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8980 | static char ** |
| 8981 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8982 | { |
| 8983 | char *cp, c; |
| 8984 | |
| 8985 | for (;;) { |
| 8986 | cp = *++argv; |
| 8987 | if (!cp) |
Denys Vlasenko | e2f32c0 | 2015-10-29 19:46:40 +0100 | [diff] [blame] | 8988 | return NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8989 | if (*cp++ != '-') |
| 8990 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8991 | c = *cp++; |
| 8992 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8993 | break; |
| 8994 | if (c == '-' && !*cp) { |
Denys Vlasenko | e2f32c0 | 2015-10-29 19:46:40 +0100 | [diff] [blame] | 8995 | if (!*++argv) |
| 8996 | return NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8997 | break; |
| 8998 | } |
| 8999 | do { |
| 9000 | switch (c) { |
| 9001 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 9002 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9003 | break; |
| 9004 | default: |
| 9005 | /* run 'typecmd' for other options */ |
Denys Vlasenko | e2f32c0 | 2015-10-29 19:46:40 +0100 | [diff] [blame] | 9006 | return NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9007 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 9008 | c = *cp++; |
| 9009 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9010 | } |
| 9011 | return argv; |
| 9012 | } |
| 9013 | #endif |
| 9014 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9015 | /* |
| 9016 | * Make a variable a local variable. When a variable is made local, it's |
| 9017 | * value and flags are saved in a localvar structure. The saved values |
| 9018 | * will be restored when the shell function returns. We handle the name |
Denys Vlasenko | e0a4e10 | 2015-05-13 02:20:14 +0200 | [diff] [blame] | 9019 | * "-" as a special case: it makes changes to "set +-options" local |
| 9020 | * (options will be restored on return from the function). |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9021 | */ |
| 9022 | static void |
| 9023 | mklocal(char *name) |
| 9024 | { |
| 9025 | struct localvar *lvp; |
| 9026 | struct var **vpp; |
| 9027 | struct var *vp; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9028 | char *eq = strchr(name, '='); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9029 | |
| 9030 | INT_OFF; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9031 | /* Cater for duplicate "local". Examples: |
| 9032 | * x=0; f() { local x=1; echo $x; local x; echo $x; }; f; echo $x |
| 9033 | * x=0; f() { local x=1; echo $x; local x=2; echo $x; }; f; echo $x |
| 9034 | */ |
| 9035 | lvp = localvars; |
| 9036 | while (lvp) { |
Eugene Rudoy | 1285aa6 | 2015-04-26 23:32:00 +0200 | [diff] [blame] | 9037 | if (lvp->vp && varcmp(lvp->vp->var_text, name) == 0) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9038 | if (eq) |
| 9039 | setvareq(name, 0); |
| 9040 | /* else: |
| 9041 | * it's a duplicate "local VAR" declaration, do nothing |
| 9042 | */ |
| 9043 | return; |
| 9044 | } |
| 9045 | lvp = lvp->next; |
| 9046 | } |
| 9047 | |
| 9048 | lvp = ckzalloc(sizeof(*lvp)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9049 | if (LONE_DASH(name)) { |
| 9050 | char *p; |
| 9051 | p = ckmalloc(sizeof(optlist)); |
| 9052 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 9053 | vp = NULL; |
| 9054 | } else { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9055 | vpp = hashvar(name); |
| 9056 | vp = *findvar(vpp, name); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9057 | if (vp == NULL) { |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9058 | /* variable did not exist yet */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9059 | if (eq) |
| 9060 | setvareq(name, VSTRFIXED); |
| 9061 | else |
| 9062 | setvar(name, NULL, VSTRFIXED); |
| 9063 | vp = *vpp; /* the new variable */ |
| 9064 | lvp->flags = VUNSET; |
| 9065 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9066 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9067 | lvp->flags = vp->flags; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9068 | /* make sure neither "struct var" nor string gets freed |
| 9069 | * during (un)setting: |
| 9070 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9071 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 9072 | if (eq) |
| 9073 | setvareq(name, 0); |
Denys Vlasenko | 109ee5d | 2014-03-16 18:41:11 +0100 | [diff] [blame] | 9074 | else |
| 9075 | /* "local VAR" unsets VAR: */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9076 | setvar0(name, NULL); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9077 | } |
| 9078 | } |
| 9079 | lvp->vp = vp; |
| 9080 | lvp->next = localvars; |
| 9081 | localvars = lvp; |
| 9082 | INT_ON; |
| 9083 | } |
| 9084 | |
| 9085 | /* |
| 9086 | * The "local" command. |
| 9087 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9088 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9089 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9090 | { |
| 9091 | char *name; |
| 9092 | |
Ron Yorston | ef2386b | 2015-10-29 16:19:14 +0000 | [diff] [blame] | 9093 | if (!funcnest) |
| 9094 | ash_msg_and_raise_error("not in a function"); |
| 9095 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9096 | argv = argptr; |
| 9097 | while ((name = *argv++) != NULL) { |
| 9098 | mklocal(name); |
| 9099 | } |
| 9100 | return 0; |
| 9101 | } |
| 9102 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9103 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9104 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9105 | { |
| 9106 | return 1; |
| 9107 | } |
| 9108 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9109 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9110 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9111 | { |
| 9112 | return 0; |
| 9113 | } |
| 9114 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9115 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9116 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9117 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9118 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9119 | iflag = 0; /* exit on error */ |
| 9120 | mflag = 0; |
| 9121 | optschanged(); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 9122 | /* We should set up signals for "exec CMD" |
| 9123 | * the same way as for "CMD" without "exec". |
| 9124 | * But optschanged->setinteractive->setsignal |
| 9125 | * still thought we are a root shell. Therefore, for example, |
| 9126 | * SIGQUIT is still set to IGN. Fix it: |
| 9127 | */ |
| 9128 | shlvl++; |
| 9129 | setsignal(SIGQUIT); |
| 9130 | /*setsignal(SIGTERM); - unnecessary because of iflag=0 */ |
| 9131 | /*setsignal(SIGTSTP); - unnecessary because of mflag=0 */ |
| 9132 | /*setsignal(SIGTTOU); - unnecessary because of mflag=0 */ |
| 9133 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9134 | shellexec(argv + 1, pathval(), 0); |
Denys Vlasenko | e5814a5 | 2016-07-16 18:33:55 +0200 | [diff] [blame] | 9135 | /* NOTREACHED */ |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9136 | } |
| 9137 | return 0; |
| 9138 | } |
| 9139 | |
| 9140 | /* |
| 9141 | * The return command. |
| 9142 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9143 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9144 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9145 | { |
| 9146 | /* |
| 9147 | * If called outside a function, do what ksh does; |
| 9148 | * skip the rest of the file. |
| 9149 | */ |
Denys Vlasenko | 6a0710e | 2016-09-30 14:18:34 +0200 | [diff] [blame^] | 9150 | evalskip = SKIPFUNC; |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9151 | return argv[1] ? number(argv[1]) : exitstatus; |
| 9152 | } |
| 9153 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9154 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9155 | static int breakcmd(int, char **) FAST_FUNC; |
| 9156 | static int dotcmd(int, char **) FAST_FUNC; |
| 9157 | static int evalcmd(int, char **) FAST_FUNC; |
| 9158 | static int exitcmd(int, char **) FAST_FUNC; |
| 9159 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9160 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9161 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9162 | #endif |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9163 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9164 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 9165 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9166 | #if MAX_HISTORY |
| 9167 | static int historycmd(int, char **) FAST_FUNC; |
| 9168 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9169 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9170 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9171 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9172 | static int readcmd(int, char **) FAST_FUNC; |
| 9173 | static int setcmd(int, char **) FAST_FUNC; |
| 9174 | static int shiftcmd(int, char **) FAST_FUNC; |
| 9175 | static int timescmd(int, char **) FAST_FUNC; |
| 9176 | static int trapcmd(int, char **) FAST_FUNC; |
| 9177 | static int umaskcmd(int, char **) FAST_FUNC; |
| 9178 | static int unsetcmd(int, char **) FAST_FUNC; |
| 9179 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9180 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9181 | #define BUILTIN_NOSPEC "0" |
| 9182 | #define BUILTIN_SPECIAL "1" |
| 9183 | #define BUILTIN_REGULAR "2" |
| 9184 | #define BUILTIN_SPEC_REG "3" |
| 9185 | #define BUILTIN_ASSIGN "4" |
| 9186 | #define BUILTIN_SPEC_ASSG "5" |
| 9187 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9188 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 9189 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9190 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9191 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9192 | static int FAST_FUNC echocmd(int argc, char **argv) { return echo_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9193 | #endif |
| 9194 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9195 | static int FAST_FUNC printfcmd(int argc, char **argv) { return printf_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9196 | #endif |
| 9197 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9198 | static int FAST_FUNC testcmd(int argc, char **argv) { return test_main(argc, argv); } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9199 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 9200 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9201 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9202 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9203 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 9204 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9205 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9206 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9207 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9208 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9209 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9210 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9211 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9212 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9213 | #endif |
| 9214 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9215 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9216 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9217 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 9218 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 9219 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9220 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9221 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9222 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9223 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9224 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9225 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9226 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9227 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 9228 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 9229 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 9230 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 9231 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9232 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9233 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9234 | #endif |
| 9235 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9236 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9237 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9238 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 9239 | #if ENABLE_ASH_HELP |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9240 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9241 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9242 | #if MAX_HISTORY |
| 9243 | { BUILTIN_NOSPEC "history" , historycmd }, |
| 9244 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9245 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9246 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 9247 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9248 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9249 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9250 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9251 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9252 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9253 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9254 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9255 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9256 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 9257 | { BUILTIN_REGULAR "read" , readcmd }, |
| 9258 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 9259 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 9260 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 9261 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9262 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9263 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9264 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9265 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9266 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9267 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9268 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 9269 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 9270 | { BUILTIN_REGULAR "true" , truecmd }, |
| 9271 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 9272 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 9273 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9274 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9275 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9276 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9277 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 9278 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9279 | }; |
| 9280 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9281 | /* Should match the above table! */ |
| 9282 | #define COMMANDCMD (builtintab + \ |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 9283 | /* . : */ 2 + \ |
| 9284 | /* [ */ 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9285 | /* [[ */ 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9286 | /* alias */ 1 * ENABLE_ASH_ALIAS + \ |
| 9287 | /* bg */ 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9288 | /* break cd cddir */ 3) |
| 9289 | #define EVALCMD (COMMANDCMD + \ |
| 9290 | /* command */ 1 * ENABLE_ASH_CMDCMD + \ |
| 9291 | /* continue */ 1 + \ |
| 9292 | /* echo */ 1 * ENABLE_ASH_BUILTIN_ECHO + \ |
| 9293 | 0) |
| 9294 | #define EXECCMD (EVALCMD + \ |
| 9295 | /* eval */ 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9296 | |
| 9297 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9298 | * Search the table of builtin commands. |
| 9299 | */ |
| 9300 | static struct builtincmd * |
| 9301 | find_builtin(const char *name) |
| 9302 | { |
| 9303 | struct builtincmd *bp; |
| 9304 | |
| 9305 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 9306 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9307 | pstrcmp |
| 9308 | ); |
| 9309 | return bp; |
| 9310 | } |
| 9311 | |
| 9312 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9313 | * Execute a simple command. |
| 9314 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9315 | static int |
| 9316 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9317 | { |
| 9318 | const char *q = endofname(p); |
| 9319 | if (p == q) |
| 9320 | return 0; |
| 9321 | return *q == '='; |
| 9322 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9323 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9324 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9325 | { |
| 9326 | /* Preserve exitstatus of a previous possible redirection |
| 9327 | * as POSIX mandates */ |
| 9328 | return back_exitstatus; |
| 9329 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9330 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9331 | evalcommand(union node *cmd, int flags) |
| 9332 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9333 | static const struct builtincmd null_bltin = { |
| 9334 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9335 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9336 | struct stackmark smark; |
| 9337 | union node *argp; |
| 9338 | struct arglist arglist; |
| 9339 | struct arglist varlist; |
| 9340 | char **argv; |
| 9341 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9342 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9343 | struct cmdentry cmdentry; |
| 9344 | struct job *jp; |
| 9345 | char *lastarg; |
| 9346 | const char *path; |
| 9347 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9348 | int status; |
| 9349 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9350 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9351 | smallint cmd_is_exec; |
| 9352 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9353 | |
| 9354 | /* First expand the arguments. */ |
| 9355 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9356 | setstackmark(&smark); |
| 9357 | back_exitstatus = 0; |
| 9358 | |
| 9359 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9360 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9361 | varlist.lastp = &varlist.list; |
| 9362 | *varlist.lastp = NULL; |
| 9363 | arglist.lastp = &arglist.list; |
| 9364 | *arglist.lastp = NULL; |
| 9365 | |
| 9366 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9367 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9368 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9369 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9370 | } |
| 9371 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9372 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9373 | struct strlist **spp; |
| 9374 | |
| 9375 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9376 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9377 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9378 | else |
| 9379 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9380 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9381 | for (sp = *spp; sp; sp = sp->next) |
| 9382 | argc++; |
| 9383 | } |
| 9384 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9385 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9386 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9387 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9388 | *nargv++ = sp->text; |
| 9389 | } |
| 9390 | *nargv = NULL; |
| 9391 | |
| 9392 | lastarg = NULL; |
| 9393 | if (iflag && funcnest == 0 && argc > 0) |
| 9394 | lastarg = nargv[-1]; |
| 9395 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9396 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9397 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9398 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9399 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9400 | path = vpath.var_text; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9401 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9402 | struct strlist **spp; |
| 9403 | char *p; |
| 9404 | |
| 9405 | spp = varlist.lastp; |
| 9406 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9407 | |
| 9408 | /* |
| 9409 | * Modify the command lookup path, if a PATH= assignment |
| 9410 | * is present |
| 9411 | */ |
| 9412 | p = (*spp)->text; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9413 | if (varcmp(p, path) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9414 | path = p; |
| 9415 | } |
| 9416 | |
| 9417 | /* Print the command if xflag is set. */ |
| 9418 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9419 | int n; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9420 | const char *p = " %s" + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9421 | |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9422 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9423 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9424 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9425 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9426 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9427 | sp = sp->next; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9428 | p = " %s"; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9429 | } |
| 9430 | sp = arglist.list; |
| 9431 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9432 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9433 | } |
| 9434 | |
| 9435 | cmd_is_exec = 0; |
| 9436 | spclbltin = -1; |
| 9437 | |
| 9438 | /* Now locate the command. */ |
| 9439 | if (argc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9440 | int cmd_flag = DO_ERR; |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 9441 | #if ENABLE_ASH_CMDCMD |
| 9442 | const char *oldpath = path + 5; |
| 9443 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9444 | path += 5; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9445 | for (;;) { |
| 9446 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9447 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9448 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9449 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9450 | goto bail; |
| 9451 | } |
| 9452 | |
| 9453 | /* implement bltin and command here */ |
| 9454 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9455 | break; |
| 9456 | if (spclbltin < 0) |
| 9457 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9458 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9459 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9460 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9461 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9462 | path = oldpath; |
| 9463 | nargv = parse_command_args(argv, &path); |
| 9464 | if (!nargv) |
| 9465 | break; |
| 9466 | argc -= nargv - argv; |
| 9467 | argv = nargv; |
| 9468 | cmd_flag |= DO_NOFUNC; |
| 9469 | } else |
| 9470 | #endif |
| 9471 | break; |
| 9472 | } |
| 9473 | } |
| 9474 | |
| 9475 | if (status) { |
| 9476 | /* We have a redirection error. */ |
| 9477 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9478 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9479 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9480 | exitstatus = status; |
| 9481 | goto out; |
| 9482 | } |
| 9483 | |
| 9484 | /* Execute the command. */ |
| 9485 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9486 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9487 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9488 | #if ENABLE_FEATURE_SH_NOFORK |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9489 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 9490 | * around run_nofork_applet() call. |
| 9491 | * (2) Should this check also be done in forkshell()? |
| 9492 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 9493 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9494 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9495 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9496 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9497 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9498 | /* run <applet>_main() */ |
| 9499 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9500 | break; |
| 9501 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9502 | #endif |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9503 | /* Can we avoid forking off? For example, very last command |
| 9504 | * in a script or a subshell does not need forking, |
| 9505 | * we can just exec it. |
| 9506 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9507 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9508 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9509 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9510 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9511 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9512 | /* parent */ |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9513 | status = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9514 | INT_ON; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9515 | TRACE(("forked child exited with %d\n", status)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9516 | break; |
| 9517 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9518 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9519 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 9520 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9521 | } |
| 9522 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9523 | shellexec(argv, path, cmdentry.u.index); |
| 9524 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9525 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9526 | case CMDBUILTIN: |
| 9527 | cmdenviron = varlist.list; |
| 9528 | if (cmdenviron) { |
| 9529 | struct strlist *list = cmdenviron; |
| 9530 | int i = VNOSET; |
| 9531 | if (spclbltin > 0 || argc == 0) { |
| 9532 | i = 0; |
| 9533 | if (cmd_is_exec && argc > 1) |
| 9534 | i = VEXPORT; |
| 9535 | } |
| 9536 | listsetvar(list, i); |
| 9537 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9538 | /* Tight loop with builtins only: |
| 9539 | * "while kill -0 $child; do true; done" |
| 9540 | * will never exit even if $child died, unless we do this |
| 9541 | * to reap the zombie and make kill detect that it's gone: */ |
| 9542 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9543 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9544 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9545 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9546 | int i = exception_type; |
Ron Yorston | 8c55dc7 | 2015-10-30 19:06:47 +0000 | [diff] [blame] | 9547 | if (i == EXEXIT || i == EXEXEC) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9548 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9549 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9550 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9551 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9552 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9553 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9554 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9555 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9556 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9557 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9558 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9559 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9560 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9561 | goto readstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9562 | |
| 9563 | case CMDFUNCTION: |
| 9564 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9565 | /* See above for the rationale */ |
| 9566 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9567 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9568 | goto raise; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9569 | readstatus: |
| 9570 | status = exitstatus; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9571 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9572 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9573 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9574 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9575 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9576 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9577 | /* dsl: I think this is intended to be used to support |
| 9578 | * '_' in 'vi' command mode during line editing... |
| 9579 | * However I implemented that within libedit itself. |
| 9580 | */ |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 9581 | setvar0("_", lastarg); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9582 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9583 | popstackmark(&smark); |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 9584 | |
| 9585 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9586 | } |
| 9587 | |
| 9588 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9589 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9590 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9591 | char *volatile savecmdname; |
| 9592 | struct jmploc *volatile savehandler; |
| 9593 | struct jmploc jmploc; |
| 9594 | int i; |
| 9595 | |
| 9596 | savecmdname = commandname; |
Denys Vlasenko | a2d121c | 2016-09-30 11:30:11 +0200 | [diff] [blame] | 9597 | savehandler = exception_handler; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9598 | i = setjmp(jmploc.loc); |
| 9599 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9600 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9601 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9602 | commandname = argv[0]; |
| 9603 | argptr = argv + 1; |
| 9604 | optptr = NULL; /* initialize nextopt */ |
| 9605 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9606 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9607 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9608 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9609 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9610 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9611 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9612 | |
| 9613 | return i; |
| 9614 | } |
| 9615 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9616 | static int |
| 9617 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9618 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 9619 | return endofname(p)[0] == '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9620 | } |
| 9621 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9622 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9623 | /* |
| 9624 | * Search for a command. This is called before we fork so that the |
| 9625 | * location of the command will be available in the parent as well as |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9626 | * the child. The check for "goodname" is an overly conservative |
| 9627 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9628 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9629 | static void |
| 9630 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9631 | { |
| 9632 | struct cmdentry entry; |
| 9633 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9634 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9635 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9636 | } |
| 9637 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9638 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9639 | /* ============ Builtin commands |
| 9640 | * |
| 9641 | * Builtin commands whose functions are closely tied to evaluation |
| 9642 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9643 | */ |
| 9644 | |
| 9645 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9646 | * Handle break and continue commands. Break, continue, and return are |
| 9647 | * all handled by setting the evalskip flag. The evaluation routines |
| 9648 | * above all check this flag, and if it is set they start skipping |
| 9649 | * commands rather than executing them. The variable skipcount is |
| 9650 | * the number of loops to break/continue, or the number of function |
| 9651 | * levels to return. (The latter is always 1.) It should probably |
| 9652 | * be an error to break out of more loops than exist, but it isn't |
| 9653 | * in the standard shell so we don't make it one here. |
| 9654 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9655 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9656 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9657 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9658 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9659 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9660 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9661 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9662 | if (n > loopnest) |
| 9663 | n = loopnest; |
| 9664 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9665 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9666 | skipcount = n; |
| 9667 | } |
| 9668 | return 0; |
| 9669 | } |
| 9670 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9671 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9672 | /* ============ input.c |
| 9673 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9674 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9675 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9676 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9677 | enum { |
| 9678 | INPUT_PUSH_FILE = 1, |
| 9679 | INPUT_NOFILE_OK = 2, |
| 9680 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9681 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9682 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9683 | /* values of checkkwd variable */ |
| 9684 | #define CHKALIAS 0x1 |
| 9685 | #define CHKKWD 0x2 |
| 9686 | #define CHKNL 0x4 |
| 9687 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9688 | /* |
| 9689 | * Push a string back onto the input at this current parsefile level. |
| 9690 | * We handle aliases this way. |
| 9691 | */ |
| 9692 | #if !ENABLE_ASH_ALIAS |
| 9693 | #define pushstring(s, ap) pushstring(s) |
| 9694 | #endif |
| 9695 | static void |
| 9696 | pushstring(char *s, struct alias *ap) |
| 9697 | { |
| 9698 | struct strpush *sp; |
| 9699 | int len; |
| 9700 | |
| 9701 | len = strlen(s); |
| 9702 | INT_OFF; |
| 9703 | if (g_parsefile->strpush) { |
| 9704 | sp = ckzalloc(sizeof(*sp)); |
| 9705 | sp->prev = g_parsefile->strpush; |
| 9706 | } else { |
| 9707 | sp = &(g_parsefile->basestrpush); |
| 9708 | } |
| 9709 | g_parsefile->strpush = sp; |
| 9710 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9711 | sp->prev_left_in_line = g_parsefile->left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9712 | sp->unget = g_parsefile->unget; |
| 9713 | memcpy(sp->lastc, g_parsefile->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9714 | #if ENABLE_ASH_ALIAS |
| 9715 | sp->ap = ap; |
| 9716 | if (ap) { |
| 9717 | ap->flag |= ALIASINUSE; |
| 9718 | sp->string = s; |
| 9719 | } |
| 9720 | #endif |
| 9721 | g_parsefile->next_to_pgetc = s; |
| 9722 | g_parsefile->left_in_line = len; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9723 | g_parsefile->unget = 0; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9724 | INT_ON; |
| 9725 | } |
| 9726 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9727 | static void |
| 9728 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9729 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9730 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9731 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9732 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9733 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9734 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9735 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9736 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9737 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9738 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9739 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9740 | if (sp->string != sp->ap->val) { |
| 9741 | free(sp->string); |
| 9742 | } |
| 9743 | sp->ap->flag &= ~ALIASINUSE; |
| 9744 | if (sp->ap->flag & ALIASDEAD) { |
| 9745 | unalias(sp->ap->name); |
| 9746 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9747 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9748 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9749 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9750 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9751 | g_parsefile->unget = sp->unget; |
| 9752 | memcpy(g_parsefile->lastc, sp->lastc, sizeof(sp->lastc)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9753 | g_parsefile->strpush = sp->prev; |
| 9754 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9755 | free(sp); |
| 9756 | INT_ON; |
| 9757 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9758 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9759 | static int |
| 9760 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9761 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9762 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9763 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9764 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9765 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9766 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9767 | retry: |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9768 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 9769 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9770 | else { |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9771 | int timeout = -1; |
| 9772 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 9773 | if (iflag) { |
| 9774 | const char *tmout_var = lookupvar("TMOUT"); |
| 9775 | if (tmout_var) { |
| 9776 | timeout = atoi(tmout_var) * 1000; |
| 9777 | if (timeout <= 0) |
| 9778 | timeout = -1; |
| 9779 | } |
| 9780 | } |
| 9781 | # endif |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 9782 | # if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9783 | line_input_state->path_lookup = pathval(); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 9784 | # endif |
Denys Vlasenko | e9ab07c | 2014-08-13 18:00:08 +0200 | [diff] [blame] | 9785 | reinit_unicode_for_ash(); |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9786 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ, timeout); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9787 | if (nr == 0) { |
| 9788 | /* Ctrl+C pressed */ |
| 9789 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9790 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9791 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9792 | raise(SIGINT); |
| 9793 | return 1; |
| 9794 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9795 | goto retry; |
| 9796 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9797 | if (nr < 0) { |
| 9798 | if (errno == 0) { |
| 9799 | /* Ctrl+D pressed */ |
| 9800 | nr = 0; |
| 9801 | } |
| 9802 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 9803 | else if (errno == EAGAIN && timeout > 0) { |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 9804 | puts("\007timed out waiting for input: auto-logout"); |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9805 | exitshell(); |
| 9806 | } |
| 9807 | # endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9808 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9809 | } |
| 9810 | #else |
Ron Yorston | 61d6ae2 | 2015-04-19 10:50:25 +0100 | [diff] [blame] | 9811 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9812 | #endif |
| 9813 | |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 9814 | #if 0 /* disabled: nonblock_immune_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9815 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9816 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9817 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9818 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9819 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9820 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9821 | out2str("sh: turning off NDELAY mode\n"); |
| 9822 | goto retry; |
| 9823 | } |
| 9824 | } |
| 9825 | } |
| 9826 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9827 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9828 | return nr; |
| 9829 | } |
| 9830 | |
| 9831 | /* |
| 9832 | * Refill the input buffer and return the next input character: |
| 9833 | * |
| 9834 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9835 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9836 | * or we are reading from a string so we can't refill the buffer, |
| 9837 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9838 | * 3) If there is more stuff in this buffer, use it else call read to fill it. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9839 | * 4) Process input up to the next newline, deleting nul characters. |
| 9840 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9841 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9842 | #define pgetc_debug(...) ((void)0) |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9843 | static int pgetc(void); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9844 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9845 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9846 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9847 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9848 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9849 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9850 | if (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9851 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9852 | if (g_parsefile->left_in_line == -1 |
| 9853 | && g_parsefile->strpush->ap |
| 9854 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9855 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9856 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9857 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9858 | return PEOA; |
| 9859 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9860 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9861 | popstring(); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9862 | return pgetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9863 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9864 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9865 | * "pgetc" needs refilling. |
| 9866 | */ |
| 9867 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9868 | /* -90 is our -BIGNUM. Below we use -99 to mark "EOF on read", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9869 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9870 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9871 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9872 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9873 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9874 | /* even in failure keep left_in_line and next_to_pgetc |
| 9875 | * in lock step, for correct multi-layer pungetc. |
| 9876 | * left_in_line was decremented before preadbuffer(), |
| 9877 | * must inc next_to_pgetc: */ |
| 9878 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9879 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9880 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9881 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9882 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9883 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9884 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9885 | again: |
| 9886 | more = preadfd(); |
| 9887 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9888 | /* don't try reading again */ |
| 9889 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9890 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9891 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9892 | return PEOF; |
| 9893 | } |
| 9894 | } |
| 9895 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9896 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9897 | * Set g_parsefile->left_in_line |
| 9898 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9899 | * NUL chars are deleted. |
| 9900 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9901 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9902 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9903 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9904 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9905 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9906 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9907 | c = *q; |
| 9908 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9909 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9910 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9911 | q++; |
| 9912 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9913 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9914 | break; |
| 9915 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9916 | } |
| 9917 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9918 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9919 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9920 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9921 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9922 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9923 | } |
| 9924 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9925 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9926 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9927 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9928 | char save = *q; |
| 9929 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9930 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9931 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9932 | } |
| 9933 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9934 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9935 | g_parsefile->left_in_line, |
| 9936 | g_parsefile->next_to_pgetc, |
| 9937 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9938 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9939 | } |
| 9940 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9941 | static int |
| 9942 | pgetc(void) |
| 9943 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9944 | int c; |
| 9945 | |
| 9946 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9947 | g_parsefile->left_in_line, |
| 9948 | g_parsefile->next_to_pgetc, |
| 9949 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9950 | if (g_parsefile->unget) |
| 9951 | return g_parsefile->lastc[--g_parsefile->unget]; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9952 | |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9953 | if (--g_parsefile->left_in_line >= 0) |
| 9954 | c = (signed char)*g_parsefile->next_to_pgetc++; |
| 9955 | else |
| 9956 | c = preadbuffer(); |
| 9957 | |
| 9958 | g_parsefile->lastc[1] = g_parsefile->lastc[0]; |
| 9959 | g_parsefile->lastc[0] = c; |
| 9960 | |
| 9961 | return c; |
| 9962 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9963 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9964 | #if ENABLE_ASH_ALIAS |
| 9965 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9966 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9967 | { |
| 9968 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9969 | do { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9970 | pgetc_debug("pgetc at %d:%p'%s'", |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9971 | g_parsefile->left_in_line, |
| 9972 | g_parsefile->next_to_pgetc, |
| 9973 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 9974 | c = pgetc(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9975 | } while (c == PEOA); |
| 9976 | return c; |
| 9977 | } |
| 9978 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9979 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9980 | #endif |
| 9981 | |
| 9982 | /* |
| 9983 | * Read a line from the script. |
| 9984 | */ |
| 9985 | static char * |
| 9986 | pfgets(char *line, int len) |
| 9987 | { |
| 9988 | char *p = line; |
| 9989 | int nleft = len; |
| 9990 | int c; |
| 9991 | |
| 9992 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9993 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9994 | if (c == PEOF) { |
| 9995 | if (p == line) |
| 9996 | return NULL; |
| 9997 | break; |
| 9998 | } |
| 9999 | *p++ = c; |
| 10000 | if (c == '\n') |
| 10001 | break; |
| 10002 | } |
| 10003 | *p = '\0'; |
| 10004 | return line; |
| 10005 | } |
| 10006 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10007 | /* |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10008 | * Undo a call to pgetc. Only two characters may be pushed back. |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10009 | * PEOF may be pushed back. |
| 10010 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10011 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10012 | pungetc(void) |
| 10013 | { |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10014 | g_parsefile->unget++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10015 | } |
| 10016 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 10017 | /* This one eats backslash+newline */ |
| 10018 | static int |
| 10019 | pgetc_eatbnl(void) |
| 10020 | { |
| 10021 | int c; |
| 10022 | |
| 10023 | while ((c = pgetc()) == '\\') { |
| 10024 | if (pgetc() != '\n') { |
| 10025 | pungetc(); |
| 10026 | break; |
| 10027 | } |
| 10028 | |
| 10029 | g_parsefile->linno++; |
| 10030 | setprompt_if(doprompt, 2); |
| 10031 | } |
| 10032 | |
| 10033 | return c; |
| 10034 | } |
| 10035 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10036 | /* |
| 10037 | * To handle the "." command, a stack of input files is used. Pushfile |
| 10038 | * adds a new entry to the stack and popfile restores the previous level. |
| 10039 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10040 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10041 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10042 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10043 | struct parsefile *pf; |
| 10044 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10045 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10046 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10047 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10048 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 10049 | /*pf->basestrpush.prev = NULL;*/ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 10050 | /*pf->unget = 0;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10051 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10052 | } |
| 10053 | |
| 10054 | static void |
| 10055 | popfile(void) |
| 10056 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10057 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10058 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10059 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10060 | if (pf->pf_fd >= 0) |
| 10061 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 10062 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10063 | while (pf->strpush) |
| 10064 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10065 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10066 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10067 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10068 | } |
| 10069 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10070 | /* |
| 10071 | * Return to top level. |
| 10072 | */ |
| 10073 | static void |
| 10074 | popallfiles(void) |
| 10075 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10076 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10077 | popfile(); |
| 10078 | } |
| 10079 | |
| 10080 | /* |
| 10081 | * Close the file(s) that the shell is reading commands from. Called |
| 10082 | * after a fork is done. |
| 10083 | */ |
| 10084 | static void |
| 10085 | closescript(void) |
| 10086 | { |
| 10087 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10088 | if (g_parsefile->pf_fd > 0) { |
| 10089 | close(g_parsefile->pf_fd); |
| 10090 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10091 | } |
| 10092 | } |
| 10093 | |
| 10094 | /* |
| 10095 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 10096 | * interrupts off. |
| 10097 | */ |
| 10098 | static void |
| 10099 | setinputfd(int fd, int push) |
| 10100 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 10101 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10102 | if (push) { |
| 10103 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 10104 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10105 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 10106 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10107 | if (g_parsefile->buf == NULL) |
| 10108 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10109 | g_parsefile->left_in_buffer = 0; |
| 10110 | g_parsefile->left_in_line = 0; |
| 10111 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 10112 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10113 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10114 | /* |
| 10115 | * Set the input to take input from a file. If push is set, push the |
| 10116 | * old input onto the stack first. |
| 10117 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10118 | static int |
| 10119 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10120 | { |
| 10121 | int fd; |
| 10122 | int fd2; |
| 10123 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10124 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10125 | fd = open(fname, O_RDONLY); |
| 10126 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10127 | if (flags & INPUT_NOFILE_OK) |
| 10128 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 10129 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10130 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10131 | if (fd < 10) { |
| 10132 | fd2 = copyfd(fd, 10); |
| 10133 | close(fd); |
| 10134 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10135 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10136 | fd = fd2; |
| 10137 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10138 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10139 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10140 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10141 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10142 | } |
| 10143 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10144 | /* |
| 10145 | * Like setinputfile, but takes input from a string. |
| 10146 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10147 | static void |
| 10148 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10149 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10150 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10151 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10152 | g_parsefile->next_to_pgetc = string; |
| 10153 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10154 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10155 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10156 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10157 | } |
| 10158 | |
| 10159 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10160 | /* ============ mail.c |
| 10161 | * |
| 10162 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10163 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10164 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10165 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10166 | |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 10167 | /* Hash of mtimes of mailboxes */ |
| 10168 | static unsigned mailtime_hash; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10169 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10170 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10171 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10172 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10173 | * Print appropriate message(s) if mail has arrived. |
| 10174 | * If mail_var_path_changed is set, |
| 10175 | * then the value of MAIL has mail_var_path_changed, |
| 10176 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10177 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10178 | static void |
| 10179 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10180 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10181 | const char *mpath; |
| 10182 | char *p; |
| 10183 | char *q; |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 10184 | unsigned new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10185 | struct stackmark smark; |
| 10186 | struct stat statb; |
| 10187 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10188 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10189 | mpath = mpathset() ? mpathval() : mailval(); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 10190 | new_hash = 0; |
| 10191 | for (;;) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 10192 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10193 | if (p == NULL) |
| 10194 | break; |
| 10195 | if (*p == '\0') |
| 10196 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10197 | for (q = p; *q; q++) |
| 10198 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 10199 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10200 | if (q[-1] != '/') |
| 10201 | abort(); |
| 10202 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10203 | q[-1] = '\0'; /* delete trailing '/' */ |
| 10204 | if (stat(p, &statb) < 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10205 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10206 | } |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 10207 | /* Very simplistic "hash": just a sum of all mtimes */ |
| 10208 | new_hash += (unsigned)statb.st_mtime; |
| 10209 | } |
| 10210 | if (!mail_var_path_changed && mailtime_hash != new_hash) { |
Denys Vlasenko | 4cd99e7 | 2015-10-09 16:02:53 +0200 | [diff] [blame] | 10211 | if (mailtime_hash != 0) |
| 10212 | out2str("you have mail\n"); |
Denys Vlasenko | 2384162 | 2015-10-09 15:52:03 +0200 | [diff] [blame] | 10213 | mailtime_hash = new_hash; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10214 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10215 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10216 | popstackmark(&smark); |
| 10217 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10218 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10219 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10220 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10221 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10222 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10223 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10224 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10225 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10226 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10227 | |
| 10228 | /* ============ ??? */ |
| 10229 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10230 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10231 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10232 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10233 | static void |
| 10234 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10235 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10236 | char **newparam; |
| 10237 | char **ap; |
| 10238 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10239 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10240 | for (nparam = 0; argv[nparam]; nparam++) |
| 10241 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10242 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 10243 | while (*argv) { |
| 10244 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10245 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10246 | *ap = NULL; |
| 10247 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10248 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10249 | shellparam.nparam = nparam; |
| 10250 | shellparam.p = newparam; |
| 10251 | #if ENABLE_ASH_GETOPTS |
| 10252 | shellparam.optind = 1; |
| 10253 | shellparam.optoff = -1; |
| 10254 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10255 | } |
| 10256 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10257 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10258 | * Process shell options. The global variable argptr contains a pointer |
| 10259 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10260 | * |
| 10261 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 10262 | * For a non-interactive shell, an error condition encountered |
| 10263 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 10264 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 10265 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10266 | * ... |
| 10267 | * Utility syntax error (option or operand error) Shall exit |
| 10268 | * ... |
| 10269 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 10270 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 10271 | * and shell continues), and people rely on this behavior! |
| 10272 | * Testcase: |
| 10273 | * set -o barfoo 2>/dev/null |
| 10274 | * echo $? |
| 10275 | * |
| 10276 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10277 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10278 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10279 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10280 | { |
| 10281 | int i; |
| 10282 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10283 | if (name) { |
| 10284 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10285 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10286 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10287 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10288 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10289 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10290 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10291 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10292 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10293 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10294 | if (val) { |
| 10295 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 10296 | } else { |
| 10297 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 10298 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10299 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10300 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10301 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10302 | static void |
| 10303 | setoption(int flag, int val) |
| 10304 | { |
| 10305 | int i; |
| 10306 | |
| 10307 | for (i = 0; i < NOPTS; i++) { |
| 10308 | if (optletters(i) == flag) { |
| 10309 | optlist[i] = val; |
| 10310 | return; |
| 10311 | } |
| 10312 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10313 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10314 | /* NOTREACHED */ |
| 10315 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10316 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10317 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10318 | { |
| 10319 | char *p; |
| 10320 | int val; |
| 10321 | int c; |
| 10322 | |
| 10323 | if (cmdline) |
| 10324 | minusc = NULL; |
| 10325 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10326 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10327 | if (c != '-' && c != '+') |
| 10328 | break; |
| 10329 | argptr++; |
| 10330 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10331 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10332 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10333 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10334 | if (!cmdline) { |
| 10335 | /* "-" means turn off -x and -v */ |
| 10336 | if (p[0] == '\0') |
| 10337 | xflag = vflag = 0; |
| 10338 | /* "--" means reset params */ |
| 10339 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10340 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10341 | } |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 10342 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10343 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10344 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10345 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10346 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10347 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10348 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10349 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10350 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10351 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10352 | /* it already printed err message */ |
| 10353 | return 1; /* error */ |
| 10354 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10355 | if (*argptr) |
| 10356 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10357 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10358 | isloginsh = 1; |
| 10359 | /* bash does not accept +-login, we also won't */ |
| 10360 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10361 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10362 | isloginsh = 1; |
| 10363 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10364 | } else { |
| 10365 | setoption(c, val); |
| 10366 | } |
| 10367 | } |
| 10368 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10369 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10370 | } |
| 10371 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10372 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10373 | * The shift builtin command. |
| 10374 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10375 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10376 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10377 | { |
| 10378 | int n; |
| 10379 | char **ap1, **ap2; |
| 10380 | |
| 10381 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10382 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10383 | n = number(argv[1]); |
| 10384 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10385 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10386 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10387 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10388 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10389 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10390 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10391 | } |
| 10392 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10393 | while ((*ap2++ = *ap1++) != NULL) |
| 10394 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10395 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10396 | shellparam.optind = 1; |
| 10397 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10398 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10399 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10400 | return 0; |
| 10401 | } |
| 10402 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10403 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10404 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10405 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10406 | * Maybe we could keep them in an ordered balanced binary tree |
| 10407 | * instead of hashed lists. |
| 10408 | * For now just roll 'em through qsort for printing... |
| 10409 | */ |
| 10410 | static int |
| 10411 | showvars(const char *sep_prefix, int on, int off) |
| 10412 | { |
| 10413 | const char *sep; |
| 10414 | char **ep, **epend; |
| 10415 | |
| 10416 | ep = listvars(on, off, &epend); |
| 10417 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10418 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10419 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10420 | |
| 10421 | for (; ep < epend; ep++) { |
| 10422 | const char *p; |
| 10423 | const char *q; |
| 10424 | |
| 10425 | p = strchrnul(*ep, '='); |
| 10426 | q = nullstr; |
| 10427 | if (*p) |
| 10428 | q = single_quote(++p); |
| 10429 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10430 | } |
| 10431 | return 0; |
| 10432 | } |
| 10433 | |
| 10434 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10435 | * The set command builtin. |
| 10436 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10437 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10438 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10439 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10440 | int retval; |
| 10441 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10442 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10443 | return showvars(nullstr, 0, VUNSET); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 10444 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10445 | INT_OFF; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 10446 | retval = options(/*cmdline:*/ 0); |
| 10447 | if (retval == 0) { /* if no parse error... */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10448 | optschanged(); |
| 10449 | if (*argptr != NULL) { |
| 10450 | setparam(argptr); |
| 10451 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10452 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10453 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10454 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10455 | } |
| 10456 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10457 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10458 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10459 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10460 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10461 | uint32_t t; |
| 10462 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10463 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10464 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10465 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10466 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 10467 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10468 | vrandom.flags &= ~VNOFUNC; |
| 10469 | } else { |
| 10470 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10471 | t = strtoul(value, NULL, 10); |
| 10472 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10473 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10474 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10475 | #endif |
| 10476 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10477 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10478 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10479 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10480 | { |
| 10481 | char *p, *q; |
| 10482 | char c = '?'; |
| 10483 | int done = 0; |
| 10484 | int err = 0; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 10485 | char sbuf[2]; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10486 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10487 | |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 10488 | sbuf[1] = '\0'; |
| 10489 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10490 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10491 | return 1; |
| 10492 | optnext = optfirst + *param_optind - 1; |
| 10493 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10494 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10495 | p = NULL; |
| 10496 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10497 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10498 | if (p == NULL || *p == '\0') { |
| 10499 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10500 | p = *optnext; |
| 10501 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10502 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10503 | p = NULL; |
| 10504 | done = 1; |
| 10505 | goto out; |
| 10506 | } |
| 10507 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10508 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10509 | goto atend; |
| 10510 | } |
| 10511 | |
| 10512 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10513 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10514 | if (*q == '\0') { |
| 10515 | if (optstr[0] == ':') { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 10516 | sbuf[0] = c; |
| 10517 | /*sbuf[1] = '\0'; - already is */ |
| 10518 | err |= setvarsafe("OPTARG", sbuf, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10519 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10520 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10521 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10522 | } |
| 10523 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10524 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10525 | } |
| 10526 | if (*++q == ':') |
| 10527 | q++; |
| 10528 | } |
| 10529 | |
| 10530 | if (*++q == ':') { |
| 10531 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10532 | if (optstr[0] == ':') { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 10533 | sbuf[0] = c; |
| 10534 | /*sbuf[1] = '\0'; - already is */ |
| 10535 | err |= setvarsafe("OPTARG", sbuf, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10536 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10537 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10538 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10539 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10540 | c = '?'; |
| 10541 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10542 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10543 | } |
| 10544 | |
| 10545 | if (p == *optnext) |
| 10546 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10547 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10548 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10549 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10550 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10551 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10552 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10553 | *param_optind = optnext - optfirst + 1; |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 10554 | err |= setvarsafe("OPTIND", itoa(*param_optind), VNOFUNC); |
| 10555 | sbuf[0] = c; |
| 10556 | /*sbuf[1] = '\0'; - already is */ |
| 10557 | err |= setvarsafe(optvar, sbuf, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10558 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10559 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10560 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10561 | flush_stdout_stderr(); |
| 10562 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10563 | } |
| 10564 | return done; |
| 10565 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10566 | |
| 10567 | /* |
| 10568 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10569 | * to be processed. Shellparam.optptr points to the next character to |
| 10570 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10571 | * then it's the first time getopts has been called. |
| 10572 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10573 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10574 | getoptscmd(int argc, char **argv) |
| 10575 | { |
| 10576 | char **optbase; |
| 10577 | |
| 10578 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10579 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10580 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10581 | optbase = shellparam.p; |
| 10582 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10583 | shellparam.optind = 1; |
| 10584 | shellparam.optoff = -1; |
| 10585 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10586 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10587 | optbase = &argv[3]; |
| 10588 | if (shellparam.optind > argc - 2) { |
| 10589 | shellparam.optind = 1; |
| 10590 | shellparam.optoff = -1; |
| 10591 | } |
| 10592 | } |
| 10593 | |
| 10594 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10595 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10596 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10597 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10598 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10599 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10600 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10601 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10602 | struct heredoc { |
| 10603 | struct heredoc *next; /* next here document in list */ |
| 10604 | union node *here; /* redirection node */ |
| 10605 | char *eofmark; /* string indicating end of input */ |
| 10606 | smallint striptabs; /* if set, strip leading tabs */ |
| 10607 | }; |
| 10608 | |
| 10609 | static smallint tokpushback; /* last token pushed back */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10610 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10611 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10612 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10613 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10614 | static struct nodelist *backquotelist; |
| 10615 | static union node *redirnode; |
| 10616 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10617 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10618 | static const char * |
| 10619 | tokname(char *buf, int tok) |
| 10620 | { |
| 10621 | if (tok < TSEMI) |
| 10622 | return tokname_array[tok] + 1; |
| 10623 | sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 10624 | return buf; |
| 10625 | } |
| 10626 | |
| 10627 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10628 | * Called when an unexpected token is read during the parse. The argument |
| 10629 | * is the token that is expected, or -1 if more than one type of token can |
| 10630 | * occur at this point. |
| 10631 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10632 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10633 | static void |
| 10634 | raise_error_unexpected_syntax(int token) |
| 10635 | { |
| 10636 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10637 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10638 | int l; |
| 10639 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10640 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10641 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10642 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10643 | raise_error_syntax(msg); |
| 10644 | /* NOTREACHED */ |
| 10645 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10646 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10647 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10648 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10649 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10650 | static union node *andor(void); |
| 10651 | static union node *pipeline(void); |
| 10652 | static union node *parse_command(void); |
| 10653 | static void parseheredoc(void); |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 10654 | static int peektoken(void); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10655 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10656 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10657 | static union node * |
| 10658 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10659 | { |
| 10660 | union node *n1, *n2, *n3; |
| 10661 | int tok; |
| 10662 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10663 | n1 = NULL; |
| 10664 | for (;;) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 10665 | switch (peektoken()) { |
| 10666 | case TNL: |
| 10667 | if (!(nlflag & 1)) |
| 10668 | break; |
| 10669 | parseheredoc(); |
| 10670 | return n1; |
| 10671 | |
| 10672 | case TEOF: |
| 10673 | if (!n1 && (nlflag & 1)) |
| 10674 | n1 = NODE_EOF; |
| 10675 | parseheredoc(); |
| 10676 | return n1; |
| 10677 | } |
| 10678 | |
| 10679 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10680 | if (nlflag == 2 && tokname_array[peektoken()][0]) |
| 10681 | return n1; |
| 10682 | nlflag |= 2; |
| 10683 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10684 | n2 = andor(); |
| 10685 | tok = readtoken(); |
| 10686 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10687 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10688 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10689 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10690 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10691 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10692 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10693 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10694 | n2 = n3; |
| 10695 | } |
| 10696 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10697 | } |
| 10698 | } |
| 10699 | if (n1 == NULL) { |
| 10700 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10701 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10702 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10703 | n3->type = NSEMI; |
| 10704 | n3->nbinary.ch1 = n1; |
| 10705 | n3->nbinary.ch2 = n2; |
| 10706 | n1 = n3; |
| 10707 | } |
| 10708 | switch (tok) { |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 10709 | case TNL: |
| 10710 | case TEOF: |
| 10711 | tokpushback = 1; |
| 10712 | /* fall through */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10713 | case TBACKGND: |
| 10714 | case TSEMI: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10715 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10716 | default: |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 10717 | if ((nlflag & 1)) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10718 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10719 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10720 | return n1; |
| 10721 | } |
| 10722 | } |
| 10723 | } |
| 10724 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10725 | static union node * |
| 10726 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10727 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10728 | union node *n1, *n2, *n3; |
| 10729 | int t; |
| 10730 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10731 | n1 = pipeline(); |
| 10732 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10733 | t = readtoken(); |
| 10734 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10735 | t = NAND; |
| 10736 | } else if (t == TOR) { |
| 10737 | t = NOR; |
| 10738 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10739 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10740 | return n1; |
| 10741 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10742 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10743 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10744 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10745 | n3->type = t; |
| 10746 | n3->nbinary.ch1 = n1; |
| 10747 | n3->nbinary.ch2 = n2; |
| 10748 | n1 = n3; |
| 10749 | } |
| 10750 | } |
| 10751 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10752 | static union node * |
| 10753 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10754 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10755 | union node *n1, *n2, *pipenode; |
| 10756 | struct nodelist *lp, *prev; |
| 10757 | int negate; |
| 10758 | |
| 10759 | negate = 0; |
| 10760 | TRACE(("pipeline: entered\n")); |
| 10761 | if (readtoken() == TNOT) { |
| 10762 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10763 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10764 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10765 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10766 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10767 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10768 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10769 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10770 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10771 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10772 | pipenode->npipe.cmdlist = lp; |
| 10773 | lp->n = n1; |
| 10774 | do { |
| 10775 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10776 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10777 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10778 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10779 | prev->next = lp; |
| 10780 | } while (readtoken() == TPIPE); |
| 10781 | lp->next = NULL; |
| 10782 | n1 = pipenode; |
| 10783 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10784 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10785 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10786 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10787 | n2->type = NNOT; |
| 10788 | n2->nnot.com = n1; |
| 10789 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10790 | } |
| 10791 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10792 | } |
| 10793 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10794 | static union node * |
| 10795 | makename(void) |
| 10796 | { |
| 10797 | union node *n; |
| 10798 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10799 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10800 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10801 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10802 | n->narg.text = wordtext; |
| 10803 | n->narg.backquote = backquotelist; |
| 10804 | return n; |
| 10805 | } |
| 10806 | |
| 10807 | static void |
| 10808 | fixredir(union node *n, const char *text, int err) |
| 10809 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10810 | int fd; |
| 10811 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10812 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10813 | if (!err) |
| 10814 | n->ndup.vname = NULL; |
| 10815 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10816 | fd = bb_strtou(text, NULL, 10); |
| 10817 | if (!errno && fd >= 0) |
| 10818 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10819 | else if (LONE_DASH(text)) |
| 10820 | n->ndup.dupfd = -1; |
| 10821 | else { |
| 10822 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10823 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10824 | n->ndup.vname = makename(); |
| 10825 | } |
| 10826 | } |
| 10827 | |
| 10828 | /* |
| 10829 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10830 | * or backquotes). |
| 10831 | */ |
| 10832 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10833 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10834 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10835 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10836 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10837 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10838 | if (c == CTLQUOTEMARK) |
| 10839 | continue; |
| 10840 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10841 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10842 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10843 | return 0; |
| 10844 | } |
| 10845 | return 1; |
| 10846 | } |
| 10847 | |
| 10848 | static void |
| 10849 | parsefname(void) |
| 10850 | { |
| 10851 | union node *n = redirnode; |
| 10852 | |
| 10853 | if (readtoken() != TWORD) |
| 10854 | raise_error_unexpected_syntax(-1); |
| 10855 | if (n->type == NHERE) { |
| 10856 | struct heredoc *here = heredoc; |
| 10857 | struct heredoc *p; |
| 10858 | int i; |
| 10859 | |
| 10860 | if (quoteflag == 0) |
| 10861 | n->type = NXHERE; |
| 10862 | TRACE(("Here document %d\n", n->type)); |
| 10863 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10864 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10865 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10866 | here->eofmark = wordtext; |
| 10867 | here->next = NULL; |
| 10868 | if (heredoclist == NULL) |
| 10869 | heredoclist = here; |
| 10870 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10871 | for (p = heredoclist; p->next; p = p->next) |
| 10872 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10873 | p->next = here; |
| 10874 | } |
| 10875 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10876 | fixredir(n, wordtext, 0); |
| 10877 | } else { |
| 10878 | n->nfile.fname = makename(); |
| 10879 | } |
| 10880 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10882 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10883 | simplecmd(void) |
| 10884 | { |
| 10885 | union node *args, **app; |
| 10886 | union node *n = NULL; |
| 10887 | union node *vars, **vpp; |
| 10888 | union node **rpp, *redir; |
| 10889 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10890 | #if ENABLE_ASH_BASH_COMPAT |
| 10891 | smallint double_brackets_flag = 0; |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 10892 | smallint function_flag = 0; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10893 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10894 | |
| 10895 | args = NULL; |
| 10896 | app = &args; |
| 10897 | vars = NULL; |
| 10898 | vpp = &vars; |
| 10899 | redir = NULL; |
| 10900 | rpp = &redir; |
| 10901 | |
| 10902 | savecheckkwd = CHKALIAS; |
| 10903 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10904 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10905 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10906 | t = readtoken(); |
| 10907 | switch (t) { |
| 10908 | #if ENABLE_ASH_BASH_COMPAT |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 10909 | case TFUNCTION: |
| 10910 | if (peektoken() != TWORD) |
| 10911 | raise_error_unexpected_syntax(TWORD); |
| 10912 | function_flag = 1; |
| 10913 | break; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10914 | case TAND: /* "&&" */ |
| 10915 | case TOR: /* "||" */ |
| 10916 | if (!double_brackets_flag) { |
| 10917 | tokpushback = 1; |
| 10918 | goto out; |
| 10919 | } |
| 10920 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10921 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10922 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10923 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10924 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10925 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10926 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10927 | #if ENABLE_ASH_BASH_COMPAT |
| 10928 | if (strcmp("[[", wordtext) == 0) |
| 10929 | double_brackets_flag = 1; |
| 10930 | else if (strcmp("]]", wordtext) == 0) |
| 10931 | double_brackets_flag = 0; |
| 10932 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10933 | n->narg.backquote = backquotelist; |
| 10934 | if (savecheckkwd && isassignment(wordtext)) { |
| 10935 | *vpp = n; |
| 10936 | vpp = &n->narg.next; |
| 10937 | } else { |
| 10938 | *app = n; |
| 10939 | app = &n->narg.next; |
| 10940 | savecheckkwd = 0; |
| 10941 | } |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 10942 | #if ENABLE_ASH_BASH_COMPAT |
| 10943 | if (function_flag) { |
| 10944 | checkkwd = CHKNL | CHKKWD; |
| 10945 | switch (peektoken()) { |
| 10946 | case TBEGIN: |
| 10947 | case TIF: |
| 10948 | case TCASE: |
| 10949 | case TUNTIL: |
| 10950 | case TWHILE: |
| 10951 | case TFOR: |
| 10952 | goto do_func; |
| 10953 | case TLP: |
| 10954 | function_flag = 0; |
| 10955 | break; |
| 10956 | case TWORD: |
| 10957 | if (strcmp("[[", wordtext) == 0) |
| 10958 | goto do_func; |
| 10959 | /* fall through */ |
| 10960 | default: |
| 10961 | raise_error_unexpected_syntax(-1); |
| 10962 | } |
| 10963 | } |
| 10964 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10965 | break; |
| 10966 | case TREDIR: |
| 10967 | *rpp = n = redirnode; |
| 10968 | rpp = &n->nfile.next; |
| 10969 | parsefname(); /* read name of redirection file */ |
| 10970 | break; |
| 10971 | case TLP: |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 10972 | IF_ASH_BASH_COMPAT(do_func:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10973 | if (args && app == &args->narg.next |
| 10974 | && !vars && !redir |
| 10975 | ) { |
| 10976 | struct builtincmd *bcmd; |
| 10977 | const char *name; |
| 10978 | |
| 10979 | /* We have a function */ |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 10980 | if (IF_ASH_BASH_COMPAT(!function_flag &&) readtoken() != TRP) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10981 | raise_error_unexpected_syntax(TRP); |
| 10982 | name = n->narg.text; |
| 10983 | if (!goodname(name) |
| 10984 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10985 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10986 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10987 | } |
| 10988 | n->type = NDEFUN; |
| 10989 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10990 | n->narg.next = parse_command(); |
| 10991 | return n; |
| 10992 | } |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 10993 | IF_ASH_BASH_COMPAT(function_flag = 0;) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10994 | /* fall through */ |
| 10995 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10996 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10997 | goto out; |
| 10998 | } |
| 10999 | } |
| 11000 | out: |
| 11001 | *app = NULL; |
| 11002 | *vpp = NULL; |
| 11003 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11004 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11005 | n->type = NCMD; |
| 11006 | n->ncmd.args = args; |
| 11007 | n->ncmd.assign = vars; |
| 11008 | n->ncmd.redirect = redir; |
| 11009 | return n; |
| 11010 | } |
| 11011 | |
| 11012 | static union node * |
| 11013 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11014 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11015 | union node *n1, *n2; |
| 11016 | union node *ap, **app; |
| 11017 | union node *cp, **cpp; |
| 11018 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11019 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11020 | int t; |
| 11021 | |
| 11022 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11023 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 11024 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11025 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11026 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11027 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11028 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11029 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11030 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11031 | n1->type = NIF; |
| 11032 | n1->nif.test = list(0); |
| 11033 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11034 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11035 | n1->nif.ifpart = list(0); |
| 11036 | n2 = n1; |
| 11037 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11038 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11039 | n2 = n2->nif.elsepart; |
| 11040 | n2->type = NIF; |
| 11041 | n2->nif.test = list(0); |
| 11042 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11043 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11044 | n2->nif.ifpart = list(0); |
| 11045 | } |
| 11046 | if (lasttoken == TELSE) |
| 11047 | n2->nif.elsepart = list(0); |
| 11048 | else { |
| 11049 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11050 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11051 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11052 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11053 | break; |
| 11054 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11055 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11056 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11057 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11058 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11059 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11060 | got = readtoken(); |
| 11061 | if (got != TDO) { |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11062 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got] + 1, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11063 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11064 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11065 | } |
| 11066 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11067 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11068 | break; |
| 11069 | } |
| 11070 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 11071 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11072 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11073 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11074 | n1->type = NFOR; |
| 11075 | n1->nfor.var = wordtext; |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 11076 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11077 | if (readtoken() == TIN) { |
| 11078 | app = ≈ |
| 11079 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11080 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11081 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11082 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11083 | n2->narg.text = wordtext; |
| 11084 | n2->narg.backquote = backquotelist; |
| 11085 | *app = n2; |
| 11086 | app = &n2->narg.next; |
| 11087 | } |
| 11088 | *app = NULL; |
| 11089 | n1->nfor.args = ap; |
| 11090 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11091 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11092 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11093 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11094 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11095 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11096 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11097 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11098 | n1->nfor.args = n2; |
| 11099 | /* |
| 11100 | * Newline or semicolon here is optional (but note |
| 11101 | * that the original Bourne shell only allowed NL). |
| 11102 | */ |
Ron Yorston | ab80e01 | 2015-08-03 13:46:00 +0100 | [diff] [blame] | 11103 | if (lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11104 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11105 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11106 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11107 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11108 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11110 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11111 | break; |
| 11112 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11113 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11114 | n1->type = NCASE; |
| 11115 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11116 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11117 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11118 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11119 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11120 | n2->narg.text = wordtext; |
| 11121 | n2->narg.backquote = backquotelist; |
Ron Yorston | 383b885 | 2015-08-03 13:46:25 +0100 | [diff] [blame] | 11122 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 11123 | if (readtoken() != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11124 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11125 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11126 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11127 | checkkwd = CHKNL | CHKKWD; |
| 11128 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11129 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11130 | if (lasttoken == TLP) |
| 11131 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11132 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11133 | cp->type = NCLIST; |
| 11134 | app = &cp->nclist.pattern; |
| 11135 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11136 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11137 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11138 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11139 | ap->narg.text = wordtext; |
| 11140 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11141 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11142 | break; |
| 11143 | app = &ap->narg.next; |
| 11144 | readtoken(); |
| 11145 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11146 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11147 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11148 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11149 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11150 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11151 | cpp = &cp->nclist.next; |
| 11152 | |
| 11153 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11154 | t = readtoken(); |
| 11155 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11156 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11157 | raise_error_unexpected_syntax(TENDCASE); |
| 11158 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11159 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11160 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11161 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11162 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11163 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11164 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11165 | n1->type = NSUBSHELL; |
| 11166 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11167 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11168 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11169 | break; |
| 11170 | case TBEGIN: |
| 11171 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11172 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11173 | break; |
Ron Yorston | 95ebcf7 | 2015-11-03 09:42:23 +0000 | [diff] [blame] | 11174 | IF_ASH_BASH_COMPAT(case TFUNCTION:) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11175 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11176 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11177 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11178 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11179 | } |
| 11180 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11181 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11182 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11183 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11184 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11185 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11186 | checkkwd = CHKKWD | CHKALIAS; |
| 11187 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11188 | while (readtoken() == TREDIR) { |
| 11189 | *rpp = n2 = redirnode; |
| 11190 | rpp = &n2->nfile.next; |
| 11191 | parsefname(); |
| 11192 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11193 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11194 | *rpp = NULL; |
| 11195 | if (redir) { |
| 11196 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11197 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11198 | n2->type = NREDIR; |
| 11199 | n2->nredir.n = n1; |
| 11200 | n1 = n2; |
| 11201 | } |
| 11202 | n1->nredir.redirect = redir; |
| 11203 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11204 | return n1; |
| 11205 | } |
| 11206 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11207 | #if ENABLE_ASH_BASH_COMPAT |
| 11208 | static int decode_dollar_squote(void) |
| 11209 | { |
| 11210 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 11211 | int c, cnt; |
| 11212 | char *p; |
| 11213 | char buf[4]; |
| 11214 | |
| 11215 | c = pgetc(); |
| 11216 | p = strchr(C_escapes, c); |
| 11217 | if (p) { |
| 11218 | buf[0] = c; |
| 11219 | p = buf; |
| 11220 | cnt = 3; |
| 11221 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 11222 | do { |
| 11223 | c = pgetc(); |
| 11224 | *++p = c; |
| 11225 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 11226 | pungetc(); |
| 11227 | } else if (c == 'x') { /* \xHH */ |
| 11228 | do { |
| 11229 | c = pgetc(); |
| 11230 | *++p = c; |
| 11231 | } while (isxdigit(c) && --cnt); |
| 11232 | pungetc(); |
| 11233 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 11234 | c = 'x'; |
| 11235 | goto unrecognized; |
| 11236 | } |
| 11237 | } else { /* simple seq like \\ or \t */ |
| 11238 | p++; |
| 11239 | } |
| 11240 | *p = '\0'; |
| 11241 | p = buf; |
| 11242 | c = bb_process_escape_sequence((void*)&p); |
| 11243 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 11244 | if (c != '\'' && c != '"') { |
| 11245 | unrecognized: |
| 11246 | c |= 0x100; /* "please encode \, then me" */ |
| 11247 | } |
| 11248 | } |
| 11249 | return c; |
| 11250 | } |
| 11251 | #endif |
| 11252 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11253 | /* |
| 11254 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 11255 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 11256 | * word which marks the end of the document and striptabs is true if |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11257 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11258 | * is the first character of the input token or document. |
| 11259 | * |
| 11260 | * Because C does not have internal subroutines, I have simulated them |
| 11261 | * using goto's to implement the subroutine linkage. The following macros |
| 11262 | * will run code that appears at the end of readtoken1. |
| 11263 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11264 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 11265 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 11266 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 11267 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 11268 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 11269 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11270 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11271 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 11272 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11273 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11274 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11275 | char *out; |
Denys Vlasenko | 50e6d42 | 2016-09-30 11:35:54 +0200 | [diff] [blame] | 11276 | size_t len; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11277 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11278 | struct nodelist *bqlist; |
| 11279 | smallint quotef; |
| 11280 | smallint dblquote; |
| 11281 | smallint oldstyle; |
| 11282 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11283 | #if ENABLE_ASH_EXPAND_PRMT |
| 11284 | smallint pssyntax; /* we are expanding a prompt string */ |
| 11285 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11286 | int varnest; /* levels of variables expansion */ |
| 11287 | int arinest; /* levels of arithmetic expansion */ |
| 11288 | int parenlevel; /* levels of parens in arithmetic */ |
| 11289 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 11290 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11291 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11292 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11293 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11294 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11295 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11296 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11297 | #if ENABLE_ASH_EXPAND_PRMT |
| 11298 | pssyntax = (syntax == PSSYNTAX); |
| 11299 | if (pssyntax) |
| 11300 | syntax = DQSYNTAX; |
| 11301 | #endif |
| 11302 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11303 | varnest = 0; |
| 11304 | arinest = 0; |
| 11305 | parenlevel = 0; |
| 11306 | dqvarnest = 0; |
| 11307 | |
| 11308 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11309 | loop: |
| 11310 | /* For each line, until end of word */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11311 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 11312 | for (;;) { /* until end of line or end of word */ |
| 11313 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
| 11314 | switch (SIT(c, syntax)) { |
| 11315 | case CNL: /* '\n' */ |
| 11316 | if (syntax == BASESYNTAX) |
| 11317 | goto endword; /* exit outer loop */ |
| 11318 | USTPUTC(c, out); |
| 11319 | g_parsefile->linno++; |
| 11320 | setprompt_if(doprompt, 2); |
| 11321 | c = pgetc(); |
| 11322 | goto loop; /* continue outer loop */ |
| 11323 | case CWORD: |
| 11324 | USTPUTC(c, out); |
| 11325 | break; |
| 11326 | case CCTL: |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11327 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11328 | if (c == '\\' && bash_dollar_squote) { |
| 11329 | c = decode_dollar_squote(); |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 11330 | if (c == '\0') { |
| 11331 | /* skip $'\000', $'\x00' (like bash) */ |
| 11332 | break; |
| 11333 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11334 | if (c & 0x100) { |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 11335 | /* Unknown escape. Encode as '\z' */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11336 | c = (unsigned char)c; |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 11337 | if (eofmark == NULL || dblquote) |
| 11338 | USTPUTC(CTLESC, out); |
| 11339 | USTPUTC('\\', out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11340 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11341 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11342 | #endif |
Denys Vlasenko | 13f2091 | 2016-09-25 20:54:25 +0200 | [diff] [blame] | 11343 | if (eofmark == NULL || dblquote) |
| 11344 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11345 | USTPUTC(c, out); |
| 11346 | break; |
| 11347 | case CBACK: /* backslash */ |
| 11348 | c = pgetc_without_PEOA(); |
| 11349 | if (c == PEOF) { |
| 11350 | USTPUTC(CTLESC, out); |
| 11351 | USTPUTC('\\', out); |
| 11352 | pungetc(); |
| 11353 | } else if (c == '\n') { |
| 11354 | setprompt_if(doprompt, 2); |
| 11355 | } else { |
| 11356 | #if ENABLE_ASH_EXPAND_PRMT |
| 11357 | if (c == '$' && pssyntax) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11358 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11359 | USTPUTC('\\', out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11360 | } |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11361 | #endif |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11362 | /* Backslash is retained if we are in "str" and next char isn't special */ |
| 11363 | if (dblquote |
| 11364 | && c != '\\' |
| 11365 | && c != '`' |
| 11366 | && c != '$' |
| 11367 | && (c != '"' || eofmark != NULL) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11368 | ) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11369 | USTPUTC('\\', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11370 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 11371 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 0ff78a0 | 2010-08-30 15:20:07 +0200 | [diff] [blame] | 11372 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11373 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11374 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11375 | break; |
| 11376 | case CSQUOTE: |
| 11377 | syntax = SQSYNTAX; |
| 11378 | quotemark: |
| 11379 | if (eofmark == NULL) { |
| 11380 | USTPUTC(CTLQUOTEMARK, out); |
| 11381 | } |
| 11382 | break; |
| 11383 | case CDQUOTE: |
| 11384 | syntax = DQSYNTAX; |
| 11385 | dblquote = 1; |
| 11386 | goto quotemark; |
| 11387 | case CENDQUOTE: |
| 11388 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Ron Yorston | 7e4ed26 | 2015-05-18 09:54:43 +0200 | [diff] [blame] | 11389 | if (eofmark != NULL && varnest == 0) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11390 | USTPUTC(c, out); |
| 11391 | } else { |
| 11392 | if (dqvarnest == 0) { |
| 11393 | syntax = BASESYNTAX; |
| 11394 | dblquote = 0; |
| 11395 | } |
| 11396 | quotef = 1; |
| 11397 | goto quotemark; |
| 11398 | } |
| 11399 | break; |
| 11400 | case CVAR: /* '$' */ |
| 11401 | PARSESUB(); /* parse substitution */ |
| 11402 | break; |
| 11403 | case CENDVAR: /* '}' */ |
| 11404 | if (varnest > 0) { |
| 11405 | varnest--; |
| 11406 | if (dqvarnest > 0) { |
| 11407 | dqvarnest--; |
| 11408 | } |
| 11409 | c = CTLENDVAR; |
| 11410 | } |
| 11411 | USTPUTC(c, out); |
| 11412 | break; |
| 11413 | #if ENABLE_SH_MATH_SUPPORT |
| 11414 | case CLP: /* '(' in arithmetic */ |
| 11415 | parenlevel++; |
| 11416 | USTPUTC(c, out); |
| 11417 | break; |
| 11418 | case CRP: /* ')' in arithmetic */ |
| 11419 | if (parenlevel > 0) { |
| 11420 | parenlevel--; |
| 11421 | } else { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 11422 | if (pgetc_eatbnl() == ')') { |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 11423 | c = CTLENDARI; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11424 | if (--arinest == 0) { |
| 11425 | syntax = prevsyntax; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11426 | } |
| 11427 | } else { |
| 11428 | /* |
| 11429 | * unbalanced parens |
| 11430 | * (don't 2nd guess - no error) |
| 11431 | */ |
| 11432 | pungetc(); |
| 11433 | } |
| 11434 | } |
| 11435 | USTPUTC(c, out); |
| 11436 | break; |
| 11437 | #endif |
| 11438 | case CBQUOTE: /* '`' */ |
| 11439 | PARSEBACKQOLD(); |
| 11440 | break; |
| 11441 | case CENDFILE: |
| 11442 | goto endword; /* exit outer loop */ |
| 11443 | case CIGN: |
| 11444 | break; |
| 11445 | default: |
| 11446 | if (varnest == 0) { |
| 11447 | #if ENABLE_ASH_BASH_COMPAT |
| 11448 | if (c == '&') { |
Denys Vlasenko | 459293b | 2016-09-29 17:58:58 +0200 | [diff] [blame] | 11449 | //Can't call pgetc_eatbnl() here, this requires three-deep pungetc() |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11450 | if (pgetc() == '>') |
| 11451 | c = 0x100 + '>'; /* flag &> */ |
| 11452 | pungetc(); |
| 11453 | } |
| 11454 | #endif |
| 11455 | goto endword; /* exit outer loop */ |
| 11456 | } |
| 11457 | IF_ASH_ALIAS(if (c != PEOA)) |
| 11458 | USTPUTC(c, out); |
| 11459 | } |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11460 | c = pgetc(); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11461 | } /* for (;;) */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11462 | endword: |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11463 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11464 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11465 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11466 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11467 | #endif |
Ron Yorston | 0e056f7 | 2015-07-01 16:45:40 +0100 | [diff] [blame] | 11468 | if (syntax != BASESYNTAX && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11469 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11470 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11471 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11472 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11473 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11474 | } |
| 11475 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11476 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11477 | out = stackblock(); |
| 11478 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11479 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11480 | && quotef == 0 |
| 11481 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11482 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11483 | PARSEREDIR(); /* passed as params: out, c */ |
| 11484 | lasttoken = TREDIR; |
| 11485 | return lasttoken; |
| 11486 | } |
| 11487 | /* else: non-number X seen, interpret it |
| 11488 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11489 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11490 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11491 | } |
| 11492 | quoteflag = quotef; |
| 11493 | backquotelist = bqlist; |
| 11494 | grabstackblock(len); |
| 11495 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11496 | lasttoken = TWORD; |
| 11497 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11498 | /* end of readtoken routine */ |
| 11499 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11500 | /* |
| 11501 | * Check to see whether we are at the end of the here document. When this |
| 11502 | * is called, c is set to the first character of the next input line. If |
| 11503 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11504 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11505 | checkend: { |
| 11506 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11507 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11508 | if (c == PEOA) |
| 11509 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11510 | #endif |
| 11511 | if (striptabs) { |
| 11512 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11513 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11514 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11515 | } |
| 11516 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11517 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11518 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11519 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11520 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11521 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11522 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11523 | if (*p == '\n' && *q == '\0') { |
| 11524 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11525 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11526 | needprompt = doprompt; |
| 11527 | } else { |
| 11528 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11529 | } |
| 11530 | } |
| 11531 | } |
| 11532 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11533 | goto checkend_return; |
| 11534 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11535 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11536 | /* |
| 11537 | * Parse a redirection operator. The variable "out" points to a string |
| 11538 | * specifying the fd to be redirected. The variable "c" contains the |
| 11539 | * first character of the redirection operator. |
| 11540 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11541 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11542 | /* out is already checked to be a valid number or "" */ |
| 11543 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11544 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11545 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11546 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11547 | if (c == '>') { |
| 11548 | np->nfile.fd = 1; |
| 11549 | c = pgetc(); |
| 11550 | if (c == '>') |
| 11551 | np->type = NAPPEND; |
| 11552 | else if (c == '|') |
| 11553 | np->type = NCLOBBER; |
| 11554 | else if (c == '&') |
| 11555 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11556 | /* it also can be NTO2 (>&file), but we can't figure it out yet */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11557 | else { |
| 11558 | np->type = NTO; |
| 11559 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11560 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11561 | } |
| 11562 | #if ENABLE_ASH_BASH_COMPAT |
| 11563 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11564 | np->nfile.fd = 1; |
| 11565 | pgetc(); /* this is '>', no need to check */ |
| 11566 | np->type = NTO2; |
| 11567 | } |
| 11568 | #endif |
| 11569 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11570 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11571 | c = pgetc(); |
| 11572 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11573 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11574 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11575 | np = stzalloc(sizeof(struct nhere)); |
| 11576 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11577 | } |
| 11578 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11579 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11580 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11581 | c = pgetc(); |
| 11582 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11583 | heredoc->striptabs = 1; |
| 11584 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11585 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11586 | pungetc(); |
| 11587 | } |
| 11588 | break; |
| 11589 | |
| 11590 | case '&': |
| 11591 | np->type = NFROMFD; |
| 11592 | break; |
| 11593 | |
| 11594 | case '>': |
| 11595 | np->type = NFROMTO; |
| 11596 | break; |
| 11597 | |
| 11598 | default: |
| 11599 | np->type = NFROM; |
| 11600 | pungetc(); |
| 11601 | break; |
| 11602 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11603 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11604 | if (fd >= 0) |
| 11605 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11606 | redirnode = np; |
| 11607 | goto parseredir_return; |
| 11608 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11609 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11610 | /* |
| 11611 | * Parse a substitution. At this point, we have read the dollar sign |
| 11612 | * and nothing else. |
| 11613 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11614 | |
| 11615 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11616 | * (assuming ascii char codes, as the original implementation did) */ |
| 11617 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11618 | (((unsigned)(c) - 33 < 32) \ |
| 11619 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11620 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11621 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11622 | int typeloc; |
| 11623 | int flags; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11624 | |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11625 | c = pgetc_eatbnl(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11626 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11627 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11628 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11629 | #if ENABLE_ASH_BASH_COMPAT |
Ron Yorston | 84ba50c | 2016-04-03 22:43:14 +0100 | [diff] [blame] | 11630 | if (syntax != DQSYNTAX && c == '\'') |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11631 | bash_dollar_squote = 1; |
| 11632 | else |
| 11633 | #endif |
| 11634 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11635 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11636 | } else if (c == '(') { |
| 11637 | /* $(command) or $((arith)) */ |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11638 | if (pgetc_eatbnl() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11639 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11640 | PARSEARITH(); |
| 11641 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11642 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11643 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11644 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11645 | pungetc(); |
| 11646 | PARSEBACKQNEW(); |
| 11647 | } |
| 11648 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11649 | /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11650 | USTPUTC(CTLVAR, out); |
| 11651 | typeloc = out - (char *)stackblock(); |
| 11652 | USTPUTC(VSNORMAL, out); |
| 11653 | subtype = VSNORMAL; |
| 11654 | if (c == '{') { |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11655 | c = pgetc_eatbnl(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11656 | if (c == '#') { |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11657 | c = pgetc_eatbnl(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11658 | if (c == '}') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11659 | c = '#'; /* ${#} - same as $# */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11660 | else |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11661 | subtype = VSLENGTH; /* ${#VAR} */ |
| 11662 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11663 | subtype = 0; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11664 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11665 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11666 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11667 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11668 | do { |
| 11669 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11670 | c = pgetc_eatbnl(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11671 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11672 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11673 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11674 | do { |
| 11675 | STPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11676 | c = pgetc_eatbnl(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11677 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11678 | } else if (is_special(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11679 | /* $[{[#]]<specialchar>[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11680 | USTPUTC(c, out); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11681 | c = pgetc_eatbnl(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11682 | } else { |
| 11683 | badsub: |
| 11684 | raise_error_syntax("bad substitution"); |
| 11685 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11686 | if (c != '}' && subtype == VSLENGTH) { |
| 11687 | /* ${#VAR didn't end with } */ |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11688 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11689 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11690 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11691 | STPUTC('=', out); |
| 11692 | flags = 0; |
| 11693 | if (subtype == 0) { |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 11694 | static const char types[] ALIGN1 = "}-+?="; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11695 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 11696 | /* c == first char after VAR */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11697 | switch (c) { |
| 11698 | case ':': |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11699 | c = pgetc_eatbnl(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11700 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | f8ddbe1 | 2016-07-25 03:56:00 +0200 | [diff] [blame] | 11701 | /* This check is only needed to not misinterpret |
| 11702 | * ${VAR:-WORD}, ${VAR:+WORD}, ${VAR:=WORD}, ${VAR:?WORD} |
| 11703 | * constructs. |
| 11704 | */ |
| 11705 | if (!strchr(types, c)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11706 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11707 | pungetc(); |
| 11708 | break; /* "goto do_pungetc" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11709 | } |
| 11710 | #endif |
| 11711 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11712 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11713 | default: { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11714 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11715 | if (p == NULL) |
| 11716 | goto badsub; |
| 11717 | subtype = p - types + VSNORMAL; |
| 11718 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11719 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11720 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11721 | case '#': { |
| 11722 | int cc = c; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11723 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11724 | c = pgetc_eatbnl(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11725 | if (c != cc) |
| 11726 | goto do_pungetc; |
| 11727 | subtype++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11728 | break; |
| 11729 | } |
| 11730 | #if ENABLE_ASH_BASH_COMPAT |
| 11731 | case '/': |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 11732 | /* ${v/[/]pattern/repl} */ |
| 11733 | //TODO: encode pattern and repl separately. |
| 11734 | // Currently ${v/$var_with_slash/repl} is horribly broken |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11735 | subtype = VSREPLACE; |
Denys Vlasenko | 73c3e07 | 2016-09-29 17:17:04 +0200 | [diff] [blame] | 11736 | c = pgetc_eatbnl(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11737 | if (c != '/') |
| 11738 | goto do_pungetc; |
| 11739 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11740 | break; |
| 11741 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11742 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11743 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11744 | do_pungetc: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11745 | pungetc(); |
| 11746 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11747 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11748 | if (subtype != VSNORMAL) { |
| 11749 | varnest++; |
Ron Yorston | 7e4ed26 | 2015-05-18 09:54:43 +0200 | [diff] [blame] | 11750 | if (dblquote) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11751 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11752 | } |
| 11753 | } |
| 11754 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11755 | goto parsesub_return; |
| 11756 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11757 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11758 | /* |
| 11759 | * Called to parse command substitutions. Newstyle is set if the command |
| 11760 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11761 | * list of commands (passed by reference), and savelen is the number of |
| 11762 | * characters on the top of the stack which must be preserved. |
| 11763 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11764 | parsebackq: { |
| 11765 | struct nodelist **nlpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11766 | union node *n; |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 11767 | char *str; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11768 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11769 | smallint saveprompt = 0; |
| 11770 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11771 | str = NULL; |
| 11772 | savelen = out - (char *)stackblock(); |
| 11773 | if (savelen > 0) { |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 11774 | /* |
| 11775 | * FIXME: this can allocate very large block on stack and SEGV. |
| 11776 | * Example: |
| 11777 | * echo "..<100kbytes>..`true` $(true) `true` ..." |
Denys Vlasenko | 7373759 | 2016-09-17 20:58:22 +0200 | [diff] [blame] | 11778 | * allocates 100kb for every command subst. With about |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 11779 | * a hundred command substitutions stack overflows. |
| 11780 | * With larger prepended string, SEGV happens sooner. |
| 11781 | */ |
Ron Yorston | 072fc60 | 2015-07-01 16:46:18 +0100 | [diff] [blame] | 11782 | str = alloca(savelen); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11783 | memcpy(str, stackblock(), savelen); |
| 11784 | } |
Denys Vlasenko | 7bc3d39 | 2016-09-17 20:53:47 +0200 | [diff] [blame] | 11785 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11786 | if (oldstyle) { |
| 11787 | /* We must read until the closing backquote, giving special |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 11788 | * treatment to some slashes, and then push the string and |
| 11789 | * reread it as input, interpreting it normally. |
| 11790 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11791 | char *pout; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11792 | size_t psavelen; |
| 11793 | char *pstr; |
| 11794 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11795 | STARTSTACKSTR(pout); |
| 11796 | for (;;) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11797 | int pc; |
| 11798 | |
| 11799 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11800 | pc = pgetc(); |
| 11801 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11802 | case '`': |
| 11803 | goto done; |
| 11804 | |
| 11805 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11806 | pc = pgetc(); |
| 11807 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11808 | g_parsefile->linno++; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11809 | setprompt_if(doprompt, 2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11810 | /* |
| 11811 | * If eating a newline, avoid putting |
| 11812 | * the newline into the new character |
| 11813 | * stream (via the STPUTC after the |
| 11814 | * switch). |
| 11815 | */ |
| 11816 | continue; |
| 11817 | } |
| 11818 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11819 | && (!dblquote || pc != '"') |
| 11820 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11821 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11822 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11823 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11824 | break; |
| 11825 | } |
| 11826 | /* fall through */ |
| 11827 | |
| 11828 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11829 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11830 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11831 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11832 | |
| 11833 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11834 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11835 | needprompt = doprompt; |
| 11836 | break; |
| 11837 | |
| 11838 | default: |
| 11839 | break; |
| 11840 | } |
| 11841 | STPUTC(pc, pout); |
| 11842 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11843 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11844 | STPUTC('\0', pout); |
| 11845 | psavelen = pout - (char *)stackblock(); |
| 11846 | if (psavelen > 0) { |
| 11847 | pstr = grabstackstr(pout); |
| 11848 | setinputstring(pstr); |
| 11849 | } |
| 11850 | } |
| 11851 | nlpp = &bqlist; |
| 11852 | while (*nlpp) |
| 11853 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11854 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11855 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11856 | |
| 11857 | if (oldstyle) { |
| 11858 | saveprompt = doprompt; |
| 11859 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11860 | } |
| 11861 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11862 | n = list(2); |
| 11863 | |
| 11864 | if (oldstyle) |
| 11865 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11866 | else if (readtoken() != TRP) |
| 11867 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11868 | |
| 11869 | (*nlpp)->n = n; |
| 11870 | if (oldstyle) { |
| 11871 | /* |
| 11872 | * Start reading from old file again, ignoring any pushed back |
| 11873 | * tokens left from the backquote parsing |
| 11874 | */ |
| 11875 | popfile(); |
| 11876 | tokpushback = 0; |
| 11877 | } |
| 11878 | while (stackblocksize() <= savelen) |
| 11879 | growstackblock(); |
| 11880 | STARTSTACKSTR(out); |
| 11881 | if (str) { |
| 11882 | memcpy(out, str, savelen); |
| 11883 | STADJUST(savelen, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11884 | } |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 11885 | USTPUTC(CTLBACKQ, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11886 | if (oldstyle) |
| 11887 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11888 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11889 | } |
| 11890 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11891 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11892 | /* |
| 11893 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11894 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11895 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11896 | if (++arinest == 1) { |
| 11897 | prevsyntax = syntax; |
| 11898 | syntax = ARISYNTAX; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11899 | } |
Ron Yorston | ad88bde | 2015-05-18 09:56:16 +0200 | [diff] [blame] | 11900 | USTPUTC(CTLARI, out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11901 | goto parsearith_return; |
| 11902 | } |
| 11903 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11904 | } /* end of readtoken */ |
| 11905 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11906 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11907 | * Read the next input token. |
| 11908 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11909 | * backquotes. We set quoteflag to true if any part of the word was |
| 11910 | * quoted. |
| 11911 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11912 | * the redirection. |
| 11913 | * In all cases, the variable startlinno is set to the number of the line |
| 11914 | * on which the token starts. |
| 11915 | * |
| 11916 | * [Change comment: here documents and internal procedures] |
| 11917 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11918 | * word parsing code into a separate routine. In this case, readtoken |
| 11919 | * doesn't need to have any internal procedures, but parseword does. |
| 11920 | * We could also make parseoperator in essence the main routine, and |
| 11921 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11922 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11923 | #define NEW_xxreadtoken |
| 11924 | #ifdef NEW_xxreadtoken |
| 11925 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11926 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11927 | '\n', '(', ')', /* singles */ |
| 11928 | '&', '|', ';', /* doubles */ |
| 11929 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11930 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11931 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11932 | #define xxreadtoken_singles 3 |
| 11933 | #define xxreadtoken_doubles 3 |
| 11934 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11935 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11936 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11937 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11938 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11939 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11940 | }; |
| 11941 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11942 | static int |
| 11943 | xxreadtoken(void) |
| 11944 | { |
| 11945 | int c; |
| 11946 | |
| 11947 | if (tokpushback) { |
| 11948 | tokpushback = 0; |
| 11949 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11950 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11951 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11952 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11953 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 11954 | c = pgetc(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11955 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11956 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11957 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11958 | if (c == '#') { |
| 11959 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11960 | continue; |
| 11961 | pungetc(); |
| 11962 | } else if (c == '\\') { |
| 11963 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11964 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11965 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11966 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11967 | startlinno = ++g_parsefile->linno; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11968 | setprompt_if(doprompt, 2); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11969 | } else { |
| 11970 | const char *p; |
| 11971 | |
| 11972 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11973 | if (c != PEOF) { |
| 11974 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11975 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11976 | needprompt = doprompt; |
| 11977 | } |
| 11978 | |
| 11979 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11980 | if (p == NULL) |
| 11981 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11982 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11983 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11984 | int cc = pgetc(); |
| 11985 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11986 | p += xxreadtoken_doubles + 1; |
| 11987 | } else { |
| 11988 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11989 | #if ENABLE_ASH_BASH_COMPAT |
| 11990 | if (c == '&' && cc == '>') /* &> */ |
| 11991 | break; /* return readtoken1(...) */ |
| 11992 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11993 | } |
| 11994 | } |
| 11995 | } |
| 11996 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11997 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11998 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11999 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 12000 | |
| 12001 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12002 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12003 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12004 | #define RETURN(token) return lasttoken = token |
| 12005 | static int |
| 12006 | xxreadtoken(void) |
| 12007 | { |
| 12008 | int c; |
| 12009 | |
| 12010 | if (tokpushback) { |
| 12011 | tokpushback = 0; |
| 12012 | return lasttoken; |
| 12013 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12014 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12015 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12016 | for (;;) { /* until token or start of word found */ |
Denys Vlasenko | 3b4d04b | 2016-09-29 02:11:19 +0200 | [diff] [blame] | 12017 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12018 | switch (c) { |
| 12019 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 12020 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12021 | continue; |
| 12022 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 12023 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 12024 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12025 | pungetc(); |
| 12026 | continue; |
| 12027 | case '\\': |
| 12028 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12029 | startlinno = ++g_parsefile->linno; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12030 | setprompt_if(doprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12031 | continue; |
| 12032 | } |
| 12033 | pungetc(); |
| 12034 | goto breakloop; |
| 12035 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12036 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12037 | needprompt = doprompt; |
| 12038 | RETURN(TNL); |
| 12039 | case PEOF: |
| 12040 | RETURN(TEOF); |
| 12041 | case '&': |
| 12042 | if (pgetc() == '&') |
| 12043 | RETURN(TAND); |
| 12044 | pungetc(); |
| 12045 | RETURN(TBACKGND); |
| 12046 | case '|': |
| 12047 | if (pgetc() == '|') |
| 12048 | RETURN(TOR); |
| 12049 | pungetc(); |
| 12050 | RETURN(TPIPE); |
| 12051 | case ';': |
| 12052 | if (pgetc() == ';') |
| 12053 | RETURN(TENDCASE); |
| 12054 | pungetc(); |
| 12055 | RETURN(TSEMI); |
| 12056 | case '(': |
| 12057 | RETURN(TLP); |
| 12058 | case ')': |
| 12059 | RETURN(TRP); |
| 12060 | default: |
| 12061 | goto breakloop; |
| 12062 | } |
| 12063 | } |
| 12064 | breakloop: |
| 12065 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 12066 | #undef RETURN |
| 12067 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 12068 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12069 | |
| 12070 | static int |
| 12071 | readtoken(void) |
| 12072 | { |
| 12073 | int t; |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 12074 | int kwd = checkkwd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12075 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12076 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12077 | #endif |
| 12078 | |
| 12079 | #if ENABLE_ASH_ALIAS |
| 12080 | top: |
| 12081 | #endif |
| 12082 | |
| 12083 | t = xxreadtoken(); |
| 12084 | |
| 12085 | /* |
| 12086 | * eat newlines |
| 12087 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 12088 | if (kwd & CHKNL) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12089 | while (t == TNL) { |
| 12090 | parseheredoc(); |
| 12091 | t = xxreadtoken(); |
| 12092 | } |
| 12093 | } |
| 12094 | |
| 12095 | if (t != TWORD || quoteflag) { |
| 12096 | goto out; |
| 12097 | } |
| 12098 | |
| 12099 | /* |
| 12100 | * check for keywords |
| 12101 | */ |
Ron Yorston | 713f07d | 2015-10-29 16:44:56 +0000 | [diff] [blame] | 12102 | if (kwd & CHKKWD) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12103 | const char *const *pp; |
| 12104 | |
| 12105 | pp = findkwd(wordtext); |
| 12106 | if (pp) { |
| 12107 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 12108 | TRACE(("keyword '%s' recognized\n", tokname_array[t] + 1)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12109 | goto out; |
| 12110 | } |
| 12111 | } |
| 12112 | |
| 12113 | if (checkkwd & CHKALIAS) { |
| 12114 | #if ENABLE_ASH_ALIAS |
| 12115 | struct alias *ap; |
| 12116 | ap = lookupalias(wordtext, 1); |
| 12117 | if (ap != NULL) { |
| 12118 | if (*ap->val) { |
| 12119 | pushstring(ap->val, ap); |
| 12120 | } |
| 12121 | goto top; |
| 12122 | } |
| 12123 | #endif |
| 12124 | } |
| 12125 | out: |
| 12126 | checkkwd = 0; |
| 12127 | #if DEBUG |
| 12128 | if (!alreadyseen) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 12129 | TRACE(("token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12130 | else |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 12131 | TRACE(("reread token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12132 | #endif |
| 12133 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12134 | } |
| 12135 | |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 12136 | static int |
Ron Yorston | 6bd2fab | 2015-10-29 11:30:22 +0000 | [diff] [blame] | 12137 | peektoken(void) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12138 | { |
| 12139 | int t; |
| 12140 | |
| 12141 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12142 | tokpushback = 1; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 12143 | return t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12144 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12145 | |
| 12146 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12147 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 12148 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12149 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12150 | static union node * |
| 12151 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12152 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12153 | tokpushback = 0; |
Ron Yorston | c0e0076 | 2015-10-29 11:30:55 +0000 | [diff] [blame] | 12154 | checkkwd = 0; |
| 12155 | heredoclist = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12156 | doprompt = interact; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12157 | setprompt_if(doprompt, doprompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12158 | needprompt = 0; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12159 | return list(1); |
| 12160 | } |
| 12161 | |
| 12162 | /* |
| 12163 | * Input any here documents. |
| 12164 | */ |
| 12165 | static void |
| 12166 | parseheredoc(void) |
| 12167 | { |
| 12168 | struct heredoc *here; |
| 12169 | union node *n; |
| 12170 | |
| 12171 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12172 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12173 | |
| 12174 | while (here) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12175 | setprompt_if(needprompt, 2); |
| 12176 | readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12177 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12178 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12179 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12180 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12181 | n->narg.text = wordtext; |
| 12182 | n->narg.backquote = backquotelist; |
| 12183 | here->here->nhere.doc = n; |
| 12184 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12185 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12186 | } |
| 12187 | |
| 12188 | |
| 12189 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12190 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12191 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12192 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12193 | static const char * |
| 12194 | expandstr(const char *ps) |
| 12195 | { |
| 12196 | union node n; |
| 12197 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12198 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 12199 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12200 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12201 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12202 | popfile(); |
| 12203 | |
| 12204 | n.narg.type = NARG; |
| 12205 | n.narg.next = NULL; |
| 12206 | n.narg.text = wordtext; |
| 12207 | n.narg.backquote = backquotelist; |
| 12208 | |
Ron Yorston | 549deab | 2015-05-18 09:57:51 +0200 | [diff] [blame] | 12209 | expandarg(&n, NULL, EXP_QUOTED); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12210 | return stackblock(); |
| 12211 | } |
| 12212 | #endif |
| 12213 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12214 | /* |
| 12215 | * Execute a command or commands contained in a string. |
| 12216 | */ |
| 12217 | static int |
| 12218 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12219 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12220 | union node *n; |
| 12221 | struct stackmark smark; |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12222 | int status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12223 | |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 12224 | s = sstrdup(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12225 | setinputstring(s); |
| 12226 | setstackmark(&smark); |
| 12227 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12228 | status = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12229 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 12230 | int i; |
| 12231 | |
| 12232 | i = evaltree(n, 0); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12233 | if (n) |
| 12234 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12235 | popstackmark(&smark); |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12236 | if (evalskip) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12237 | break; |
| 12238 | } |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 12239 | popstackmark(&smark); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12240 | popfile(); |
Denys Vlasenko | 8e2bc47 | 2016-09-28 23:02:57 +0200 | [diff] [blame] | 12241 | stunalloc(s); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12242 | |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12243 | evalskip &= mask; |
| 12244 | return status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12245 | } |
| 12246 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12247 | /* |
| 12248 | * The eval command. |
| 12249 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12250 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12251 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12252 | { |
| 12253 | char *p; |
| 12254 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12255 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12256 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12257 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12258 | argv += 2; |
| 12259 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12260 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12261 | for (;;) { |
| 12262 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12263 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12264 | if (p == NULL) |
| 12265 | break; |
| 12266 | STPUTC(' ', concat); |
| 12267 | } |
| 12268 | STPUTC('\0', concat); |
| 12269 | p = grabstackstr(concat); |
| 12270 | } |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12271 | return evalstring(p, ~SKIPEVAL); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12272 | } |
Denys Vlasenko | 928e2a7 | 2016-09-29 00:30:31 +0200 | [diff] [blame] | 12273 | return 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12274 | } |
| 12275 | |
| 12276 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 12277 | * Read and execute commands. |
| 12278 | * "Top" is nonzero for the top level command loop; |
| 12279 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12280 | */ |
| 12281 | static int |
| 12282 | cmdloop(int top) |
| 12283 | { |
| 12284 | union node *n; |
| 12285 | struct stackmark smark; |
| 12286 | int inter; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 12287 | int status = 0; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12288 | int numeof = 0; |
| 12289 | |
| 12290 | TRACE(("cmdloop(%d) called\n", top)); |
| 12291 | for (;;) { |
| 12292 | int skip; |
| 12293 | |
| 12294 | setstackmark(&smark); |
| 12295 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 12296 | if (doing_jobctl) |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 12297 | showjobs(SHOW_CHANGED|SHOW_STDERR); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12298 | #endif |
| 12299 | inter = 0; |
| 12300 | if (iflag && top) { |
| 12301 | inter++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12302 | chkmail(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12303 | } |
| 12304 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 12305 | #if DEBUG |
| 12306 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 12307 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 12308 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12309 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12310 | if (!top || numeof >= 50) |
| 12311 | break; |
| 12312 | if (!stoppedjobs()) { |
| 12313 | if (!Iflag) |
| 12314 | break; |
| 12315 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 12316 | } |
| 12317 | numeof++; |
| 12318 | } else if (nflag == 0) { |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 12319 | int i; |
| 12320 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 12321 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 12322 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12323 | numeof = 0; |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 12324 | i = evaltree(n, 0); |
| 12325 | if (n) |
| 12326 | status = i; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12327 | } |
| 12328 | popstackmark(&smark); |
| 12329 | skip = evalskip; |
| 12330 | |
| 12331 | if (skip) { |
Denys Vlasenko | 6a0710e | 2016-09-30 14:18:34 +0200 | [diff] [blame^] | 12332 | evalskip &= ~SKIPFUNC; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12333 | return skip & SKIPEVAL; |
| 12334 | } |
| 12335 | } |
Denys Vlasenko | eb17b6f | 2016-09-28 19:41:57 +0200 | [diff] [blame] | 12336 | return status; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12337 | } |
| 12338 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12339 | /* |
| 12340 | * Take commands from a file. To be compatible we should do a path |
| 12341 | * search for the file, which is necessary to find sub-commands. |
| 12342 | */ |
| 12343 | static char * |
| 12344 | find_dot_file(char *name) |
| 12345 | { |
| 12346 | char *fullname; |
| 12347 | const char *path = pathval(); |
| 12348 | struct stat statb; |
| 12349 | |
| 12350 | /* don't try this for absolute or relative paths */ |
| 12351 | if (strchr(name, '/')) |
| 12352 | return name; |
| 12353 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12354 | /* IIRC standards do not say whether . is to be searched. |
| 12355 | * And it is even smaller this way, making it unconditional for now: |
| 12356 | */ |
| 12357 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12358 | fullname = name; |
| 12359 | goto try_cur_dir; |
| 12360 | } |
| 12361 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12362 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12363 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12364 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12365 | /* |
| 12366 | * Don't bother freeing here, since it will |
| 12367 | * be freed by the caller. |
| 12368 | */ |
| 12369 | return fullname; |
| 12370 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12371 | if (fullname != name) |
| 12372 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12373 | } |
| 12374 | |
| 12375 | /* not found in the PATH */ |
| 12376 | ash_msg_and_raise_error("%s: not found", name); |
| 12377 | /* NOTREACHED */ |
| 12378 | } |
| 12379 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12380 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12381 | dotcmd(int argc, char **argv) |
| 12382 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12383 | char *fullname; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12384 | struct strlist *sp; |
| 12385 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12386 | |
| 12387 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12388 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12389 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12390 | if (!argv[1]) { |
| 12391 | /* bash says: "bash: .: filename argument required" */ |
| 12392 | return 2; /* bash compat */ |
| 12393 | } |
| 12394 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12395 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 12396 | exitstatus = 0; |
| 12397 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 12398 | /* This aborts if file isn't found, which is POSIXly correct. |
| 12399 | * bash returns exitcode 1 instead. |
| 12400 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12401 | fullname = find_dot_file(argv[1]); |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12402 | argv += 2; |
| 12403 | argc -= 2; |
| 12404 | if (argc) { /* argc > 0, argv[0] != NULL */ |
| 12405 | saveparam = shellparam; |
| 12406 | shellparam.malloced = 0; |
| 12407 | shellparam.nparam = argc; |
| 12408 | shellparam.p = argv; |
| 12409 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12410 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 12411 | /* This aborts if file can't be opened, which is POSIXly correct. |
| 12412 | * bash returns exitcode 1 instead. |
| 12413 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12414 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12415 | commandname = fullname; |
| 12416 | cmdloop(0); |
| 12417 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12418 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12419 | if (argc) { |
| 12420 | freeparam(&shellparam); |
| 12421 | shellparam = saveparam; |
| 12422 | }; |
| 12423 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12424 | return exitstatus; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12425 | } |
| 12426 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12427 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12428 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12429 | { |
| 12430 | if (stoppedjobs()) |
| 12431 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12432 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12433 | exitstatus = number(argv[1]); |
| 12434 | raise_exception(EXEXIT); |
| 12435 | /* NOTREACHED */ |
| 12436 | } |
| 12437 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12438 | /* |
| 12439 | * Read a file containing shell functions. |
| 12440 | */ |
| 12441 | static void |
| 12442 | readcmdfile(char *name) |
| 12443 | { |
| 12444 | setinputfile(name, INPUT_PUSH_FILE); |
| 12445 | cmdloop(0); |
| 12446 | popfile(); |
| 12447 | } |
| 12448 | |
| 12449 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12450 | /* ============ find_command inplementation */ |
| 12451 | |
| 12452 | /* |
| 12453 | * Resolve a command name. If you change this routine, you may have to |
| 12454 | * change the shellexec routine as well. |
| 12455 | */ |
| 12456 | static void |
| 12457 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12458 | { |
| 12459 | struct tblentry *cmdp; |
| 12460 | int idx; |
| 12461 | int prev; |
| 12462 | char *fullname; |
| 12463 | struct stat statb; |
| 12464 | int e; |
| 12465 | int updatetbl; |
| 12466 | struct builtincmd *bcmd; |
| 12467 | |
| 12468 | /* If name contains a slash, don't use PATH or hash table */ |
| 12469 | if (strchr(name, '/') != NULL) { |
| 12470 | entry->u.index = -1; |
| 12471 | if (act & DO_ABS) { |
| 12472 | while (stat(name, &statb) < 0) { |
| 12473 | #ifdef SYSV |
| 12474 | if (errno == EINTR) |
| 12475 | continue; |
| 12476 | #endif |
| 12477 | entry->cmdtype = CMDUNKNOWN; |
| 12478 | return; |
| 12479 | } |
| 12480 | } |
| 12481 | entry->cmdtype = CMDNORMAL; |
| 12482 | return; |
| 12483 | } |
| 12484 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12485 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12486 | |
| 12487 | updatetbl = (path == pathval()); |
| 12488 | if (!updatetbl) { |
| 12489 | act |= DO_ALTPATH; |
| 12490 | if (strstr(path, "%builtin") != NULL) |
| 12491 | act |= DO_ALTBLTIN; |
| 12492 | } |
| 12493 | |
| 12494 | /* If name is in the table, check answer will be ok */ |
| 12495 | cmdp = cmdlookup(name, 0); |
| 12496 | if (cmdp != NULL) { |
| 12497 | int bit; |
| 12498 | |
| 12499 | switch (cmdp->cmdtype) { |
| 12500 | default: |
| 12501 | #if DEBUG |
| 12502 | abort(); |
| 12503 | #endif |
| 12504 | case CMDNORMAL: |
| 12505 | bit = DO_ALTPATH; |
| 12506 | break; |
| 12507 | case CMDFUNCTION: |
| 12508 | bit = DO_NOFUNC; |
| 12509 | break; |
| 12510 | case CMDBUILTIN: |
| 12511 | bit = DO_ALTBLTIN; |
| 12512 | break; |
| 12513 | } |
| 12514 | if (act & bit) { |
| 12515 | updatetbl = 0; |
| 12516 | cmdp = NULL; |
| 12517 | } else if (cmdp->rehash == 0) |
| 12518 | /* if not invalidated by cd, we're done */ |
| 12519 | goto success; |
| 12520 | } |
| 12521 | |
| 12522 | /* If %builtin not in path, check for builtin next */ |
| 12523 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12524 | if (bcmd) { |
| 12525 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12526 | goto builtin_success; |
| 12527 | if (act & DO_ALTPATH) { |
| 12528 | if (!(act & DO_ALTBLTIN)) |
| 12529 | goto builtin_success; |
| 12530 | } else if (builtinloc <= 0) { |
| 12531 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12532 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12533 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12534 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12535 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12536 | { |
| 12537 | int applet_no = find_applet_by_name(name); |
| 12538 | if (applet_no >= 0) { |
| 12539 | entry->cmdtype = CMDNORMAL; |
| 12540 | entry->u.index = -2 - applet_no; |
| 12541 | return; |
| 12542 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12543 | } |
| 12544 | #endif |
| 12545 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12546 | /* We have to search path. */ |
| 12547 | prev = -1; /* where to start */ |
| 12548 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12549 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12550 | prev = builtinloc; |
| 12551 | else |
| 12552 | prev = cmdp->param.index; |
| 12553 | } |
| 12554 | |
| 12555 | e = ENOENT; |
| 12556 | idx = -1; |
| 12557 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12558 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12559 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12560 | /* NB: code below will still use fullname |
| 12561 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12562 | idx++; |
| 12563 | if (pathopt) { |
| 12564 | if (prefix(pathopt, "builtin")) { |
| 12565 | if (bcmd) |
| 12566 | goto builtin_success; |
| 12567 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12568 | } |
| 12569 | if ((act & DO_NOFUNC) |
| 12570 | || !prefix(pathopt, "func") |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 12571 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12572 | continue; |
| 12573 | } |
| 12574 | } |
| 12575 | /* if rehash, don't redo absolute path names */ |
| 12576 | if (fullname[0] == '/' && idx <= prev) { |
| 12577 | if (idx < prev) |
| 12578 | continue; |
| 12579 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12580 | goto success; |
| 12581 | } |
| 12582 | while (stat(fullname, &statb) < 0) { |
| 12583 | #ifdef SYSV |
| 12584 | if (errno == EINTR) |
| 12585 | continue; |
| 12586 | #endif |
| 12587 | if (errno != ENOENT && errno != ENOTDIR) |
| 12588 | e = errno; |
| 12589 | goto loop; |
| 12590 | } |
| 12591 | e = EACCES; /* if we fail, this will be the error */ |
| 12592 | if (!S_ISREG(statb.st_mode)) |
| 12593 | continue; |
| 12594 | if (pathopt) { /* this is a %func directory */ |
| 12595 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12596 | /* NB: stalloc will return space pointed by fullname |
| 12597 | * (because we don't have any intervening allocations |
| 12598 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12599 | readcmdfile(fullname); |
| 12600 | cmdp = cmdlookup(name, 0); |
| 12601 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12602 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12603 | stunalloc(fullname); |
| 12604 | goto success; |
| 12605 | } |
| 12606 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12607 | if (!updatetbl) { |
| 12608 | entry->cmdtype = CMDNORMAL; |
| 12609 | entry->u.index = idx; |
| 12610 | return; |
| 12611 | } |
| 12612 | INT_OFF; |
| 12613 | cmdp = cmdlookup(name, 1); |
| 12614 | cmdp->cmdtype = CMDNORMAL; |
| 12615 | cmdp->param.index = idx; |
| 12616 | INT_ON; |
| 12617 | goto success; |
| 12618 | } |
| 12619 | |
| 12620 | /* We failed. If there was an entry for this command, delete it */ |
| 12621 | if (cmdp && updatetbl) |
| 12622 | delete_cmd_entry(); |
| 12623 | if (act & DO_ERR) |
| 12624 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12625 | entry->cmdtype = CMDUNKNOWN; |
| 12626 | return; |
| 12627 | |
| 12628 | builtin_success: |
| 12629 | if (!updatetbl) { |
| 12630 | entry->cmdtype = CMDBUILTIN; |
| 12631 | entry->u.cmd = bcmd; |
| 12632 | return; |
| 12633 | } |
| 12634 | INT_OFF; |
| 12635 | cmdp = cmdlookup(name, 1); |
| 12636 | cmdp->cmdtype = CMDBUILTIN; |
| 12637 | cmdp->param.cmd = bcmd; |
| 12638 | INT_ON; |
| 12639 | success: |
| 12640 | cmdp->rehash = 0; |
| 12641 | entry->cmdtype = cmdp->cmdtype; |
| 12642 | entry->u = cmdp->param; |
| 12643 | } |
| 12644 | |
| 12645 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12646 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12647 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12648 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12649 | * The trap builtin. |
| 12650 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12651 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12652 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12653 | { |
| 12654 | char *action; |
| 12655 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12656 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12657 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12658 | nextopt(nullstr); |
| 12659 | ap = argptr; |
| 12660 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12661 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12662 | char *tr = trap_ptr[signo]; |
| 12663 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12664 | /* note: bash adds "SIG", but only if invoked |
| 12665 | * as "bash". If called as "sh", or if set -o posix, |
| 12666 | * then it prints short signal names. |
| 12667 | * We are printing short names: */ |
| 12668 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12669 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12670 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12671 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12672 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12673 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12674 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12675 | } |
| 12676 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12677 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12678 | if (trap_ptr != trap) { |
| 12679 | free(trap_ptr); |
| 12680 | trap_ptr = trap; |
| 12681 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12682 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12683 | return 0; |
| 12684 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12685 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12686 | action = NULL; |
| 12687 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12688 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12689 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12690 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12691 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12692 | if (signo < 0) { |
| 12693 | /* Mimic bash message exactly */ |
| 12694 | ash_msg("%s: invalid signal specification", *ap); |
| 12695 | exitcode = 1; |
| 12696 | goto next; |
| 12697 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12698 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12699 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12700 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12701 | action = NULL; |
| 12702 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12703 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12704 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12705 | free(trap[signo]); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 12706 | if (action) |
| 12707 | may_have_traps = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12708 | trap[signo] = action; |
| 12709 | if (signo != 0) |
| 12710 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12711 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12712 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12713 | ap++; |
| 12714 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12715 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12716 | } |
| 12717 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12718 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12719 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12720 | |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 12721 | #if ENABLE_ASH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12722 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12723 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12724 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12725 | unsigned col; |
| 12726 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12727 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12728 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12729 | "Built-in commands:\n" |
| 12730 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12731 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12732 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12733 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12734 | if (col > 60) { |
| 12735 | out1fmt("\n"); |
| 12736 | col = 0; |
| 12737 | } |
| 12738 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 12739 | # if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12740 | { |
| 12741 | const char *a = applet_names; |
| 12742 | while (*a) { |
| 12743 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12744 | if (col > 60) { |
| 12745 | out1fmt("\n"); |
| 12746 | col = 0; |
| 12747 | } |
Ron Yorston | 2b91958 | 2016-04-08 11:57:20 +0100 | [diff] [blame] | 12748 | while (*a++ != '\0') |
| 12749 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12750 | } |
| 12751 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 12752 | # endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12753 | out1fmt("\n\n"); |
| 12754 | return EXIT_SUCCESS; |
| 12755 | } |
Denys Vlasenko | 2ec3496 | 2014-09-08 16:52:39 +0200 | [diff] [blame] | 12756 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12757 | |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 12758 | #if MAX_HISTORY |
| 12759 | static int FAST_FUNC |
| 12760 | historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 12761 | { |
| 12762 | show_history(line_input_state); |
| 12763 | return EXIT_SUCCESS; |
| 12764 | } |
| 12765 | #endif |
| 12766 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12767 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12768 | * The export and readonly commands. |
| 12769 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12770 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12771 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12772 | { |
| 12773 | struct var *vp; |
| 12774 | char *name; |
| 12775 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12776 | char **aptr; |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12777 | char opt; |
| 12778 | int flag; |
| 12779 | int flag_off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12780 | |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12781 | /* "readonly" in bash accepts, but ignores -n. |
| 12782 | * We do the same: it saves a conditional in nextopt's param. |
| 12783 | */ |
| 12784 | flag_off = 0; |
| 12785 | while ((opt = nextopt("np")) != '\0') { |
| 12786 | if (opt == 'n') |
| 12787 | flag_off = VEXPORT; |
| 12788 | } |
| 12789 | flag = VEXPORT; |
| 12790 | if (argv[0][0] == 'r') { |
| 12791 | flag = VREADONLY; |
| 12792 | flag_off = 0; /* readonly ignores -n */ |
| 12793 | } |
| 12794 | flag_off = ~flag_off; |
| 12795 | |
| 12796 | /*if (opt_p_not_specified) - bash doesnt check this. Try "export -p NAME" */ |
| 12797 | { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12798 | aptr = argptr; |
| 12799 | name = *aptr; |
| 12800 | if (name) { |
| 12801 | do { |
| 12802 | p = strchr(name, '='); |
| 12803 | if (p != NULL) { |
| 12804 | p++; |
| 12805 | } else { |
| 12806 | vp = *findvar(hashvar(name), name); |
| 12807 | if (vp) { |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12808 | vp->flags = ((vp->flags | flag) & flag_off); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12809 | continue; |
| 12810 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12811 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12812 | setvar(name, p, (flag & flag_off)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12813 | } while ((name = *++aptr) != NULL); |
| 12814 | return 0; |
| 12815 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12816 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12817 | |
| 12818 | /* No arguments. Show the list of exported or readonly vars. |
| 12819 | * -n is ignored. |
| 12820 | */ |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12821 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12822 | return 0; |
| 12823 | } |
| 12824 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12825 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12826 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12827 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12828 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12829 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12830 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12831 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12832 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12833 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12834 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12835 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12836 | } |
| 12837 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12838 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12839 | * The unset builtin command. We unset the function before we unset the |
| 12840 | * variable to allow a function to be unset when there is a readonly variable |
| 12841 | * with the same name. |
| 12842 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12843 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12844 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12845 | { |
| 12846 | char **ap; |
| 12847 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12848 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12849 | int ret = 0; |
| 12850 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12851 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12852 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12853 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12854 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12855 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12856 | if (flag != 'f') { |
| 12857 | i = unsetvar(*ap); |
| 12858 | ret |= i; |
| 12859 | if (!(i & 2)) |
| 12860 | continue; |
| 12861 | } |
| 12862 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12863 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12864 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12865 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12866 | } |
| 12867 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12868 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12869 | ' ', offsetof(struct tms, tms_utime), |
| 12870 | '\n', offsetof(struct tms, tms_stime), |
| 12871 | ' ', offsetof(struct tms, tms_cutime), |
| 12872 | '\n', offsetof(struct tms, tms_cstime), |
| 12873 | 0 |
| 12874 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12875 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12876 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12877 | { |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12878 | unsigned long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12879 | const unsigned char *p; |
| 12880 | struct tms buf; |
| 12881 | |
Bartosz Golaszewski | 5d2e409 | 2014-06-22 14:01:13 +0200 | [diff] [blame] | 12882 | clk_tck = bb_clk_tck(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12883 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12884 | |
| 12885 | p = timescmd_str; |
| 12886 | do { |
| 12887 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12888 | s = t / clk_tck; |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12889 | t = t % clk_tck; |
| 12890 | out1fmt("%lum%lu.%03lus%c", |
| 12891 | s / 60, s % 60, |
| 12892 | (t * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12893 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12894 | p += 2; |
| 12895 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12896 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12897 | return 0; |
| 12898 | } |
| 12899 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12900 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12901 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12902 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12903 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12904 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12905 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12906 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12907 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12908 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12909 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12910 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12911 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12912 | argv++; |
| 12913 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12914 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12915 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12916 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12917 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12918 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12919 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12920 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12921 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12922 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12923 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12924 | * The read builtin. Options: |
| 12925 | * -r Do not interpret '\' specially |
| 12926 | * -s Turn off echo (tty only) |
| 12927 | * -n NCHARS Read NCHARS max |
| 12928 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12929 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12930 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12931 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12932 | * TODO: bash also has: |
| 12933 | * -a ARRAY Read into array[0],[1],etc |
| 12934 | * -d DELIM End on DELIM char, not newline |
| 12935 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12936 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12937 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12938 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12939 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12940 | char *opt_n = NULL; |
| 12941 | char *opt_p = NULL; |
| 12942 | char *opt_t = NULL; |
| 12943 | char *opt_u = NULL; |
| 12944 | int read_flags = 0; |
| 12945 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12946 | int i; |
| 12947 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12948 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12949 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12950 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12951 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12952 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12953 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12954 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12955 | break; |
| 12956 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12957 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12958 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12959 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12960 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12961 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12962 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12963 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12964 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12965 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12966 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12967 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12968 | default: |
| 12969 | break; |
| 12970 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12971 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12972 | |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 12973 | /* "read -s" needs to save/restore termios, can't allow ^C |
| 12974 | * to jump out of it. |
| 12975 | */ |
| 12976 | INT_OFF; |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 12977 | r = shell_builtin_read(setvar0, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12978 | argptr, |
| 12979 | bltinlookup("IFS"), /* can be NULL */ |
| 12980 | read_flags, |
| 12981 | opt_n, |
| 12982 | opt_p, |
| 12983 | opt_t, |
| 12984 | opt_u |
| 12985 | ); |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 12986 | INT_ON; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12987 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12988 | if ((uintptr_t)r > 1) |
| 12989 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12990 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12991 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12992 | } |
| 12993 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12994 | static int FAST_FUNC |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 12995 | umaskcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12996 | { |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 12997 | static const char permuser[3] ALIGN1 = "ogu"; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12998 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12999 | mode_t mask; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13000 | int symbolic_mode = 0; |
| 13001 | |
| 13002 | while (nextopt("S") != '\0') { |
| 13003 | symbolic_mode = 1; |
| 13004 | } |
| 13005 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13006 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13007 | mask = umask(0); |
| 13008 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 13009 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13010 | |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 13011 | if (*argptr == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13012 | if (symbolic_mode) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 13013 | char buf[sizeof(",u=rwx,g=rwx,o=rwx")]; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13014 | char *p = buf; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 13015 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13016 | |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 13017 | i = 2; |
| 13018 | for (;;) { |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 13019 | *p++ = ','; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13020 | *p++ = permuser[i]; |
| 13021 | *p++ = '='; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 13022 | /* mask is 0..0uuugggooo. i=2 selects uuu bits */ |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 13023 | if (!(mask & 0400)) *p++ = 'r'; |
| 13024 | if (!(mask & 0200)) *p++ = 'w'; |
| 13025 | if (!(mask & 0100)) *p++ = 'x'; |
| 13026 | mask <<= 3; |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 13027 | if (--i < 0) |
| 13028 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13029 | } |
Denys Vlasenko | c1e2e00 | 2015-10-07 17:32:56 +0200 | [diff] [blame] | 13030 | *p = '\0'; |
Denys Vlasenko | 005c492 | 2015-10-10 20:17:12 +0200 | [diff] [blame] | 13031 | puts(buf + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13032 | } else { |
Denys Vlasenko | ec046f7 | 2015-10-07 17:57:53 +0200 | [diff] [blame] | 13033 | out1fmt("%04o\n", mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13034 | } |
| 13035 | } else { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 13036 | char *modestr = *argptr; |
| 13037 | /* numeric umasks are taken as-is */ |
| 13038 | /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */ |
| 13039 | if (!isdigit(modestr[0])) |
| 13040 | mask ^= 0777; |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 13041 | mask = bb_parse_mode(modestr, mask); |
| 13042 | if ((unsigned)mask > 0777) { |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 13043 | ash_msg_and_raise_error("illegal mode: %s", modestr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13044 | } |
Denys Vlasenko | 6283f98 | 2015-10-07 16:56:20 +0200 | [diff] [blame] | 13045 | if (!isdigit(modestr[0])) |
| 13046 | mask ^= 0777; |
| 13047 | umask(mask); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13048 | } |
| 13049 | return 0; |
| 13050 | } |
| 13051 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 13052 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 13053 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13054 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 13055 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 13056 | } |
| 13057 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13058 | /* ============ main() and helpers */ |
| 13059 | |
| 13060 | /* |
| 13061 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13062 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13063 | static void |
| 13064 | exitshell(void) |
| 13065 | { |
| 13066 | struct jmploc loc; |
| 13067 | char *p; |
| 13068 | int status; |
| 13069 | |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 13070 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 13071 | save_history(line_input_state); |
| 13072 | #endif |
| 13073 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13074 | status = exitstatus; |
| 13075 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 13076 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13077 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13078 | /* dash bug: it just does _exit(exitstatus) here |
| 13079 | * but we have to do setjobctl(0) first! |
| 13080 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 13081 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 13082 | status = exitstatus; |
| 13083 | goto out; |
| 13084 | } |
| 13085 | exception_handler = &loc; |
| 13086 | p = trap[0]; |
| 13087 | if (p) { |
| 13088 | trap[0] = NULL; |
| 13089 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 13090 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13091 | } |
| 13092 | flush_stdout_stderr(); |
| 13093 | out: |
| 13094 | setjobctl(0); |
| 13095 | _exit(status); |
| 13096 | /* NOTREACHED */ |
| 13097 | } |
| 13098 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 13099 | static void |
| 13100 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13101 | { |
| 13102 | /* from input.c: */ |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 13103 | /* we will never free this */ |
| 13104 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13105 | |
| 13106 | /* from trap.c: */ |
| 13107 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 13108 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 13109 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 13110 | */ |
Denys Vlasenko | cacb2cd | 2010-10-05 00:13:02 +0200 | [diff] [blame] | 13111 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13112 | |
| 13113 | /* from var.c: */ |
| 13114 | { |
| 13115 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13116 | const char *p; |
| 13117 | struct stat st1, st2; |
| 13118 | |
| 13119 | initvar(); |
| 13120 | for (envp = environ; envp && *envp; envp++) { |
Denys Vlasenko | b6838b5 | 2016-09-30 11:33:47 +0200 | [diff] [blame] | 13121 | p = endofname(*envp); |
| 13122 | if (p != *envp && *p == '=') { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13123 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 13124 | } |
| 13125 | } |
| 13126 | |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 13127 | setvar0("PPID", utoa(getppid())); |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 13128 | #if ENABLE_ASH_BASH_COMPAT |
| 13129 | p = lookupvar("SHLVL"); |
Denys Vlasenko | 5680e98 | 2014-01-07 16:12:48 +0100 | [diff] [blame] | 13130 | setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 13131 | if (!lookupvar("HOSTNAME")) { |
| 13132 | struct utsname uts; |
| 13133 | uname(&uts); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 13134 | setvar0("HOSTNAME", uts.nodename); |
Denys Vlasenko | 3fa97af | 2014-04-15 11:43:29 +0200 | [diff] [blame] | 13135 | } |
Bernhard Reutner-Fischer | 80f8cdf | 2013-11-08 14:25:24 +0100 | [diff] [blame] | 13136 | #endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13137 | p = lookupvar("PWD"); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13138 | if (p) { |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 13139 | if (p[0] != '/' || stat(p, &st1) || stat(".", &st2) |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13140 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino |
| 13141 | ) { |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 13142 | p = NULL; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13143 | } |
| 13144 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13145 | setpwd(p, 0); |
| 13146 | } |
| 13147 | } |
| 13148 | |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13149 | |
| 13150 | //usage:#define ash_trivial_usage |
Denys Vlasenko | 6b6af53 | 2011-03-08 10:24:17 +0100 | [diff] [blame] | 13151 | //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13152 | //usage:#define ash_full_usage "\n\n" |
| 13153 | //usage: "Unix shell interpreter" |
| 13154 | |
| 13155 | //usage:#if ENABLE_FEATURE_SH_IS_ASH |
| 13156 | //usage:# define sh_trivial_usage ash_trivial_usage |
| 13157 | //usage:# define sh_full_usage ash_full_usage |
| 13158 | //usage:#endif |
| 13159 | //usage:#if ENABLE_FEATURE_BASH_IS_ASH |
| 13160 | //usage:# define bash_trivial_usage ash_trivial_usage |
| 13161 | //usage:# define bash_full_usage ash_full_usage |
| 13162 | //usage:#endif |
| 13163 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13164 | /* |
| 13165 | * Process the shell command line arguments. |
| 13166 | */ |
| 13167 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13168 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13169 | { |
| 13170 | int i; |
| 13171 | const char *xminusc; |
| 13172 | char **xargv; |
| 13173 | |
| 13174 | xargv = argv; |
| 13175 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13176 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13177 | xargv++; |
| 13178 | for (i = 0; i < NOPTS; i++) |
| 13179 | optlist[i] = 2; |
| 13180 | argptr = xargv; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13181 | if (options(/*cmdline:*/ 1)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 13182 | /* it already printed err message */ |
| 13183 | raise_exception(EXERROR); |
| 13184 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13185 | xargv = argptr; |
| 13186 | xminusc = minusc; |
| 13187 | if (*xargv == NULL) { |
| 13188 | if (xminusc) |
| 13189 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 13190 | sflag = 1; |
| 13191 | } |
| 13192 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 13193 | iflag = 1; |
| 13194 | if (mflag == 2) |
| 13195 | mflag = iflag; |
| 13196 | for (i = 0; i < NOPTS; i++) |
| 13197 | if (optlist[i] == 2) |
| 13198 | optlist[i] = 0; |
| 13199 | #if DEBUG == 2 |
| 13200 | debug = 1; |
| 13201 | #endif |
| 13202 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 13203 | if (xminusc) { |
| 13204 | minusc = *xargv++; |
| 13205 | if (*xargv) |
| 13206 | goto setarg0; |
| 13207 | } else if (!sflag) { |
| 13208 | setinputfile(*xargv, 0); |
| 13209 | setarg0: |
| 13210 | arg0 = *xargv++; |
| 13211 | commandname = arg0; |
| 13212 | } |
| 13213 | |
| 13214 | shellparam.p = xargv; |
| 13215 | #if ENABLE_ASH_GETOPTS |
| 13216 | shellparam.optind = 1; |
| 13217 | shellparam.optoff = -1; |
| 13218 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13219 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13220 | while (*xargv) { |
| 13221 | shellparam.nparam++; |
| 13222 | xargv++; |
| 13223 | } |
| 13224 | optschanged(); |
| 13225 | } |
| 13226 | |
| 13227 | /* |
| 13228 | * Read /etc/profile or .profile. |
| 13229 | */ |
| 13230 | static void |
| 13231 | read_profile(const char *name) |
| 13232 | { |
| 13233 | int skip; |
| 13234 | |
| 13235 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 13236 | return; |
| 13237 | skip = cmdloop(0); |
| 13238 | popfile(); |
| 13239 | if (skip) |
| 13240 | exitshell(); |
| 13241 | } |
| 13242 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13243 | /* |
| 13244 | * This routine is called when an error or an interrupt occurs in an |
| 13245 | * interactive shell and control is returned to the main command loop. |
| 13246 | */ |
| 13247 | static void |
| 13248 | reset(void) |
| 13249 | { |
| 13250 | /* from eval.c: */ |
| 13251 | evalskip = 0; |
| 13252 | loopnest = 0; |
| 13253 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 13254 | g_parsefile->left_in_buffer = 0; |
| 13255 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13256 | popallfiles(); |
| 13257 | /* from parser.c: */ |
| 13258 | tokpushback = 0; |
| 13259 | checkkwd = 0; |
| 13260 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 13261 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13262 | } |
| 13263 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13264 | #if PROFILE |
| 13265 | static short profile_buf[16384]; |
| 13266 | extern int etext(); |
| 13267 | #endif |
| 13268 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13269 | /* |
| 13270 | * Main routine. We initialize things, parse the arguments, execute |
| 13271 | * profiles if we're a login shell, and then call cmdloop to execute |
| 13272 | * commands. The setjmp call sets up the location to jump to when an |
| 13273 | * exception occurs. When an exception occurs the variable "state" |
| 13274 | * is used to figure out how far we had gotten. |
| 13275 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 13276 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13277 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13278 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 13279 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13280 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13281 | struct jmploc jmploc; |
| 13282 | struct stackmark smark; |
| 13283 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13284 | /* Initialize global data */ |
| 13285 | INIT_G_misc(); |
| 13286 | INIT_G_memstack(); |
| 13287 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13288 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13289 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13290 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13291 | INIT_G_cmdtable(); |
| 13292 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13293 | #if PROFILE |
| 13294 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13295 | #endif |
| 13296 | |
| 13297 | #if ENABLE_FEATURE_EDITING |
| 13298 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13299 | #endif |
| 13300 | state = 0; |
| 13301 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13302 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13303 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13304 | |
| 13305 | reset(); |
| 13306 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13307 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13308 | if (e == EXERROR) |
| 13309 | exitstatus = 2; |
| 13310 | s = state; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13311 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13312 | exitshell(); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13313 | } |
| 13314 | if (e == EXINT) { |
Denys Vlasenko | 9c54100 | 2015-10-07 15:44:36 +0200 | [diff] [blame] | 13315 | newline_and_flush(stderr); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13316 | } |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13317 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13318 | popstackmark(&smark); |
| 13319 | FORCE_INT_ON; /* enable interrupts */ |
| 13320 | if (s == 1) |
| 13321 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13322 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13323 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13324 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13325 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13326 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13327 | } |
| 13328 | exception_handler = &jmploc; |
| 13329 | #if DEBUG |
| 13330 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 13331 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13332 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13333 | #endif |
| 13334 | rootpid = getpid(); |
| 13335 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13336 | init(); |
| 13337 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13338 | procargs(argv); |
| 13339 | |
Denys Vlasenko | 6088e13 | 2010-12-25 23:58:42 +0100 | [diff] [blame] | 13340 | if (argv[0] && argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13341 | isloginsh = 1; |
| 13342 | if (isloginsh) { |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 13343 | const char *hp; |
| 13344 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13345 | state = 1; |
| 13346 | read_profile("/etc/profile"); |
| 13347 | state1: |
| 13348 | state = 2; |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 13349 | hp = lookupvar("HOME"); |
| 13350 | if (hp) { |
| 13351 | hp = concat_path_file(hp, ".profile"); |
| 13352 | read_profile(hp); |
| 13353 | free((char*)hp); |
| 13354 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13355 | } |
| 13356 | state2: |
| 13357 | state = 3; |
| 13358 | if ( |
| 13359 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13360 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13361 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13362 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13363 | ) { |
| 13364 | shinit = lookupvar("ENV"); |
| 13365 | if (shinit != NULL && *shinit != '\0') { |
| 13366 | read_profile(shinit); |
| 13367 | } |
| 13368 | } |
| 13369 | state3: |
| 13370 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13371 | if (minusc) { |
| 13372 | /* evalstring pushes parsefile stack. |
| 13373 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13374 | * is one of stacked source fds. |
| 13375 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 13376 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 13377 | // ^^ not necessary since now we special-case fd 0 |
| 13378 | // in is_hidden_fd() to not be considered "hidden fd" |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13379 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13380 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13381 | |
| 13382 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 13383 | #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13384 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13385 | const char *hp = lookupvar("HISTFILE"); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 13386 | if (!hp) { |
| 13387 | hp = lookupvar("HOME"); |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 13388 | if (hp) { |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 13389 | hp = concat_path_file(hp, ".ash_history"); |
Denys Vlasenko | 0a0acb5 | 2015-04-18 19:36:38 +0200 | [diff] [blame] | 13390 | setvar0("HISTFILE", hp); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 13391 | free((char*)hp); |
| 13392 | hp = lookupvar("HISTFILE"); |
| 13393 | } |
| 13394 | } |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13395 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13396 | line_input_state->hist_file = hp; |
Denys Vlasenko | 2c4de5b | 2011-03-31 13:16:52 +0200 | [diff] [blame] | 13397 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 13398 | hp = lookupvar("HISTFILESIZE"); |
| 13399 | line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 13400 | # endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13401 | } |
| 13402 | #endif |
| 13403 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13404 | cmdloop(1); |
| 13405 | } |
| 13406 | #if PROFILE |
| 13407 | monitor(0); |
| 13408 | #endif |
| 13409 | #ifdef GPROF |
| 13410 | { |
| 13411 | extern void _mcleanup(void); |
| 13412 | _mcleanup(); |
| 13413 | } |
| 13414 | #endif |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13415 | TRACE(("End of main reached\n")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13416 | exitshell(); |
| 13417 | /* NOTREACHED */ |
| 13418 | } |
| 13419 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13420 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13421 | /*- |
| 13422 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13423 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13424 | * |
| 13425 | * This code is derived from software contributed to Berkeley by |
| 13426 | * Kenneth Almquist. |
| 13427 | * |
| 13428 | * Redistribution and use in source and binary forms, with or without |
| 13429 | * modification, are permitted provided that the following conditions |
| 13430 | * are met: |
| 13431 | * 1. Redistributions of source code must retain the above copyright |
| 13432 | * notice, this list of conditions and the following disclaimer. |
| 13433 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13434 | * notice, this list of conditions and the following disclaimer in the |
| 13435 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13436 | * 3. Neither the name of the University nor the names of its contributors |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13437 | * may be used to endorse or promote products derived from this software |
| 13438 | * without specific prior written permission. |
| 13439 | * |
| 13440 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13441 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13442 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13443 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13444 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13445 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13446 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13447 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13448 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13449 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13450 | * SUCH DAMAGE. |
| 13451 | */ |