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 <paths.h> |
| 41 | #include <setjmp.h> |
| 42 | #include <fnmatch.h> |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 43 | #include <sys/times.h> |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 44 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 45 | #include "busybox.h" /* for applet_names */ |
| 46 | #include "unicode.h" |
| 47 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 48 | #include "shell_common.h" |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 49 | #if ENABLE_SH_MATH_SUPPORT |
| 50 | # include "math.h" |
| 51 | #endif |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 52 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 53 | # include "random.h" |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 54 | #else |
| 55 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 56 | #endif |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 57 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 58 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 59 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 60 | /* STANDALONE does not make sense, and won't compile */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 61 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 62 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 63 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 64 | # undef IF_NOT_FEATURE_SH_STANDALONE |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 65 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 66 | # define IF_FEATURE_SH_STANDALONE(...) |
| 67 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 68 | #endif |
| 69 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 70 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 71 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 74 | #if !BB_MMU |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 75 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 76 | #endif |
| 77 | |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 78 | //config:config ASH |
| 79 | //config: bool "ash" |
| 80 | //config: default y |
| 81 | //config: depends on !NOMMU |
| 82 | //config: help |
| 83 | //config: Tha 'ash' shell adds about 60k in the default configuration and is |
| 84 | //config: the most complete and most pedantically correct shell included with |
| 85 | //config: busybox. This shell is actually a derivative of the Debian 'dash' |
| 86 | //config: shell (by Herbert Xu), which was created by porting the 'ash' shell |
| 87 | //config: (written by Kenneth Almquist) from NetBSD. |
| 88 | //config: |
| 89 | //config:config ASH_BASH_COMPAT |
| 90 | //config: bool "bash-compatible extensions" |
| 91 | //config: default y |
| 92 | //config: depends on ASH |
| 93 | //config: help |
| 94 | //config: Enable bash-compatible extensions. |
| 95 | //config: |
Denys Vlasenko | 046341e | 2011-02-04 17:53:59 +0100 | [diff] [blame] | 96 | //config:config ASH_IDLE_TIMEOUT |
| 97 | //config: bool "Idle timeout variable" |
| 98 | //config: default n |
| 99 | //config: depends on ASH |
| 100 | //config: help |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 101 | //config: Enables bash-like auto-logout after $TMOUT seconds of idle time. |
Denys Vlasenko | 046341e | 2011-02-04 17:53:59 +0100 | [diff] [blame] | 102 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 103 | //config:config ASH_JOB_CONTROL |
| 104 | //config: bool "Job control" |
| 105 | //config: default y |
| 106 | //config: depends on ASH |
| 107 | //config: help |
| 108 | //config: Enable job control in the ash shell. |
| 109 | //config: |
| 110 | //config:config ASH_ALIAS |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 111 | //config: bool "Alias support" |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 112 | //config: default y |
| 113 | //config: depends on ASH |
| 114 | //config: help |
| 115 | //config: Enable alias support in the ash shell. |
| 116 | //config: |
| 117 | //config:config ASH_GETOPTS |
| 118 | //config: bool "Builtin getopt to parse positional parameters" |
| 119 | //config: default y |
| 120 | //config: depends on ASH |
| 121 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 122 | //config: Enable support for getopts builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 123 | //config: |
| 124 | //config:config ASH_BUILTIN_ECHO |
| 125 | //config: bool "Builtin version of 'echo'" |
| 126 | //config: default y |
| 127 | //config: depends on ASH |
| 128 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 129 | //config: Enable support for echo builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 130 | //config: |
| 131 | //config:config ASH_BUILTIN_PRINTF |
| 132 | //config: bool "Builtin version of 'printf'" |
| 133 | //config: default y |
| 134 | //config: depends on ASH |
| 135 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 136 | //config: Enable support for printf builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 137 | //config: |
| 138 | //config:config ASH_BUILTIN_TEST |
| 139 | //config: bool "Builtin version of 'test'" |
| 140 | //config: default y |
| 141 | //config: depends on ASH |
| 142 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 143 | //config: Enable support for test builtin in ash. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 144 | //config: |
| 145 | //config:config ASH_CMDCMD |
| 146 | //config: bool "'command' command to override shell builtins" |
| 147 | //config: default y |
| 148 | //config: depends on ASH |
| 149 | //config: help |
| 150 | //config: Enable support for the ash 'command' builtin, which allows |
| 151 | //config: you to run the specified command with the specified arguments, |
| 152 | //config: even when there is an ash builtin command with the same name. |
| 153 | //config: |
| 154 | //config:config ASH_MAIL |
| 155 | //config: bool "Check for new mail on interactive shells" |
| 156 | //config: default n |
| 157 | //config: depends on ASH |
| 158 | //config: help |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 159 | //config: Enable "check for new mail" function in the ash shell. |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 160 | //config: |
| 161 | //config:config ASH_OPTIMIZE_FOR_SIZE |
| 162 | //config: bool "Optimize for size instead of speed" |
| 163 | //config: default y |
| 164 | //config: depends on ASH |
| 165 | //config: help |
| 166 | //config: Compile ash for reduced size at the price of speed. |
| 167 | //config: |
| 168 | //config:config ASH_RANDOM_SUPPORT |
| 169 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 170 | //config: default y |
| 171 | //config: depends on ASH |
| 172 | //config: help |
| 173 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 174 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 175 | //config: You can reset the generator by using a specified start value. |
| 176 | //config: After "unset RANDOM" the generator will switch off and this |
| 177 | //config: variable will no longer have special treatment. |
| 178 | //config: |
| 179 | //config:config ASH_EXPAND_PRMT |
| 180 | //config: bool "Expand prompt string" |
| 181 | //config: default y |
| 182 | //config: depends on ASH |
| 183 | //config: help |
| 184 | //config: "PS#" may contain volatile content, such as backquote commands. |
| 185 | //config: This option recreates the prompt string from the environment |
| 186 | //config: variable each time it is displayed. |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 187 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 188 | |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 189 | //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) |
| 190 | //applet:IF_FEATURE_SH_IS_ASH(APPLET_ODDNAME(sh, ash, BB_DIR_BIN, BB_SUID_DROP, sh)) |
| 191 | //applet:IF_FEATURE_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, BB_DIR_BIN, BB_SUID_DROP, bash)) |
| 192 | |
| 193 | //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o |
| 194 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o |
| 195 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 196 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 197 | /* ============ Hash table sizes. Configurable. */ |
| 198 | |
| 199 | #define VTABSIZE 39 |
| 200 | #define ATABSIZE 39 |
| 201 | #define CMDTABLESIZE 31 /* should be prime */ |
| 202 | |
| 203 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 204 | /* ============ Shell options */ |
| 205 | |
| 206 | static const char *const optletters_optnames[] = { |
| 207 | "e" "errexit", |
| 208 | "f" "noglob", |
| 209 | "I" "ignoreeof", |
| 210 | "i" "interactive", |
| 211 | "m" "monitor", |
| 212 | "n" "noexec", |
| 213 | "s" "stdin", |
| 214 | "x" "xtrace", |
| 215 | "v" "verbose", |
| 216 | "C" "noclobber", |
| 217 | "a" "allexport", |
| 218 | "b" "notify", |
| 219 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 220 | "\0" "vi" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 221 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 222 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 223 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 224 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 225 | ,"\0" "nolog" |
| 226 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 227 | #endif |
| 228 | }; |
| 229 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 230 | #define optletters(n) optletters_optnames[n][0] |
| 231 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 232 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 233 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 234 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 235 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 236 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 237 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 238 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 239 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 240 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 241 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 242 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 243 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 244 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 245 | * exception handlers, the user should save the value of handler on entry |
| 246 | * to an inner scope, set handler to point to a jmploc structure for the |
| 247 | * inner scope, and restore handler on exit from the scope. |
| 248 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 249 | struct jmploc { |
| 250 | jmp_buf loc; |
| 251 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 252 | |
| 253 | struct globals_misc { |
| 254 | /* pid of main shell */ |
| 255 | int rootpid; |
| 256 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 257 | int shlvl; |
| 258 | #define rootshell (!shlvl) |
| 259 | char *minusc; /* argument to -c option */ |
| 260 | |
| 261 | char *curdir; // = nullstr; /* current working directory */ |
| 262 | char *physdir; // = nullstr; /* physical working directory */ |
| 263 | |
| 264 | char *arg0; /* value of $0 */ |
| 265 | |
| 266 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 267 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 268 | volatile int suppress_int; /* counter */ |
| 269 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 270 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 271 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 272 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 273 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 274 | #define EXINT 0 /* SIGINT received */ |
| 275 | #define EXERROR 1 /* a generic error */ |
| 276 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 277 | #define EXEXEC 3 /* command execution failed */ |
| 278 | #define EXEXIT 4 /* exit the shell */ |
| 279 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 280 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 281 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 282 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 283 | |
| 284 | char optlist[NOPTS]; |
| 285 | #define eflag optlist[0] |
| 286 | #define fflag optlist[1] |
| 287 | #define Iflag optlist[2] |
| 288 | #define iflag optlist[3] |
| 289 | #define mflag optlist[4] |
| 290 | #define nflag optlist[5] |
| 291 | #define sflag optlist[6] |
| 292 | #define xflag optlist[7] |
| 293 | #define vflag optlist[8] |
| 294 | #define Cflag optlist[9] |
| 295 | #define aflag optlist[10] |
| 296 | #define bflag optlist[11] |
| 297 | #define uflag optlist[12] |
| 298 | #define viflag optlist[13] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 299 | #if ENABLE_ASH_BASH_COMPAT |
| 300 | # define pipefail optlist[14] |
| 301 | #else |
| 302 | # define pipefail 0 |
| 303 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 304 | #if DEBUG |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 305 | # define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT] |
| 306 | # define debug optlist[15 + ENABLE_ASH_BASH_COMPAT] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 307 | #endif |
| 308 | |
| 309 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 310 | /* |
| 311 | * Sigmode records the current value of the signal handlers for the various |
| 312 | * 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] | 313 | * 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] | 314 | */ |
| 315 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 316 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 317 | #define S_CATCH 2 /* signal is caught */ |
| 318 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 319 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 320 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 321 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 322 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 323 | 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] | 324 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 325 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 326 | |
| 327 | /* Rarely referenced stuff */ |
| 328 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 329 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 330 | #endif |
| 331 | pid_t backgndpid; /* pid of last background process */ |
| 332 | 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] | 333 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 334 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 335 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 336 | #define rootpid (G_misc.rootpid ) |
| 337 | #define shlvl (G_misc.shlvl ) |
| 338 | #define minusc (G_misc.minusc ) |
| 339 | #define curdir (G_misc.curdir ) |
| 340 | #define physdir (G_misc.physdir ) |
| 341 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 342 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 343 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 344 | #define suppress_int (G_misc.suppress_int ) |
| 345 | #define pending_int (G_misc.pending_int ) |
| 346 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 347 | #define isloginsh (G_misc.isloginsh ) |
| 348 | #define nullstr (G_misc.nullstr ) |
| 349 | #define optlist (G_misc.optlist ) |
| 350 | #define sigmode (G_misc.sigmode ) |
| 351 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 352 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 353 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 354 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 355 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 356 | #define backgndpid (G_misc.backgndpid ) |
| 357 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 358 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 359 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 360 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 361 | curdir = nullstr; \ |
| 362 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 363 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 364 | } while (0) |
| 365 | |
| 366 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 367 | /* ============ DEBUG */ |
| 368 | #if DEBUG |
| 369 | static void trace_printf(const char *fmt, ...); |
| 370 | static void trace_vprintf(const char *fmt, va_list va); |
| 371 | # define TRACE(param) trace_printf param |
| 372 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 373 | # define close(fd) do { \ |
| 374 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 375 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 376 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 377 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 378 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 379 | #else |
| 380 | # define TRACE(param) |
| 381 | # define TRACEV(param) |
| 382 | #endif |
| 383 | |
| 384 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 385 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 386 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 387 | |
Denys Vlasenko | 1961aea | 2013-02-26 00:36:53 +0100 | [diff] [blame] | 388 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 389 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 390 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 391 | static int isdigit_str9(const char *str) |
| 392 | { |
| 393 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 394 | while (--maxlen && isdigit(*str)) |
| 395 | str++; |
| 396 | return (*str == '\0'); |
| 397 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 398 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 399 | static const char *var_end(const char *var) |
| 400 | { |
| 401 | while (*var) |
| 402 | if (*var++ == '=') |
| 403 | break; |
| 404 | return var; |
| 405 | } |
| 406 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 407 | |
| 408 | /* ============ Interrupts / exceptions */ |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 409 | |
| 410 | static void exitshell(void) NORETURN; |
| 411 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 412 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 413 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 414 | * 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] | 415 | * much more efficient and portable. (But hacking the kernel is so much |
| 416 | * more fun than worrying about efficiency and portability. :-)) |
| 417 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 418 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 419 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 420 | xbarrier(); \ |
| 421 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 425 | * 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] | 426 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 427 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 428 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 429 | static void |
| 430 | raise_exception(int e) |
| 431 | { |
| 432 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 433 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 434 | abort(); |
| 435 | #endif |
| 436 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 437 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 438 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 439 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 440 | #if DEBUG |
| 441 | #define raise_exception(e) do { \ |
| 442 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 443 | raise_exception(e); \ |
| 444 | } while (0) |
| 445 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 449 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 450 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 451 | * are held using the INT_OFF macro. (The test for iflag is just |
| 452 | * defensive programming.) |
| 453 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 454 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 455 | static void |
| 456 | raise_interrupt(void) |
| 457 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 458 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 459 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 460 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 461 | /* Signal is not automatically unmasked after it is raised, |
| 462 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 463 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 464 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 465 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 466 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 467 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 468 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 469 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 470 | signal(SIGINT, SIG_DFL); |
| 471 | raise(SIGINT); |
| 472 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 473 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 474 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 475 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 476 | /* NOTREACHED */ |
| 477 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 478 | #if DEBUG |
| 479 | #define raise_interrupt() do { \ |
| 480 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 481 | raise_interrupt(); \ |
| 482 | } while (0) |
| 483 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 484 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 485 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 486 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 487 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 488 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 489 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 490 | raise_interrupt(); |
| 491 | } |
| 492 | } |
| 493 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 494 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 495 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 496 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 497 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 498 | suppress_int = 0; |
| 499 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 500 | raise_interrupt(); |
| 501 | } |
| 502 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 503 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 504 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 505 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 506 | #define RESTORE_INT(v) do { \ |
| 507 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 508 | suppress_int = (v); \ |
| 509 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 510 | raise_interrupt(); \ |
| 511 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 512 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 513 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 514 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 515 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 516 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 517 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 518 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 519 | INT_OFF; |
| 520 | fputs(p, file); |
| 521 | INT_ON; |
| 522 | } |
| 523 | |
| 524 | static void |
| 525 | flush_stdout_stderr(void) |
| 526 | { |
| 527 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 528 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 529 | INT_ON; |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | outcslow(int c, FILE *dest) |
| 534 | { |
| 535 | INT_OFF; |
| 536 | putc(c, dest); |
| 537 | fflush(dest); |
| 538 | INT_ON; |
| 539 | } |
| 540 | |
| 541 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 542 | static int |
| 543 | out1fmt(const char *fmt, ...) |
| 544 | { |
| 545 | va_list ap; |
| 546 | int r; |
| 547 | |
| 548 | INT_OFF; |
| 549 | va_start(ap, fmt); |
| 550 | r = vprintf(fmt, ap); |
| 551 | va_end(ap); |
| 552 | INT_ON; |
| 553 | return r; |
| 554 | } |
| 555 | |
| 556 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 557 | static int |
| 558 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 559 | { |
| 560 | va_list ap; |
| 561 | int ret; |
| 562 | |
| 563 | va_start(ap, fmt); |
| 564 | INT_OFF; |
| 565 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 566 | va_end(ap); |
| 567 | INT_ON; |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | static void |
| 572 | out1str(const char *p) |
| 573 | { |
| 574 | outstr(p, stdout); |
| 575 | } |
| 576 | |
| 577 | static void |
| 578 | out2str(const char *p) |
| 579 | { |
| 580 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 581 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 585 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 586 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 587 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 588 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 589 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 590 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 591 | #define CTLENDVAR ((unsigned char)'\203') |
| 592 | #define CTLBACKQ ((unsigned char)'\204') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 593 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 594 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 595 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 596 | #define CTLENDARI ((unsigned char)'\207') |
| 597 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 598 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 599 | |
| 600 | /* variable substitution byte (follows CTLVAR) */ |
| 601 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 602 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 603 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 604 | |
| 605 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 606 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 607 | #define VSMINUS 0x2 /* ${var-text} */ |
| 608 | #define VSPLUS 0x3 /* ${var+text} */ |
| 609 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 610 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 611 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 612 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 613 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 614 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 615 | #define VSLENGTH 0xa /* ${#var} */ |
| 616 | #if ENABLE_ASH_BASH_COMPAT |
| 617 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 618 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 619 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 620 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 621 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 622 | static const char dolatstr[] ALIGN1 = { |
| 623 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 624 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 625 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 626 | #define NCMD 0 |
| 627 | #define NPIPE 1 |
| 628 | #define NREDIR 2 |
| 629 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 630 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 631 | #define NAND 5 |
| 632 | #define NOR 6 |
| 633 | #define NSEMI 7 |
| 634 | #define NIF 8 |
| 635 | #define NWHILE 9 |
| 636 | #define NUNTIL 10 |
| 637 | #define NFOR 11 |
| 638 | #define NCASE 12 |
| 639 | #define NCLIST 13 |
| 640 | #define NDEFUN 14 |
| 641 | #define NARG 15 |
| 642 | #define NTO 16 |
| 643 | #if ENABLE_ASH_BASH_COMPAT |
| 644 | #define NTO2 17 |
| 645 | #endif |
| 646 | #define NCLOBBER 18 |
| 647 | #define NFROM 19 |
| 648 | #define NFROMTO 20 |
| 649 | #define NAPPEND 21 |
| 650 | #define NTOFD 22 |
| 651 | #define NFROMFD 23 |
| 652 | #define NHERE 24 |
| 653 | #define NXHERE 25 |
| 654 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 655 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 656 | |
| 657 | union node; |
| 658 | |
| 659 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 660 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 661 | union node *assign; |
| 662 | union node *args; |
| 663 | union node *redirect; |
| 664 | }; |
| 665 | |
| 666 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 667 | smallint type; |
| 668 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 669 | struct nodelist *cmdlist; |
| 670 | }; |
| 671 | |
| 672 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 673 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 674 | union node *n; |
| 675 | union node *redirect; |
| 676 | }; |
| 677 | |
| 678 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 679 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 680 | union node *ch1; |
| 681 | union node *ch2; |
| 682 | }; |
| 683 | |
| 684 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 685 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 686 | union node *test; |
| 687 | union node *ifpart; |
| 688 | union node *elsepart; |
| 689 | }; |
| 690 | |
| 691 | struct nfor { |
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 *args; |
| 694 | union node *body; |
| 695 | char *var; |
| 696 | }; |
| 697 | |
| 698 | struct ncase { |
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 *expr; |
| 701 | union node *cases; |
| 702 | }; |
| 703 | |
| 704 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 705 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 706 | union node *next; |
| 707 | union node *pattern; |
| 708 | union node *body; |
| 709 | }; |
| 710 | |
| 711 | struct narg { |
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 | char *text; |
| 715 | struct nodelist *backquote; |
| 716 | }; |
| 717 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 718 | /* nfile and ndup layout must match! |
| 719 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 720 | * that it is actually NTO2 (>&file), and change its type. |
| 721 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 722 | struct nfile { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 723 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 724 | union node *next; |
| 725 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 726 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 727 | union node *fname; |
| 728 | char *expfname; |
| 729 | }; |
| 730 | |
| 731 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 732 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 733 | union node *next; |
| 734 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 735 | int dupfd; |
| 736 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 737 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 738 | }; |
| 739 | |
| 740 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 741 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 742 | union node *next; |
| 743 | int fd; |
| 744 | union node *doc; |
| 745 | }; |
| 746 | |
| 747 | struct nnot { |
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 *com; |
| 750 | }; |
| 751 | |
| 752 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 753 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 754 | struct ncmd ncmd; |
| 755 | struct npipe npipe; |
| 756 | struct nredir nredir; |
| 757 | struct nbinary nbinary; |
| 758 | struct nif nif; |
| 759 | struct nfor nfor; |
| 760 | struct ncase ncase; |
| 761 | struct nclist nclist; |
| 762 | struct narg narg; |
| 763 | struct nfile nfile; |
| 764 | struct ndup ndup; |
| 765 | struct nhere nhere; |
| 766 | struct nnot nnot; |
| 767 | }; |
| 768 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 769 | /* |
| 770 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 771 | * It must be distinct from NULL. |
| 772 | */ |
| 773 | #define NODE_EOF ((union node *) -1L) |
| 774 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 775 | struct nodelist { |
| 776 | struct nodelist *next; |
| 777 | union node *n; |
| 778 | }; |
| 779 | |
| 780 | struct funcnode { |
| 781 | int count; |
| 782 | union node n; |
| 783 | }; |
| 784 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 785 | /* |
| 786 | * Free a parse tree. |
| 787 | */ |
| 788 | static void |
| 789 | freefunc(struct funcnode *f) |
| 790 | { |
| 791 | if (f && --f->count < 0) |
| 792 | free(f); |
| 793 | } |
| 794 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 795 | |
| 796 | /* ============ Debugging output */ |
| 797 | |
| 798 | #if DEBUG |
| 799 | |
| 800 | static FILE *tracefile; |
| 801 | |
| 802 | static void |
| 803 | trace_printf(const char *fmt, ...) |
| 804 | { |
| 805 | va_list va; |
| 806 | |
| 807 | if (debug != 1) |
| 808 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 809 | if (DEBUG_TIME) |
| 810 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 811 | if (DEBUG_PID) |
| 812 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 813 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 814 | 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] | 815 | va_start(va, fmt); |
| 816 | vfprintf(tracefile, fmt, va); |
| 817 | va_end(va); |
| 818 | } |
| 819 | |
| 820 | static void |
| 821 | trace_vprintf(const char *fmt, va_list va) |
| 822 | { |
| 823 | if (debug != 1) |
| 824 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 825 | if (DEBUG_TIME) |
| 826 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 827 | if (DEBUG_PID) |
| 828 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 829 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 830 | 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] | 831 | vfprintf(tracefile, fmt, va); |
| 832 | } |
| 833 | |
| 834 | static void |
| 835 | trace_puts(const char *s) |
| 836 | { |
| 837 | if (debug != 1) |
| 838 | return; |
| 839 | fputs(s, tracefile); |
| 840 | } |
| 841 | |
| 842 | static void |
| 843 | trace_puts_quoted(char *s) |
| 844 | { |
| 845 | char *p; |
| 846 | char c; |
| 847 | |
| 848 | if (debug != 1) |
| 849 | return; |
| 850 | putc('"', tracefile); |
| 851 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 852 | switch ((unsigned char)*p) { |
| 853 | case '\n': c = 'n'; goto backslash; |
| 854 | case '\t': c = 't'; goto backslash; |
| 855 | case '\r': c = 'r'; goto backslash; |
| 856 | case '\"': c = '\"'; goto backslash; |
| 857 | case '\\': c = '\\'; goto backslash; |
| 858 | case CTLESC: c = 'e'; goto backslash; |
| 859 | case CTLVAR: c = 'v'; goto backslash; |
| 860 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 861 | case CTLBACKQ: c = 'q'; goto backslash; |
| 862 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 863 | backslash: |
| 864 | putc('\\', tracefile); |
| 865 | putc(c, tracefile); |
| 866 | break; |
| 867 | default: |
| 868 | if (*p >= ' ' && *p <= '~') |
| 869 | putc(*p, tracefile); |
| 870 | else { |
| 871 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 872 | putc((*p >> 6) & 03, tracefile); |
| 873 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 874 | putc(*p & 07, tracefile); |
| 875 | } |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | putc('"', tracefile); |
| 880 | } |
| 881 | |
| 882 | static void |
| 883 | trace_puts_args(char **ap) |
| 884 | { |
| 885 | if (debug != 1) |
| 886 | return; |
| 887 | if (!*ap) |
| 888 | return; |
| 889 | while (1) { |
| 890 | trace_puts_quoted(*ap); |
| 891 | if (!*++ap) { |
| 892 | putc('\n', tracefile); |
| 893 | break; |
| 894 | } |
| 895 | putc(' ', tracefile); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | static void |
| 900 | opentrace(void) |
| 901 | { |
| 902 | char s[100]; |
| 903 | #ifdef O_APPEND |
| 904 | int flags; |
| 905 | #endif |
| 906 | |
| 907 | if (debug != 1) { |
| 908 | if (tracefile) |
| 909 | fflush(tracefile); |
| 910 | /* leave open because libedit might be using it */ |
| 911 | return; |
| 912 | } |
| 913 | strcpy(s, "./trace"); |
| 914 | if (tracefile) { |
| 915 | if (!freopen(s, "a", tracefile)) { |
| 916 | fprintf(stderr, "Can't re-open %s\n", s); |
| 917 | debug = 0; |
| 918 | return; |
| 919 | } |
| 920 | } else { |
| 921 | tracefile = fopen(s, "a"); |
| 922 | if (tracefile == NULL) { |
| 923 | fprintf(stderr, "Can't open %s\n", s); |
| 924 | debug = 0; |
| 925 | return; |
| 926 | } |
| 927 | } |
| 928 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 929 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 930 | if (flags >= 0) |
| 931 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 932 | #endif |
| 933 | setlinebuf(tracefile); |
| 934 | fputs("\nTracing started.\n", tracefile); |
| 935 | } |
| 936 | |
| 937 | static void |
| 938 | indent(int amount, char *pfx, FILE *fp) |
| 939 | { |
| 940 | int i; |
| 941 | |
| 942 | for (i = 0; i < amount; i++) { |
| 943 | if (pfx && i == amount - 1) |
| 944 | fputs(pfx, fp); |
| 945 | putc('\t', fp); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | /* little circular references here... */ |
| 950 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 951 | |
| 952 | static void |
| 953 | sharg(union node *arg, FILE *fp) |
| 954 | { |
| 955 | char *p; |
| 956 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 957 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 958 | |
| 959 | if (arg->type != NARG) { |
| 960 | out1fmt("<node type %d>\n", arg->type); |
| 961 | abort(); |
| 962 | } |
| 963 | bqlist = arg->narg.backquote; |
| 964 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 965 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 966 | case CTLESC: |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 967 | p++; |
| 968 | putc(*p, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 969 | break; |
| 970 | case CTLVAR: |
| 971 | putc('$', fp); |
| 972 | putc('{', fp); |
| 973 | subtype = *++p; |
| 974 | if (subtype == VSLENGTH) |
| 975 | putc('#', fp); |
| 976 | |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 977 | while (*p != '=') { |
| 978 | putc(*p, fp); |
| 979 | p++; |
| 980 | } |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 981 | |
| 982 | if (subtype & VSNUL) |
| 983 | putc(':', fp); |
| 984 | |
| 985 | switch (subtype & VSTYPE) { |
| 986 | case VSNORMAL: |
| 987 | putc('}', fp); |
| 988 | break; |
| 989 | case VSMINUS: |
| 990 | putc('-', fp); |
| 991 | break; |
| 992 | case VSPLUS: |
| 993 | putc('+', fp); |
| 994 | break; |
| 995 | case VSQUESTION: |
| 996 | putc('?', fp); |
| 997 | break; |
| 998 | case VSASSIGN: |
| 999 | putc('=', fp); |
| 1000 | break; |
| 1001 | case VSTRIMLEFT: |
| 1002 | putc('#', fp); |
| 1003 | break; |
| 1004 | case VSTRIMLEFTMAX: |
| 1005 | putc('#', fp); |
| 1006 | putc('#', fp); |
| 1007 | break; |
| 1008 | case VSTRIMRIGHT: |
| 1009 | putc('%', fp); |
| 1010 | break; |
| 1011 | case VSTRIMRIGHTMAX: |
| 1012 | putc('%', fp); |
| 1013 | putc('%', fp); |
| 1014 | break; |
| 1015 | case VSLENGTH: |
| 1016 | break; |
| 1017 | default: |
| 1018 | out1fmt("<subtype %d>", subtype); |
| 1019 | } |
| 1020 | break; |
| 1021 | case CTLENDVAR: |
| 1022 | putc('}', fp); |
| 1023 | break; |
| 1024 | case CTLBACKQ: |
| 1025 | case CTLBACKQ|CTLQUOTE: |
| 1026 | putc('$', fp); |
| 1027 | putc('(', fp); |
| 1028 | shtree(bqlist->n, -1, NULL, fp); |
| 1029 | putc(')', fp); |
| 1030 | break; |
| 1031 | default: |
| 1032 | putc(*p, fp); |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1038 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1039 | shcmd(union node *cmd, FILE *fp) |
| 1040 | { |
| 1041 | union node *np; |
| 1042 | int first; |
| 1043 | const char *s; |
| 1044 | int dftfd; |
| 1045 | |
| 1046 | first = 1; |
| 1047 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1048 | if (!first) |
| 1049 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1050 | sharg(np, fp); |
| 1051 | first = 0; |
| 1052 | } |
| 1053 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1054 | if (!first) |
| 1055 | putc(' ', fp); |
| 1056 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1057 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1058 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1059 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1060 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1061 | #if ENABLE_ASH_BASH_COMPAT |
| 1062 | case NTO2: |
| 1063 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1064 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1065 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1066 | case NFROMFD: s = "<&"; break; |
| 1067 | case NFROMTO: s = "<>"; break; |
| 1068 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1069 | } |
| 1070 | if (np->nfile.fd != dftfd) |
| 1071 | fprintf(fp, "%d", np->nfile.fd); |
| 1072 | fputs(s, fp); |
| 1073 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1074 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1075 | } else { |
| 1076 | sharg(np->nfile.fname, fp); |
| 1077 | } |
| 1078 | first = 0; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | static void |
| 1083 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1084 | { |
| 1085 | struct nodelist *lp; |
| 1086 | const char *s; |
| 1087 | |
| 1088 | if (n == NULL) |
| 1089 | return; |
| 1090 | |
| 1091 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1092 | |
| 1093 | if (n == NODE_EOF) { |
| 1094 | fputs("<EOF>", fp); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1098 | switch (n->type) { |
| 1099 | case NSEMI: |
| 1100 | s = "; "; |
| 1101 | goto binop; |
| 1102 | case NAND: |
| 1103 | s = " && "; |
| 1104 | goto binop; |
| 1105 | case NOR: |
| 1106 | s = " || "; |
| 1107 | binop: |
| 1108 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1109 | /* if (ind < 0) */ |
| 1110 | fputs(s, fp); |
| 1111 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1112 | break; |
| 1113 | case NCMD: |
| 1114 | shcmd(n, fp); |
| 1115 | if (ind >= 0) |
| 1116 | putc('\n', fp); |
| 1117 | break; |
| 1118 | case NPIPE: |
| 1119 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1120 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1121 | if (lp->next) |
| 1122 | fputs(" | ", fp); |
| 1123 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1124 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1125 | fputs(" &", fp); |
| 1126 | if (ind >= 0) |
| 1127 | putc('\n', fp); |
| 1128 | break; |
| 1129 | default: |
| 1130 | fprintf(fp, "<node type %d>", n->type); |
| 1131 | if (ind >= 0) |
| 1132 | putc('\n', fp); |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | static void |
| 1138 | showtree(union node *n) |
| 1139 | { |
| 1140 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1141 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1144 | #endif /* DEBUG */ |
| 1145 | |
| 1146 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1147 | /* ============ Parser data */ |
| 1148 | |
| 1149 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1150 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1151 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1152 | struct strlist { |
| 1153 | struct strlist *next; |
| 1154 | char *text; |
| 1155 | }; |
| 1156 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1157 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1158 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1159 | struct strpush { |
| 1160 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1161 | char *prev_string; |
| 1162 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1163 | #if ENABLE_ASH_ALIAS |
| 1164 | struct alias *ap; /* if push was associated with an alias */ |
| 1165 | #endif |
| 1166 | char *string; /* remember the string since it may change */ |
| 1167 | }; |
| 1168 | |
| 1169 | struct parsefile { |
| 1170 | struct parsefile *prev; /* preceding file on stack */ |
| 1171 | int linno; /* current line */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1172 | int pf_fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1173 | int left_in_line; /* number of chars left in this line */ |
| 1174 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1175 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1176 | char *buf; /* input buffer */ |
| 1177 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1178 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1179 | }; |
| 1180 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1181 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1182 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1183 | static int startlinno; /* line # where last token started */ |
| 1184 | static char *commandname; /* currently executing command */ |
| 1185 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1186 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1187 | |
| 1188 | |
| 1189 | /* ============ Message printing */ |
| 1190 | |
| 1191 | static void |
| 1192 | ash_vmsg(const char *msg, va_list ap) |
| 1193 | { |
| 1194 | fprintf(stderr, "%s: ", arg0); |
| 1195 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1196 | if (strcmp(arg0, commandname)) |
| 1197 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1198 | if (!iflag || g_parsefile->pf_fd > 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1199 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1200 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1201 | vfprintf(stderr, msg, ap); |
| 1202 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1203 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1204 | |
| 1205 | /* |
| 1206 | * Exverror is called to raise the error exception. If the second argument |
| 1207 | * is not NULL then error prints an error message using printf style |
| 1208 | * formatting. It then raises the error exception. |
| 1209 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1210 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1211 | static void |
| 1212 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1213 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1214 | #if DEBUG |
| 1215 | if (msg) { |
| 1216 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1217 | TRACEV((msg, ap)); |
| 1218 | TRACE(("\") pid=%d\n", getpid())); |
| 1219 | } else |
| 1220 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1221 | if (msg) |
| 1222 | #endif |
| 1223 | ash_vmsg(msg, ap); |
| 1224 | |
| 1225 | flush_stdout_stderr(); |
| 1226 | raise_exception(cond); |
| 1227 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1228 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1229 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1230 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1231 | static void |
| 1232 | ash_msg_and_raise_error(const char *msg, ...) |
| 1233 | { |
| 1234 | va_list ap; |
| 1235 | |
| 1236 | va_start(ap, msg); |
| 1237 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1238 | /* NOTREACHED */ |
| 1239 | va_end(ap); |
| 1240 | } |
| 1241 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1242 | static void raise_error_syntax(const char *) NORETURN; |
| 1243 | static void |
| 1244 | raise_error_syntax(const char *msg) |
| 1245 | { |
| 1246 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1247 | /* NOTREACHED */ |
| 1248 | } |
| 1249 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1250 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1251 | static void |
| 1252 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1253 | { |
| 1254 | va_list ap; |
| 1255 | |
| 1256 | va_start(ap, msg); |
| 1257 | ash_vmsg_and_raise(cond, msg, ap); |
| 1258 | /* NOTREACHED */ |
| 1259 | va_end(ap); |
| 1260 | } |
| 1261 | |
| 1262 | /* |
| 1263 | * error/warning routines for external builtins |
| 1264 | */ |
| 1265 | static void |
| 1266 | ash_msg(const char *fmt, ...) |
| 1267 | { |
| 1268 | va_list ap; |
| 1269 | |
| 1270 | va_start(ap, fmt); |
| 1271 | ash_vmsg(fmt, ap); |
| 1272 | va_end(ap); |
| 1273 | } |
| 1274 | |
| 1275 | /* |
| 1276 | * Return a string describing an error. The returned string may be a |
| 1277 | * pointer to a static buffer that will be overwritten on the next call. |
| 1278 | * Action describes the operation that got the error. |
| 1279 | */ |
| 1280 | static const char * |
| 1281 | errmsg(int e, const char *em) |
| 1282 | { |
| 1283 | if (e == ENOENT || e == ENOTDIR) { |
| 1284 | return em; |
| 1285 | } |
| 1286 | return strerror(e); |
| 1287 | } |
| 1288 | |
| 1289 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1290 | /* ============ Memory allocation */ |
| 1291 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1292 | #if 0 |
| 1293 | /* I consider these wrappers nearly useless: |
| 1294 | * ok, they return you to nearest exception handler, but |
| 1295 | * how much memory do you leak in the process, making |
| 1296 | * memory starvation worse? |
| 1297 | */ |
| 1298 | static void * |
| 1299 | ckrealloc(void * p, size_t nbytes) |
| 1300 | { |
| 1301 | p = realloc(p, nbytes); |
| 1302 | if (!p) |
| 1303 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1304 | return p; |
| 1305 | } |
| 1306 | |
| 1307 | static void * |
| 1308 | ckmalloc(size_t nbytes) |
| 1309 | { |
| 1310 | return ckrealloc(NULL, nbytes); |
| 1311 | } |
| 1312 | |
| 1313 | static void * |
| 1314 | ckzalloc(size_t nbytes) |
| 1315 | { |
| 1316 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1317 | } |
| 1318 | |
| 1319 | static char * |
| 1320 | ckstrdup(const char *s) |
| 1321 | { |
| 1322 | char *p = strdup(s); |
| 1323 | if (!p) |
| 1324 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1325 | return p; |
| 1326 | } |
| 1327 | #else |
| 1328 | /* Using bbox equivalents. They exit if out of memory */ |
| 1329 | # define ckrealloc xrealloc |
| 1330 | # define ckmalloc xmalloc |
| 1331 | # define ckzalloc xzalloc |
| 1332 | # define ckstrdup xstrdup |
| 1333 | #endif |
| 1334 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1335 | /* |
| 1336 | * It appears that grabstackstr() will barf with such alignments |
| 1337 | * because stalloc() will return a string allocated in a new stackblock. |
| 1338 | */ |
| 1339 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1340 | enum { |
| 1341 | /* Most machines require the value returned from malloc to be aligned |
| 1342 | * in some way. The following macro will get this right |
| 1343 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1344 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1345 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1346 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1347 | }; |
| 1348 | |
| 1349 | struct stack_block { |
| 1350 | struct stack_block *prev; |
| 1351 | char space[MINSIZE]; |
| 1352 | }; |
| 1353 | |
| 1354 | struct stackmark { |
| 1355 | struct stack_block *stackp; |
| 1356 | char *stacknxt; |
| 1357 | size_t stacknleft; |
| 1358 | struct stackmark *marknext; |
| 1359 | }; |
| 1360 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1361 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1362 | struct globals_memstack { |
| 1363 | struct stack_block *g_stackp; // = &stackbase; |
| 1364 | struct stackmark *markp; |
| 1365 | char *g_stacknxt; // = stackbase.space; |
| 1366 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1367 | size_t g_stacknleft; // = MINSIZE; |
| 1368 | int herefd; // = -1; |
| 1369 | struct stack_block stackbase; |
| 1370 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1371 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1372 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1373 | #define g_stackp (G_memstack.g_stackp ) |
| 1374 | #define markp (G_memstack.markp ) |
| 1375 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1376 | #define sstrend (G_memstack.sstrend ) |
| 1377 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1378 | #define herefd (G_memstack.herefd ) |
| 1379 | #define stackbase (G_memstack.stackbase ) |
| 1380 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1381 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1382 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1383 | g_stackp = &stackbase; \ |
| 1384 | g_stacknxt = stackbase.space; \ |
| 1385 | g_stacknleft = MINSIZE; \ |
| 1386 | sstrend = stackbase.space + MINSIZE; \ |
| 1387 | herefd = -1; \ |
| 1388 | } while (0) |
| 1389 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1390 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1391 | #define stackblock() ((void *)g_stacknxt) |
| 1392 | #define stackblocksize() g_stacknleft |
| 1393 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1394 | /* |
| 1395 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1396 | * to make this more efficient, and also to avoid all sorts of exception |
| 1397 | * handling code to handle interrupts in the middle of a parse. |
| 1398 | * |
| 1399 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1400 | * well. |
| 1401 | */ |
| 1402 | static void * |
| 1403 | stalloc(size_t nbytes) |
| 1404 | { |
| 1405 | char *p; |
| 1406 | size_t aligned; |
| 1407 | |
| 1408 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1409 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1410 | size_t len; |
| 1411 | size_t blocksize; |
| 1412 | struct stack_block *sp; |
| 1413 | |
| 1414 | blocksize = aligned; |
| 1415 | if (blocksize < MINSIZE) |
| 1416 | blocksize = MINSIZE; |
| 1417 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1418 | if (len < blocksize) |
| 1419 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1420 | INT_OFF; |
| 1421 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1422 | sp->prev = g_stackp; |
| 1423 | g_stacknxt = sp->space; |
| 1424 | g_stacknleft = blocksize; |
| 1425 | sstrend = g_stacknxt + blocksize; |
| 1426 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1427 | INT_ON; |
| 1428 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1429 | p = g_stacknxt; |
| 1430 | g_stacknxt += aligned; |
| 1431 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1432 | return p; |
| 1433 | } |
| 1434 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1435 | static void * |
| 1436 | stzalloc(size_t nbytes) |
| 1437 | { |
| 1438 | return memset(stalloc(nbytes), 0, nbytes); |
| 1439 | } |
| 1440 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1441 | static void |
| 1442 | stunalloc(void *p) |
| 1443 | { |
| 1444 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1445 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1446 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1447 | abort(); |
| 1448 | } |
| 1449 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1450 | g_stacknleft += g_stacknxt - (char *)p; |
| 1451 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1454 | /* |
| 1455 | * Like strdup but works with the ash stack. |
| 1456 | */ |
| 1457 | static char * |
| 1458 | ststrdup(const char *p) |
| 1459 | { |
| 1460 | size_t len = strlen(p) + 1; |
| 1461 | return memcpy(stalloc(len), p, len); |
| 1462 | } |
| 1463 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1464 | static void |
| 1465 | setstackmark(struct stackmark *mark) |
| 1466 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1467 | mark->stackp = g_stackp; |
| 1468 | mark->stacknxt = g_stacknxt; |
| 1469 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1470 | mark->marknext = markp; |
| 1471 | markp = mark; |
| 1472 | } |
| 1473 | |
| 1474 | static void |
| 1475 | popstackmark(struct stackmark *mark) |
| 1476 | { |
| 1477 | struct stack_block *sp; |
| 1478 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1479 | if (!mark->stackp) |
| 1480 | return; |
| 1481 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1482 | INT_OFF; |
| 1483 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1484 | while (g_stackp != mark->stackp) { |
| 1485 | sp = g_stackp; |
| 1486 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1487 | free(sp); |
| 1488 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1489 | g_stacknxt = mark->stacknxt; |
| 1490 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1491 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1492 | INT_ON; |
| 1493 | } |
| 1494 | |
| 1495 | /* |
| 1496 | * When the parser reads in a string, it wants to stick the string on the |
| 1497 | * stack and only adjust the stack pointer when it knows how big the |
| 1498 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1499 | * of space on top of the stack and stackblocklen returns the length of |
| 1500 | * this block. Growstackblock will grow this space by at least one byte, |
| 1501 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1502 | * part of the block that has been used. |
| 1503 | */ |
| 1504 | static void |
| 1505 | growstackblock(void) |
| 1506 | { |
| 1507 | size_t newlen; |
| 1508 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1509 | newlen = g_stacknleft * 2; |
| 1510 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1511 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1512 | if (newlen < 128) |
| 1513 | newlen += 128; |
| 1514 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1515 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1516 | struct stack_block *oldstackp; |
| 1517 | struct stackmark *xmark; |
| 1518 | struct stack_block *sp; |
| 1519 | struct stack_block *prevstackp; |
| 1520 | size_t grosslen; |
| 1521 | |
| 1522 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1523 | oldstackp = g_stackp; |
| 1524 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1525 | prevstackp = sp->prev; |
| 1526 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1527 | sp = ckrealloc(sp, grosslen); |
| 1528 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1529 | g_stackp = sp; |
| 1530 | g_stacknxt = sp->space; |
| 1531 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1532 | sstrend = sp->space + newlen; |
| 1533 | |
| 1534 | /* |
| 1535 | * Stack marks pointing to the start of the old block |
| 1536 | * must be relocated to point to the new block |
| 1537 | */ |
| 1538 | xmark = markp; |
| 1539 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1540 | xmark->stackp = g_stackp; |
| 1541 | xmark->stacknxt = g_stacknxt; |
| 1542 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1543 | xmark = xmark->marknext; |
| 1544 | } |
| 1545 | INT_ON; |
| 1546 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1547 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1548 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1549 | char *p = stalloc(newlen); |
| 1550 | |
| 1551 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1552 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1553 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | static void |
| 1558 | grabstackblock(size_t len) |
| 1559 | { |
| 1560 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1561 | g_stacknxt += len; |
| 1562 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * The following routines are somewhat easier to use than the above. |
| 1567 | * The user declares a variable of type STACKSTR, which may be declared |
| 1568 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1569 | * the user uses the macro STPUTC to add characters to the string. In |
| 1570 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1571 | * grown as necessary. When the user is done, she can just leave the |
| 1572 | * string there and refer to it using stackblock(). Or she can allocate |
| 1573 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1574 | * someone else to use the stack temporarily and then continue to grow |
| 1575 | * the string, the user should use grabstack to allocate the space, and |
| 1576 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1577 | * |
| 1578 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1579 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1580 | * is space for at least one character. |
| 1581 | */ |
| 1582 | static void * |
| 1583 | growstackstr(void) |
| 1584 | { |
| 1585 | size_t len = stackblocksize(); |
| 1586 | if (herefd >= 0 && len >= 1024) { |
| 1587 | full_write(herefd, stackblock(), len); |
| 1588 | return stackblock(); |
| 1589 | } |
| 1590 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1591 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * Called from CHECKSTRSPACE. |
| 1596 | */ |
| 1597 | static char * |
| 1598 | makestrspace(size_t newlen, char *p) |
| 1599 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1600 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1601 | size_t size = stackblocksize(); |
| 1602 | |
| 1603 | for (;;) { |
| 1604 | size_t nleft; |
| 1605 | |
| 1606 | size = stackblocksize(); |
| 1607 | nleft = size - len; |
| 1608 | if (nleft >= newlen) |
| 1609 | break; |
| 1610 | growstackblock(); |
| 1611 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1612 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | static char * |
| 1616 | stack_nputstr(const char *s, size_t n, char *p) |
| 1617 | { |
| 1618 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1619 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1620 | return p; |
| 1621 | } |
| 1622 | |
| 1623 | static char * |
| 1624 | stack_putstr(const char *s, char *p) |
| 1625 | { |
| 1626 | return stack_nputstr(s, strlen(s), p); |
| 1627 | } |
| 1628 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1629 | static char * |
| 1630 | _STPUTC(int c, char *p) |
| 1631 | { |
| 1632 | if (p == sstrend) |
| 1633 | p = growstackstr(); |
| 1634 | *p++ = c; |
| 1635 | return p; |
| 1636 | } |
| 1637 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1638 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1639 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1640 | #define CHECKSTRSPACE(n, p) do { \ |
| 1641 | char *q = (p); \ |
| 1642 | size_t l = (n); \ |
| 1643 | size_t m = sstrend - q; \ |
| 1644 | if (l > m) \ |
| 1645 | (p) = makestrspace(l, q); \ |
| 1646 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1647 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1648 | #define STACKSTRNUL(p) do { \ |
| 1649 | if ((p) == sstrend) \ |
| 1650 | (p) = growstackstr(); \ |
| 1651 | *(p) = '\0'; \ |
| 1652 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1653 | #define STUNPUTC(p) (--(p)) |
| 1654 | #define STTOPC(p) ((p)[-1]) |
| 1655 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1656 | |
| 1657 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1658 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1659 | #define stackstrend() ((void *)sstrend) |
| 1660 | |
| 1661 | |
| 1662 | /* ============ String helpers */ |
| 1663 | |
| 1664 | /* |
| 1665 | * prefix -- see if pfx is a prefix of string. |
| 1666 | */ |
| 1667 | static char * |
| 1668 | prefix(const char *string, const char *pfx) |
| 1669 | { |
| 1670 | while (*pfx) { |
| 1671 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1672 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1673 | } |
| 1674 | return (char *) string; |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * Check for a valid number. This should be elsewhere. |
| 1679 | */ |
| 1680 | static int |
| 1681 | is_number(const char *p) |
| 1682 | { |
| 1683 | do { |
| 1684 | if (!isdigit(*p)) |
| 1685 | return 0; |
| 1686 | } while (*++p != '\0'); |
| 1687 | return 1; |
| 1688 | } |
| 1689 | |
| 1690 | /* |
| 1691 | * Convert a string of digits to an integer, printing an error message on |
| 1692 | * failure. |
| 1693 | */ |
| 1694 | static int |
| 1695 | number(const char *s) |
| 1696 | { |
| 1697 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1698 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1699 | return atoi(s); |
| 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1704 | * The return string is allocated on the stack. |
| 1705 | */ |
| 1706 | static char * |
| 1707 | single_quote(const char *s) |
| 1708 | { |
| 1709 | char *p; |
| 1710 | |
| 1711 | STARTSTACKSTR(p); |
| 1712 | |
| 1713 | do { |
| 1714 | char *q; |
| 1715 | size_t len; |
| 1716 | |
| 1717 | len = strchrnul(s, '\'') - s; |
| 1718 | |
| 1719 | q = p = makestrspace(len + 3, p); |
| 1720 | |
| 1721 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1722 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1723 | *q++ = '\''; |
| 1724 | s += len; |
| 1725 | |
| 1726 | STADJUST(q - p, p); |
| 1727 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1728 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1729 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1730 | len = 0; |
| 1731 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1732 | |
| 1733 | q = p = makestrspace(len + 3, p); |
| 1734 | |
| 1735 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1736 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1737 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1738 | |
| 1739 | STADJUST(q - p, p); |
| 1740 | } while (*s); |
| 1741 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1742 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1743 | |
| 1744 | return stackblock(); |
| 1745 | } |
| 1746 | |
| 1747 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1748 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1749 | |
| 1750 | static char **argptr; /* argument list for builtin commands */ |
| 1751 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1752 | static char *optptr; /* used by nextopt */ |
| 1753 | |
| 1754 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1755 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1756 | * The library getopt must have the BSD extension static variable |
| 1757 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1758 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1759 | * Standard option processing (a la getopt) for builtin routines. |
| 1760 | * The only argument that is passed to nextopt is the option string; |
| 1761 | * the other arguments are unnecessary. It returns the character, |
| 1762 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1763 | */ |
| 1764 | static int |
| 1765 | nextopt(const char *optstring) |
| 1766 | { |
| 1767 | char *p; |
| 1768 | const char *q; |
| 1769 | char c; |
| 1770 | |
| 1771 | p = optptr; |
| 1772 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1773 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1774 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1775 | if (p == NULL) |
| 1776 | return '\0'; |
| 1777 | if (*p != '-') |
| 1778 | return '\0'; |
| 1779 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1780 | return '\0'; |
| 1781 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1782 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1783 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1784 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1785 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1786 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1787 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1788 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1789 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1790 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1791 | if (*++q == ':') |
| 1792 | q++; |
| 1793 | } |
| 1794 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1795 | if (*p == '\0') { |
| 1796 | p = *argptr++; |
| 1797 | if (p == NULL) |
| 1798 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1799 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1800 | optionarg = p; |
| 1801 | p = NULL; |
| 1802 | } |
| 1803 | optptr = p; |
| 1804 | return c; |
| 1805 | } |
| 1806 | |
| 1807 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1808 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1809 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1810 | /* |
| 1811 | * The parsefile structure pointed to by the global variable parsefile |
| 1812 | * contains information about the current file being read. |
| 1813 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1814 | struct shparam { |
| 1815 | int nparam; /* # of positional parameters (without $0) */ |
| 1816 | #if ENABLE_ASH_GETOPTS |
| 1817 | int optind; /* next parameter to be processed by getopts */ |
| 1818 | int optoff; /* used by getopts */ |
| 1819 | #endif |
| 1820 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1821 | char **p; /* parameter list */ |
| 1822 | }; |
| 1823 | |
| 1824 | /* |
| 1825 | * Free the list of positional parameters. |
| 1826 | */ |
| 1827 | static void |
| 1828 | freeparam(volatile struct shparam *param) |
| 1829 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1830 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1831 | char **ap, **ap1; |
| 1832 | ap = ap1 = param->p; |
| 1833 | while (*ap) |
| 1834 | free(*ap++); |
| 1835 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1840 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1841 | #endif |
| 1842 | |
| 1843 | struct var { |
| 1844 | struct var *next; /* next entry in hash list */ |
| 1845 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1846 | const char *var_text; /* name=value */ |
| 1847 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1848 | /* the variable gets set/unset */ |
| 1849 | }; |
| 1850 | |
| 1851 | struct localvar { |
| 1852 | struct localvar *next; /* next local variable in list */ |
| 1853 | struct var *vp; /* the variable that was made local */ |
| 1854 | int flags; /* saved flags */ |
| 1855 | const char *text; /* saved text */ |
| 1856 | }; |
| 1857 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1858 | /* flags */ |
| 1859 | #define VEXPORT 0x01 /* variable is exported */ |
| 1860 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1861 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1862 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1863 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1864 | #define VUNSET 0x20 /* the variable is not set */ |
| 1865 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1866 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1867 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1868 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1869 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1870 | #else |
| 1871 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1872 | #endif |
| 1873 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1874 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1875 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1876 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1877 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1878 | change_lc_all(const char *value) |
| 1879 | { |
| 1880 | if (value && *value != '\0') |
| 1881 | setlocale(LC_ALL, value); |
| 1882 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1883 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1884 | change_lc_ctype(const char *value) |
| 1885 | { |
| 1886 | if (value && *value != '\0') |
| 1887 | setlocale(LC_CTYPE, value); |
| 1888 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1889 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1890 | #if ENABLE_ASH_MAIL |
| 1891 | static void chkmail(void); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 1892 | static void changemail(const char *var_value) FAST_FUNC; |
| 1893 | #else |
| 1894 | # define chkmail() ((void)0) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1895 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1896 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1897 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1898 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1899 | #endif |
| 1900 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1901 | static const struct { |
| 1902 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1903 | const char *var_text; |
| 1904 | void (*var_func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1905 | } varinit_data[] = { |
Denys Vlasenko | 566a313 | 2012-07-07 21:40:35 +0200 | [diff] [blame] | 1906 | /* |
| 1907 | * Note: VEXPORT would not work correctly here for NOFORK applets: |
| 1908 | * some environment strings may be constant. |
| 1909 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1910 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1911 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1912 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 1913 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1914 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1915 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1916 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1917 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1918 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1919 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1920 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1921 | #endif |
| 1922 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1923 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1924 | #endif |
| 1925 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1926 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 1927 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1928 | #endif |
| 1929 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1930 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1931 | #endif |
| 1932 | }; |
| 1933 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1934 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1935 | |
| 1936 | struct globals_var { |
| 1937 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1938 | struct redirtab *redirlist; |
| 1939 | int g_nullredirs; |
| 1940 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1941 | struct var *vartab[VTABSIZE]; |
| 1942 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1943 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1944 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1945 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1946 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1947 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1948 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1949 | #define preverrout_fd (G_var.preverrout_fd) |
| 1950 | #define vartab (G_var.vartab ) |
| 1951 | #define varinit (G_var.varinit ) |
| 1952 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1953 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1954 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1955 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1956 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1957 | varinit[i].flags = varinit_data[i].flags; \ |
| 1958 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 1959 | varinit[i].var_func = varinit_data[i].var_func; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1960 | } \ |
| 1961 | } while (0) |
| 1962 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1963 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1964 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1965 | # define vmail (&vifs)[1] |
| 1966 | # define vmpath (&vmail)[1] |
| 1967 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1968 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1969 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1970 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1971 | #define vps1 (&vpath)[1] |
| 1972 | #define vps2 (&vps1)[1] |
| 1973 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1974 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1975 | # define voptind (&vps4)[1] |
| 1976 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1977 | # define vrandom (&voptind)[1] |
| 1978 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1979 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1980 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1981 | # define vrandom (&vps4)[1] |
| 1982 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1983 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1984 | |
| 1985 | /* |
| 1986 | * The following macros access the values of the above variables. |
| 1987 | * They have to skip over the name. They return the null string |
| 1988 | * for unset variables. |
| 1989 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1990 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1991 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1992 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1993 | # define mailval() (vmail.var_text + 5) |
| 1994 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1995 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1996 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1997 | #define pathval() (vpath.var_text + 5) |
| 1998 | #define ps1val() (vps1.var_text + 4) |
| 1999 | #define ps2val() (vps2.var_text + 4) |
| 2000 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2001 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2002 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 2003 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2004 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2005 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2006 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2007 | getoptsreset(const char *value) |
| 2008 | { |
| 2009 | shellparam.optind = number(value); |
| 2010 | shellparam.optoff = -1; |
| 2011 | } |
| 2012 | #endif |
| 2013 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2014 | /* |
| 2015 | * Compares two strings up to the first = or '\0'. The first |
| 2016 | * string must be terminated by '='; the second may be terminated by |
| 2017 | * either '=' or '\0'. |
| 2018 | */ |
| 2019 | static int |
| 2020 | varcmp(const char *p, const char *q) |
| 2021 | { |
| 2022 | int c, d; |
| 2023 | |
| 2024 | while ((c = *p) == (d = *q)) { |
| 2025 | if (!c || c == '=') |
| 2026 | goto out; |
| 2027 | p++; |
| 2028 | q++; |
| 2029 | } |
| 2030 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2031 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2032 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2033 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2034 | out: |
| 2035 | return c - d; |
| 2036 | } |
| 2037 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2038 | /* |
| 2039 | * Find the appropriate entry in the hash table from the name. |
| 2040 | */ |
| 2041 | static struct var ** |
| 2042 | hashvar(const char *p) |
| 2043 | { |
| 2044 | unsigned hashval; |
| 2045 | |
| 2046 | hashval = ((unsigned char) *p) << 4; |
| 2047 | while (*p && *p != '=') |
| 2048 | hashval += (unsigned char) *p++; |
| 2049 | return &vartab[hashval % VTABSIZE]; |
| 2050 | } |
| 2051 | |
| 2052 | static int |
| 2053 | vpcmp(const void *a, const void *b) |
| 2054 | { |
| 2055 | return varcmp(*(const char **)a, *(const char **)b); |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * This routine initializes the builtin variables. |
| 2060 | */ |
| 2061 | static void |
| 2062 | initvar(void) |
| 2063 | { |
| 2064 | struct var *vp; |
| 2065 | struct var *end; |
| 2066 | struct var **vpp; |
| 2067 | |
| 2068 | /* |
| 2069 | * PS1 depends on uid |
| 2070 | */ |
| 2071 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2072 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2073 | #else |
| 2074 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2075 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2076 | #endif |
| 2077 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2078 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2079 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2080 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2081 | vp->next = *vpp; |
| 2082 | *vpp = vp; |
| 2083 | } while (++vp < end); |
| 2084 | } |
| 2085 | |
| 2086 | static struct var ** |
| 2087 | findvar(struct var **vpp, const char *name) |
| 2088 | { |
| 2089 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2090 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2091 | break; |
| 2092 | } |
| 2093 | } |
| 2094 | return vpp; |
| 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * Find the value of a variable. Returns NULL if not set. |
| 2099 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2100 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2101 | lookupvar(const char *name) |
| 2102 | { |
| 2103 | struct var *v; |
| 2104 | |
| 2105 | v = *findvar(hashvar(name), name); |
| 2106 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2107 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2108 | /* |
| 2109 | * Dynamic variables are implemented roughly the same way they are |
| 2110 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2111 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2112 | * lookup will no longer happen at that point. -- PFM. |
| 2113 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2114 | if (v->flags & VDYNAMIC) |
| 2115 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2116 | #endif |
| 2117 | if (!(v->flags & VUNSET)) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2118 | return var_end(v->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2119 | } |
| 2120 | return NULL; |
| 2121 | } |
| 2122 | |
| 2123 | /* |
| 2124 | * Search the environment of a builtin command. |
| 2125 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2126 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2127 | bltinlookup(const char *name) |
| 2128 | { |
| 2129 | struct strlist *sp; |
| 2130 | |
| 2131 | for (sp = cmdenviron; sp; sp = sp->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2132 | if (varcmp(sp->text, name) == 0) |
| 2133 | return var_end(sp->text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2134 | } |
| 2135 | return lookupvar(name); |
| 2136 | } |
| 2137 | |
| 2138 | /* |
| 2139 | * Same as setvar except that the variable and value are passed in |
| 2140 | * the first argument as name=value. Since the first argument will |
| 2141 | * be actually stored in the table, it should not be a string that |
| 2142 | * will go away. |
| 2143 | * Called with interrupts off. |
| 2144 | */ |
| 2145 | static void |
| 2146 | setvareq(char *s, int flags) |
| 2147 | { |
| 2148 | struct var *vp, **vpp; |
| 2149 | |
| 2150 | vpp = hashvar(s); |
| 2151 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2152 | vp = *findvar(vpp, s); |
| 2153 | if (vp) { |
| 2154 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2155 | const char *n; |
| 2156 | |
| 2157 | if (flags & VNOSAVE) |
| 2158 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2159 | n = vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2160 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2161 | } |
| 2162 | |
| 2163 | if (flags & VNOSET) |
| 2164 | return; |
| 2165 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2166 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2167 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2168 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2169 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2170 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2171 | |
| 2172 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2173 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2174 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2175 | if (flags & VNOSET) |
| 2176 | return; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2177 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2178 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2179 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2180 | *vpp = vp; |
| 2181 | } |
| 2182 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2183 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2184 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2185 | vp->flags = flags; |
| 2186 | } |
| 2187 | |
| 2188 | /* |
| 2189 | * Set the value of a variable. The flags argument is ored with the |
| 2190 | * flags of the variable. If val is NULL, the variable is unset. |
| 2191 | */ |
| 2192 | static void |
| 2193 | setvar(const char *name, const char *val, int flags) |
| 2194 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2195 | const char *q; |
| 2196 | char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2197 | char *nameeq; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2198 | size_t namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2199 | size_t vallen; |
| 2200 | |
| 2201 | q = endofname(name); |
| 2202 | p = strchrnul(q, '='); |
| 2203 | namelen = p - name; |
| 2204 | if (!namelen || p != q) |
| 2205 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2206 | vallen = 0; |
| 2207 | if (val == NULL) { |
| 2208 | flags |= VUNSET; |
| 2209 | } else { |
| 2210 | vallen = strlen(val); |
| 2211 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2212 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2213 | INT_OFF; |
| 2214 | nameeq = ckmalloc(namelen + vallen + 2); |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2215 | p = memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2216 | if (val) { |
| 2217 | *p++ = '='; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2218 | p = memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2219 | } |
| 2220 | *p = '\0'; |
| 2221 | setvareq(nameeq, flags | VNOSAVE); |
| 2222 | INT_ON; |
| 2223 | } |
| 2224 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2225 | static void FAST_FUNC |
| 2226 | setvar2(const char *name, const char *val) |
| 2227 | { |
| 2228 | setvar(name, val, 0); |
| 2229 | } |
| 2230 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2231 | #if ENABLE_ASH_GETOPTS |
| 2232 | /* |
| 2233 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2234 | */ |
| 2235 | static int |
| 2236 | setvarsafe(const char *name, const char *val, int flags) |
| 2237 | { |
| 2238 | int err; |
| 2239 | volatile int saveint; |
| 2240 | struct jmploc *volatile savehandler = exception_handler; |
| 2241 | struct jmploc jmploc; |
| 2242 | |
| 2243 | SAVE_INT(saveint); |
| 2244 | if (setjmp(jmploc.loc)) |
| 2245 | err = 1; |
| 2246 | else { |
| 2247 | exception_handler = &jmploc; |
| 2248 | setvar(name, val, flags); |
| 2249 | err = 0; |
| 2250 | } |
| 2251 | exception_handler = savehandler; |
| 2252 | RESTORE_INT(saveint); |
| 2253 | return err; |
| 2254 | } |
| 2255 | #endif |
| 2256 | |
| 2257 | /* |
| 2258 | * Unset the specified variable. |
| 2259 | */ |
| 2260 | static int |
| 2261 | unsetvar(const char *s) |
| 2262 | { |
| 2263 | struct var **vpp; |
| 2264 | struct var *vp; |
| 2265 | int retval; |
| 2266 | |
| 2267 | vpp = findvar(hashvar(s), s); |
| 2268 | vp = *vpp; |
| 2269 | retval = 2; |
| 2270 | if (vp) { |
| 2271 | int flags = vp->flags; |
| 2272 | |
| 2273 | retval = 1; |
| 2274 | if (flags & VREADONLY) |
| 2275 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2276 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2277 | vp->flags &= ~VDYNAMIC; |
| 2278 | #endif |
| 2279 | if (flags & VUNSET) |
| 2280 | goto ok; |
| 2281 | if ((flags & VSTRFIXED) == 0) { |
| 2282 | INT_OFF; |
| 2283 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2284 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2285 | *vpp = vp->next; |
| 2286 | free(vp); |
| 2287 | INT_ON; |
| 2288 | } else { |
| 2289 | setvar(s, 0, 0); |
| 2290 | vp->flags &= ~VEXPORT; |
| 2291 | } |
| 2292 | ok: |
| 2293 | retval = 0; |
| 2294 | } |
| 2295 | out: |
| 2296 | return retval; |
| 2297 | } |
| 2298 | |
| 2299 | /* |
| 2300 | * Process a linked list of variable assignments. |
| 2301 | */ |
| 2302 | static void |
| 2303 | listsetvar(struct strlist *list_set_var, int flags) |
| 2304 | { |
| 2305 | struct strlist *lp = list_set_var; |
| 2306 | |
| 2307 | if (!lp) |
| 2308 | return; |
| 2309 | INT_OFF; |
| 2310 | do { |
| 2311 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2312 | lp = lp->next; |
| 2313 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2314 | INT_ON; |
| 2315 | } |
| 2316 | |
| 2317 | /* |
| 2318 | * Generate a list of variables satisfying the given conditions. |
| 2319 | */ |
| 2320 | static char ** |
| 2321 | listvars(int on, int off, char ***end) |
| 2322 | { |
| 2323 | struct var **vpp; |
| 2324 | struct var *vp; |
| 2325 | char **ep; |
| 2326 | int mask; |
| 2327 | |
| 2328 | STARTSTACKSTR(ep); |
| 2329 | vpp = vartab; |
| 2330 | mask = on | off; |
| 2331 | do { |
| 2332 | for (vp = *vpp; vp; vp = vp->next) { |
| 2333 | if ((vp->flags & mask) == on) { |
| 2334 | if (ep == stackstrend()) |
| 2335 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2336 | *ep++ = (char*)vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2337 | } |
| 2338 | } |
| 2339 | } while (++vpp < vartab + VTABSIZE); |
| 2340 | if (ep == stackstrend()) |
| 2341 | ep = growstackstr(); |
| 2342 | if (end) |
| 2343 | *end = ep; |
| 2344 | *ep++ = NULL; |
| 2345 | return grabstackstr(ep); |
| 2346 | } |
| 2347 | |
| 2348 | |
| 2349 | /* ============ Path search helper |
| 2350 | * |
| 2351 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2352 | * of the path before the first call; path_advance will update |
| 2353 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2354 | * the possible path expansions in sequence. If an option (indicated by |
| 2355 | * a percent sign) appears in the path entry then the global variable |
| 2356 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2357 | * NULL. |
| 2358 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2359 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2360 | |
| 2361 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2362 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2363 | { |
| 2364 | const char *p; |
| 2365 | char *q; |
| 2366 | const char *start; |
| 2367 | size_t len; |
| 2368 | |
| 2369 | if (*path == NULL) |
| 2370 | return NULL; |
| 2371 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2372 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2373 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2374 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2375 | while (stackblocksize() < len) |
| 2376 | growstackblock(); |
| 2377 | q = stackblock(); |
| 2378 | if (p != start) { |
| 2379 | memcpy(q, start, p - start); |
| 2380 | q += p - start; |
| 2381 | *q++ = '/'; |
| 2382 | } |
| 2383 | strcpy(q, name); |
| 2384 | pathopt = NULL; |
| 2385 | if (*p == '%') { |
| 2386 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2387 | while (*p && *p != ':') |
| 2388 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2389 | } |
| 2390 | if (*p == ':') |
| 2391 | *path = p + 1; |
| 2392 | else |
| 2393 | *path = NULL; |
| 2394 | return stalloc(len); |
| 2395 | } |
| 2396 | |
| 2397 | |
| 2398 | /* ============ Prompt */ |
| 2399 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2400 | static smallint doprompt; /* if set, prompt the user */ |
| 2401 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2402 | |
| 2403 | #if ENABLE_FEATURE_EDITING |
| 2404 | static line_input_t *line_input_state; |
| 2405 | static const char *cmdedit_prompt; |
| 2406 | static void |
| 2407 | putprompt(const char *s) |
| 2408 | { |
| 2409 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2410 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2411 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
| 2414 | cmdedit_prompt = s; |
| 2415 | } |
| 2416 | #else |
| 2417 | static void |
| 2418 | putprompt(const char *s) |
| 2419 | { |
| 2420 | out2str(s); |
| 2421 | } |
| 2422 | #endif |
| 2423 | |
| 2424 | #if ENABLE_ASH_EXPAND_PRMT |
| 2425 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2426 | static const char *expandstr(const char *ps); |
| 2427 | #else |
| 2428 | #define expandstr(s) s |
| 2429 | #endif |
| 2430 | |
| 2431 | static void |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2432 | setprompt_if(smallint do_set, int whichprompt) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2433 | { |
| 2434 | const char *prompt; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2435 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
| 2436 | |
| 2437 | if (!do_set) |
| 2438 | return; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2439 | |
| 2440 | needprompt = 0; |
| 2441 | |
| 2442 | switch (whichprompt) { |
| 2443 | case 1: |
| 2444 | prompt = ps1val(); |
| 2445 | break; |
| 2446 | case 2: |
| 2447 | prompt = ps2val(); |
| 2448 | break; |
| 2449 | default: /* 0 */ |
| 2450 | prompt = nullstr; |
| 2451 | } |
| 2452 | #if ENABLE_ASH_EXPAND_PRMT |
| 2453 | setstackmark(&smark); |
| 2454 | stalloc(stackblocksize()); |
| 2455 | #endif |
| 2456 | putprompt(expandstr(prompt)); |
| 2457 | #if ENABLE_ASH_EXPAND_PRMT |
| 2458 | popstackmark(&smark); |
| 2459 | #endif |
| 2460 | } |
| 2461 | |
| 2462 | |
| 2463 | /* ============ The cd and pwd commands */ |
| 2464 | |
| 2465 | #define CD_PHYSICAL 1 |
| 2466 | #define CD_PRINT 2 |
| 2467 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2468 | static int |
| 2469 | cdopt(void) |
| 2470 | { |
| 2471 | int flags = 0; |
| 2472 | int i, j; |
| 2473 | |
| 2474 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2475 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2476 | if (i != j) { |
| 2477 | flags ^= CD_PHYSICAL; |
| 2478 | j = i; |
| 2479 | } |
| 2480 | } |
| 2481 | |
| 2482 | return flags; |
| 2483 | } |
| 2484 | |
| 2485 | /* |
| 2486 | * Update curdir (the name of the current directory) in response to a |
| 2487 | * cd command. |
| 2488 | */ |
| 2489 | static const char * |
| 2490 | updatepwd(const char *dir) |
| 2491 | { |
| 2492 | char *new; |
| 2493 | char *p; |
| 2494 | char *cdcomppath; |
| 2495 | const char *lim; |
| 2496 | |
| 2497 | cdcomppath = ststrdup(dir); |
| 2498 | STARTSTACKSTR(new); |
| 2499 | if (*dir != '/') { |
| 2500 | if (curdir == nullstr) |
| 2501 | return 0; |
| 2502 | new = stack_putstr(curdir, new); |
| 2503 | } |
| 2504 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2505 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2506 | if (*dir != '/') { |
| 2507 | if (new[-1] != '/') |
| 2508 | USTPUTC('/', new); |
| 2509 | if (new > lim && *lim == '/') |
| 2510 | lim++; |
| 2511 | } else { |
| 2512 | USTPUTC('/', new); |
| 2513 | cdcomppath++; |
| 2514 | if (dir[1] == '/' && dir[2] != '/') { |
| 2515 | USTPUTC('/', new); |
| 2516 | cdcomppath++; |
| 2517 | lim++; |
| 2518 | } |
| 2519 | } |
| 2520 | p = strtok(cdcomppath, "/"); |
| 2521 | while (p) { |
| 2522 | switch (*p) { |
| 2523 | case '.': |
| 2524 | if (p[1] == '.' && p[2] == '\0') { |
| 2525 | while (new > lim) { |
| 2526 | STUNPUTC(new); |
| 2527 | if (new[-1] == '/') |
| 2528 | break; |
| 2529 | } |
| 2530 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2531 | } |
| 2532 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2533 | break; |
| 2534 | /* fall through */ |
| 2535 | default: |
| 2536 | new = stack_putstr(p, new); |
| 2537 | USTPUTC('/', new); |
| 2538 | } |
| 2539 | p = strtok(0, "/"); |
| 2540 | } |
| 2541 | if (new > lim) |
| 2542 | STUNPUTC(new); |
| 2543 | *new = 0; |
| 2544 | return stackblock(); |
| 2545 | } |
| 2546 | |
| 2547 | /* |
| 2548 | * Find out what the current directory is. If we already know the current |
| 2549 | * directory, this routine returns immediately. |
| 2550 | */ |
| 2551 | static char * |
| 2552 | getpwd(void) |
| 2553 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2554 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2555 | return dir ? dir : nullstr; |
| 2556 | } |
| 2557 | |
| 2558 | static void |
| 2559 | setpwd(const char *val, int setold) |
| 2560 | { |
| 2561 | char *oldcur, *dir; |
| 2562 | |
| 2563 | oldcur = dir = curdir; |
| 2564 | |
| 2565 | if (setold) { |
| 2566 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2567 | } |
| 2568 | INT_OFF; |
| 2569 | if (physdir != nullstr) { |
| 2570 | if (physdir != oldcur) |
| 2571 | free(physdir); |
| 2572 | physdir = nullstr; |
| 2573 | } |
| 2574 | if (oldcur == val || !val) { |
| 2575 | char *s = getpwd(); |
| 2576 | physdir = s; |
| 2577 | if (!val) |
| 2578 | dir = s; |
| 2579 | } else |
| 2580 | dir = ckstrdup(val); |
| 2581 | if (oldcur != dir && oldcur != nullstr) { |
| 2582 | free(oldcur); |
| 2583 | } |
| 2584 | curdir = dir; |
| 2585 | INT_ON; |
| 2586 | setvar("PWD", dir, VEXPORT); |
| 2587 | } |
| 2588 | |
| 2589 | static void hashcd(void); |
| 2590 | |
| 2591 | /* |
| 2592 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2593 | * know that the current directory has changed. |
| 2594 | */ |
| 2595 | static int |
| 2596 | docd(const char *dest, int flags) |
| 2597 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2598 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2599 | int err; |
| 2600 | |
| 2601 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2602 | |
| 2603 | INT_OFF; |
| 2604 | if (!(flags & CD_PHYSICAL)) { |
| 2605 | dir = updatepwd(dest); |
| 2606 | if (dir) |
| 2607 | dest = dir; |
| 2608 | } |
| 2609 | err = chdir(dest); |
| 2610 | if (err) |
| 2611 | goto out; |
| 2612 | setpwd(dir, 1); |
| 2613 | hashcd(); |
| 2614 | out: |
| 2615 | INT_ON; |
| 2616 | return err; |
| 2617 | } |
| 2618 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2619 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2620 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2621 | { |
| 2622 | const char *dest; |
| 2623 | const char *path; |
| 2624 | const char *p; |
| 2625 | char c; |
| 2626 | struct stat statb; |
| 2627 | int flags; |
| 2628 | |
| 2629 | flags = cdopt(); |
| 2630 | dest = *argptr; |
| 2631 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2632 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2633 | else if (LONE_DASH(dest)) { |
| 2634 | dest = bltinlookup("OLDPWD"); |
| 2635 | flags |= CD_PRINT; |
| 2636 | } |
| 2637 | if (!dest) |
| 2638 | dest = nullstr; |
| 2639 | if (*dest == '/') |
| 2640 | goto step7; |
| 2641 | if (*dest == '.') { |
| 2642 | c = dest[1]; |
| 2643 | dotdot: |
| 2644 | switch (c) { |
| 2645 | case '\0': |
| 2646 | case '/': |
| 2647 | goto step6; |
| 2648 | case '.': |
| 2649 | c = dest[2]; |
| 2650 | if (c != '.') |
| 2651 | goto dotdot; |
| 2652 | } |
| 2653 | } |
| 2654 | if (!*dest) |
| 2655 | dest = "."; |
| 2656 | path = bltinlookup("CDPATH"); |
| 2657 | if (!path) { |
| 2658 | step6: |
| 2659 | step7: |
| 2660 | p = dest; |
| 2661 | goto docd; |
| 2662 | } |
| 2663 | do { |
| 2664 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2665 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2666 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2667 | if (c && c != ':') |
| 2668 | flags |= CD_PRINT; |
| 2669 | docd: |
| 2670 | if (!docd(p, flags)) |
| 2671 | goto out; |
| 2672 | break; |
| 2673 | } |
| 2674 | } while (path); |
| 2675 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2676 | /* NOTREACHED */ |
| 2677 | out: |
| 2678 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2679 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2680 | return 0; |
| 2681 | } |
| 2682 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2683 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2684 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2685 | { |
| 2686 | int flags; |
| 2687 | const char *dir = curdir; |
| 2688 | |
| 2689 | flags = cdopt(); |
| 2690 | if (flags) { |
| 2691 | if (physdir == nullstr) |
| 2692 | setpwd(dir, 0); |
| 2693 | dir = physdir; |
| 2694 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2695 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2696 | return 0; |
| 2697 | } |
| 2698 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2699 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2700 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2701 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2702 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2703 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2704 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2705 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2706 | #define CWORD 0 /* character is nothing special */ |
| 2707 | #define CNL 1 /* newline character */ |
| 2708 | #define CBACK 2 /* a backslash character */ |
| 2709 | #define CSQUOTE 3 /* single quote */ |
| 2710 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2711 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2712 | #define CBQUOTE 6 /* backwards single quote */ |
| 2713 | #define CVAR 7 /* a dollar sign */ |
| 2714 | #define CENDVAR 8 /* a '}' character */ |
| 2715 | #define CLP 9 /* a left paren in arithmetic */ |
| 2716 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2717 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2718 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2719 | #define CSPCL 13 /* these terminate a word */ |
| 2720 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2721 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2722 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2723 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2724 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2725 | #endif |
| 2726 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2727 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2728 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2729 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2730 | # 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] | 2731 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2732 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2733 | #endif |
Denys Vlasenko | 068d386 | 2009-11-29 01:41:11 +0100 | [diff] [blame] | 2734 | static const uint16_t S_I_T[] = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2735 | #if ENABLE_ASH_ALIAS |
| 2736 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2737 | #endif |
| 2738 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2739 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2740 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2741 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2742 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2743 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2744 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 2745 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 2746 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 2747 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 2748 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2749 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2750 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 2751 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 2752 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 2753 | #endif |
| 2754 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2755 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2756 | /* Constants below must match table above */ |
| 2757 | enum { |
| 2758 | #if ENABLE_ASH_ALIAS |
| 2759 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 2760 | #endif |
| 2761 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 2762 | CNL_CNL_CNL_CNL , /* 2 */ |
| 2763 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 2764 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 2765 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 2766 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 2767 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 2768 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 2769 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 2770 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 2771 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 2772 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 2773 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 2774 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 2775 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2776 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2777 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 2778 | * caller must ensure proper cast on it if c is *char_ptr! |
| 2779 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2780 | /* Values for syntax param */ |
| 2781 | #define BASESYNTAX 0 /* not in quotes */ |
| 2782 | #define DQSYNTAX 1 /* in double quotes */ |
| 2783 | #define SQSYNTAX 2 /* in single quotes */ |
| 2784 | #define ARISYNTAX 3 /* in arithmetic */ |
| 2785 | #define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2786 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2787 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2788 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2789 | static int |
| 2790 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2791 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2792 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2793 | # if ENABLE_ASH_ALIAS |
| 2794 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2795 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2796 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2797 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2798 | 11, 3 /* "}~" */ |
| 2799 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2800 | # else |
| 2801 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2802 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2803 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2804 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2805 | 10, 2 /* "}~" */ |
| 2806 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2807 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2808 | const char *s; |
| 2809 | int indx; |
| 2810 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2811 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2812 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2813 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2814 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2815 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2816 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2817 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2818 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2819 | /* Cast is purely for paranoia here, |
| 2820 | * just in case someone passed signed char to us */ |
| 2821 | if ((unsigned char)c >= CTL_FIRST |
| 2822 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2823 | ) { |
| 2824 | return CCTL; |
| 2825 | } |
| 2826 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2827 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2828 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2829 | indx = syntax_index_table[s - spec_symbls]; |
| 2830 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2831 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2834 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2835 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2836 | static const uint8_t syntax_index_table[] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2837 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2838 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2839 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2840 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2841 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2842 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2848 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2849 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2852 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2853 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2854 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2855 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2856 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2859 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2860 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2861 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2862 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2863 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2864 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2871 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 2872 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 2873 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2875 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2876 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 2877 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 2878 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2879 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2880 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2881 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2882 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2883 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2884 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2885 | /* 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2886 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2888 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2890 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2897 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2898 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2899 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2900 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2901 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2902 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2903 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2904 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2905 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2906 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2924 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2926 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2927 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2928 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2929 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2930 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2931 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2932 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2934 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2935 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2936 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2939 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2963 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2964 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2965 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2967 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2968 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2969 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2970 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2971 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2972 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2973 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2974 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 2975 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 2976 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 2977 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 2978 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 2979 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 2985 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 2986 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 2987 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 2988 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 2989 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 2990 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 2991 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 2992 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 2993 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 2994 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 2995 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 2996 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 2997 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 2998 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 2999 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 3000 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 3001 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 3002 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 3003 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 3004 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 3005 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 3006 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 3007 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 3008 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 3009 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 3010 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 3011 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 3012 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 3013 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 3014 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 3015 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 3016 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 3017 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 3018 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 3019 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 3020 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 3021 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 3022 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 3023 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 3024 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 3025 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 3026 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 3027 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 3028 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 3029 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 3030 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 3031 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 3032 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 3033 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 3034 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 3035 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 3036 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 3037 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 3038 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 3039 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 3040 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 3041 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 3042 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 3043 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 3044 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 3045 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 3046 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 3047 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 3048 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 3049 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 3050 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 3051 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 3052 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 3053 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 3054 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 3055 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 3056 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 3057 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 3058 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 3059 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 3060 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 3061 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 3062 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 3063 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 3064 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 3065 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 3066 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 3067 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 3068 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3069 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 3070 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 3071 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 3072 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 3073 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 3074 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 3075 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 3076 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 3077 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 3078 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3079 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 3080 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 3081 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 3082 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 3083 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 3084 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 3085 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 3086 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 3087 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 3088 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 3089 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 3090 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 3091 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 3092 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 3093 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3094 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3095 | # if ENABLE_ASH_ALIAS |
| 3096 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 3097 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3098 | }; |
| 3099 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3100 | # 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] | 3101 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3102 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3103 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3104 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3105 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3106 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3107 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3108 | |
| 3109 | #define ALIASINUSE 1 |
| 3110 | #define ALIASDEAD 2 |
| 3111 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3112 | struct alias { |
| 3113 | struct alias *next; |
| 3114 | char *name; |
| 3115 | char *val; |
| 3116 | int flag; |
| 3117 | }; |
| 3118 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3119 | |
| 3120 | static struct alias **atab; // [ATABSIZE]; |
| 3121 | #define INIT_G_alias() do { \ |
| 3122 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3123 | } while (0) |
| 3124 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3125 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3126 | static struct alias ** |
| 3127 | __lookupalias(const char *name) { |
| 3128 | unsigned int hashval; |
| 3129 | struct alias **app; |
| 3130 | const char *p; |
| 3131 | unsigned int ch; |
| 3132 | |
| 3133 | p = name; |
| 3134 | |
| 3135 | ch = (unsigned char)*p; |
| 3136 | hashval = ch << 4; |
| 3137 | while (ch) { |
| 3138 | hashval += ch; |
| 3139 | ch = (unsigned char)*++p; |
| 3140 | } |
| 3141 | app = &atab[hashval % ATABSIZE]; |
| 3142 | |
| 3143 | for (; *app; app = &(*app)->next) { |
| 3144 | if (strcmp(name, (*app)->name) == 0) { |
| 3145 | break; |
| 3146 | } |
| 3147 | } |
| 3148 | |
| 3149 | return app; |
| 3150 | } |
| 3151 | |
| 3152 | static struct alias * |
| 3153 | lookupalias(const char *name, int check) |
| 3154 | { |
| 3155 | struct alias *ap = *__lookupalias(name); |
| 3156 | |
| 3157 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3158 | return NULL; |
| 3159 | return ap; |
| 3160 | } |
| 3161 | |
| 3162 | static struct alias * |
| 3163 | freealias(struct alias *ap) |
| 3164 | { |
| 3165 | struct alias *next; |
| 3166 | |
| 3167 | if (ap->flag & ALIASINUSE) { |
| 3168 | ap->flag |= ALIASDEAD; |
| 3169 | return ap; |
| 3170 | } |
| 3171 | |
| 3172 | next = ap->next; |
| 3173 | free(ap->name); |
| 3174 | free(ap->val); |
| 3175 | free(ap); |
| 3176 | return next; |
| 3177 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3178 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3179 | static void |
| 3180 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3181 | { |
| 3182 | struct alias *ap, **app; |
| 3183 | |
| 3184 | app = __lookupalias(name); |
| 3185 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3186 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3187 | if (ap) { |
| 3188 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3189 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3190 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3191 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3192 | ap->flag &= ~ALIASDEAD; |
| 3193 | } else { |
| 3194 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3195 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3196 | ap->name = ckstrdup(name); |
| 3197 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3198 | /*ap->flag = 0; - ckzalloc did it */ |
| 3199 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3200 | *app = ap; |
| 3201 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3202 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3205 | static int |
| 3206 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3207 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3208 | struct alias **app; |
| 3209 | |
| 3210 | app = __lookupalias(name); |
| 3211 | |
| 3212 | if (*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 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3215 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3216 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3219 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3222 | static void |
| 3223 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3224 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3225 | struct alias *ap, **app; |
| 3226 | int i; |
| 3227 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3228 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3229 | for (i = 0; i < ATABSIZE; i++) { |
| 3230 | app = &atab[i]; |
| 3231 | for (ap = *app; ap; ap = *app) { |
| 3232 | *app = freealias(*app); |
| 3233 | if (ap == *app) { |
| 3234 | app = &ap->next; |
| 3235 | } |
| 3236 | } |
| 3237 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3238 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3241 | static void |
| 3242 | printalias(const struct alias *ap) |
| 3243 | { |
| 3244 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3245 | } |
| 3246 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3247 | /* |
| 3248 | * TODO - sort output |
| 3249 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3250 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3251 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3252 | { |
| 3253 | char *n, *v; |
| 3254 | int ret = 0; |
| 3255 | struct alias *ap; |
| 3256 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3257 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3258 | int i; |
| 3259 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3260 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3261 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3262 | printalias(ap); |
| 3263 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3264 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3265 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3266 | } |
| 3267 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3268 | v = strchr(n+1, '='); |
| 3269 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3270 | ap = *__lookupalias(n); |
| 3271 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3272 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3273 | ret = 1; |
| 3274 | } else |
| 3275 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3276 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3277 | *v++ = '\0'; |
| 3278 | setalias(n, v); |
| 3279 | } |
| 3280 | } |
| 3281 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3282 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3285 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3286 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3287 | { |
| 3288 | int i; |
| 3289 | |
| 3290 | while ((i = nextopt("a")) != '\0') { |
| 3291 | if (i == 'a') { |
| 3292 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3293 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3294 | } |
| 3295 | } |
| 3296 | for (i = 0; *argptr; argptr++) { |
| 3297 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3298 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3299 | i = 1; |
| 3300 | } |
| 3301 | } |
| 3302 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3303 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3304 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3305 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3306 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3307 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3308 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3309 | /* ============ jobs.c */ |
| 3310 | |
| 3311 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3312 | #define FORK_FG 0 |
| 3313 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3314 | #define FORK_NOJOB 2 |
| 3315 | |
| 3316 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3317 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3318 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3319 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3320 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3321 | /* |
| 3322 | * A job structure contains information about a job. A job is either a |
| 3323 | * single process or a set of processes contained in a pipeline. In the |
| 3324 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3325 | * array of pids. |
| 3326 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3327 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3328 | pid_t ps_pid; /* process id */ |
| 3329 | int ps_status; /* last process status from wait() */ |
| 3330 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3331 | }; |
| 3332 | |
| 3333 | struct job { |
| 3334 | struct procstat ps0; /* status of process */ |
| 3335 | struct procstat *ps; /* status or processes when more than one */ |
| 3336 | #if JOBS |
| 3337 | int stopstatus; /* status of a stopped job */ |
| 3338 | #endif |
| 3339 | uint32_t |
| 3340 | nprocs: 16, /* number of processes */ |
| 3341 | state: 8, |
| 3342 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3343 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3344 | #define JOBDONE 2 /* all procs are completed */ |
| 3345 | #if JOBS |
| 3346 | sigint: 1, /* job was killed by SIGINT */ |
| 3347 | jobctl: 1, /* job running under job control */ |
| 3348 | #endif |
| 3349 | waited: 1, /* true if this entry has been waited for */ |
| 3350 | used: 1, /* true if this entry is in used */ |
| 3351 | changed: 1; /* true if status has changed */ |
| 3352 | struct job *prev_job; /* previous job */ |
| 3353 | }; |
| 3354 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3355 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3356 | static int forkshell(struct job *, union node *, int); |
| 3357 | static int waitforjob(struct job *); |
| 3358 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3359 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3360 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3361 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3362 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3363 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3364 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3365 | #endif |
| 3366 | |
| 3367 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3368 | * Ignore a signal. |
| 3369 | */ |
| 3370 | static void |
| 3371 | ignoresig(int signo) |
| 3372 | { |
| 3373 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3374 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3375 | /* No, need to do it */ |
| 3376 | signal(signo, SIG_IGN); |
| 3377 | } |
| 3378 | sigmode[signo - 1] = S_HARD_IGN; |
| 3379 | } |
| 3380 | |
| 3381 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3382 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3383 | */ |
| 3384 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3385 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3386 | { |
| 3387 | gotsig[signo - 1] = 1; |
| 3388 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3389 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3390 | if (!suppress_int) { |
| 3391 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3392 | raise_interrupt(); /* does not return */ |
| 3393 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3394 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3395 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3396 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3401 | * Set the signal handler for the specified signal. The routine figures |
| 3402 | * out what it should be set to. |
| 3403 | */ |
| 3404 | static void |
| 3405 | setsignal(int signo) |
| 3406 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3407 | char *t; |
| 3408 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3409 | struct sigaction act; |
| 3410 | |
| 3411 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3412 | new_act = S_DFL; |
| 3413 | if (t != NULL) { /* trap for this sig is set */ |
| 3414 | new_act = S_CATCH; |
| 3415 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3416 | new_act = S_IGN; |
| 3417 | } |
| 3418 | |
| 3419 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3420 | switch (signo) { |
| 3421 | case SIGINT: |
| 3422 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3423 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | case SIGQUIT: |
| 3426 | #if DEBUG |
| 3427 | if (debug) |
| 3428 | break; |
| 3429 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3430 | /* man bash: |
| 3431 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3432 | * commands run by bash have signal handlers |
| 3433 | * set to the values inherited by the shell |
| 3434 | * from its parent". */ |
| 3435 | new_act = S_IGN; |
| 3436 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3437 | case SIGTERM: |
| 3438 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3439 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3440 | break; |
| 3441 | #if JOBS |
| 3442 | case SIGTSTP: |
| 3443 | case SIGTTOU: |
| 3444 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3445 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3446 | break; |
| 3447 | #endif |
| 3448 | } |
| 3449 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3450 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3451 | //whereas we have to restore it to what shell got on entry |
| 3452 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3453 | |
| 3454 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3455 | cur_act = *t; |
| 3456 | if (cur_act == 0) { |
| 3457 | /* current setting is not yet known */ |
| 3458 | if (sigaction(signo, NULL, &act)) { |
| 3459 | /* pretend it worked; maybe we should give a warning, |
| 3460 | * but other shells don't. We don't alter sigmode, |
| 3461 | * so we retry every time. |
| 3462 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3463 | return; |
| 3464 | } |
| 3465 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3466 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3467 | if (mflag |
| 3468 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3469 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3470 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3471 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3472 | } |
| 3473 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3474 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3475 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3476 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3477 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3478 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3479 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3480 | act.sa_handler = signal_handler; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3481 | break; |
| 3482 | case S_IGN: |
| 3483 | act.sa_handler = SIG_IGN; |
| 3484 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3485 | } |
Ian Wienand | 89b3cba | 2011-04-16 20:05:14 +0200 | [diff] [blame] | 3486 | |
| 3487 | /* flags and mask matter only if !DFL and !IGN, but we do it |
| 3488 | * for all cases for more deterministic behavior: |
| 3489 | */ |
| 3490 | act.sa_flags = 0; |
| 3491 | sigfillset(&act.sa_mask); |
| 3492 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3493 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3494 | |
| 3495 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | /* mode flags for set_curjob */ |
| 3499 | #define CUR_DELETE 2 |
| 3500 | #define CUR_RUNNING 1 |
| 3501 | #define CUR_STOPPED 0 |
| 3502 | |
| 3503 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3504 | #define DOWAIT_NONBLOCK WNOHANG |
| 3505 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3506 | |
| 3507 | #if JOBS |
| 3508 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3509 | static int initialpgrp; //references:2 |
| 3510 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3511 | #endif |
| 3512 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3513 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3514 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3515 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3516 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3517 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3518 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3519 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3520 | |
| 3521 | static void |
| 3522 | set_curjob(struct job *jp, unsigned mode) |
| 3523 | { |
| 3524 | struct job *jp1; |
| 3525 | struct job **jpp, **curp; |
| 3526 | |
| 3527 | /* first remove from list */ |
| 3528 | jpp = curp = &curjob; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3529 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3530 | jp1 = *jpp; |
| 3531 | if (jp1 == jp) |
| 3532 | break; |
| 3533 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3534 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3535 | *jpp = jp1->prev_job; |
| 3536 | |
| 3537 | /* Then re-insert in correct position */ |
| 3538 | jpp = curp; |
| 3539 | switch (mode) { |
| 3540 | default: |
| 3541 | #if DEBUG |
| 3542 | abort(); |
| 3543 | #endif |
| 3544 | case CUR_DELETE: |
| 3545 | /* job being deleted */ |
| 3546 | break; |
| 3547 | case CUR_RUNNING: |
| 3548 | /* newly created job or backgrounded job, |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 3549 | * put after all stopped jobs. |
| 3550 | */ |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3551 | while (1) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3552 | jp1 = *jpp; |
| 3553 | #if JOBS |
| 3554 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3555 | #endif |
| 3556 | break; |
| 3557 | jpp = &jp1->prev_job; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3558 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3559 | /* FALLTHROUGH */ |
| 3560 | #if JOBS |
| 3561 | case CUR_STOPPED: |
| 3562 | #endif |
| 3563 | /* newly stopped job - becomes curjob */ |
| 3564 | jp->prev_job = *jpp; |
| 3565 | *jpp = jp; |
| 3566 | break; |
| 3567 | } |
| 3568 | } |
| 3569 | |
| 3570 | #if JOBS || DEBUG |
| 3571 | static int |
| 3572 | jobno(const struct job *jp) |
| 3573 | { |
| 3574 | return jp - jobtab + 1; |
| 3575 | } |
| 3576 | #endif |
| 3577 | |
| 3578 | /* |
| 3579 | * Convert a job name to a job structure. |
| 3580 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3581 | #if !JOBS |
| 3582 | #define getjob(name, getctl) getjob(name) |
| 3583 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3584 | static struct job * |
| 3585 | getjob(const char *name, int getctl) |
| 3586 | { |
| 3587 | struct job *jp; |
| 3588 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3589 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3590 | unsigned num; |
| 3591 | int c; |
| 3592 | const char *p; |
| 3593 | char *(*match)(const char *, const char *); |
| 3594 | |
| 3595 | jp = curjob; |
| 3596 | p = name; |
| 3597 | if (!p) |
| 3598 | goto currentjob; |
| 3599 | |
| 3600 | if (*p != '%') |
| 3601 | goto err; |
| 3602 | |
| 3603 | c = *++p; |
| 3604 | if (!c) |
| 3605 | goto currentjob; |
| 3606 | |
| 3607 | if (!p[1]) { |
| 3608 | if (c == '+' || c == '%') { |
| 3609 | currentjob: |
| 3610 | err_msg = "No current job"; |
| 3611 | goto check; |
| 3612 | } |
| 3613 | if (c == '-') { |
| 3614 | if (jp) |
| 3615 | jp = jp->prev_job; |
| 3616 | err_msg = "No previous job"; |
| 3617 | check: |
| 3618 | if (!jp) |
| 3619 | goto err; |
| 3620 | goto gotit; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | if (is_number(p)) { |
| 3625 | num = atoi(p); |
| 3626 | if (num < njobs) { |
| 3627 | jp = jobtab + num - 1; |
| 3628 | if (jp->used) |
| 3629 | goto gotit; |
| 3630 | goto err; |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | match = prefix; |
| 3635 | if (*p == '?') { |
| 3636 | match = strstr; |
| 3637 | p++; |
| 3638 | } |
| 3639 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3640 | found = NULL; |
| 3641 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3642 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3643 | if (found) |
| 3644 | goto err; |
| 3645 | found = jp; |
| 3646 | err_msg = "%s: ambiguous"; |
| 3647 | } |
| 3648 | jp = jp->prev_job; |
| 3649 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3650 | if (!found) |
| 3651 | goto err; |
| 3652 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3653 | |
| 3654 | gotit: |
| 3655 | #if JOBS |
| 3656 | err_msg = "job %s not created under job control"; |
| 3657 | if (getctl && jp->jobctl == 0) |
| 3658 | goto err; |
| 3659 | #endif |
| 3660 | return jp; |
| 3661 | err: |
| 3662 | ash_msg_and_raise_error(err_msg, name); |
| 3663 | } |
| 3664 | |
| 3665 | /* |
| 3666 | * Mark a job structure as unused. |
| 3667 | */ |
| 3668 | static void |
| 3669 | freejob(struct job *jp) |
| 3670 | { |
| 3671 | struct procstat *ps; |
| 3672 | int i; |
| 3673 | |
| 3674 | INT_OFF; |
| 3675 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3676 | if (ps->ps_cmd != nullstr) |
| 3677 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3678 | } |
| 3679 | if (jp->ps != &jp->ps0) |
| 3680 | free(jp->ps); |
| 3681 | jp->used = 0; |
| 3682 | set_curjob(jp, CUR_DELETE); |
| 3683 | INT_ON; |
| 3684 | } |
| 3685 | |
| 3686 | #if JOBS |
| 3687 | static void |
| 3688 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3689 | { |
| 3690 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3691 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /* |
| 3695 | * Turn job control on and off. |
| 3696 | * |
| 3697 | * Note: This code assumes that the third arg to ioctl is a character |
| 3698 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3699 | * System V doesn't have job control yet, this isn't a problem now. |
| 3700 | * |
| 3701 | * Called with interrupts off. |
| 3702 | */ |
| 3703 | static void |
| 3704 | setjobctl(int on) |
| 3705 | { |
| 3706 | int fd; |
| 3707 | int pgrp; |
| 3708 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3709 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3710 | return; |
| 3711 | if (on) { |
| 3712 | int ofd; |
| 3713 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3714 | if (fd < 0) { |
| 3715 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3716 | * That sometimes helps to acquire controlling tty. |
| 3717 | * Obviously, a workaround for bugs when someone |
| 3718 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3719 | fd = 2; |
| 3720 | while (!isatty(fd)) |
| 3721 | if (--fd < 0) |
| 3722 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3723 | } |
| 3724 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3725 | if (ofd >= 0) |
| 3726 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3727 | if (fd < 0) |
| 3728 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3729 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3730 | close_on_exec_on(fd); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3731 | while (1) { /* while we are in the background */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3732 | pgrp = tcgetpgrp(fd); |
| 3733 | if (pgrp < 0) { |
| 3734 | out: |
| 3735 | ash_msg("can't access tty; job control turned off"); |
| 3736 | mflag = on = 0; |
| 3737 | goto close; |
| 3738 | } |
| 3739 | if (pgrp == getpgrp()) |
| 3740 | break; |
| 3741 | killpg(0, SIGTTIN); |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 3742 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3743 | initialpgrp = pgrp; |
| 3744 | |
| 3745 | setsignal(SIGTSTP); |
| 3746 | setsignal(SIGTTOU); |
| 3747 | setsignal(SIGTTIN); |
| 3748 | pgrp = rootpid; |
| 3749 | setpgid(0, pgrp); |
| 3750 | xtcsetpgrp(fd, pgrp); |
| 3751 | } else { |
| 3752 | /* turning job control off */ |
| 3753 | fd = ttyfd; |
| 3754 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3755 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3756 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3757 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3758 | setpgid(0, pgrp); |
| 3759 | setsignal(SIGTSTP); |
| 3760 | setsignal(SIGTTOU); |
| 3761 | setsignal(SIGTTIN); |
| 3762 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3763 | if (fd >= 0) |
| 3764 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3765 | fd = -1; |
| 3766 | } |
| 3767 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3768 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3771 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3772 | killcmd(int argc, char **argv) |
| 3773 | { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3774 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 3775 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3776 | do { |
| 3777 | if (argv[i][0] == '%') { |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 3778 | /* |
| 3779 | * "kill %N" - job kill |
| 3780 | * Converting to pgrp / pid kill |
| 3781 | */ |
| 3782 | struct job *jp; |
| 3783 | char *dst; |
| 3784 | int j, n; |
| 3785 | |
| 3786 | jp = getjob(argv[i], 0); |
| 3787 | /* |
| 3788 | * In jobs started under job control, we signal |
| 3789 | * entire process group by kill -PGRP_ID. |
| 3790 | * This happens, f.e., in interactive shell. |
| 3791 | * |
| 3792 | * Otherwise, we signal each child via |
| 3793 | * kill PID1 PID2 PID3. |
| 3794 | * Testcases: |
| 3795 | * sh -c 'sleep 1|sleep 1 & kill %1' |
| 3796 | * sh -c 'true|sleep 2 & sleep 1; kill %1' |
| 3797 | * sh -c 'true|sleep 1 & sleep 2; kill %1' |
| 3798 | */ |
| 3799 | n = jp->nprocs; /* can't be 0 (I hope) */ |
| 3800 | if (jp->jobctl) |
| 3801 | n = 1; |
| 3802 | dst = alloca(n * sizeof(int)*4); |
| 3803 | argv[i] = dst; |
| 3804 | for (j = 0; j < n; j++) { |
| 3805 | struct procstat *ps = &jp->ps[j]; |
| 3806 | /* Skip non-running and not-stopped members |
| 3807 | * (i.e. dead members) of the job |
| 3808 | */ |
| 3809 | if (ps->ps_status != -1 && !WIFSTOPPED(ps->ps_status)) |
| 3810 | continue; |
| 3811 | /* |
| 3812 | * kill_main has matching code to expect |
| 3813 | * leading space. Needed to not confuse |
| 3814 | * negative pids with "kill -SIGNAL_NO" syntax |
| 3815 | */ |
| 3816 | dst += sprintf(dst, jp->jobctl ? " -%u" : " %u", (int)ps->ps_pid); |
| 3817 | } |
| 3818 | *dst = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3819 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3820 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3821 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3822 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
| 3825 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3826 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3827 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3828 | struct procstat *ps; |
| 3829 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3830 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3831 | psend = jp->ps + jp->nprocs; |
| 3832 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3833 | printf(" | %s", ps->ps_cmd); |
| 3834 | outcslow('\n', stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3835 | flush_stdout_stderr(); |
| 3836 | } |
| 3837 | |
| 3838 | |
| 3839 | static int |
| 3840 | restartjob(struct job *jp, int mode) |
| 3841 | { |
| 3842 | struct procstat *ps; |
| 3843 | int i; |
| 3844 | int status; |
| 3845 | pid_t pgid; |
| 3846 | |
| 3847 | INT_OFF; |
| 3848 | if (jp->state == JOBDONE) |
| 3849 | goto out; |
| 3850 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3851 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3852 | if (mode == FORK_FG) |
| 3853 | xtcsetpgrp(ttyfd, pgid); |
| 3854 | killpg(pgid, SIGCONT); |
| 3855 | ps = jp->ps; |
| 3856 | i = jp->nprocs; |
| 3857 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3858 | if (WIFSTOPPED(ps->ps_status)) { |
| 3859 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3860 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3861 | ps++; |
| 3862 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3863 | out: |
| 3864 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3865 | INT_ON; |
| 3866 | return status; |
| 3867 | } |
| 3868 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3869 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3870 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3871 | { |
| 3872 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3873 | int mode; |
| 3874 | int retval; |
| 3875 | |
| 3876 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3877 | nextopt(nullstr); |
| 3878 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3879 | do { |
| 3880 | jp = getjob(*argv, 1); |
| 3881 | if (mode == FORK_BG) { |
| 3882 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3883 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3884 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3885 | out1str(jp->ps[0].ps_cmd); |
| 3886 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3887 | retval = restartjob(jp, mode); |
| 3888 | } while (*argv && *++argv); |
| 3889 | return retval; |
| 3890 | } |
| 3891 | #endif |
| 3892 | |
| 3893 | static int |
| 3894 | sprint_status(char *s, int status, int sigonly) |
| 3895 | { |
| 3896 | int col; |
| 3897 | int st; |
| 3898 | |
| 3899 | col = 0; |
| 3900 | if (!WIFEXITED(status)) { |
| 3901 | #if JOBS |
| 3902 | if (WIFSTOPPED(status)) |
| 3903 | st = WSTOPSIG(status); |
| 3904 | else |
| 3905 | #endif |
| 3906 | st = WTERMSIG(status); |
| 3907 | if (sigonly) { |
| 3908 | if (st == SIGINT || st == SIGPIPE) |
| 3909 | goto out; |
| 3910 | #if JOBS |
| 3911 | if (WIFSTOPPED(status)) |
| 3912 | goto out; |
| 3913 | #endif |
| 3914 | } |
| 3915 | st &= 0x7f; |
Denys Vlasenko | 7c6f246 | 2011-02-14 17:17:10 +0100 | [diff] [blame] | 3916 | //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] | 3917 | col = fmtstr(s, 32, strsignal(st)); |
| 3918 | if (WCOREDUMP(status)) { |
| 3919 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3920 | } |
| 3921 | } else if (!sigonly) { |
| 3922 | st = WEXITSTATUS(status); |
| 3923 | if (st) |
| 3924 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3925 | else |
| 3926 | col = fmtstr(s, 16, "Done"); |
| 3927 | } |
| 3928 | out: |
| 3929 | return col; |
| 3930 | } |
| 3931 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3932 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3933 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3934 | { |
| 3935 | int pid; |
| 3936 | int status; |
| 3937 | struct job *jp; |
| 3938 | struct job *thisjob; |
| 3939 | int state; |
| 3940 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3941 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3942 | |
| 3943 | /* Do a wait system call. If job control is compiled in, we accept |
| 3944 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3945 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3946 | if (doing_jobctl) |
| 3947 | wait_flags |= WUNTRACED; |
| 3948 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3949 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3950 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3951 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3952 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3953 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3954 | INT_OFF; |
| 3955 | thisjob = NULL; |
| 3956 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3957 | struct procstat *ps; |
| 3958 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3959 | if (jp->state == JOBDONE) |
| 3960 | continue; |
| 3961 | state = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3962 | ps = jp->ps; |
| 3963 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3964 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3965 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3966 | TRACE(("Job %d: changing status of proc %d " |
| 3967 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3968 | jobno(jp), pid, ps->ps_status, status)); |
| 3969 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3970 | thisjob = jp; |
| 3971 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3972 | if (ps->ps_status == -1) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3973 | state = JOBRUNNING; |
| 3974 | #if JOBS |
| 3975 | if (state == JOBRUNNING) |
| 3976 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3977 | if (WIFSTOPPED(ps->ps_status)) { |
| 3978 | jp->stopstatus = ps->ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3979 | state = JOBSTOPPED; |
| 3980 | } |
| 3981 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3982 | } while (++ps < psend); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3983 | if (thisjob) |
| 3984 | goto gotjob; |
| 3985 | } |
| 3986 | #if JOBS |
| 3987 | if (!WIFSTOPPED(status)) |
| 3988 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3989 | jobless--; |
| 3990 | goto out; |
| 3991 | |
| 3992 | gotjob: |
| 3993 | if (state != JOBRUNNING) { |
| 3994 | thisjob->changed = 1; |
| 3995 | |
| 3996 | if (thisjob->state != state) { |
| 3997 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3998 | jobno(thisjob), thisjob->state, state)); |
| 3999 | thisjob->state = state; |
| 4000 | #if JOBS |
| 4001 | if (state == JOBSTOPPED) { |
| 4002 | set_curjob(thisjob, CUR_STOPPED); |
| 4003 | } |
| 4004 | #endif |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | out: |
| 4009 | INT_ON; |
| 4010 | |
| 4011 | if (thisjob && thisjob == job) { |
| 4012 | char s[48 + 1]; |
| 4013 | int len; |
| 4014 | |
| 4015 | len = sprint_status(s, status, 1); |
| 4016 | if (len) { |
| 4017 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4018 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4019 | out2str(s); |
| 4020 | } |
| 4021 | } |
| 4022 | return pid; |
| 4023 | } |
| 4024 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4025 | static int |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4026 | blocking_wait_with_raise_on_sig(void) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4027 | { |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4028 | pid_t pid = dowait(DOWAIT_BLOCK, NULL); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4029 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4030 | raise_exception(EXSIG); |
| 4031 | return pid; |
| 4032 | } |
| 4033 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4034 | #if JOBS |
| 4035 | static void |
| 4036 | showjob(FILE *out, struct job *jp, int mode) |
| 4037 | { |
| 4038 | struct procstat *ps; |
| 4039 | struct procstat *psend; |
| 4040 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4041 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4042 | char s[80]; |
| 4043 | |
| 4044 | ps = jp->ps; |
| 4045 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4046 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4047 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4048 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4049 | return; |
| 4050 | } |
| 4051 | |
| 4052 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4053 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4054 | |
| 4055 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4056 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4057 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4058 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4059 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4060 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4061 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4062 | |
| 4063 | psend = ps + jp->nprocs; |
| 4064 | |
| 4065 | if (jp->state == JOBRUNNING) { |
| 4066 | strcpy(s + col, "Running"); |
| 4067 | col += sizeof("Running") - 1; |
| 4068 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4069 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4070 | if (jp->state == JOBSTOPPED) |
| 4071 | status = jp->stopstatus; |
| 4072 | col += sprint_status(s + col, status, 0); |
| 4073 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4074 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4075 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4076 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4077 | * or prints several "PID | <cmdN>" lines, |
| 4078 | * depending on SHOW_PIDS bit. |
| 4079 | * We do not print status of individual processes |
| 4080 | * between PID and <cmdN>. bash does it, but not very well: |
| 4081 | * first line shows overall job status, not process status, |
| 4082 | * making it impossible to know 1st process status. |
| 4083 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4084 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4085 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4086 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4087 | s[0] = '\0'; |
| 4088 | col = 33; |
| 4089 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4090 | 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] | 4091 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4092 | fprintf(out, "%s%*c%s%s", |
| 4093 | s, |
| 4094 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4095 | ps == jp->ps ? "" : "| ", |
| 4096 | ps->ps_cmd |
| 4097 | ); |
| 4098 | } while (++ps != psend); |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4099 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4100 | |
| 4101 | jp->changed = 0; |
| 4102 | |
| 4103 | if (jp->state == JOBDONE) { |
| 4104 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4105 | freejob(jp); |
| 4106 | } |
| 4107 | } |
| 4108 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4109 | /* |
| 4110 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4111 | * statuses have changed since the last call to showjobs. |
| 4112 | */ |
| 4113 | static void |
| 4114 | showjobs(FILE *out, int mode) |
| 4115 | { |
| 4116 | struct job *jp; |
| 4117 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4118 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4119 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4120 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4121 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4122 | continue; |
| 4123 | |
| 4124 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4125 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4126 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4127 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4128 | } |
| 4129 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4130 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4131 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4132 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4133 | { |
| 4134 | int mode, m; |
| 4135 | |
| 4136 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4137 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4138 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4139 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4140 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4141 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
| 4144 | argv = argptr; |
| 4145 | if (*argv) { |
| 4146 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4147 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4148 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4149 | } else { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4150 | showjobs(stdout, mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4151 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4152 | |
| 4153 | return 0; |
| 4154 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4155 | #endif /* JOBS */ |
| 4156 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4157 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4158 | static int |
| 4159 | getstatus(struct job *job) |
| 4160 | { |
| 4161 | int status; |
| 4162 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4163 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4164 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4165 | /* Fetch last member's status */ |
| 4166 | ps = job->ps + job->nprocs - 1; |
| 4167 | status = ps->ps_status; |
| 4168 | if (pipefail) { |
| 4169 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4170 | while (status == 0 && --ps >= job->ps) |
| 4171 | status = ps->ps_status; |
| 4172 | } |
| 4173 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4174 | retval = WEXITSTATUS(status); |
| 4175 | if (!WIFEXITED(status)) { |
| 4176 | #if JOBS |
| 4177 | retval = WSTOPSIG(status); |
| 4178 | if (!WIFSTOPPED(status)) |
| 4179 | #endif |
| 4180 | { |
| 4181 | /* XXX: limits number of signals */ |
| 4182 | retval = WTERMSIG(status); |
| 4183 | #if JOBS |
| 4184 | if (retval == SIGINT) |
| 4185 | job->sigint = 1; |
| 4186 | #endif |
| 4187 | } |
| 4188 | retval += 128; |
| 4189 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4190 | 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] | 4191 | jobno(job), job->nprocs, status, retval)); |
| 4192 | return retval; |
| 4193 | } |
| 4194 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4195 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4196 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4197 | { |
| 4198 | struct job *job; |
| 4199 | int retval; |
| 4200 | struct job *jp; |
| 4201 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4202 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4203 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4204 | |
| 4205 | nextopt(nullstr); |
| 4206 | retval = 0; |
| 4207 | |
| 4208 | argv = argptr; |
| 4209 | if (!*argv) { |
| 4210 | /* wait for all jobs */ |
| 4211 | for (;;) { |
| 4212 | jp = curjob; |
| 4213 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4214 | if (!jp) /* no running procs */ |
| 4215 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4216 | if (jp->state == JOBRUNNING) |
| 4217 | break; |
| 4218 | jp->waited = 1; |
| 4219 | jp = jp->prev_job; |
| 4220 | } |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4221 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4222 | /* man bash: |
| 4223 | * "When bash is waiting for an asynchronous command via |
| 4224 | * the wait builtin, the reception of a signal for which a trap |
| 4225 | * has been set will cause the wait builtin to return immediately |
| 4226 | * with an exit status greater than 128, immediately after which |
| 4227 | * the trap is executed." |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4228 | * |
| 4229 | * blocking_wait_with_raise_on_sig raises signal handlers |
| 4230 | * if it gets no pid (pid < 0). However, |
| 4231 | * if child sends us a signal *and immediately exits*, |
| 4232 | * blocking_wait_with_raise_on_sig gets pid > 0 |
| 4233 | * and does not handle pending_sig. Check this case: */ |
| 4234 | if (pending_sig) |
| 4235 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | retval = 127; |
| 4240 | do { |
| 4241 | if (**argv != '%') { |
| 4242 | pid_t pid = number(*argv); |
| 4243 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4244 | while (1) { |
| 4245 | if (!job) |
| 4246 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4247 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4248 | break; |
| 4249 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4250 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4251 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4252 | job = getjob(*argv, 0); |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4253 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4254 | /* loop until process terminated or stopped */ |
| 4255 | while (job->state == JOBRUNNING) |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4256 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4257 | job->waited = 1; |
| 4258 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4259 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4260 | } while (*++argv); |
| 4261 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4262 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4263 | return retval; |
| 4264 | } |
| 4265 | |
| 4266 | static struct job * |
| 4267 | growjobtab(void) |
| 4268 | { |
| 4269 | size_t len; |
| 4270 | ptrdiff_t offset; |
| 4271 | struct job *jp, *jq; |
| 4272 | |
| 4273 | len = njobs * sizeof(*jp); |
| 4274 | jq = jobtab; |
| 4275 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4276 | |
| 4277 | offset = (char *)jp - (char *)jq; |
| 4278 | if (offset) { |
| 4279 | /* Relocate pointers */ |
| 4280 | size_t l = len; |
| 4281 | |
| 4282 | jq = (struct job *)((char *)jq + l); |
| 4283 | while (l) { |
| 4284 | l -= sizeof(*jp); |
| 4285 | jq--; |
| 4286 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4287 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4288 | if (joff(jp)->ps == &jq->ps0) |
| 4289 | jmove(joff(jp)->ps); |
| 4290 | if (joff(jp)->prev_job) |
| 4291 | jmove(joff(jp)->prev_job); |
| 4292 | } |
| 4293 | if (curjob) |
| 4294 | jmove(curjob); |
| 4295 | #undef joff |
| 4296 | #undef jmove |
| 4297 | } |
| 4298 | |
| 4299 | njobs += 4; |
| 4300 | jobtab = jp; |
| 4301 | jp = (struct job *)((char *)jp + len); |
| 4302 | jq = jp + 3; |
| 4303 | do { |
| 4304 | jq->used = 0; |
| 4305 | } while (--jq >= jp); |
| 4306 | return jp; |
| 4307 | } |
| 4308 | |
| 4309 | /* |
| 4310 | * Return a new job structure. |
| 4311 | * Called with interrupts off. |
| 4312 | */ |
| 4313 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4314 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4315 | { |
| 4316 | int i; |
| 4317 | struct job *jp; |
| 4318 | |
| 4319 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4320 | if (--i < 0) { |
| 4321 | jp = growjobtab(); |
| 4322 | break; |
| 4323 | } |
| 4324 | if (jp->used == 0) |
| 4325 | break; |
| 4326 | if (jp->state != JOBDONE || !jp->waited) |
| 4327 | continue; |
| 4328 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4329 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4330 | continue; |
| 4331 | #endif |
| 4332 | freejob(jp); |
| 4333 | break; |
| 4334 | } |
| 4335 | memset(jp, 0, sizeof(*jp)); |
| 4336 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4337 | /* jp->jobctl is a bitfield. |
| 4338 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4339 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4340 | jp->jobctl = 1; |
| 4341 | #endif |
| 4342 | jp->prev_job = curjob; |
| 4343 | curjob = jp; |
| 4344 | jp->used = 1; |
| 4345 | jp->ps = &jp->ps0; |
| 4346 | if (nprocs > 1) { |
| 4347 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4348 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4349 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4350 | jobno(jp))); |
| 4351 | return jp; |
| 4352 | } |
| 4353 | |
| 4354 | #if JOBS |
| 4355 | /* |
| 4356 | * Return a string identifying a command (to be printed by the |
| 4357 | * jobs command). |
| 4358 | */ |
| 4359 | static char *cmdnextc; |
| 4360 | |
| 4361 | static void |
| 4362 | cmdputs(const char *s) |
| 4363 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4364 | static const char vstype[VSTYPE + 1][3] = { |
| 4365 | "", "}", "-", "+", "?", "=", |
| 4366 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4367 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4368 | }; |
| 4369 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4370 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4371 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4372 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4373 | unsigned char c; |
| 4374 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4375 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4376 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4377 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4378 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4379 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4380 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4381 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4382 | switch (c) { |
| 4383 | case CTLESC: |
| 4384 | c = *p++; |
| 4385 | break; |
| 4386 | case CTLVAR: |
| 4387 | subtype = *p++; |
| 4388 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4389 | str = "${#"; |
| 4390 | else |
| 4391 | str = "${"; |
| 4392 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4393 | goto dostr; |
| 4394 | quoted ^= 1; |
| 4395 | c = '"'; |
| 4396 | break; |
| 4397 | case CTLENDVAR: |
| 4398 | str = "\"}" + !(quoted & 1); |
| 4399 | quoted >>= 1; |
| 4400 | subtype = 0; |
| 4401 | goto dostr; |
| 4402 | case CTLBACKQ: |
| 4403 | str = "$(...)"; |
| 4404 | goto dostr; |
| 4405 | case CTLBACKQ+CTLQUOTE: |
| 4406 | str = "\"$(...)\""; |
| 4407 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4408 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4409 | case CTLARI: |
| 4410 | str = "$(("; |
| 4411 | goto dostr; |
| 4412 | case CTLENDARI: |
| 4413 | str = "))"; |
| 4414 | goto dostr; |
| 4415 | #endif |
| 4416 | case CTLQUOTEMARK: |
| 4417 | quoted ^= 1; |
| 4418 | c = '"'; |
| 4419 | break; |
| 4420 | case '=': |
| 4421 | if (subtype == 0) |
| 4422 | break; |
| 4423 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4424 | quoted <<= 1; |
| 4425 | str = vstype[subtype & VSTYPE]; |
| 4426 | if (subtype & VSNUL) |
| 4427 | c = ':'; |
| 4428 | else |
| 4429 | goto checkstr; |
| 4430 | break; |
| 4431 | case '\'': |
| 4432 | case '\\': |
| 4433 | case '"': |
| 4434 | case '$': |
| 4435 | /* These can only happen inside quotes */ |
| 4436 | cc[0] = c; |
| 4437 | str = cc; |
| 4438 | c = '\\'; |
| 4439 | break; |
| 4440 | default: |
| 4441 | break; |
| 4442 | } |
| 4443 | USTPUTC(c, nextc); |
| 4444 | checkstr: |
| 4445 | if (!str) |
| 4446 | continue; |
| 4447 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4448 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4449 | USTPUTC(c, nextc); |
| 4450 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4451 | } /* while *p++ not NUL */ |
| 4452 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4453 | if (quoted & 1) { |
| 4454 | USTPUTC('"', nextc); |
| 4455 | } |
| 4456 | *nextc = 0; |
| 4457 | cmdnextc = nextc; |
| 4458 | } |
| 4459 | |
| 4460 | /* cmdtxt() and cmdlist() call each other */ |
| 4461 | static void cmdtxt(union node *n); |
| 4462 | |
| 4463 | static void |
| 4464 | cmdlist(union node *np, int sep) |
| 4465 | { |
| 4466 | for (; np; np = np->narg.next) { |
| 4467 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4468 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4469 | cmdtxt(np); |
| 4470 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4471 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | static void |
| 4476 | cmdtxt(union node *n) |
| 4477 | { |
| 4478 | union node *np; |
| 4479 | struct nodelist *lp; |
| 4480 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4481 | |
| 4482 | if (!n) |
| 4483 | return; |
| 4484 | switch (n->type) { |
| 4485 | default: |
| 4486 | #if DEBUG |
| 4487 | abort(); |
| 4488 | #endif |
| 4489 | case NPIPE: |
| 4490 | lp = n->npipe.cmdlist; |
| 4491 | for (;;) { |
| 4492 | cmdtxt(lp->n); |
| 4493 | lp = lp->next; |
| 4494 | if (!lp) |
| 4495 | break; |
| 4496 | cmdputs(" | "); |
| 4497 | } |
| 4498 | break; |
| 4499 | case NSEMI: |
| 4500 | p = "; "; |
| 4501 | goto binop; |
| 4502 | case NAND: |
| 4503 | p = " && "; |
| 4504 | goto binop; |
| 4505 | case NOR: |
| 4506 | p = " || "; |
| 4507 | binop: |
| 4508 | cmdtxt(n->nbinary.ch1); |
| 4509 | cmdputs(p); |
| 4510 | n = n->nbinary.ch2; |
| 4511 | goto donode; |
| 4512 | case NREDIR: |
| 4513 | case NBACKGND: |
| 4514 | n = n->nredir.n; |
| 4515 | goto donode; |
| 4516 | case NNOT: |
| 4517 | cmdputs("!"); |
| 4518 | n = n->nnot.com; |
| 4519 | donode: |
| 4520 | cmdtxt(n); |
| 4521 | break; |
| 4522 | case NIF: |
| 4523 | cmdputs("if "); |
| 4524 | cmdtxt(n->nif.test); |
| 4525 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4526 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4527 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4528 | cmdputs("; else "); |
| 4529 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4530 | } else { |
| 4531 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4532 | } |
| 4533 | p = "; fi"; |
| 4534 | goto dotail; |
| 4535 | case NSUBSHELL: |
| 4536 | cmdputs("("); |
| 4537 | n = n->nredir.n; |
| 4538 | p = ")"; |
| 4539 | goto dotail; |
| 4540 | case NWHILE: |
| 4541 | p = "while "; |
| 4542 | goto until; |
| 4543 | case NUNTIL: |
| 4544 | p = "until "; |
| 4545 | until: |
| 4546 | cmdputs(p); |
| 4547 | cmdtxt(n->nbinary.ch1); |
| 4548 | n = n->nbinary.ch2; |
| 4549 | p = "; done"; |
| 4550 | dodo: |
| 4551 | cmdputs("; do "); |
| 4552 | dotail: |
| 4553 | cmdtxt(n); |
| 4554 | goto dotail2; |
| 4555 | case NFOR: |
| 4556 | cmdputs("for "); |
| 4557 | cmdputs(n->nfor.var); |
| 4558 | cmdputs(" in "); |
| 4559 | cmdlist(n->nfor.args, 1); |
| 4560 | n = n->nfor.body; |
| 4561 | p = "; done"; |
| 4562 | goto dodo; |
| 4563 | case NDEFUN: |
| 4564 | cmdputs(n->narg.text); |
| 4565 | p = "() { ... }"; |
| 4566 | goto dotail2; |
| 4567 | case NCMD: |
| 4568 | cmdlist(n->ncmd.args, 1); |
| 4569 | cmdlist(n->ncmd.redirect, 0); |
| 4570 | break; |
| 4571 | case NARG: |
| 4572 | p = n->narg.text; |
| 4573 | dotail2: |
| 4574 | cmdputs(p); |
| 4575 | break; |
| 4576 | case NHERE: |
| 4577 | case NXHERE: |
| 4578 | p = "<<..."; |
| 4579 | goto dotail2; |
| 4580 | case NCASE: |
| 4581 | cmdputs("case "); |
| 4582 | cmdputs(n->ncase.expr->narg.text); |
| 4583 | cmdputs(" in "); |
| 4584 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4585 | cmdtxt(np->nclist.pattern); |
| 4586 | cmdputs(") "); |
| 4587 | cmdtxt(np->nclist.body); |
| 4588 | cmdputs(";; "); |
| 4589 | } |
| 4590 | p = "esac"; |
| 4591 | goto dotail2; |
| 4592 | case NTO: |
| 4593 | p = ">"; |
| 4594 | goto redir; |
| 4595 | case NCLOBBER: |
| 4596 | p = ">|"; |
| 4597 | goto redir; |
| 4598 | case NAPPEND: |
| 4599 | p = ">>"; |
| 4600 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4601 | #if ENABLE_ASH_BASH_COMPAT |
| 4602 | case NTO2: |
| 4603 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4604 | case NTOFD: |
| 4605 | p = ">&"; |
| 4606 | goto redir; |
| 4607 | case NFROM: |
| 4608 | p = "<"; |
| 4609 | goto redir; |
| 4610 | case NFROMFD: |
| 4611 | p = "<&"; |
| 4612 | goto redir; |
| 4613 | case NFROMTO: |
| 4614 | p = "<>"; |
| 4615 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4616 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4617 | cmdputs(p); |
| 4618 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4619 | cmdputs(utoa(n->ndup.dupfd)); |
| 4620 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4621 | } |
| 4622 | n = n->nfile.fname; |
| 4623 | goto donode; |
| 4624 | } |
| 4625 | } |
| 4626 | |
| 4627 | static char * |
| 4628 | commandtext(union node *n) |
| 4629 | { |
| 4630 | char *name; |
| 4631 | |
| 4632 | STARTSTACKSTR(cmdnextc); |
| 4633 | cmdtxt(n); |
| 4634 | name = stackblock(); |
| 4635 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4636 | name, cmdnextc, cmdnextc)); |
| 4637 | return ckstrdup(name); |
| 4638 | } |
| 4639 | #endif /* JOBS */ |
| 4640 | |
| 4641 | /* |
| 4642 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4643 | * own process group. Jp is a job structure that the job is to be added to. |
| 4644 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4645 | * be NULL. The mode parameter can be one of the following: |
| 4646 | * FORK_FG - Fork off a foreground process. |
| 4647 | * FORK_BG - Fork off a background process. |
| 4648 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4649 | * process group even if job control is on. |
| 4650 | * |
| 4651 | * When job control is turned off, background processes have their standard |
| 4652 | * input redirected to /dev/null (except for the second and later processes |
| 4653 | * in a pipeline). |
| 4654 | * |
| 4655 | * Called with interrupts off. |
| 4656 | */ |
| 4657 | /* |
| 4658 | * Clear traps on a fork. |
| 4659 | */ |
| 4660 | static void |
| 4661 | clear_traps(void) |
| 4662 | { |
| 4663 | char **tp; |
| 4664 | |
| 4665 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4666 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4667 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4668 | if (trap_ptr == trap) |
| 4669 | free(*tp); |
| 4670 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4671 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4672 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4673 | setsignal(tp - trap); |
| 4674 | INT_ON; |
| 4675 | } |
| 4676 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 4677 | may_have_traps = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4678 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4679 | |
| 4680 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4681 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4682 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4683 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4684 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4685 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4686 | { |
| 4687 | int oldlvl; |
| 4688 | |
| 4689 | TRACE(("Child shell %d\n", getpid())); |
| 4690 | oldlvl = shlvl; |
| 4691 | shlvl++; |
| 4692 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4693 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4694 | * set to the values inherited by the shell from its parent". |
| 4695 | * Do we do it correctly? */ |
| 4696 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4697 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4698 | |
| 4699 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4700 | && n && n->type == NCMD /* is it single cmd? */ |
| 4701 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4702 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4703 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4704 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4705 | ) { |
| 4706 | TRACE(("Trap hack\n")); |
| 4707 | /* Awful hack for `trap` or $(trap). |
| 4708 | * |
| 4709 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4710 | * contains an example where "trap" is executed in a subshell: |
| 4711 | * |
| 4712 | * save_traps=$(trap) |
| 4713 | * ... |
| 4714 | * eval "$save_traps" |
| 4715 | * |
| 4716 | * Standard does not say that "trap" in subshell shall print |
| 4717 | * parent shell's traps. It only says that its output |
| 4718 | * must have suitable form, but then, in the above example |
| 4719 | * (which is not supposed to be normative), it implies that. |
| 4720 | * |
| 4721 | * bash (and probably other shell) does implement it |
| 4722 | * (traps are reset to defaults, but "trap" still shows them), |
| 4723 | * but as a result, "trap" logic is hopelessly messed up: |
| 4724 | * |
| 4725 | * # trap |
| 4726 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4727 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4728 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4729 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4730 | * trap -- 'echo Ho' SIGWINCH |
| 4731 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4732 | * trap -- 'echo Ho' SIGWINCH |
| 4733 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4734 | * trap -- 'echo Ho' SIGWINCH |
| 4735 | * |
| 4736 | * The rules when to forget and when to not forget traps |
| 4737 | * get really complex and nonsensical. |
| 4738 | * |
| 4739 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4740 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4741 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4742 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4743 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4744 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4745 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4746 | #if JOBS |
| 4747 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4748 | doing_jobctl = 0; |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4749 | if (mode != FORK_NOJOB && jp->jobctl && oldlvl == 0) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4750 | pid_t pgrp; |
| 4751 | |
| 4752 | if (jp->nprocs == 0) |
| 4753 | pgrp = getpid(); |
| 4754 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4755 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4756 | /* this can fail because we are doing it in the parent also */ |
| 4757 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4758 | if (mode == FORK_FG) |
| 4759 | xtcsetpgrp(ttyfd, pgrp); |
| 4760 | setsignal(SIGTSTP); |
| 4761 | setsignal(SIGTTOU); |
| 4762 | } else |
| 4763 | #endif |
| 4764 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4765 | /* man bash: "When job control is not in effect, |
| 4766 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4767 | ignoresig(SIGINT); |
| 4768 | ignoresig(SIGQUIT); |
| 4769 | if (jp->nprocs == 0) { |
| 4770 | close(0); |
| 4771 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4772 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4773 | } |
| 4774 | } |
Denys Vlasenko | b12553f | 2011-02-21 03:22:20 +0100 | [diff] [blame] | 4775 | if (oldlvl == 0) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4776 | if (iflag) { /* why if iflag only? */ |
| 4777 | setsignal(SIGINT); |
| 4778 | setsignal(SIGTERM); |
| 4779 | } |
| 4780 | /* man bash: |
| 4781 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4782 | * commands run by bash have signal handlers |
| 4783 | * set to the values inherited by the shell |
| 4784 | * from its parent". |
| 4785 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4786 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4787 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4788 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4789 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4790 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4791 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4792 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4793 | /* "jobs": we do not want to clear job list for it, |
| 4794 | * instead we remove only _its_ own_ job from job list. |
| 4795 | * This makes "jobs .... | cat" more useful. |
| 4796 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4797 | freejob(curjob); |
| 4798 | return; |
| 4799 | } |
| 4800 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4801 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4802 | freejob(jp); |
| 4803 | jobless = 0; |
| 4804 | } |
| 4805 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4806 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4807 | #if !JOBS |
| 4808 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4809 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4810 | static void |
| 4811 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4812 | { |
| 4813 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4814 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4815 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4816 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4817 | jobless++; |
| 4818 | return; |
| 4819 | } |
| 4820 | #if JOBS |
| 4821 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4822 | int pgrp; |
| 4823 | |
| 4824 | if (jp->nprocs == 0) |
| 4825 | pgrp = pid; |
| 4826 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4827 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4828 | /* This can fail because we are doing it in the child also */ |
| 4829 | setpgid(pid, pgrp); |
| 4830 | } |
| 4831 | #endif |
| 4832 | if (mode == FORK_BG) { |
| 4833 | backgndpid = pid; /* set $! */ |
| 4834 | set_curjob(jp, CUR_RUNNING); |
| 4835 | } |
| 4836 | if (jp) { |
| 4837 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4838 | ps->ps_pid = pid; |
| 4839 | ps->ps_status = -1; |
| 4840 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4841 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4842 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4843 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4844 | #endif |
| 4845 | } |
| 4846 | } |
| 4847 | |
| 4848 | static int |
| 4849 | forkshell(struct job *jp, union node *n, int mode) |
| 4850 | { |
| 4851 | int pid; |
| 4852 | |
| 4853 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4854 | pid = fork(); |
| 4855 | if (pid < 0) { |
| 4856 | TRACE(("Fork failed, errno=%d", errno)); |
| 4857 | if (jp) |
| 4858 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4859 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4860 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4861 | if (pid == 0) { |
| 4862 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4863 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4864 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4865 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4866 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4867 | return pid; |
| 4868 | } |
| 4869 | |
| 4870 | /* |
| 4871 | * Wait for job to finish. |
| 4872 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4873 | * Under job control we have the problem that while a child process |
| 4874 | * is running interrupts generated by the user are sent to the child |
| 4875 | * but not to the shell. This means that an infinite loop started by |
| 4876 | * an interactive user may be hard to kill. With job control turned off, |
| 4877 | * an interactive user may place an interactive program inside a loop. |
| 4878 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4879 | * these interrupts to also abort the loop. The approach we take here |
| 4880 | * is to have the shell ignore interrupt signals while waiting for a |
| 4881 | * foreground process to terminate, and then send itself an interrupt |
| 4882 | * signal if the child process was terminated by an interrupt signal. |
| 4883 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4884 | * exit on interrupt; unless these processes terminate themselves by |
| 4885 | * sending a signal to themselves (instead of calling exit) they will |
| 4886 | * confuse this approach. |
| 4887 | * |
| 4888 | * Called with interrupts off. |
| 4889 | */ |
| 4890 | static int |
| 4891 | waitforjob(struct job *jp) |
| 4892 | { |
| 4893 | int st; |
| 4894 | |
| 4895 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4896 | |
| 4897 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4898 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4899 | /* In non-interactive shells, we _can_ get |
| 4900 | * a keyboard signal here and be EINTRed, |
| 4901 | * but we just loop back, waiting for command to complete. |
| 4902 | * |
| 4903 | * man bash: |
| 4904 | * "If bash is waiting for a command to complete and receives |
| 4905 | * a signal for which a trap has been set, the trap |
| 4906 | * will not be executed until the command completes." |
| 4907 | * |
| 4908 | * Reality is that even if trap is not set, bash |
| 4909 | * will not act on the signal until command completes. |
| 4910 | * Try this. sleep5intoff.c: |
| 4911 | * #include <signal.h> |
| 4912 | * #include <unistd.h> |
| 4913 | * int main() { |
| 4914 | * sigset_t set; |
| 4915 | * sigemptyset(&set); |
| 4916 | * sigaddset(&set, SIGINT); |
| 4917 | * sigaddset(&set, SIGQUIT); |
| 4918 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4919 | * sleep(5); |
| 4920 | * return 0; |
| 4921 | * } |
| 4922 | * $ bash -c './sleep5intoff; echo hi' |
| 4923 | * ^C^C^C^C <--- pressing ^C once a second |
| 4924 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4925 | * $ bash -c './sleep5intoff; echo hi' |
| 4926 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4927 | * $ _ |
| 4928 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4929 | dowait(DOWAIT_BLOCK, jp); |
| 4930 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4931 | INT_ON; |
| 4932 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4933 | st = getstatus(jp); |
| 4934 | #if JOBS |
| 4935 | if (jp->jobctl) { |
| 4936 | xtcsetpgrp(ttyfd, rootpid); |
| 4937 | /* |
| 4938 | * This is truly gross. |
| 4939 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4940 | * caused us (the shell) to no longer be in the controlling |
| 4941 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4942 | * intuit from the subprocess exit status whether a SIGINT |
| 4943 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4944 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4945 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4946 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4947 | } |
| 4948 | if (jp->state == JOBDONE) |
| 4949 | #endif |
| 4950 | freejob(jp); |
| 4951 | return st; |
| 4952 | } |
| 4953 | |
| 4954 | /* |
| 4955 | * return 1 if there are stopped jobs, otherwise 0 |
| 4956 | */ |
| 4957 | static int |
| 4958 | stoppedjobs(void) |
| 4959 | { |
| 4960 | struct job *jp; |
| 4961 | int retval; |
| 4962 | |
| 4963 | retval = 0; |
| 4964 | if (job_warning) |
| 4965 | goto out; |
| 4966 | jp = curjob; |
| 4967 | if (jp && jp->state == JOBSTOPPED) { |
| 4968 | out2str("You have stopped jobs.\n"); |
| 4969 | job_warning = 2; |
| 4970 | retval++; |
| 4971 | } |
| 4972 | out: |
| 4973 | return retval; |
| 4974 | } |
| 4975 | |
| 4976 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4977 | /* ============ redir.c |
| 4978 | * |
| 4979 | * Code for dealing with input/output redirection. |
| 4980 | */ |
| 4981 | |
Denys Vlasenko | 8d0e0cd | 2011-01-25 23:21:46 +0100 | [diff] [blame] | 4982 | #undef EMPTY |
| 4983 | #undef CLOSED |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4984 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4985 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4986 | |
| 4987 | /* |
| 4988 | * Open a file in noclobber mode. |
| 4989 | * The code was copied from bash. |
| 4990 | */ |
| 4991 | static int |
| 4992 | noclobberopen(const char *fname) |
| 4993 | { |
| 4994 | int r, fd; |
| 4995 | struct stat finfo, finfo2; |
| 4996 | |
| 4997 | /* |
| 4998 | * If the file exists and is a regular file, return an error |
| 4999 | * immediately. |
| 5000 | */ |
| 5001 | r = stat(fname, &finfo); |
| 5002 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 5003 | errno = EEXIST; |
| 5004 | return -1; |
| 5005 | } |
| 5006 | |
| 5007 | /* |
| 5008 | * If the file was not present (r != 0), make sure we open it |
| 5009 | * exclusively so that if it is created before we open it, our open |
| 5010 | * will fail. Make sure that we do not truncate an existing file. |
| 5011 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 5012 | * file was not a regular file, we leave O_EXCL off. |
| 5013 | */ |
| 5014 | if (r != 0) |
| 5015 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 5016 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 5017 | |
| 5018 | /* If the open failed, return the file descriptor right away. */ |
| 5019 | if (fd < 0) |
| 5020 | return fd; |
| 5021 | |
| 5022 | /* |
| 5023 | * OK, the open succeeded, but the file may have been changed from a |
| 5024 | * non-regular file to a regular file between the stat and the open. |
| 5025 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 5026 | * did not exist and is symlinked to an existing file between the stat |
| 5027 | * and open. |
| 5028 | */ |
| 5029 | |
| 5030 | /* |
| 5031 | * If we can open it and fstat the file descriptor, and neither check |
| 5032 | * revealed that it was a regular file, and the file has not been |
| 5033 | * replaced, return the file descriptor. |
| 5034 | */ |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 5035 | if (fstat(fd, &finfo2) == 0 |
| 5036 | && !S_ISREG(finfo2.st_mode) |
| 5037 | && finfo.st_dev == finfo2.st_dev |
| 5038 | && finfo.st_ino == finfo2.st_ino |
| 5039 | ) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5040 | return fd; |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 5041 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5042 | |
| 5043 | /* The file has been replaced. badness. */ |
| 5044 | close(fd); |
| 5045 | errno = EEXIST; |
| 5046 | return -1; |
| 5047 | } |
| 5048 | |
| 5049 | /* |
| 5050 | * Handle here documents. Normally we fork off a process to write the |
| 5051 | * data to a pipe. If the document is short, we can stuff the data in |
| 5052 | * the pipe without forking. |
| 5053 | */ |
| 5054 | /* openhere needs this forward reference */ |
| 5055 | static void expandhere(union node *arg, int fd); |
| 5056 | static int |
| 5057 | openhere(union node *redir) |
| 5058 | { |
| 5059 | int pip[2]; |
| 5060 | size_t len = 0; |
| 5061 | |
| 5062 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5063 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5064 | if (redir->type == NHERE) { |
| 5065 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5066 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5067 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 5068 | goto out; |
| 5069 | } |
| 5070 | } |
| 5071 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5072 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5073 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5074 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5075 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5076 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5077 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5078 | signal(SIGPIPE, SIG_DFL); |
| 5079 | if (redir->type == NHERE) |
| 5080 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5081 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5082 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5083 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5084 | } |
| 5085 | out: |
| 5086 | close(pip[1]); |
| 5087 | return pip[0]; |
| 5088 | } |
| 5089 | |
| 5090 | static int |
| 5091 | openredirect(union node *redir) |
| 5092 | { |
| 5093 | char *fname; |
| 5094 | int f; |
| 5095 | |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 5096 | fname = redir->nfile.expfname; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5097 | switch (redir->nfile.type) { |
| 5098 | case NFROM: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5099 | f = open(fname, O_RDONLY); |
| 5100 | if (f < 0) |
| 5101 | goto eopen; |
| 5102 | break; |
| 5103 | case NFROMTO: |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5104 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5105 | if (f < 0) |
| 5106 | goto ecreate; |
| 5107 | break; |
| 5108 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5109 | #if ENABLE_ASH_BASH_COMPAT |
| 5110 | case NTO2: |
| 5111 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5112 | /* Take care of noclobber mode. */ |
| 5113 | if (Cflag) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5114 | f = noclobberopen(fname); |
| 5115 | if (f < 0) |
| 5116 | goto ecreate; |
| 5117 | break; |
| 5118 | } |
| 5119 | /* FALLTHROUGH */ |
| 5120 | case NCLOBBER: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5121 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5122 | if (f < 0) |
| 5123 | goto ecreate; |
| 5124 | break; |
| 5125 | case NAPPEND: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5126 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5127 | if (f < 0) |
| 5128 | goto ecreate; |
| 5129 | break; |
| 5130 | default: |
| 5131 | #if DEBUG |
| 5132 | abort(); |
| 5133 | #endif |
| 5134 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5135 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5136 | // case NTOFD: |
| 5137 | // case NFROMFD: |
| 5138 | // f = -1; |
| 5139 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5140 | case NHERE: |
| 5141 | case NXHERE: |
| 5142 | f = openhere(redir); |
| 5143 | break; |
| 5144 | } |
| 5145 | |
| 5146 | return f; |
| 5147 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5148 | 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] | 5149 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5150 | 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] | 5151 | } |
| 5152 | |
| 5153 | /* |
| 5154 | * Copy a file descriptor to be >= to. Returns -1 |
| 5155 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5156 | * file descriptors left. |
| 5157 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5158 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5159 | * 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] | 5160 | enum { |
| 5161 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5162 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5163 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5164 | static int |
| 5165 | copyfd(int from, int to) |
| 5166 | { |
| 5167 | int newfd; |
| 5168 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5169 | if (to & COPYFD_EXACT) { |
| 5170 | to &= ~COPYFD_EXACT; |
| 5171 | /*if (from != to)*/ |
| 5172 | newfd = dup2(from, to); |
| 5173 | } else { |
| 5174 | newfd = fcntl(from, F_DUPFD, to); |
| 5175 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5176 | if (newfd < 0) { |
| 5177 | if (errno == EMFILE) |
| 5178 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5179 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5180 | ash_msg_and_raise_error("%d: %m", from); |
| 5181 | } |
| 5182 | return newfd; |
| 5183 | } |
| 5184 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5185 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5186 | struct two_fd_t { |
| 5187 | int orig, copy; |
| 5188 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5189 | struct redirtab { |
| 5190 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5191 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5192 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5193 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5194 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5195 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5196 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5197 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5198 | { |
| 5199 | int i; |
| 5200 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5201 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5202 | return 0; |
| 5203 | |
| 5204 | for (i = 0; i < rp->pair_count; i++) { |
| 5205 | if (rp->two_fd[i].orig == fd) { |
| 5206 | /* already remembered */ |
| 5207 | return 0; |
| 5208 | } |
| 5209 | } |
| 5210 | return 1; |
| 5211 | } |
| 5212 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5213 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5214 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5215 | { |
| 5216 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5217 | struct parsefile *pf; |
| 5218 | |
| 5219 | if (fd == -1) |
| 5220 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5221 | /* Check open scripts' fds */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5222 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5223 | while (pf) { |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5224 | /* We skip pf_fd == 0 case because of the following case: |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5225 | * $ ash # running ash interactively |
| 5226 | * $ . ./script.sh |
| 5227 | * and in script.sh: "exec 9>&0". |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5228 | * Even though top-level pf_fd _is_ 0, |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5229 | * it's still ok to use it: "read" builtin uses it, |
| 5230 | * why should we cripple "exec" builtin? |
| 5231 | */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5232 | if (pf->pf_fd > 0 && fd == pf->pf_fd) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5233 | return 1; |
| 5234 | } |
| 5235 | pf = pf->prev; |
| 5236 | } |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5237 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5238 | if (!rp) |
| 5239 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5240 | /* Check saved fds of redirects */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5241 | fd |= COPYFD_RESTORE; |
| 5242 | for (i = 0; i < rp->pair_count; i++) { |
| 5243 | if (rp->two_fd[i].copy == fd) { |
| 5244 | return 1; |
| 5245 | } |
| 5246 | } |
| 5247 | return 0; |
| 5248 | } |
| 5249 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5250 | /* |
| 5251 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5252 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5253 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5254 | */ |
| 5255 | /* flags passed to redirect */ |
| 5256 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5257 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5258 | static void |
| 5259 | redirect(union node *redir, int flags) |
| 5260 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5261 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5262 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5263 | int i; |
| 5264 | int fd; |
| 5265 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5266 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5267 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5268 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5269 | if (!redir) { |
| 5270 | return; |
| 5271 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5272 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5273 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5274 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5275 | INT_OFF; |
| 5276 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5277 | union node *tmp = redir; |
| 5278 | do { |
| 5279 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5280 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5281 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5282 | sv_pos++; |
| 5283 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5284 | tmp = tmp->nfile.next; |
| 5285 | } while (tmp); |
| 5286 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5287 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5288 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5289 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5290 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5291 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5292 | while (sv_pos > 0) { |
| 5293 | sv_pos--; |
| 5294 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5295 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5296 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5297 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5298 | do { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5299 | int right_fd = -1; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5300 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5301 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5302 | right_fd = redir->ndup.dupfd; |
| 5303 | //bb_error_msg("doing %d > %d", fd, right_fd); |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5304 | /* redirect from/to same file descriptor? */ |
| 5305 | if (right_fd == fd) |
| 5306 | continue; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5307 | /* "echo >&10" and 10 is a fd opened to a sh script? */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5308 | if (is_hidden_fd(sv, right_fd)) { |
| 5309 | errno = EBADF; /* as if it is closed */ |
| 5310 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5311 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5312 | newfd = -1; |
| 5313 | } else { |
| 5314 | newfd = openredirect(redir); /* always >= 0 */ |
| 5315 | if (fd == newfd) { |
| 5316 | /* Descriptor wasn't open before redirect. |
| 5317 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5318 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5319 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5320 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5321 | continue; |
| 5322 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5323 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5324 | #if ENABLE_ASH_BASH_COMPAT |
| 5325 | redirect_more: |
| 5326 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5327 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5328 | /* Copy old descriptor */ |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5329 | /* Careful to not accidentally "save" |
| 5330 | * to the same fd as right side fd in N>&M */ |
| 5331 | int minfd = right_fd < 10 ? 10 : right_fd + 1; |
| 5332 | i = fcntl(fd, F_DUPFD, minfd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5333 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5334 | * are closed in popredir() in the child, preventing them from leaking |
| 5335 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5336 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5337 | if (i == -1) { |
| 5338 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5339 | if (i != EBADF) { |
| 5340 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5341 | if (newfd >= 0) |
| 5342 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5343 | errno = i; |
| 5344 | ash_msg_and_raise_error("%d: %m", fd); |
| 5345 | /* NOTREACHED */ |
| 5346 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5347 | /* EBADF: it is not open - good, remember to close it */ |
| 5348 | remember_to_close: |
| 5349 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5350 | } else { /* fd is open, save its copy */ |
| 5351 | /* "exec fd>&-" should not close fds |
| 5352 | * which point to script file(s). |
| 5353 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5354 | if (is_hidden_fd(sv, fd)) |
| 5355 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5356 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5357 | if (fd == 2) |
| 5358 | copied_fd2 = i; |
| 5359 | sv->two_fd[sv_pos].orig = fd; |
| 5360 | sv->two_fd[sv_pos].copy = i; |
| 5361 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5362 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5363 | if (newfd < 0) { |
| 5364 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5365 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5366 | /* Don't want to trigger debugging */ |
| 5367 | if (fd != -1) |
| 5368 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5369 | } else { |
| 5370 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5371 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5372 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5373 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5374 | #if ENABLE_ASH_BASH_COMPAT |
| 5375 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5376 | #endif |
| 5377 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5378 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5379 | #if ENABLE_ASH_BASH_COMPAT |
| 5380 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5381 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5382 | newfd = 1; |
| 5383 | fd = 2; |
| 5384 | goto redirect_more; |
| 5385 | } |
| 5386 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5387 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5388 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5389 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5390 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5391 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | /* |
| 5395 | * Undo the effects of the last redirection. |
| 5396 | */ |
| 5397 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5398 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5399 | { |
| 5400 | struct redirtab *rp; |
| 5401 | int i; |
| 5402 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5403 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5404 | return; |
| 5405 | INT_OFF; |
| 5406 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5407 | for (i = 0; i < rp->pair_count; i++) { |
| 5408 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5409 | int copy = rp->two_fd[i].copy; |
| 5410 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5411 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5412 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5413 | continue; |
| 5414 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5415 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5416 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5417 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5418 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5419 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5420 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5421 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5422 | } |
| 5423 | } |
| 5424 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5425 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5426 | free(rp); |
| 5427 | INT_ON; |
| 5428 | } |
| 5429 | |
| 5430 | /* |
| 5431 | * Undo all redirections. Called on error or interrupt. |
| 5432 | */ |
| 5433 | |
| 5434 | /* |
| 5435 | * Discard all saved file descriptors. |
| 5436 | */ |
| 5437 | static void |
| 5438 | clearredir(int drop) |
| 5439 | { |
| 5440 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5441 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5442 | if (!redirlist) |
| 5443 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5444 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | static int |
| 5449 | redirectsafe(union node *redir, int flags) |
| 5450 | { |
| 5451 | int err; |
| 5452 | volatile int saveint; |
| 5453 | struct jmploc *volatile savehandler = exception_handler; |
| 5454 | struct jmploc jmploc; |
| 5455 | |
| 5456 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5457 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5458 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5459 | if (!err) { |
| 5460 | exception_handler = &jmploc; |
| 5461 | redirect(redir, flags); |
| 5462 | } |
| 5463 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5464 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5465 | longjmp(exception_handler->loc, 1); |
| 5466 | RESTORE_INT(saveint); |
| 5467 | return err; |
| 5468 | } |
| 5469 | |
| 5470 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5471 | /* ============ Routines to expand arguments to commands |
| 5472 | * |
| 5473 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5474 | */ |
| 5475 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5476 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5477 | static arith_t |
| 5478 | ash_arith(const char *s) |
| 5479 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5480 | arith_state_t math_state; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5481 | arith_t result; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5482 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5483 | math_state.lookupvar = lookupvar; |
| 5484 | math_state.setvar = setvar2; |
| 5485 | //math_state.endofname = endofname; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5486 | |
| 5487 | INT_OFF; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5488 | result = arith(&math_state, s); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5489 | if (math_state.errmsg) |
| 5490 | ash_msg_and_raise_error(math_state.errmsg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5491 | INT_ON; |
| 5492 | |
| 5493 | return result; |
| 5494 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5495 | #endif |
| 5496 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5497 | /* |
| 5498 | * expandarg flags |
| 5499 | */ |
| 5500 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5501 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5502 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5503 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5504 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5505 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5506 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5507 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5508 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5509 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5510 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5511 | */ |
| 5512 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5513 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5514 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5515 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5516 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5517 | |
| 5518 | /* |
| 5519 | * Structure specifying which parts of the string should be searched |
| 5520 | * for IFS characters. |
| 5521 | */ |
| 5522 | struct ifsregion { |
| 5523 | struct ifsregion *next; /* next region in list */ |
| 5524 | int begoff; /* offset of start of region */ |
| 5525 | int endoff; /* offset of end of region */ |
| 5526 | int nulonly; /* search for nul bytes only */ |
| 5527 | }; |
| 5528 | |
| 5529 | struct arglist { |
| 5530 | struct strlist *list; |
| 5531 | struct strlist **lastp; |
| 5532 | }; |
| 5533 | |
| 5534 | /* output of current string */ |
| 5535 | static char *expdest; |
| 5536 | /* list of back quote expressions */ |
| 5537 | static struct nodelist *argbackq; |
| 5538 | /* first struct in list of ifs regions */ |
| 5539 | static struct ifsregion ifsfirst; |
| 5540 | /* last struct in list */ |
| 5541 | static struct ifsregion *ifslastp; |
| 5542 | /* holds expanded arg list */ |
| 5543 | static struct arglist exparg; |
| 5544 | |
| 5545 | /* |
| 5546 | * Our own itoa(). |
| 5547 | */ |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 5548 | #if !ENABLE_SH_MATH_SUPPORT |
| 5549 | /* cvtnum() is used even if math support is off (to prepare $? values and such) */ |
| 5550 | typedef long arith_t; |
| 5551 | # define ARITH_FMT "%ld" |
| 5552 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5553 | static int |
| 5554 | cvtnum(arith_t num) |
| 5555 | { |
| 5556 | int len; |
| 5557 | |
| 5558 | expdest = makestrspace(32, expdest); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5559 | len = fmtstr(expdest, 32, ARITH_FMT, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5560 | STADJUST(len, expdest); |
| 5561 | return len; |
| 5562 | } |
| 5563 | |
| 5564 | static size_t |
| 5565 | esclen(const char *start, const char *p) |
| 5566 | { |
| 5567 | size_t esc = 0; |
| 5568 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5569 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5570 | esc++; |
| 5571 | } |
| 5572 | return esc; |
| 5573 | } |
| 5574 | |
| 5575 | /* |
| 5576 | * Remove any CTLESC characters from a string. |
| 5577 | */ |
| 5578 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5579 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5580 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5581 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5582 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5583 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5584 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5585 | unsigned protect_against_glob; |
| 5586 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5587 | |
| 5588 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5589 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5590 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5591 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5592 | q = p; |
| 5593 | r = str; |
| 5594 | if (flag & RMESCAPE_ALLOC) { |
| 5595 | size_t len = p - str; |
| 5596 | size_t fulllen = len + strlen(p) + 1; |
| 5597 | |
| 5598 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5599 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5600 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5601 | /* p and str may be invalidated by makestrspace */ |
| 5602 | str = (char *)stackblock() + strloc; |
| 5603 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5604 | } else if (flag & RMESCAPE_HEAP) { |
| 5605 | r = ckmalloc(fulllen); |
| 5606 | } else { |
| 5607 | r = stalloc(fulllen); |
| 5608 | } |
| 5609 | q = r; |
| 5610 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5611 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5612 | } |
| 5613 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5614 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5615 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5616 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5617 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5618 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5619 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5620 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5621 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5622 | // Note: both inquotes and protect_against_glob only affect whether |
| 5623 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5624 | inquotes = ~inquotes; |
| 5625 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5626 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5627 | continue; |
| 5628 | } |
| 5629 | if (*p == '\\') { |
| 5630 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5631 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5632 | goto copy; |
| 5633 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5634 | if ((unsigned char)*p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5635 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5636 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5637 | *q++ = '\\'; |
| 5638 | } |
| 5639 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5640 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5641 | copy: |
| 5642 | *q++ = *p++; |
| 5643 | } |
| 5644 | *q = '\0'; |
| 5645 | if (flag & RMESCAPE_GROW) { |
| 5646 | expdest = r; |
| 5647 | STADJUST(q - r + 1, expdest); |
| 5648 | } |
| 5649 | return r; |
| 5650 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5651 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5652 | |
| 5653 | /* |
| 5654 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5655 | * |
| 5656 | * Returns an stalloced string. |
| 5657 | */ |
| 5658 | static char * |
| 5659 | preglob(const char *pattern, int quoted, int flag) |
| 5660 | { |
| 5661 | flag |= RMESCAPE_GLOB; |
| 5662 | if (quoted) { |
| 5663 | flag |= RMESCAPE_QUOTED; |
| 5664 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5665 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5666 | } |
| 5667 | |
| 5668 | /* |
| 5669 | * Put a string on the stack. |
| 5670 | */ |
| 5671 | static void |
| 5672 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5673 | { |
| 5674 | char *q = expdest; |
| 5675 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5676 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5677 | |
| 5678 | while (len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5679 | unsigned char c = *p++; |
| 5680 | if (c == '\0') |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5681 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5682 | if (quotes) { |
| 5683 | int n = SIT(c, syntax); |
| 5684 | if (n == CCTL || n == CBACK) |
| 5685 | USTPUTC(CTLESC, q); |
| 5686 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5687 | USTPUTC(c, q); |
| 5688 | } |
| 5689 | |
| 5690 | expdest = q; |
| 5691 | } |
| 5692 | |
| 5693 | static void |
| 5694 | strtodest(const char *p, int syntax, int quotes) |
| 5695 | { |
| 5696 | memtodest(p, strlen(p), syntax, quotes); |
| 5697 | } |
| 5698 | |
| 5699 | /* |
| 5700 | * Record the fact that we have to scan this region of the |
| 5701 | * string for IFS characters. |
| 5702 | */ |
| 5703 | static void |
| 5704 | recordregion(int start, int end, int nulonly) |
| 5705 | { |
| 5706 | struct ifsregion *ifsp; |
| 5707 | |
| 5708 | if (ifslastp == NULL) { |
| 5709 | ifsp = &ifsfirst; |
| 5710 | } else { |
| 5711 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5712 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5713 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5714 | ifslastp->next = ifsp; |
| 5715 | INT_ON; |
| 5716 | } |
| 5717 | ifslastp = ifsp; |
| 5718 | ifslastp->begoff = start; |
| 5719 | ifslastp->endoff = end; |
| 5720 | ifslastp->nulonly = nulonly; |
| 5721 | } |
| 5722 | |
| 5723 | static void |
| 5724 | removerecordregions(int endoff) |
| 5725 | { |
| 5726 | if (ifslastp == NULL) |
| 5727 | return; |
| 5728 | |
| 5729 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5730 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5731 | struct ifsregion *ifsp; |
| 5732 | INT_OFF; |
| 5733 | ifsp = ifsfirst.next->next; |
| 5734 | free(ifsfirst.next); |
| 5735 | ifsfirst.next = ifsp; |
| 5736 | INT_ON; |
| 5737 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5738 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5739 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5740 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5741 | ifslastp = &ifsfirst; |
| 5742 | ifsfirst.endoff = endoff; |
| 5743 | } |
| 5744 | return; |
| 5745 | } |
| 5746 | |
| 5747 | ifslastp = &ifsfirst; |
| 5748 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5749 | ifslastp = ifslastp->next; |
| 5750 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5751 | struct ifsregion *ifsp; |
| 5752 | INT_OFF; |
| 5753 | ifsp = ifslastp->next->next; |
| 5754 | free(ifslastp->next); |
| 5755 | ifslastp->next = ifsp; |
| 5756 | INT_ON; |
| 5757 | } |
| 5758 | if (ifslastp->endoff > endoff) |
| 5759 | ifslastp->endoff = endoff; |
| 5760 | } |
| 5761 | |
| 5762 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5763 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5764 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5765 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5766 | char *name; |
| 5767 | struct passwd *pw; |
| 5768 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5769 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5770 | int startloc; |
| 5771 | |
| 5772 | name = p + 1; |
| 5773 | |
| 5774 | while ((c = *++p) != '\0') { |
| 5775 | switch (c) { |
| 5776 | case CTLESC: |
| 5777 | return startp; |
| 5778 | case CTLQUOTEMARK: |
| 5779 | return startp; |
| 5780 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5781 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5782 | goto done; |
| 5783 | break; |
| 5784 | case '/': |
| 5785 | case CTLENDVAR: |
| 5786 | goto done; |
| 5787 | } |
| 5788 | } |
| 5789 | done: |
| 5790 | *p = '\0'; |
| 5791 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 5792 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5793 | } else { |
| 5794 | pw = getpwnam(name); |
| 5795 | if (pw == NULL) |
| 5796 | goto lose; |
| 5797 | home = pw->pw_dir; |
| 5798 | } |
| 5799 | if (!home || !*home) |
| 5800 | goto lose; |
| 5801 | *p = c; |
| 5802 | startloc = expdest - (char *)stackblock(); |
| 5803 | strtodest(home, SQSYNTAX, quotes); |
| 5804 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5805 | return p; |
| 5806 | lose: |
| 5807 | *p = c; |
| 5808 | return startp; |
| 5809 | } |
| 5810 | |
| 5811 | /* |
| 5812 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5813 | * want to save its output in a block obtained from malloc. Otherwise |
| 5814 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5815 | * Should be called with interrupts off. |
| 5816 | */ |
| 5817 | struct backcmd { /* result of evalbackcmd */ |
| 5818 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5819 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5820 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5821 | struct job *jp; /* job structure for command */ |
| 5822 | }; |
| 5823 | |
| 5824 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5825 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5826 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5827 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5828 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5829 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5830 | evalbackcmd(union node *n, struct backcmd *result) |
| 5831 | { |
| 5832 | int saveherefd; |
| 5833 | |
| 5834 | result->fd = -1; |
| 5835 | result->buf = NULL; |
| 5836 | result->nleft = 0; |
| 5837 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5838 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5839 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5840 | |
| 5841 | saveherefd = herefd; |
| 5842 | herefd = -1; |
| 5843 | |
| 5844 | { |
| 5845 | int pip[2]; |
| 5846 | struct job *jp; |
| 5847 | |
| 5848 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5849 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5850 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5851 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5852 | FORCE_INT_ON; |
| 5853 | close(pip[0]); |
| 5854 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5855 | /*close(1);*/ |
| 5856 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5857 | close(pip[1]); |
| 5858 | } |
| 5859 | eflag = 0; |
| 5860 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5861 | /* NOTREACHED */ |
| 5862 | } |
| 5863 | close(pip[1]); |
| 5864 | result->fd = pip[0]; |
| 5865 | result->jp = jp; |
| 5866 | } |
| 5867 | herefd = saveherefd; |
| 5868 | out: |
| 5869 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5870 | result->fd, result->buf, result->nleft, result->jp)); |
| 5871 | } |
| 5872 | |
| 5873 | /* |
| 5874 | * Expand stuff in backwards quotes. |
| 5875 | */ |
| 5876 | static void |
| 5877 | expbackq(union node *cmd, int quoted, int quotes) |
| 5878 | { |
| 5879 | struct backcmd in; |
| 5880 | int i; |
| 5881 | char buf[128]; |
| 5882 | char *p; |
| 5883 | char *dest; |
| 5884 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5885 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5886 | struct stackmark smark; |
| 5887 | |
| 5888 | INT_OFF; |
| 5889 | setstackmark(&smark); |
| 5890 | dest = expdest; |
| 5891 | startloc = dest - (char *)stackblock(); |
| 5892 | grabstackstr(dest); |
| 5893 | evalbackcmd(cmd, &in); |
| 5894 | popstackmark(&smark); |
| 5895 | |
| 5896 | p = in.buf; |
| 5897 | i = in.nleft; |
| 5898 | if (i == 0) |
| 5899 | goto read; |
| 5900 | for (;;) { |
| 5901 | memtodest(p, i, syntax, quotes); |
| 5902 | read: |
| 5903 | if (in.fd < 0) |
| 5904 | break; |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 5905 | i = nonblock_immune_read(in.fd, buf, sizeof(buf), /*loop_on_EINTR:*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5906 | TRACE(("expbackq: read returns %d\n", i)); |
| 5907 | if (i <= 0) |
| 5908 | break; |
| 5909 | p = buf; |
| 5910 | } |
| 5911 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5912 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5913 | if (in.fd >= 0) { |
| 5914 | close(in.fd); |
| 5915 | back_exitstatus = waitforjob(in.jp); |
| 5916 | } |
| 5917 | INT_ON; |
| 5918 | |
| 5919 | /* Eat all trailing newlines */ |
| 5920 | dest = expdest; |
| 5921 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5922 | STUNPUTC(dest); |
| 5923 | expdest = dest; |
| 5924 | |
| 5925 | if (quoted == 0) |
| 5926 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5927 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 5928 | (int)((dest - (char *)stackblock()) - startloc), |
| 5929 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5930 | stackblock() + startloc)); |
| 5931 | } |
| 5932 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5933 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5934 | /* |
| 5935 | * Expand arithmetic expression. Backup to start of expression, |
| 5936 | * evaluate, place result in (backed up) result, adjust string position. |
| 5937 | */ |
| 5938 | static void |
| 5939 | expari(int quotes) |
| 5940 | { |
| 5941 | char *p, *start; |
| 5942 | int begoff; |
| 5943 | int flag; |
| 5944 | int len; |
| 5945 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5946 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5947 | |
| 5948 | /* |
| 5949 | * This routine is slightly over-complicated for |
| 5950 | * efficiency. Next we scan backwards looking for the |
| 5951 | * start of arithmetic. |
| 5952 | */ |
| 5953 | start = stackblock(); |
| 5954 | p = expdest - 1; |
| 5955 | *p = '\0'; |
| 5956 | p--; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 5957 | while (1) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5958 | int esc; |
| 5959 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5960 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5961 | p--; |
| 5962 | #if DEBUG |
| 5963 | if (p < start) { |
| 5964 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5965 | } |
| 5966 | #endif |
| 5967 | } |
| 5968 | |
| 5969 | esc = esclen(start, p); |
| 5970 | if (!(esc % 2)) { |
| 5971 | break; |
| 5972 | } |
| 5973 | |
| 5974 | p -= esc + 1; |
Denys Vlasenko | 940c720 | 2011-03-02 04:07:14 +0100 | [diff] [blame] | 5975 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5976 | |
| 5977 | begoff = p - start; |
| 5978 | |
| 5979 | removerecordregions(begoff); |
| 5980 | |
| 5981 | flag = p[1]; |
| 5982 | |
| 5983 | expdest = p; |
| 5984 | |
| 5985 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5986 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5987 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5988 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5989 | |
| 5990 | if (flag != '"') |
| 5991 | recordregion(begoff, begoff + len, 0); |
| 5992 | } |
| 5993 | #endif |
| 5994 | |
| 5995 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5996 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5997 | |
| 5998 | /* |
| 5999 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 6000 | * characters to allow for further processing. Otherwise treat |
| 6001 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6002 | * |
| 6003 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 6004 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 6005 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6006 | */ |
| 6007 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6008 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6009 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 6010 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6011 | '=', |
| 6012 | ':', |
| 6013 | CTLQUOTEMARK, |
| 6014 | CTLENDVAR, |
| 6015 | CTLESC, |
| 6016 | CTLVAR, |
| 6017 | CTLBACKQ, |
| 6018 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6019 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6020 | CTLENDARI, |
| 6021 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6022 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6023 | }; |
| 6024 | const char *reject = spclchars; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6025 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 6026 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6027 | int inquotes; |
| 6028 | size_t length; |
| 6029 | int startloc; |
| 6030 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6031 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6032 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6033 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6034 | reject++; |
| 6035 | } |
| 6036 | inquotes = 0; |
| 6037 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6038 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6039 | char *q; |
| 6040 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6041 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6042 | tilde: |
| 6043 | q = p; |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6044 | if ((unsigned char)*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6045 | q++; |
| 6046 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6047 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6048 | } |
| 6049 | start: |
| 6050 | startloc = expdest - (char *)stackblock(); |
| 6051 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6052 | unsigned char c; |
| 6053 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6054 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6055 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6056 | if (c) { |
| 6057 | if (!(c & 0x80) |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 6058 | IF_SH_MATH_SUPPORT(|| c == CTLENDARI) |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6059 | ) { |
| 6060 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 6061 | length++; |
| 6062 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6063 | } |
| 6064 | if (length > 0) { |
| 6065 | int newloc; |
| 6066 | expdest = stack_nputstr(p, length, expdest); |
| 6067 | newloc = expdest - (char *)stackblock(); |
| 6068 | if (breakall && !inquotes && newloc > startloc) { |
| 6069 | recordregion(startloc, newloc, 0); |
| 6070 | } |
| 6071 | startloc = newloc; |
| 6072 | } |
| 6073 | p += length + 1; |
| 6074 | length = 0; |
| 6075 | |
| 6076 | switch (c) { |
| 6077 | case '\0': |
| 6078 | goto breakloop; |
| 6079 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6080 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6081 | p--; |
| 6082 | continue; |
| 6083 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6084 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6085 | reject++; |
| 6086 | /* fall through */ |
| 6087 | case ':': |
| 6088 | /* |
| 6089 | * sort of a hack - expand tildes in variable |
| 6090 | * assignments (after the first '=' and after ':'s). |
| 6091 | */ |
| 6092 | if (*--p == '~') { |
| 6093 | goto tilde; |
| 6094 | } |
| 6095 | continue; |
| 6096 | } |
| 6097 | |
| 6098 | switch (c) { |
| 6099 | case CTLENDVAR: /* ??? */ |
| 6100 | goto breakloop; |
| 6101 | case CTLQUOTEMARK: |
| 6102 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6103 | if (!inquotes |
| 6104 | && memcmp(p, dolatstr, 4) == 0 |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6105 | && ( p[4] == (char)CTLQUOTEMARK |
| 6106 | || (p[4] == (char)CTLENDVAR && p[5] == (char)CTLQUOTEMARK) |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6107 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6108 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6109 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6110 | goto start; |
| 6111 | } |
| 6112 | inquotes = !inquotes; |
| 6113 | addquote: |
| 6114 | if (quotes) { |
| 6115 | p--; |
| 6116 | length++; |
| 6117 | startloc++; |
| 6118 | } |
| 6119 | break; |
| 6120 | case CTLESC: |
| 6121 | startloc++; |
| 6122 | length++; |
| 6123 | goto addquote; |
| 6124 | case CTLVAR: |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6125 | TRACE(("argstr: evalvar('%s')\n", p)); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6126 | p = evalvar(p, flags, var_str_list); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6127 | TRACE(("argstr: evalvar:'%s'\n", (char *)stackblock())); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6128 | goto start; |
| 6129 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6130 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6131 | case CTLBACKQ|CTLQUOTE: |
| 6132 | expbackq(argbackq->n, c, quotes); |
| 6133 | argbackq = argbackq->next; |
| 6134 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6135 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6136 | case CTLENDARI: |
| 6137 | p--; |
| 6138 | expari(quotes); |
| 6139 | goto start; |
| 6140 | #endif |
| 6141 | } |
| 6142 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 6143 | breakloop: ; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6147 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6148 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6149 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6150 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6151 | char c; |
| 6152 | |
| 6153 | loc = startp; |
| 6154 | loc2 = rmesc; |
| 6155 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6156 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6157 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6158 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6159 | c = *loc2; |
| 6160 | if (zero) { |
| 6161 | *loc2 = '\0'; |
| 6162 | s = rmesc; |
| 6163 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6164 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6165 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6166 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6167 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6168 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6169 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6170 | loc++; |
| 6171 | loc++; |
| 6172 | loc2++; |
| 6173 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6174 | return NULL; |
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 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6179 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6180 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6181 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6182 | int try2optimize = match_at_start; |
| 6183 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6184 | int esc = 0; |
| 6185 | char *loc; |
| 6186 | char *loc2; |
| 6187 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6188 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6189 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6190 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6191 | * Logic: |
| 6192 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6193 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6194 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6195 | */ |
| 6196 | /* TODO: document in what other circumstances we are called. */ |
| 6197 | |
| 6198 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6199 | int match; |
| 6200 | char c = *loc2; |
| 6201 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6202 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6203 | *loc2 = '\0'; |
| 6204 | s = rmesc; |
| 6205 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6206 | match = pmatch(pattern, s); |
| 6207 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6208 | *loc2 = c; |
| 6209 | if (match) |
| 6210 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6211 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6212 | if (try2optimize) { |
| 6213 | /* Maybe we can optimize this: |
| 6214 | * if pattern ends with unescaped *, we can avoid checking |
| 6215 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6216 | * it wont match truncated "raw_value_of_" strings too. |
| 6217 | */ |
| 6218 | unsigned plen = strlen(pattern); |
| 6219 | /* Does it end with "*"? */ |
| 6220 | if (plen != 0 && pattern[--plen] == '*') { |
| 6221 | /* "xxxx*" is not escaped */ |
| 6222 | /* "xxx\*" is escaped */ |
| 6223 | /* "xx\\*" is not escaped */ |
| 6224 | /* "x\\\*" is escaped */ |
| 6225 | int slashes = 0; |
| 6226 | while (plen != 0 && pattern[--plen] == '\\') |
| 6227 | slashes++; |
| 6228 | if (!(slashes & 1)) |
| 6229 | break; /* ends with unescaped "*" */ |
| 6230 | } |
| 6231 | try2optimize = 0; |
| 6232 | } |
| 6233 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6234 | loc--; |
| 6235 | if (quotes) { |
| 6236 | if (--esc < 0) { |
| 6237 | esc = esclen(startp, loc); |
| 6238 | } |
| 6239 | if (esc % 2) { |
| 6240 | esc--; |
| 6241 | loc--; |
| 6242 | } |
| 6243 | } |
| 6244 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6245 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6248 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6249 | static void |
| 6250 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6251 | { |
| 6252 | const char *msg; |
| 6253 | const char *tail; |
| 6254 | |
| 6255 | tail = nullstr; |
| 6256 | msg = "parameter not set"; |
| 6257 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6258 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6259 | if (varflags & VSNUL) |
| 6260 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6261 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6262 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6263 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6264 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6265 | 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] | 6266 | } |
| 6267 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6268 | #if ENABLE_ASH_BASH_COMPAT |
| 6269 | static char * |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6270 | parse_sub_pattern(char *arg, int varflags) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6271 | { |
| 6272 | char *idx, *repl = NULL; |
| 6273 | unsigned char c; |
| 6274 | |
Denys Vlasenko | 1614900 | 2010-08-06 22:06:21 +0200 | [diff] [blame] | 6275 | //char *org_arg = arg; |
Denys Vlasenko | 33bbb27 | 2010-08-07 22:24:36 +0200 | [diff] [blame] | 6276 | //bb_error_msg("arg:'%s' varflags:%x", arg, varflags); |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6277 | idx = arg; |
| 6278 | while (1) { |
| 6279 | c = *arg; |
| 6280 | if (!c) |
| 6281 | break; |
| 6282 | if (c == '/') { |
| 6283 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6284 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6285 | repl = idx + 1; |
| 6286 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6287 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6288 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6289 | *idx++ = c; |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6290 | arg++; |
Denys Vlasenko | 33bbb27 | 2010-08-07 22:24:36 +0200 | [diff] [blame] | 6291 | /* |
| 6292 | * Example: v='ab\c'; echo ${v/\\b/_\\_\z_} |
| 6293 | * The result is a_\_z_c (not a\_\_z_c)! |
| 6294 | * |
| 6295 | * Enable debug prints in this function and you'll see: |
| 6296 | * ash: arg:'\\b/_\\_z_' varflags:d |
| 6297 | * ash: pattern:'\\b' repl:'_\_z_' |
| 6298 | * That is, \\b is interpreted as \\b, but \\_ as \_! |
| 6299 | * IOW: search pattern and replace string treat backslashes |
| 6300 | * differently! That is the reason why we check repl below: |
| 6301 | */ |
| 6302 | if (c == '\\' && *arg == '\\' && repl && !(varflags & VSQUOTE)) |
| 6303 | arg++; /* skip both '\', not just first one */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6304 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6305 | *idx = c; /* NUL */ |
Denys Vlasenko | 1614900 | 2010-08-06 22:06:21 +0200 | [diff] [blame] | 6306 | //bb_error_msg("pattern:'%s' repl:'%s'", org_arg, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6307 | |
| 6308 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6309 | } |
| 6310 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6311 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6312 | static const char * |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6313 | subevalvar(char *p, char *varname, int strloc, int subtype, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6314 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6315 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6316 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6317 | char *startp; |
| 6318 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6319 | char *rmesc, *rmescend; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6320 | char *str; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6321 | IF_ASH_BASH_COMPAT(const char *repl = NULL;) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6322 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6323 | int saveherefd = herefd; |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 6324 | int amount, resetloc; |
| 6325 | IF_ASH_BASH_COMPAT(int workloc;) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6326 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6327 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6328 | |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6329 | //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)", |
| 6330 | // p, varname, strloc, subtype, startloc, varflags, quotes); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6331 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6332 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6333 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6334 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6335 | STPUTC('\0', expdest); |
| 6336 | herefd = saveherefd; |
| 6337 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6338 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6339 | |
| 6340 | switch (subtype) { |
| 6341 | case VSASSIGN: |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6342 | setvar(varname, startp, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6343 | amount = startp - expdest; |
| 6344 | STADJUST(amount, expdest); |
| 6345 | return startp; |
| 6346 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6347 | case VSQUESTION: |
| 6348 | varunset(p, varname, startp, varflags); |
| 6349 | /* NOTREACHED */ |
| 6350 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6351 | #if ENABLE_ASH_BASH_COMPAT |
| 6352 | case VSSUBSTR: |
| 6353 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6354 | /* Read POS in ${var:POS:LEN} */ |
| 6355 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6356 | len = str - startp - 1; |
| 6357 | |
| 6358 | /* *loc != '\0', guaranteed by parser */ |
| 6359 | if (quotes) { |
| 6360 | char *ptr; |
| 6361 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6362 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6363 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6364 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6365 | len--; |
| 6366 | ptr++; |
| 6367 | } |
| 6368 | } |
| 6369 | } |
| 6370 | orig_len = len; |
| 6371 | |
| 6372 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6373 | /* ${var::LEN} */ |
| 6374 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6375 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6376 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6377 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6378 | while (*loc && *loc != ':') { |
| 6379 | /* TODO? |
| 6380 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6381 | if (!isdigit(*loc)) |
| 6382 | ash_msg_and_raise_error(msg_illnum, str); |
| 6383 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6384 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6385 | } |
| 6386 | if (*loc++ == ':') { |
| 6387 | len = number(loc); |
| 6388 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6389 | } |
| 6390 | if (pos >= orig_len) { |
| 6391 | pos = 0; |
| 6392 | len = 0; |
| 6393 | } |
| 6394 | if (len > (orig_len - pos)) |
| 6395 | len = orig_len - pos; |
| 6396 | |
| 6397 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6398 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6399 | str++; |
| 6400 | } |
| 6401 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6402 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6403 | *loc++ = *str++; |
| 6404 | *loc++ = *str++; |
| 6405 | } |
| 6406 | *loc = '\0'; |
| 6407 | amount = loc - expdest; |
| 6408 | STADJUST(amount, expdest); |
| 6409 | return loc; |
| 6410 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6411 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6412 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6413 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6414 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6415 | /* We'll comeback here if we grow the stack while handling |
| 6416 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6417 | * stack will need rebasing, and we'll need to remove our work |
| 6418 | * areas each time |
| 6419 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6420 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6421 | |
| 6422 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6423 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6424 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6425 | |
| 6426 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6427 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6428 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6429 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6430 | if (rmesc != startp) { |
| 6431 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6432 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6433 | } |
| 6434 | } |
| 6435 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6436 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6437 | preglob(str, varflags & VSQUOTE, 0); |
| 6438 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6439 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 6440 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6441 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6442 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6443 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6444 | if (!repl) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6445 | repl = parse_sub_pattern(str, varflags); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6446 | //bb_error_msg("repl:'%s'", repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6447 | if (!repl) |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6448 | repl = nullstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6449 | } |
| 6450 | |
| 6451 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6452 | if (str[0] == '\0') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6453 | return NULL; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6454 | |
| 6455 | len = 0; |
| 6456 | idx = startp; |
| 6457 | end = str - 1; |
| 6458 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6459 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6460 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6461 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6462 | if (!loc) { |
| 6463 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6464 | char *restart_detect = stackblock(); |
| 6465 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6466 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6467 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6468 | idx++; |
| 6469 | len++; |
| 6470 | STPUTC(*idx, expdest); |
| 6471 | } |
| 6472 | if (stackblock() != restart_detect) |
| 6473 | goto restart; |
| 6474 | idx++; |
| 6475 | len++; |
| 6476 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6477 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6478 | if (idx >= end) |
| 6479 | break; |
| 6480 | if (str[0] == '*') { |
| 6481 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6482 | * it would never match "ong_string" etc, no point in trying. |
| 6483 | */ |
| 6484 | goto skip_matching; |
| 6485 | } |
| 6486 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | if (subtype == VSREPLACEALL) { |
| 6490 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6491 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6492 | idx++; |
| 6493 | idx++; |
| 6494 | rmesc++; |
| 6495 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6496 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6497 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6498 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6499 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6500 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6501 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6502 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6503 | if (quotes && *loc == '\\') { |
| 6504 | STPUTC(CTLESC, expdest); |
| 6505 | len++; |
| 6506 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6507 | STPUTC(*loc, expdest); |
| 6508 | if (stackblock() != restart_detect) |
| 6509 | goto restart; |
| 6510 | len++; |
| 6511 | } |
| 6512 | |
| 6513 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6514 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6515 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6516 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6517 | STPUTC(*idx, expdest); |
| 6518 | if (stackblock() != restart_detect) |
| 6519 | goto restart; |
| 6520 | len++; |
| 6521 | idx++; |
| 6522 | } |
| 6523 | break; |
| 6524 | } |
| 6525 | } |
| 6526 | |
| 6527 | /* We've put the replaced text into a buffer at workloc, now |
| 6528 | * move it to the right place and adjust the stack. |
| 6529 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6530 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6531 | startp = (char *)stackblock() + startloc; |
| 6532 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6533 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6534 | amount = expdest - (startp + len); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6535 | STADJUST(-amount, expdest); |
| 6536 | return startp; |
| 6537 | } |
| 6538 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6539 | |
| 6540 | subtype -= VSTRIMRIGHT; |
| 6541 | #if DEBUG |
| 6542 | if (subtype < 0 || subtype > 7) |
| 6543 | abort(); |
| 6544 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6545 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6546 | zero = subtype >> 1; |
| 6547 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6548 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6549 | |
| 6550 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6551 | if (loc) { |
| 6552 | if (zero) { |
| 6553 | memmove(startp, loc, str - loc); |
| 6554 | loc = startp + (str - loc) - 1; |
| 6555 | } |
| 6556 | *loc = '\0'; |
| 6557 | amount = loc - expdest; |
| 6558 | STADJUST(amount, expdest); |
| 6559 | } |
| 6560 | return loc; |
| 6561 | } |
| 6562 | |
| 6563 | /* |
| 6564 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6565 | * name parameter (examples): |
| 6566 | * ash -c 'echo $1' name:'1=' |
| 6567 | * ash -c 'echo $qwe' name:'qwe=' |
| 6568 | * ash -c 'echo $$' name:'$=' |
| 6569 | * ash -c 'echo ${$}' name:'$=' |
| 6570 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6571 | * ash -c 'echo ${#$}' name:'$=' |
| 6572 | * note: examples with bad shell syntax: |
| 6573 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6574 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6575 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6576 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6577 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6578 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6579 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6580 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6581 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6582 | int sepq = 0; |
| 6583 | ssize_t len = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6584 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6585 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6586 | int quoted = varflags & VSQUOTE; |
| 6587 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6588 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6589 | switch (*name) { |
| 6590 | case '$': |
| 6591 | num = rootpid; |
| 6592 | goto numvar; |
| 6593 | case '?': |
| 6594 | num = exitstatus; |
| 6595 | goto numvar; |
| 6596 | case '#': |
| 6597 | num = shellparam.nparam; |
| 6598 | goto numvar; |
| 6599 | case '!': |
| 6600 | num = backgndpid; |
| 6601 | if (num == 0) |
| 6602 | return -1; |
| 6603 | numvar: |
| 6604 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6605 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6606 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6607 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6608 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6609 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6610 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6611 | len++; |
| 6612 | } |
| 6613 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6614 | check_1char_name: |
| 6615 | #if 0 |
| 6616 | /* handles cases similar to ${#$1} */ |
| 6617 | if (name[2] != '\0') |
| 6618 | raise_error_syntax("bad substitution"); |
| 6619 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6620 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6621 | case '@': { |
| 6622 | char **ap; |
| 6623 | int sep; |
| 6624 | |
| 6625 | if (quoted && (flags & EXP_FULL)) { |
| 6626 | /* note: this is not meant as PEOF value */ |
| 6627 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6628 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6629 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6630 | /* fall through */ |
| 6631 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6632 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6633 | i = SIT(sep, syntax); |
| 6634 | if (quotes && (i == CCTL || i == CBACK)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6635 | sepq = 1; |
| 6636 | param: |
| 6637 | ap = shellparam.p; |
| 6638 | if (!ap) |
| 6639 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6640 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6641 | size_t partlen; |
| 6642 | |
| 6643 | partlen = strlen(p); |
| 6644 | len += partlen; |
| 6645 | |
| 6646 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6647 | memtodest(p, partlen, syntax, quotes); |
| 6648 | |
| 6649 | if (*ap && sep) { |
| 6650 | char *q; |
| 6651 | |
| 6652 | len++; |
| 6653 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6654 | continue; |
| 6655 | } |
| 6656 | q = expdest; |
| 6657 | if (sepq) |
| 6658 | STPUTC(CTLESC, q); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6659 | /* note: may put NUL despite sep != 0 |
| 6660 | * (see sep = 1 << CHAR_BIT above) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6661 | STPUTC(sep, q); |
| 6662 | expdest = q; |
| 6663 | } |
| 6664 | } |
| 6665 | return len; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6666 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6667 | case '0': |
| 6668 | case '1': |
| 6669 | case '2': |
| 6670 | case '3': |
| 6671 | case '4': |
| 6672 | case '5': |
| 6673 | case '6': |
| 6674 | case '7': |
| 6675 | case '8': |
| 6676 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6677 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6678 | if (num < 0 || num > shellparam.nparam) |
| 6679 | return -1; |
| 6680 | p = num ? shellparam.p[num - 1] : arg0; |
| 6681 | goto value; |
| 6682 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6683 | /* NB: name has form "VAR=..." */ |
| 6684 | |
| 6685 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6686 | * which should be considered before we check variables. */ |
| 6687 | if (var_str_list) { |
| 6688 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6689 | p = NULL; |
| 6690 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6691 | char *str, *eq; |
| 6692 | str = var_str_list->text; |
| 6693 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6694 | if (!eq) /* stop at first non-assignment */ |
| 6695 | break; |
| 6696 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6697 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6698 | && strncmp(str, name, name_len) == 0 |
| 6699 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6700 | p = eq; |
| 6701 | /* goto value; - WRONG! */ |
| 6702 | /* think "A=1 A=2 B=$A" */ |
| 6703 | } |
| 6704 | var_str_list = var_str_list->next; |
| 6705 | } while (var_str_list); |
| 6706 | if (p) |
| 6707 | goto value; |
| 6708 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6709 | p = lookupvar(name); |
| 6710 | value: |
| 6711 | if (!p) |
| 6712 | return -1; |
| 6713 | |
| 6714 | len = strlen(p); |
| 6715 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6716 | memtodest(p, len, syntax, quotes); |
| 6717 | return len; |
| 6718 | } |
| 6719 | |
| 6720 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6721 | STADJUST(-len, expdest); |
| 6722 | return len; |
| 6723 | } |
| 6724 | |
| 6725 | /* |
| 6726 | * Expand a variable, and return a pointer to the next character in the |
| 6727 | * input string. |
| 6728 | */ |
| 6729 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6730 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6731 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6732 | char varflags; |
| 6733 | char subtype; |
| 6734 | char quoted; |
| 6735 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6736 | char *var; |
| 6737 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6738 | int startloc; |
| 6739 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6740 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6741 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6742 | subtype = varflags & VSTYPE; |
| 6743 | quoted = varflags & VSQUOTE; |
| 6744 | var = p; |
| 6745 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6746 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 6747 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6748 | |
| 6749 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6750 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6751 | if (varflags & VSNUL) |
| 6752 | varlen--; |
| 6753 | |
| 6754 | if (subtype == VSPLUS) { |
| 6755 | varlen = -1 - varlen; |
| 6756 | goto vsplus; |
| 6757 | } |
| 6758 | |
| 6759 | if (subtype == VSMINUS) { |
| 6760 | vsplus: |
| 6761 | if (varlen < 0) { |
| 6762 | argstr( |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6763 | p, |
| 6764 | flags | (quoted ? EXP_TILDE|EXP_QWORD : EXP_TILDE|EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6765 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6766 | ); |
| 6767 | goto end; |
| 6768 | } |
| 6769 | if (easy) |
| 6770 | goto record; |
| 6771 | goto end; |
| 6772 | } |
| 6773 | |
| 6774 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6775 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6776 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6777 | subtype, startloc, varflags, |
| 6778 | /* quotes: */ 0, |
| 6779 | var_str_list) |
| 6780 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6781 | varflags &= ~VSNUL; |
| 6782 | /* |
| 6783 | * Remove any recorded regions beyond |
| 6784 | * start of variable |
| 6785 | */ |
| 6786 | removerecordregions(startloc); |
| 6787 | goto again; |
| 6788 | } |
| 6789 | goto end; |
| 6790 | } |
| 6791 | if (easy) |
| 6792 | goto record; |
| 6793 | goto end; |
| 6794 | } |
| 6795 | |
| 6796 | if (varlen < 0 && uflag) |
| 6797 | varunset(p, var, 0, 0); |
| 6798 | |
| 6799 | if (subtype == VSLENGTH) { |
| 6800 | cvtnum(varlen > 0 ? varlen : 0); |
| 6801 | goto record; |
| 6802 | } |
| 6803 | |
| 6804 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6805 | if (easy) |
| 6806 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6807 | goto end; |
| 6808 | } |
| 6809 | |
| 6810 | #if DEBUG |
| 6811 | switch (subtype) { |
| 6812 | case VSTRIMLEFT: |
| 6813 | case VSTRIMLEFTMAX: |
| 6814 | case VSTRIMRIGHT: |
| 6815 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6816 | #if ENABLE_ASH_BASH_COMPAT |
| 6817 | case VSSUBSTR: |
| 6818 | case VSREPLACE: |
| 6819 | case VSREPLACEALL: |
| 6820 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6821 | break; |
| 6822 | default: |
| 6823 | abort(); |
| 6824 | } |
| 6825 | #endif |
| 6826 | |
| 6827 | if (varlen >= 0) { |
| 6828 | /* |
| 6829 | * Terminate the string and start recording the pattern |
| 6830 | * right after it |
| 6831 | */ |
| 6832 | STPUTC('\0', expdest); |
| 6833 | patloc = expdest - (char *)stackblock(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6834 | if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6835 | startloc, varflags, |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 6836 | /* quotes: */ flags & (EXP_FULL | EXP_CASE | EXP_REDIR), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6837 | var_str_list) |
| 6838 | ) { |
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++; |
| 6857 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 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)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6991 | sp->text = ststrdup(name); |
| 6992 | *exparg.lastp = sp; |
| 6993 | exparg.lastp = &sp->next; |
| 6994 | } |
| 6995 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6996 | /* |
| 6997 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6998 | */ |
| 6999 | static void |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7000 | expmeta(char *expdir, char *enddir, char *name) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7001 | { |
| 7002 | char *p; |
| 7003 | const char *cp; |
| 7004 | char *start; |
| 7005 | char *endname; |
| 7006 | int metaflag; |
| 7007 | struct stat statb; |
| 7008 | DIR *dirp; |
| 7009 | struct dirent *dp; |
| 7010 | int atend; |
| 7011 | int matchdot; |
| 7012 | |
| 7013 | metaflag = 0; |
| 7014 | start = name; |
| 7015 | for (p = name; *p; p++) { |
| 7016 | if (*p == '*' || *p == '?') |
| 7017 | metaflag = 1; |
| 7018 | else if (*p == '[') { |
| 7019 | char *q = p + 1; |
| 7020 | if (*q == '!') |
| 7021 | q++; |
| 7022 | for (;;) { |
| 7023 | if (*q == '\\') |
| 7024 | q++; |
| 7025 | if (*q == '/' || *q == '\0') |
| 7026 | break; |
| 7027 | if (*++q == ']') { |
| 7028 | metaflag = 1; |
| 7029 | break; |
| 7030 | } |
| 7031 | } |
| 7032 | } else if (*p == '\\') |
| 7033 | p++; |
| 7034 | else if (*p == '/') { |
| 7035 | if (metaflag) |
| 7036 | goto out; |
| 7037 | start = p + 1; |
| 7038 | } |
| 7039 | } |
| 7040 | out: |
| 7041 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 7042 | if (enddir != expdir) |
| 7043 | metaflag++; |
| 7044 | p = name; |
| 7045 | do { |
| 7046 | if (*p == '\\') |
| 7047 | p++; |
| 7048 | *enddir++ = *p; |
| 7049 | } while (*p++); |
| 7050 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 7051 | addfname(expdir); |
| 7052 | return; |
| 7053 | } |
| 7054 | endname = p; |
| 7055 | if (name < start) { |
| 7056 | p = name; |
| 7057 | do { |
| 7058 | if (*p == '\\') |
| 7059 | p++; |
| 7060 | *enddir++ = *p++; |
| 7061 | } while (p < start); |
| 7062 | } |
| 7063 | if (enddir == expdir) { |
| 7064 | cp = "."; |
| 7065 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 7066 | cp = "/"; |
| 7067 | } else { |
| 7068 | cp = expdir; |
| 7069 | enddir[-1] = '\0'; |
| 7070 | } |
| 7071 | dirp = opendir(cp); |
| 7072 | if (dirp == NULL) |
| 7073 | return; |
| 7074 | if (enddir != expdir) |
| 7075 | enddir[-1] = '/'; |
| 7076 | if (*endname == 0) { |
| 7077 | atend = 1; |
| 7078 | } else { |
| 7079 | atend = 0; |
| 7080 | *endname++ = '\0'; |
| 7081 | } |
| 7082 | matchdot = 0; |
| 7083 | p = start; |
| 7084 | if (*p == '\\') |
| 7085 | p++; |
| 7086 | if (*p == '.') |
| 7087 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7088 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7089 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7090 | continue; |
| 7091 | if (pmatch(start, dp->d_name)) { |
| 7092 | if (atend) { |
| 7093 | strcpy(enddir, dp->d_name); |
| 7094 | addfname(expdir); |
| 7095 | } else { |
| 7096 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 7097 | continue; |
| 7098 | p[-1] = '/'; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7099 | expmeta(expdir, p, endname); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7100 | } |
| 7101 | } |
| 7102 | } |
| 7103 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7104 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7105 | endname[-1] = '/'; |
| 7106 | } |
| 7107 | |
| 7108 | static struct strlist * |
| 7109 | msort(struct strlist *list, int len) |
| 7110 | { |
| 7111 | struct strlist *p, *q = NULL; |
| 7112 | struct strlist **lpp; |
| 7113 | int half; |
| 7114 | int n; |
| 7115 | |
| 7116 | if (len <= 1) |
| 7117 | return list; |
| 7118 | half = len >> 1; |
| 7119 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7120 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7121 | q = p; |
| 7122 | p = p->next; |
| 7123 | } |
| 7124 | q->next = NULL; /* terminate first half of list */ |
| 7125 | q = msort(list, half); /* sort first half of list */ |
| 7126 | p = msort(p, len - half); /* sort second half */ |
| 7127 | lpp = &list; |
| 7128 | for (;;) { |
| 7129 | #if ENABLE_LOCALE_SUPPORT |
| 7130 | if (strcoll(p->text, q->text) < 0) |
| 7131 | #else |
| 7132 | if (strcmp(p->text, q->text) < 0) |
| 7133 | #endif |
| 7134 | { |
| 7135 | *lpp = p; |
| 7136 | lpp = &p->next; |
| 7137 | p = *lpp; |
| 7138 | if (p == NULL) { |
| 7139 | *lpp = q; |
| 7140 | break; |
| 7141 | } |
| 7142 | } else { |
| 7143 | *lpp = q; |
| 7144 | lpp = &q->next; |
| 7145 | q = *lpp; |
| 7146 | if (q == NULL) { |
| 7147 | *lpp = p; |
| 7148 | break; |
| 7149 | } |
| 7150 | } |
| 7151 | } |
| 7152 | return list; |
| 7153 | } |
| 7154 | |
| 7155 | /* |
| 7156 | * Sort the results of file name expansion. It calculates the number of |
| 7157 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7158 | * work. |
| 7159 | */ |
| 7160 | static struct strlist * |
| 7161 | expsort(struct strlist *str) |
| 7162 | { |
| 7163 | int len; |
| 7164 | struct strlist *sp; |
| 7165 | |
| 7166 | len = 0; |
| 7167 | for (sp = str; sp; sp = sp->next) |
| 7168 | len++; |
| 7169 | return msort(str, len); |
| 7170 | } |
| 7171 | |
| 7172 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7173 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7174 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7175 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7176 | '*', '?', '[', 0 |
| 7177 | }; |
| 7178 | /* TODO - EXP_REDIR */ |
| 7179 | |
| 7180 | while (str) { |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7181 | char *expdir; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7182 | struct strlist **savelastp; |
| 7183 | struct strlist *sp; |
| 7184 | char *p; |
| 7185 | |
| 7186 | if (fflag) |
| 7187 | goto nometa; |
| 7188 | if (!strpbrk(str->text, metachars)) |
| 7189 | goto nometa; |
| 7190 | savelastp = exparg.lastp; |
| 7191 | |
| 7192 | INT_OFF; |
| 7193 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7194 | { |
| 7195 | int i = strlen(str->text); |
| 7196 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 7197 | } |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7198 | expmeta(expdir, expdir, p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7199 | free(expdir); |
| 7200 | if (p != str->text) |
| 7201 | free(p); |
| 7202 | INT_ON; |
| 7203 | if (exparg.lastp == savelastp) { |
| 7204 | /* |
| 7205 | * no matches |
| 7206 | */ |
| 7207 | nometa: |
| 7208 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7209 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7210 | exparg.lastp = &str->next; |
| 7211 | } else { |
| 7212 | *exparg.lastp = NULL; |
| 7213 | *savelastp = sp = expsort(*savelastp); |
| 7214 | while (sp->next != NULL) |
| 7215 | sp = sp->next; |
| 7216 | exparg.lastp = &sp->next; |
| 7217 | } |
| 7218 | str = str->next; |
| 7219 | } |
| 7220 | } |
| 7221 | |
| 7222 | /* |
| 7223 | * Perform variable substitution and command substitution on an argument, |
| 7224 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7225 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7226 | * here document expansion. |
| 7227 | */ |
| 7228 | static void |
| 7229 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7230 | { |
| 7231 | struct strlist *sp; |
| 7232 | char *p; |
| 7233 | |
| 7234 | argbackq = arg->narg.backquote; |
| 7235 | STARTSTACKSTR(expdest); |
| 7236 | ifsfirst.next = NULL; |
| 7237 | ifslastp = NULL; |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7238 | TRACE(("expandarg: argstr('%s',flags:%x)\n", arg->narg.text, flag)); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7239 | argstr(arg->narg.text, flag, |
| 7240 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7241 | p = _STPUTC('\0', expdest); |
| 7242 | expdest = p - 1; |
| 7243 | if (arglist == NULL) { |
| 7244 | return; /* here document expanded */ |
| 7245 | } |
| 7246 | p = grabstackstr(p); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7247 | TRACE(("expandarg: p:'%s'\n", p)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7248 | exparg.lastp = &exparg.list; |
| 7249 | /* |
| 7250 | * TODO - EXP_REDIR |
| 7251 | */ |
| 7252 | if (flag & EXP_FULL) { |
| 7253 | ifsbreakup(p, &exparg); |
| 7254 | *exparg.lastp = NULL; |
| 7255 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7256 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7257 | } else { |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7258 | if (flag & EXP_REDIR) { /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7259 | rmescapes(p, 0); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 7260 | TRACE(("expandarg: rmescapes:'%s'\n", p)); |
| 7261 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7262 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7263 | sp->text = p; |
| 7264 | *exparg.lastp = sp; |
| 7265 | exparg.lastp = &sp->next; |
| 7266 | } |
| 7267 | if (ifsfirst.next) |
| 7268 | ifsfree(); |
| 7269 | *exparg.lastp = NULL; |
| 7270 | if (exparg.list) { |
| 7271 | *arglist->lastp = exparg.list; |
| 7272 | arglist->lastp = exparg.lastp; |
| 7273 | } |
| 7274 | } |
| 7275 | |
| 7276 | /* |
| 7277 | * Expand shell variables and backquotes inside a here document. |
| 7278 | */ |
| 7279 | static void |
| 7280 | expandhere(union node *arg, int fd) |
| 7281 | { |
| 7282 | herefd = fd; |
| 7283 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7284 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7285 | } |
| 7286 | |
| 7287 | /* |
| 7288 | * Returns true if the pattern matches the string. |
| 7289 | */ |
| 7290 | static int |
| 7291 | patmatch(char *pattern, const char *string) |
| 7292 | { |
| 7293 | return pmatch(preglob(pattern, 0, 0), string); |
| 7294 | } |
| 7295 | |
| 7296 | /* |
| 7297 | * See if a pattern matches in a case statement. |
| 7298 | */ |
| 7299 | static int |
| 7300 | casematch(union node *pattern, char *val) |
| 7301 | { |
| 7302 | struct stackmark smark; |
| 7303 | int result; |
| 7304 | |
| 7305 | setstackmark(&smark); |
| 7306 | argbackq = pattern->narg.backquote; |
| 7307 | STARTSTACKSTR(expdest); |
| 7308 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7309 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7310 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7311 | STACKSTRNUL(expdest); |
| 7312 | result = patmatch(stackblock(), val); |
| 7313 | popstackmark(&smark); |
| 7314 | return result; |
| 7315 | } |
| 7316 | |
| 7317 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7318 | /* ============ find_command */ |
| 7319 | |
| 7320 | struct builtincmd { |
| 7321 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7322 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7323 | /* unsigned flags; */ |
| 7324 | }; |
| 7325 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7326 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7327 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7328 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7329 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7330 | |
| 7331 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7332 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7333 | union param { |
| 7334 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7335 | /* index >= 0 for commands without path (slashes) */ |
| 7336 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7337 | /* index == -1 for commands with slashes */ |
| 7338 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7339 | const struct builtincmd *cmd; |
| 7340 | struct funcnode *func; |
| 7341 | } u; |
| 7342 | }; |
| 7343 | /* values of cmdtype */ |
| 7344 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7345 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7346 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7347 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7348 | |
| 7349 | /* action to find_command() */ |
| 7350 | #define DO_ERR 0x01 /* prints errors */ |
| 7351 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7352 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7353 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7354 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7355 | |
| 7356 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7357 | |
| 7358 | |
| 7359 | /* ============ Hashing commands */ |
| 7360 | |
| 7361 | /* |
| 7362 | * When commands are first encountered, they are entered in a hash table. |
| 7363 | * This ensures that a full path search will not have to be done for them |
| 7364 | * on each invocation. |
| 7365 | * |
| 7366 | * We should investigate converting to a linear search, even though that |
| 7367 | * would make the command name "hash" a misnomer. |
| 7368 | */ |
| 7369 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7370 | struct tblentry { |
| 7371 | struct tblentry *next; /* next entry in hash chain */ |
| 7372 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7373 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7374 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7375 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7376 | }; |
| 7377 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7378 | static struct tblentry **cmdtable; |
| 7379 | #define INIT_G_cmdtable() do { \ |
| 7380 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7381 | } while (0) |
| 7382 | |
| 7383 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7384 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7385 | |
| 7386 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7387 | 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] | 7388 | { |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7389 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7390 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7391 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 7392 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7393 | while (*envp) |
| 7394 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7395 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7396 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7397 | /* re-exec ourselves with the new arguments */ |
| 7398 | execve(bb_busybox_exec_path, argv, envp); |
| 7399 | /* If they called chroot or otherwise made the binary no longer |
| 7400 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7401 | } |
| 7402 | #endif |
| 7403 | |
| 7404 | repeat: |
| 7405 | #ifdef SYSV |
| 7406 | do { |
| 7407 | execve(cmd, argv, envp); |
| 7408 | } while (errno == EINTR); |
| 7409 | #else |
| 7410 | execve(cmd, argv, envp); |
| 7411 | #endif |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7412 | if (cmd == (char*) bb_busybox_exec_path) { |
| 7413 | /* We already visited ENOEXEC branch below, don't do it again */ |
| 7414 | //TODO: try execve(initial_argv0_of_shell, argv, envp) before giving up? |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7415 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7416 | return; |
| 7417 | } |
| 7418 | if (errno == ENOEXEC) { |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7419 | /* Run "cmd" as a shell script: |
| 7420 | * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
| 7421 | * "If the execve() function fails with ENOEXEC, the shell |
| 7422 | * shall execute a command equivalent to having a shell invoked |
| 7423 | * with the command name as its first operand, |
| 7424 | * with any remaining arguments passed to the new shell" |
| 7425 | * |
| 7426 | * That is, do not use $SHELL, user's shell, or /bin/sh; |
| 7427 | * just call ourselves. |
Denys Vlasenko | 2bef526 | 2011-12-16 00:25:17 +0100 | [diff] [blame] | 7428 | * |
| 7429 | * Note that bash reads ~80 chars of the file, and if it sees |
| 7430 | * a zero byte before it sees newline, it doesn't try to |
| 7431 | * interpret it, but fails with "cannot execute binary file" |
Denys Vlasenko | cda6ea9 | 2011-12-16 00:44:36 +0100 | [diff] [blame] | 7432 | * message and exit code 126. For one, this prevents attempts |
| 7433 | * to interpret foreign ELF binaries as shell scripts. |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7434 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7435 | char **ap; |
| 7436 | char **new; |
| 7437 | |
| 7438 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7439 | continue; |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7440 | new = ckmalloc((ap - argv + 2) * sizeof(new[0])); |
| 7441 | new[0] = (char*) "ash"; |
| 7442 | new[1] = cmd; |
| 7443 | ap = new + 2; |
| 7444 | while ((*ap++ = *++argv) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7445 | continue; |
Denys Vlasenko | aefe1c2 | 2011-03-07 12:02:40 +0100 | [diff] [blame] | 7446 | cmd = (char*) bb_busybox_exec_path; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7447 | argv = new; |
| 7448 | goto repeat; |
| 7449 | } |
| 7450 | } |
| 7451 | |
| 7452 | /* |
| 7453 | * Exec a program. Never returns. If you change this routine, you may |
| 7454 | * have to change the find_command routine as well. |
| 7455 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7456 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7457 | static void |
| 7458 | shellexec(char **argv, const char *path, int idx) |
| 7459 | { |
| 7460 | char *cmdname; |
| 7461 | int e; |
| 7462 | char **envp; |
| 7463 | int exerrno; |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 7464 | int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7465 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7466 | clearredir(/*drop:*/ 1); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 7467 | envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7468 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7469 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7470 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7471 | #endif |
| 7472 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7473 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 7474 | if (applet_no >= 0) { |
| 7475 | /* We tried execing ourself, but it didn't work. |
| 7476 | * Maybe /proc/self/exe doesn't exist? |
| 7477 | * Try $PATH search. |
| 7478 | */ |
| 7479 | goto try_PATH; |
| 7480 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7481 | e = errno; |
| 7482 | } else { |
Denys Vlasenko | 83f103b | 2011-12-20 06:10:35 +0100 | [diff] [blame] | 7483 | try_PATH: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7484 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7485 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7486 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7487 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7488 | if (errno != ENOENT && errno != ENOTDIR) |
| 7489 | e = errno; |
| 7490 | } |
| 7491 | stunalloc(cmdname); |
| 7492 | } |
| 7493 | } |
| 7494 | |
| 7495 | /* Map to POSIX errors */ |
| 7496 | switch (e) { |
| 7497 | case EACCES: |
| 7498 | exerrno = 126; |
| 7499 | break; |
| 7500 | case ENOENT: |
| 7501 | exerrno = 127; |
| 7502 | break; |
| 7503 | default: |
| 7504 | exerrno = 2; |
| 7505 | break; |
| 7506 | } |
| 7507 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7508 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7509 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7510 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7511 | /* NOTREACHED */ |
| 7512 | } |
| 7513 | |
| 7514 | static void |
| 7515 | printentry(struct tblentry *cmdp) |
| 7516 | { |
| 7517 | int idx; |
| 7518 | const char *path; |
| 7519 | char *name; |
| 7520 | |
| 7521 | idx = cmdp->param.index; |
| 7522 | path = pathval(); |
| 7523 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7524 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7525 | stunalloc(name); |
| 7526 | } while (--idx >= 0); |
| 7527 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7528 | } |
| 7529 | |
| 7530 | /* |
| 7531 | * Clear out command entries. The argument specifies the first entry in |
| 7532 | * PATH which has changed. |
| 7533 | */ |
| 7534 | static void |
| 7535 | clearcmdentry(int firstchange) |
| 7536 | { |
| 7537 | struct tblentry **tblp; |
| 7538 | struct tblentry **pp; |
| 7539 | struct tblentry *cmdp; |
| 7540 | |
| 7541 | INT_OFF; |
| 7542 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7543 | pp = tblp; |
| 7544 | while ((cmdp = *pp) != NULL) { |
| 7545 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7546 | cmdp->param.index >= firstchange) |
| 7547 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7548 | builtinloc >= firstchange) |
| 7549 | ) { |
| 7550 | *pp = cmdp->next; |
| 7551 | free(cmdp); |
| 7552 | } else { |
| 7553 | pp = &cmdp->next; |
| 7554 | } |
| 7555 | } |
| 7556 | } |
| 7557 | INT_ON; |
| 7558 | } |
| 7559 | |
| 7560 | /* |
| 7561 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7562 | * add the command to the table if it is not already present. The |
| 7563 | * variable "lastcmdentry" is set to point to the address of the link |
| 7564 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7565 | * entry. |
| 7566 | * |
| 7567 | * Interrupts must be off if called with add != 0. |
| 7568 | */ |
| 7569 | static struct tblentry **lastcmdentry; |
| 7570 | |
| 7571 | static struct tblentry * |
| 7572 | cmdlookup(const char *name, int add) |
| 7573 | { |
| 7574 | unsigned int hashval; |
| 7575 | const char *p; |
| 7576 | struct tblentry *cmdp; |
| 7577 | struct tblentry **pp; |
| 7578 | |
| 7579 | p = name; |
| 7580 | hashval = (unsigned char)*p << 4; |
| 7581 | while (*p) |
| 7582 | hashval += (unsigned char)*p++; |
| 7583 | hashval &= 0x7FFF; |
| 7584 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7585 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7586 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7587 | break; |
| 7588 | pp = &cmdp->next; |
| 7589 | } |
| 7590 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7591 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7592 | + strlen(name) |
| 7593 | /* + 1 - already done because |
| 7594 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7595 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7596 | cmdp->cmdtype = CMDUNKNOWN; |
| 7597 | strcpy(cmdp->cmdname, name); |
| 7598 | } |
| 7599 | lastcmdentry = pp; |
| 7600 | return cmdp; |
| 7601 | } |
| 7602 | |
| 7603 | /* |
| 7604 | * Delete the command entry returned on the last lookup. |
| 7605 | */ |
| 7606 | static void |
| 7607 | delete_cmd_entry(void) |
| 7608 | { |
| 7609 | struct tblentry *cmdp; |
| 7610 | |
| 7611 | INT_OFF; |
| 7612 | cmdp = *lastcmdentry; |
| 7613 | *lastcmdentry = cmdp->next; |
| 7614 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7615 | freefunc(cmdp->param.func); |
| 7616 | free(cmdp); |
| 7617 | INT_ON; |
| 7618 | } |
| 7619 | |
| 7620 | /* |
| 7621 | * Add a new command entry, replacing any existing command entry for |
| 7622 | * the same name - except special builtins. |
| 7623 | */ |
| 7624 | static void |
| 7625 | addcmdentry(char *name, struct cmdentry *entry) |
| 7626 | { |
| 7627 | struct tblentry *cmdp; |
| 7628 | |
| 7629 | cmdp = cmdlookup(name, 1); |
| 7630 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7631 | freefunc(cmdp->param.func); |
| 7632 | } |
| 7633 | cmdp->cmdtype = entry->cmdtype; |
| 7634 | cmdp->param = entry->u; |
| 7635 | cmdp->rehash = 0; |
| 7636 | } |
| 7637 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7638 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7639 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7640 | { |
| 7641 | struct tblentry **pp; |
| 7642 | struct tblentry *cmdp; |
| 7643 | int c; |
| 7644 | struct cmdentry entry; |
| 7645 | char *name; |
| 7646 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7647 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7648 | clearcmdentry(0); |
| 7649 | return 0; |
| 7650 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7651 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7652 | if (*argptr == NULL) { |
| 7653 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7654 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7655 | if (cmdp->cmdtype == CMDNORMAL) |
| 7656 | printentry(cmdp); |
| 7657 | } |
| 7658 | } |
| 7659 | return 0; |
| 7660 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7661 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7662 | c = 0; |
| 7663 | while ((name = *argptr) != NULL) { |
| 7664 | cmdp = cmdlookup(name, 0); |
| 7665 | if (cmdp != NULL |
| 7666 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7667 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7668 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7669 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7670 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7671 | find_command(name, &entry, DO_ERR, pathval()); |
| 7672 | if (entry.cmdtype == CMDUNKNOWN) |
| 7673 | c = 1; |
| 7674 | argptr++; |
| 7675 | } |
| 7676 | return c; |
| 7677 | } |
| 7678 | |
| 7679 | /* |
| 7680 | * Called when a cd is done. Marks all commands so the next time they |
| 7681 | * are executed they will be rehashed. |
| 7682 | */ |
| 7683 | static void |
| 7684 | hashcd(void) |
| 7685 | { |
| 7686 | struct tblentry **pp; |
| 7687 | struct tblentry *cmdp; |
| 7688 | |
| 7689 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7690 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7691 | if (cmdp->cmdtype == CMDNORMAL |
| 7692 | || (cmdp->cmdtype == CMDBUILTIN |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 7693 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7694 | && builtinloc > 0) |
| 7695 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7696 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7697 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7698 | } |
| 7699 | } |
| 7700 | } |
| 7701 | |
| 7702 | /* |
| 7703 | * Fix command hash table when PATH changed. |
| 7704 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7705 | * pathval() still returns the old value at this point. |
| 7706 | * Called with interrupts off. |
| 7707 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7708 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7709 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7710 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7711 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7712 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7713 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7714 | int idx_bltin; |
| 7715 | |
| 7716 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7717 | firstchange = 9999; /* assume no change */ |
| 7718 | idx = 0; |
| 7719 | idx_bltin = -1; |
| 7720 | for (;;) { |
| 7721 | if (*old != *new) { |
| 7722 | firstchange = idx; |
| 7723 | if ((*old == '\0' && *new == ':') |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7724 | || (*old == ':' && *new == '\0') |
| 7725 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7726 | firstchange++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7727 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7728 | old = new; /* ignore subsequent differences */ |
| 7729 | } |
| 7730 | if (*new == '\0') |
| 7731 | break; |
| 7732 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7733 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7734 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7735 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7736 | new++; |
| 7737 | old++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7738 | } |
| 7739 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7740 | builtinloc = idx_bltin; /* zap builtins */ |
| 7741 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7742 | firstchange = 0; |
| 7743 | clearcmdentry(firstchange); |
| 7744 | builtinloc = idx_bltin; |
| 7745 | } |
| 7746 | |
| 7747 | #define TEOF 0 |
| 7748 | #define TNL 1 |
| 7749 | #define TREDIR 2 |
| 7750 | #define TWORD 3 |
| 7751 | #define TSEMI 4 |
| 7752 | #define TBACKGND 5 |
| 7753 | #define TAND 6 |
| 7754 | #define TOR 7 |
| 7755 | #define TPIPE 8 |
| 7756 | #define TLP 9 |
| 7757 | #define TRP 10 |
| 7758 | #define TENDCASE 11 |
| 7759 | #define TENDBQUOTE 12 |
| 7760 | #define TNOT 13 |
| 7761 | #define TCASE 14 |
| 7762 | #define TDO 15 |
| 7763 | #define TDONE 16 |
| 7764 | #define TELIF 17 |
| 7765 | #define TELSE 18 |
| 7766 | #define TESAC 19 |
| 7767 | #define TFI 20 |
| 7768 | #define TFOR 21 |
| 7769 | #define TIF 22 |
| 7770 | #define TIN 23 |
| 7771 | #define TTHEN 24 |
| 7772 | #define TUNTIL 25 |
| 7773 | #define TWHILE 26 |
| 7774 | #define TBEGIN 27 |
| 7775 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7776 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7777 | |
| 7778 | /* first char is indicating which tokens mark the end of a list */ |
| 7779 | static const char *const tokname_array[] = { |
| 7780 | "\1end of file", |
| 7781 | "\0newline", |
| 7782 | "\0redirection", |
| 7783 | "\0word", |
| 7784 | "\0;", |
| 7785 | "\0&", |
| 7786 | "\0&&", |
| 7787 | "\0||", |
| 7788 | "\0|", |
| 7789 | "\0(", |
| 7790 | "\1)", |
| 7791 | "\1;;", |
| 7792 | "\1`", |
| 7793 | #define KWDOFFSET 13 |
| 7794 | /* the following are keywords */ |
| 7795 | "\0!", |
| 7796 | "\0case", |
| 7797 | "\1do", |
| 7798 | "\1done", |
| 7799 | "\1elif", |
| 7800 | "\1else", |
| 7801 | "\1esac", |
| 7802 | "\1fi", |
| 7803 | "\0for", |
| 7804 | "\0if", |
| 7805 | "\0in", |
| 7806 | "\1then", |
| 7807 | "\0until", |
| 7808 | "\0while", |
| 7809 | "\0{", |
| 7810 | "\1}", |
| 7811 | }; |
| 7812 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7813 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7814 | static int |
| 7815 | pstrcmp(const void *a, const void *b) |
| 7816 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7817 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7818 | } |
| 7819 | |
| 7820 | static const char *const * |
| 7821 | findkwd(const char *s) |
| 7822 | { |
| 7823 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7824 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7825 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7826 | } |
| 7827 | |
| 7828 | /* |
| 7829 | * Locate and print what a word is... |
| 7830 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7831 | static int |
| 7832 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7833 | { |
| 7834 | struct cmdentry entry; |
| 7835 | struct tblentry *cmdp; |
| 7836 | #if ENABLE_ASH_ALIAS |
| 7837 | const struct alias *ap; |
| 7838 | #endif |
| 7839 | const char *path = pathval(); |
| 7840 | |
| 7841 | if (describe_command_verbose) { |
| 7842 | out1str(command); |
| 7843 | } |
| 7844 | |
| 7845 | /* First look at the keywords */ |
| 7846 | if (findkwd(command)) { |
| 7847 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7848 | goto out; |
| 7849 | } |
| 7850 | |
| 7851 | #if ENABLE_ASH_ALIAS |
| 7852 | /* Then look at the aliases */ |
| 7853 | ap = lookupalias(command, 0); |
| 7854 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7855 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7856 | out1str("alias "); |
| 7857 | printalias(ap); |
| 7858 | return 0; |
| 7859 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7860 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7861 | goto out; |
| 7862 | } |
| 7863 | #endif |
| 7864 | /* Then check if it is a tracked alias */ |
| 7865 | cmdp = cmdlookup(command, 0); |
| 7866 | if (cmdp != NULL) { |
| 7867 | entry.cmdtype = cmdp->cmdtype; |
| 7868 | entry.u = cmdp->param; |
| 7869 | } else { |
| 7870 | /* Finally use brute force */ |
| 7871 | find_command(command, &entry, DO_ABS, path); |
| 7872 | } |
| 7873 | |
| 7874 | switch (entry.cmdtype) { |
| 7875 | case CMDNORMAL: { |
| 7876 | int j = entry.u.index; |
| 7877 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7878 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7879 | p = command; |
| 7880 | } else { |
| 7881 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7882 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7883 | stunalloc(p); |
| 7884 | } while (--j >= 0); |
| 7885 | } |
| 7886 | if (describe_command_verbose) { |
| 7887 | out1fmt(" is%s %s", |
| 7888 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7889 | ); |
| 7890 | } else { |
| 7891 | out1str(p); |
| 7892 | } |
| 7893 | break; |
| 7894 | } |
| 7895 | |
| 7896 | case CMDFUNCTION: |
| 7897 | if (describe_command_verbose) { |
| 7898 | out1str(" is a shell function"); |
| 7899 | } else { |
| 7900 | out1str(command); |
| 7901 | } |
| 7902 | break; |
| 7903 | |
| 7904 | case CMDBUILTIN: |
| 7905 | if (describe_command_verbose) { |
| 7906 | out1fmt(" is a %sshell builtin", |
| 7907 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7908 | "special " : nullstr |
| 7909 | ); |
| 7910 | } else { |
| 7911 | out1str(command); |
| 7912 | } |
| 7913 | break; |
| 7914 | |
| 7915 | default: |
| 7916 | if (describe_command_verbose) { |
| 7917 | out1str(": not found\n"); |
| 7918 | } |
| 7919 | return 127; |
| 7920 | } |
| 7921 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7922 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7923 | return 0; |
| 7924 | } |
| 7925 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7926 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7927 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7928 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7929 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7930 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7931 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7932 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7933 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7934 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7935 | i++; |
| 7936 | verbose = 0; |
| 7937 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7938 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7939 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7940 | } |
| 7941 | return err; |
| 7942 | } |
| 7943 | |
| 7944 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7945 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7946 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7947 | { |
| 7948 | int c; |
| 7949 | enum { |
| 7950 | VERIFY_BRIEF = 1, |
| 7951 | VERIFY_VERBOSE = 2, |
| 7952 | } verify = 0; |
| 7953 | |
| 7954 | while ((c = nextopt("pvV")) != '\0') |
| 7955 | if (c == 'V') |
| 7956 | verify |= VERIFY_VERBOSE; |
| 7957 | else if (c == 'v') |
| 7958 | verify |= VERIFY_BRIEF; |
| 7959 | #if DEBUG |
| 7960 | else if (c != 'p') |
| 7961 | abort(); |
| 7962 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7963 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7964 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7965 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7966 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7967 | |
| 7968 | return 0; |
| 7969 | } |
| 7970 | #endif |
| 7971 | |
| 7972 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7973 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7974 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7975 | static int funcblocksize; /* size of structures in function */ |
| 7976 | static int funcstringsize; /* size of strings in node */ |
| 7977 | static void *funcblock; /* block to allocate function from */ |
| 7978 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7979 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7980 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7981 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7982 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7983 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7984 | |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 7985 | static const uint8_t nodesize[N_NUMBER] = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7986 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7987 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7988 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7989 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7990 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7991 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7992 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7993 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7994 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7995 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7996 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7997 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7998 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7999 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 8000 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8001 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 8002 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8003 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8004 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 8005 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 8006 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8007 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8008 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8009 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 8010 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8011 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 8012 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8013 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 8014 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8015 | }; |
| 8016 | |
| 8017 | static void calcsize(union node *n); |
| 8018 | |
| 8019 | static void |
| 8020 | sizenodelist(struct nodelist *lp) |
| 8021 | { |
| 8022 | while (lp) { |
| 8023 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 8024 | calcsize(lp->n); |
| 8025 | lp = lp->next; |
| 8026 | } |
| 8027 | } |
| 8028 | |
| 8029 | static void |
| 8030 | calcsize(union node *n) |
| 8031 | { |
| 8032 | if (n == NULL) |
| 8033 | return; |
| 8034 | funcblocksize += nodesize[n->type]; |
| 8035 | switch (n->type) { |
| 8036 | case NCMD: |
| 8037 | calcsize(n->ncmd.redirect); |
| 8038 | calcsize(n->ncmd.args); |
| 8039 | calcsize(n->ncmd.assign); |
| 8040 | break; |
| 8041 | case NPIPE: |
| 8042 | sizenodelist(n->npipe.cmdlist); |
| 8043 | break; |
| 8044 | case NREDIR: |
| 8045 | case NBACKGND: |
| 8046 | case NSUBSHELL: |
| 8047 | calcsize(n->nredir.redirect); |
| 8048 | calcsize(n->nredir.n); |
| 8049 | break; |
| 8050 | case NAND: |
| 8051 | case NOR: |
| 8052 | case NSEMI: |
| 8053 | case NWHILE: |
| 8054 | case NUNTIL: |
| 8055 | calcsize(n->nbinary.ch2); |
| 8056 | calcsize(n->nbinary.ch1); |
| 8057 | break; |
| 8058 | case NIF: |
| 8059 | calcsize(n->nif.elsepart); |
| 8060 | calcsize(n->nif.ifpart); |
| 8061 | calcsize(n->nif.test); |
| 8062 | break; |
| 8063 | case NFOR: |
| 8064 | funcstringsize += strlen(n->nfor.var) + 1; |
| 8065 | calcsize(n->nfor.body); |
| 8066 | calcsize(n->nfor.args); |
| 8067 | break; |
| 8068 | case NCASE: |
| 8069 | calcsize(n->ncase.cases); |
| 8070 | calcsize(n->ncase.expr); |
| 8071 | break; |
| 8072 | case NCLIST: |
| 8073 | calcsize(n->nclist.body); |
| 8074 | calcsize(n->nclist.pattern); |
| 8075 | calcsize(n->nclist.next); |
| 8076 | break; |
| 8077 | case NDEFUN: |
| 8078 | case NARG: |
| 8079 | sizenodelist(n->narg.backquote); |
| 8080 | funcstringsize += strlen(n->narg.text) + 1; |
| 8081 | calcsize(n->narg.next); |
| 8082 | break; |
| 8083 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8084 | #if ENABLE_ASH_BASH_COMPAT |
| 8085 | case NTO2: |
| 8086 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8087 | case NCLOBBER: |
| 8088 | case NFROM: |
| 8089 | case NFROMTO: |
| 8090 | case NAPPEND: |
| 8091 | calcsize(n->nfile.fname); |
| 8092 | calcsize(n->nfile.next); |
| 8093 | break; |
| 8094 | case NTOFD: |
| 8095 | case NFROMFD: |
| 8096 | calcsize(n->ndup.vname); |
| 8097 | calcsize(n->ndup.next); |
| 8098 | break; |
| 8099 | case NHERE: |
| 8100 | case NXHERE: |
| 8101 | calcsize(n->nhere.doc); |
| 8102 | calcsize(n->nhere.next); |
| 8103 | break; |
| 8104 | case NNOT: |
| 8105 | calcsize(n->nnot.com); |
| 8106 | break; |
| 8107 | }; |
| 8108 | } |
| 8109 | |
| 8110 | static char * |
| 8111 | nodeckstrdup(char *s) |
| 8112 | { |
| 8113 | char *rtn = funcstring; |
| 8114 | |
| 8115 | strcpy(funcstring, s); |
| 8116 | funcstring += strlen(s) + 1; |
| 8117 | return rtn; |
| 8118 | } |
| 8119 | |
| 8120 | static union node *copynode(union node *); |
| 8121 | |
| 8122 | static struct nodelist * |
| 8123 | copynodelist(struct nodelist *lp) |
| 8124 | { |
| 8125 | struct nodelist *start; |
| 8126 | struct nodelist **lpp; |
| 8127 | |
| 8128 | lpp = &start; |
| 8129 | while (lp) { |
| 8130 | *lpp = funcblock; |
| 8131 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 8132 | (*lpp)->n = copynode(lp->n); |
| 8133 | lp = lp->next; |
| 8134 | lpp = &(*lpp)->next; |
| 8135 | } |
| 8136 | *lpp = NULL; |
| 8137 | return start; |
| 8138 | } |
| 8139 | |
| 8140 | static union node * |
| 8141 | copynode(union node *n) |
| 8142 | { |
| 8143 | union node *new; |
| 8144 | |
| 8145 | if (n == NULL) |
| 8146 | return NULL; |
| 8147 | new = funcblock; |
| 8148 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8149 | |
| 8150 | switch (n->type) { |
| 8151 | case NCMD: |
| 8152 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8153 | new->ncmd.args = copynode(n->ncmd.args); |
| 8154 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8155 | break; |
| 8156 | case NPIPE: |
| 8157 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8158 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8159 | break; |
| 8160 | case NREDIR: |
| 8161 | case NBACKGND: |
| 8162 | case NSUBSHELL: |
| 8163 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8164 | new->nredir.n = copynode(n->nredir.n); |
| 8165 | break; |
| 8166 | case NAND: |
| 8167 | case NOR: |
| 8168 | case NSEMI: |
| 8169 | case NWHILE: |
| 8170 | case NUNTIL: |
| 8171 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8172 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8173 | break; |
| 8174 | case NIF: |
| 8175 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8176 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8177 | new->nif.test = copynode(n->nif.test); |
| 8178 | break; |
| 8179 | case NFOR: |
| 8180 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8181 | new->nfor.body = copynode(n->nfor.body); |
| 8182 | new->nfor.args = copynode(n->nfor.args); |
| 8183 | break; |
| 8184 | case NCASE: |
| 8185 | new->ncase.cases = copynode(n->ncase.cases); |
| 8186 | new->ncase.expr = copynode(n->ncase.expr); |
| 8187 | break; |
| 8188 | case NCLIST: |
| 8189 | new->nclist.body = copynode(n->nclist.body); |
| 8190 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8191 | new->nclist.next = copynode(n->nclist.next); |
| 8192 | break; |
| 8193 | case NDEFUN: |
| 8194 | case NARG: |
| 8195 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8196 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8197 | new->narg.next = copynode(n->narg.next); |
| 8198 | break; |
| 8199 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8200 | #if ENABLE_ASH_BASH_COMPAT |
| 8201 | case NTO2: |
| 8202 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8203 | case NCLOBBER: |
| 8204 | case NFROM: |
| 8205 | case NFROMTO: |
| 8206 | case NAPPEND: |
| 8207 | new->nfile.fname = copynode(n->nfile.fname); |
| 8208 | new->nfile.fd = n->nfile.fd; |
| 8209 | new->nfile.next = copynode(n->nfile.next); |
| 8210 | break; |
| 8211 | case NTOFD: |
| 8212 | case NFROMFD: |
| 8213 | new->ndup.vname = copynode(n->ndup.vname); |
| 8214 | new->ndup.dupfd = n->ndup.dupfd; |
| 8215 | new->ndup.fd = n->ndup.fd; |
| 8216 | new->ndup.next = copynode(n->ndup.next); |
| 8217 | break; |
| 8218 | case NHERE: |
| 8219 | case NXHERE: |
| 8220 | new->nhere.doc = copynode(n->nhere.doc); |
| 8221 | new->nhere.fd = n->nhere.fd; |
| 8222 | new->nhere.next = copynode(n->nhere.next); |
| 8223 | break; |
| 8224 | case NNOT: |
| 8225 | new->nnot.com = copynode(n->nnot.com); |
| 8226 | break; |
| 8227 | }; |
| 8228 | new->type = n->type; |
| 8229 | return new; |
| 8230 | } |
| 8231 | |
| 8232 | /* |
| 8233 | * Make a copy of a parse tree. |
| 8234 | */ |
| 8235 | static struct funcnode * |
| 8236 | copyfunc(union node *n) |
| 8237 | { |
| 8238 | struct funcnode *f; |
| 8239 | size_t blocksize; |
| 8240 | |
| 8241 | funcblocksize = offsetof(struct funcnode, n); |
| 8242 | funcstringsize = 0; |
| 8243 | calcsize(n); |
| 8244 | blocksize = funcblocksize; |
| 8245 | f = ckmalloc(blocksize + funcstringsize); |
| 8246 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8247 | funcstring = (char *) f + blocksize; |
| 8248 | copynode(n); |
| 8249 | f->count = 0; |
| 8250 | return f; |
| 8251 | } |
| 8252 | |
| 8253 | /* |
| 8254 | * Define a shell function. |
| 8255 | */ |
| 8256 | static void |
| 8257 | defun(char *name, union node *func) |
| 8258 | { |
| 8259 | struct cmdentry entry; |
| 8260 | |
| 8261 | INT_OFF; |
| 8262 | entry.cmdtype = CMDFUNCTION; |
| 8263 | entry.u.func = copyfunc(func); |
| 8264 | addcmdentry(name, &entry); |
| 8265 | INT_ON; |
| 8266 | } |
| 8267 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8268 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8269 | #define SKIPBREAK (1 << 0) |
| 8270 | #define SKIPCONT (1 << 1) |
| 8271 | #define SKIPFUNC (1 << 2) |
| 8272 | #define SKIPFILE (1 << 3) |
| 8273 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8274 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8275 | static int skipcount; /* number of levels to skip */ |
| 8276 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8277 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8278 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8279 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8280 | static int evalstring(char *s, int mask); |
| 8281 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8282 | /* Called to execute a trap. |
| 8283 | * Single callsite - at the end of evaltree(). |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8284 | * If we return non-zero, evaltree raises EXEXIT exception. |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8285 | * |
| 8286 | * Perhaps we should avoid entering new trap handlers |
| 8287 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8288 | */ |
| 8289 | static int |
| 8290 | dotrap(void) |
| 8291 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8292 | uint8_t *g; |
| 8293 | int sig; |
| 8294 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8295 | |
| 8296 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8297 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8298 | xbarrier(); |
| 8299 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8300 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8301 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8302 | int want_exexit; |
| 8303 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8304 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8305 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8306 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8307 | t = trap[sig]; |
| 8308 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8309 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8310 | if (sig == SIGINT && !t) |
| 8311 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8312 | |
| 8313 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8314 | *g = 0; |
| 8315 | if (!t) |
| 8316 | continue; |
| 8317 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8318 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8319 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8320 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8321 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8322 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8323 | } |
| 8324 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8325 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8326 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8327 | } |
| 8328 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8329 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8330 | static void evalloop(union node *, int); |
| 8331 | static void evalfor(union node *, int); |
| 8332 | static void evalcase(union node *, int); |
| 8333 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8334 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8335 | static void evalpipe(union node *, int); |
| 8336 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8337 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8338 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8339 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8340 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8341 | * Evaluate a parse tree. The value is left in the global variable |
| 8342 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8343 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8344 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8345 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8346 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8347 | struct jmploc *volatile savehandler = exception_handler; |
| 8348 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8349 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8350 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8351 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8352 | int int_level; |
| 8353 | |
| 8354 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8355 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8356 | if (n == NULL) { |
| 8357 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8358 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8359 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8360 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8361 | |
| 8362 | exception_handler = &jmploc; |
| 8363 | { |
| 8364 | int err = setjmp(jmploc.loc); |
| 8365 | if (err) { |
| 8366 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8367 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8368 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8369 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8370 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8371 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8372 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8373 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8374 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8375 | exception_handler = savehandler; |
| 8376 | longjmp(exception_handler->loc, err); |
| 8377 | } |
| 8378 | } |
| 8379 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8380 | switch (n->type) { |
| 8381 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8382 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8383 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8384 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8385 | break; |
| 8386 | #endif |
| 8387 | case NNOT: |
| 8388 | evaltree(n->nnot.com, EV_TESTED); |
| 8389 | status = !exitstatus; |
| 8390 | goto setstatus; |
| 8391 | case NREDIR: |
| 8392 | expredir(n->nredir.redirect); |
| 8393 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8394 | if (!status) { |
| 8395 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8396 | status = exitstatus; |
| 8397 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8398 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8399 | goto setstatus; |
| 8400 | case NCMD: |
| 8401 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8402 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8403 | if (eflag && !(flags & EV_TESTED)) |
| 8404 | checkexit = ~0; |
| 8405 | goto calleval; |
| 8406 | case NFOR: |
| 8407 | evalfn = evalfor; |
| 8408 | goto calleval; |
| 8409 | case NWHILE: |
| 8410 | case NUNTIL: |
| 8411 | evalfn = evalloop; |
| 8412 | goto calleval; |
| 8413 | case NSUBSHELL: |
| 8414 | case NBACKGND: |
| 8415 | evalfn = evalsubshell; |
| 8416 | goto calleval; |
| 8417 | case NPIPE: |
| 8418 | evalfn = evalpipe; |
| 8419 | goto checkexit; |
| 8420 | case NCASE: |
| 8421 | evalfn = evalcase; |
| 8422 | goto calleval; |
| 8423 | case NAND: |
| 8424 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8425 | case NSEMI: { |
| 8426 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8427 | #if NAND + 1 != NOR |
| 8428 | #error NAND + 1 != NOR |
| 8429 | #endif |
| 8430 | #if NOR + 1 != NSEMI |
| 8431 | #error NOR + 1 != NSEMI |
| 8432 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8433 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8434 | evaltree( |
| 8435 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8436 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8437 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8438 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8439 | break; |
| 8440 | if (!evalskip) { |
| 8441 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8442 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8443 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8444 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8445 | evalfn(n, flags); |
| 8446 | break; |
| 8447 | } |
| 8448 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8449 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8450 | case NIF: |
| 8451 | evaltree(n->nif.test, EV_TESTED); |
| 8452 | if (evalskip) |
| 8453 | break; |
| 8454 | if (exitstatus == 0) { |
| 8455 | n = n->nif.ifpart; |
| 8456 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8457 | } |
| 8458 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8459 | n = n->nif.elsepart; |
| 8460 | goto evaln; |
| 8461 | } |
| 8462 | goto success; |
| 8463 | case NDEFUN: |
| 8464 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8465 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8466 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8467 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8468 | exitstatus = status; |
| 8469 | break; |
| 8470 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8471 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8472 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8473 | exception_handler = savehandler; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8474 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8475 | out1: |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8476 | /* Order of checks below is important: |
| 8477 | * signal handlers trigger before exit caused by "set -e". |
| 8478 | */ |
| 8479 | if (pending_sig && dotrap()) |
| 8480 | goto exexit; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8481 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8482 | evalskip |= SKIPEVAL; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8483 | |
| 8484 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8485 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8486 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8487 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8488 | |
| 8489 | RESTORE_INT(int_level); |
| 8490 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8491 | } |
| 8492 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8493 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8494 | static |
| 8495 | #endif |
| 8496 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8497 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8498 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8499 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8500 | { |
| 8501 | int status; |
| 8502 | |
| 8503 | loopnest++; |
| 8504 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8505 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8506 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8507 | int i; |
| 8508 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8509 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8510 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8511 | skipping: |
| 8512 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8513 | evalskip = 0; |
| 8514 | continue; |
| 8515 | } |
| 8516 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8517 | evalskip = 0; |
| 8518 | break; |
| 8519 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8520 | i = exitstatus; |
| 8521 | if (n->type != NWHILE) |
| 8522 | i = !i; |
| 8523 | if (i != 0) |
| 8524 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8525 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8526 | status = exitstatus; |
| 8527 | if (evalskip) |
| 8528 | goto skipping; |
| 8529 | } |
| 8530 | loopnest--; |
| 8531 | exitstatus = status; |
| 8532 | } |
| 8533 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8534 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8535 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8536 | { |
| 8537 | struct arglist arglist; |
| 8538 | union node *argp; |
| 8539 | struct strlist *sp; |
| 8540 | struct stackmark smark; |
| 8541 | |
| 8542 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8543 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8544 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8545 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8546 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8547 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8548 | if (evalskip) |
| 8549 | goto out; |
| 8550 | } |
| 8551 | *arglist.lastp = NULL; |
| 8552 | |
| 8553 | exitstatus = 0; |
| 8554 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8555 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8556 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8557 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8558 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8559 | if (evalskip) { |
| 8560 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8561 | evalskip = 0; |
| 8562 | continue; |
| 8563 | } |
| 8564 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8565 | evalskip = 0; |
| 8566 | break; |
| 8567 | } |
| 8568 | } |
| 8569 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8570 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8571 | popstackmark(&smark); |
| 8572 | } |
| 8573 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8574 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8575 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8576 | { |
| 8577 | union node *cp; |
| 8578 | union node *patp; |
| 8579 | struct arglist arglist; |
| 8580 | struct stackmark smark; |
| 8581 | |
| 8582 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8583 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8584 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8585 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8586 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8587 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8588 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8589 | if (casematch(patp, arglist.list->text)) { |
| 8590 | if (evalskip == 0) { |
| 8591 | evaltree(cp->nclist.body, flags); |
| 8592 | } |
| 8593 | goto out; |
| 8594 | } |
| 8595 | } |
| 8596 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8597 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8598 | popstackmark(&smark); |
| 8599 | } |
| 8600 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8601 | /* |
| 8602 | * Kick off a subshell to evaluate a tree. |
| 8603 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8604 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8605 | evalsubshell(union node *n, int flags) |
| 8606 | { |
| 8607 | struct job *jp; |
| 8608 | int backgnd = (n->type == NBACKGND); |
| 8609 | int status; |
| 8610 | |
| 8611 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8612 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8613 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8614 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8615 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8616 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8617 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8618 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8619 | flags |= EV_EXIT; |
| 8620 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8621 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8622 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8623 | redirect(n->nredir.redirect, 0); |
| 8624 | evaltreenr(n->nredir.n, flags); |
| 8625 | /* never returns */ |
| 8626 | } |
| 8627 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8628 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8629 | status = waitforjob(jp); |
| 8630 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8631 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8632 | } |
| 8633 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8634 | /* |
| 8635 | * Compute the names of the files in a redirection list. |
| 8636 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8637 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8638 | static void |
| 8639 | expredir(union node *n) |
| 8640 | { |
| 8641 | union node *redir; |
| 8642 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8643 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8644 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8645 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8646 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8647 | fn.lastp = &fn.list; |
| 8648 | switch (redir->type) { |
| 8649 | case NFROMTO: |
| 8650 | case NFROM: |
| 8651 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8652 | #if ENABLE_ASH_BASH_COMPAT |
| 8653 | case NTO2: |
| 8654 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8655 | case NCLOBBER: |
| 8656 | case NAPPEND: |
| 8657 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denys Vlasenko | f451b2c | 2012-06-09 02:06:57 +0200 | [diff] [blame] | 8658 | TRACE(("expredir expanded to '%s'\n", fn.list->text)); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8659 | #if ENABLE_ASH_BASH_COMPAT |
| 8660 | store_expfname: |
| 8661 | #endif |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 8662 | #if 0 |
| 8663 | // By the design of stack allocator, the loop of this kind: |
| 8664 | // while true; do while true; do break; done </dev/null; done |
| 8665 | // will look like a memory leak: ash plans to free expfname's |
| 8666 | // of "/dev/null" as soon as it finishes running the loop |
| 8667 | // (in this case, never). |
| 8668 | // This "fix" is wrong: |
Jon Tollefson | 4ba6c5d | 2012-11-13 19:26:53 +0100 | [diff] [blame] | 8669 | if (redir->nfile.expfname) |
| 8670 | stunalloc(redir->nfile.expfname); |
Denys Vlasenko | 7c4b13e | 2013-01-17 13:02:27 +0100 | [diff] [blame] | 8671 | // It results in corrupted state of stacked allocations. |
| 8672 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8673 | redir->nfile.expfname = fn.list->text; |
| 8674 | break; |
| 8675 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8676 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8677 | if (redir->ndup.vname) { |
| 8678 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8679 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8680 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8681 | #if ENABLE_ASH_BASH_COMPAT |
| 8682 | //FIXME: we used expandarg with different args! |
| 8683 | if (!isdigit_str9(fn.list->text)) { |
| 8684 | /* >&file, not >&fd */ |
| 8685 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8686 | ash_msg_and_raise_error("redir error"); |
| 8687 | redir->type = NTO2; |
| 8688 | goto store_expfname; |
| 8689 | } |
| 8690 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8691 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8692 | } |
| 8693 | break; |
| 8694 | } |
| 8695 | } |
| 8696 | } |
| 8697 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8698 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8699 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8700 | * of the process creating the pipeline. (This differs from some versions |
| 8701 | * of the shell, which make the last process in a pipeline the parent |
| 8702 | * of all the rest.) |
| 8703 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8704 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8705 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8706 | { |
| 8707 | struct job *jp; |
| 8708 | struct nodelist *lp; |
| 8709 | int pipelen; |
| 8710 | int prevfd; |
| 8711 | int pip[2]; |
| 8712 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8713 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8714 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8715 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8716 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8717 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8718 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8719 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8720 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8721 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8722 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8723 | pip[1] = -1; |
| 8724 | if (lp->next) { |
| 8725 | if (pipe(pip) < 0) { |
| 8726 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8727 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8728 | } |
| 8729 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8730 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8731 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8732 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8733 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8734 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8735 | if (prevfd > 0) { |
| 8736 | dup2(prevfd, 0); |
| 8737 | close(prevfd); |
| 8738 | } |
| 8739 | if (pip[1] > 1) { |
| 8740 | dup2(pip[1], 1); |
| 8741 | close(pip[1]); |
| 8742 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8743 | evaltreenr(lp->n, flags); |
| 8744 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8745 | } |
| 8746 | if (prevfd >= 0) |
| 8747 | close(prevfd); |
| 8748 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8749 | /* Don't want to trigger debugging */ |
| 8750 | if (pip[1] != -1) |
| 8751 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8752 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8753 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8754 | exitstatus = waitforjob(jp); |
| 8755 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8756 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8757 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8758 | } |
| 8759 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8760 | /* |
| 8761 | * Controls whether the shell is interactive or not. |
| 8762 | */ |
| 8763 | static void |
| 8764 | setinteractive(int on) |
| 8765 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8766 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8767 | |
| 8768 | if (++on == is_interactive) |
| 8769 | return; |
| 8770 | is_interactive = on; |
| 8771 | setsignal(SIGINT); |
| 8772 | setsignal(SIGQUIT); |
| 8773 | setsignal(SIGTERM); |
| 8774 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8775 | if (is_interactive > 1) { |
| 8776 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8777 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8778 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8779 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8780 | /* note: ash and hush share this string */ |
| 8781 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8782 | "Enter 'help' for a list of built-in commands." |
| 8783 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8784 | bb_banner, |
| 8785 | "built-in shell (ash)" |
| 8786 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8787 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8788 | } |
| 8789 | } |
| 8790 | #endif |
| 8791 | } |
| 8792 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8793 | static void |
| 8794 | optschanged(void) |
| 8795 | { |
| 8796 | #if DEBUG |
| 8797 | opentrace(); |
| 8798 | #endif |
| 8799 | setinteractive(iflag); |
| 8800 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8801 | #if ENABLE_FEATURE_EDITING_VI |
| 8802 | if (viflag) |
| 8803 | line_input_state->flags |= VI_MODE; |
| 8804 | else |
| 8805 | line_input_state->flags &= ~VI_MODE; |
| 8806 | #else |
| 8807 | viflag = 0; /* forcibly keep the option off */ |
| 8808 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8809 | } |
| 8810 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8811 | static struct localvar *localvars; |
| 8812 | |
| 8813 | /* |
| 8814 | * Called after a function returns. |
| 8815 | * Interrupts must be off. |
| 8816 | */ |
| 8817 | static void |
| 8818 | poplocalvars(void) |
| 8819 | { |
| 8820 | struct localvar *lvp; |
| 8821 | struct var *vp; |
| 8822 | |
| 8823 | while ((lvp = localvars) != NULL) { |
| 8824 | localvars = lvp->next; |
| 8825 | vp = lvp->vp; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 8826 | TRACE(("poplocalvar %s\n", vp ? vp->var_text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8827 | if (vp == NULL) { /* $- saved */ |
| 8828 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8829 | free((char*)lvp->text); |
| 8830 | optschanged(); |
| 8831 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8832 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8833 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8834 | if (vp->var_func) |
| 8835 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8836 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8837 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8838 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8839 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8840 | } |
| 8841 | free(lvp); |
| 8842 | } |
| 8843 | } |
| 8844 | |
| 8845 | static int |
| 8846 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8847 | { |
| 8848 | volatile struct shparam saveparam; |
| 8849 | struct localvar *volatile savelocalvars; |
| 8850 | struct jmploc *volatile savehandler; |
| 8851 | struct jmploc jmploc; |
| 8852 | int e; |
| 8853 | |
| 8854 | saveparam = shellparam; |
| 8855 | savelocalvars = localvars; |
| 8856 | e = setjmp(jmploc.loc); |
| 8857 | if (e) { |
| 8858 | goto funcdone; |
| 8859 | } |
| 8860 | INT_OFF; |
| 8861 | savehandler = exception_handler; |
| 8862 | exception_handler = &jmploc; |
| 8863 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8864 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8865 | func->count++; |
| 8866 | funcnest++; |
| 8867 | INT_ON; |
| 8868 | shellparam.nparam = argc - 1; |
| 8869 | shellparam.p = argv + 1; |
| 8870 | #if ENABLE_ASH_GETOPTS |
| 8871 | shellparam.optind = 1; |
| 8872 | shellparam.optoff = -1; |
| 8873 | #endif |
| 8874 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8875 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8876 | INT_OFF; |
| 8877 | funcnest--; |
| 8878 | freefunc(func); |
| 8879 | poplocalvars(); |
| 8880 | localvars = savelocalvars; |
| 8881 | freeparam(&shellparam); |
| 8882 | shellparam = saveparam; |
| 8883 | exception_handler = savehandler; |
| 8884 | INT_ON; |
| 8885 | evalskip &= ~SKIPFUNC; |
| 8886 | return e; |
| 8887 | } |
| 8888 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8889 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8890 | static char ** |
| 8891 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8892 | { |
| 8893 | char *cp, c; |
| 8894 | |
| 8895 | for (;;) { |
| 8896 | cp = *++argv; |
| 8897 | if (!cp) |
| 8898 | return 0; |
| 8899 | if (*cp++ != '-') |
| 8900 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8901 | c = *cp++; |
| 8902 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8903 | break; |
| 8904 | if (c == '-' && !*cp) { |
| 8905 | argv++; |
| 8906 | break; |
| 8907 | } |
| 8908 | do { |
| 8909 | switch (c) { |
| 8910 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8911 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8912 | break; |
| 8913 | default: |
| 8914 | /* run 'typecmd' for other options */ |
| 8915 | return 0; |
| 8916 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8917 | c = *cp++; |
| 8918 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8919 | } |
| 8920 | return argv; |
| 8921 | } |
| 8922 | #endif |
| 8923 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8924 | /* |
| 8925 | * Make a variable a local variable. When a variable is made local, it's |
| 8926 | * value and flags are saved in a localvar structure. The saved values |
| 8927 | * will be restored when the shell function returns. We handle the name |
| 8928 | * "-" as a special case. |
| 8929 | */ |
| 8930 | static void |
| 8931 | mklocal(char *name) |
| 8932 | { |
| 8933 | struct localvar *lvp; |
| 8934 | struct var **vpp; |
| 8935 | struct var *vp; |
| 8936 | |
| 8937 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8938 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8939 | if (LONE_DASH(name)) { |
| 8940 | char *p; |
| 8941 | p = ckmalloc(sizeof(optlist)); |
| 8942 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8943 | vp = NULL; |
| 8944 | } else { |
| 8945 | char *eq; |
| 8946 | |
| 8947 | vpp = hashvar(name); |
| 8948 | vp = *findvar(vpp, name); |
| 8949 | eq = strchr(name, '='); |
| 8950 | if (vp == NULL) { |
| 8951 | if (eq) |
| 8952 | setvareq(name, VSTRFIXED); |
| 8953 | else |
| 8954 | setvar(name, NULL, VSTRFIXED); |
| 8955 | vp = *vpp; /* the new variable */ |
| 8956 | lvp->flags = VUNSET; |
| 8957 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8958 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8959 | lvp->flags = vp->flags; |
| 8960 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8961 | if (eq) |
| 8962 | setvareq(name, 0); |
| 8963 | } |
| 8964 | } |
| 8965 | lvp->vp = vp; |
| 8966 | lvp->next = localvars; |
| 8967 | localvars = lvp; |
| 8968 | INT_ON; |
| 8969 | } |
| 8970 | |
| 8971 | /* |
| 8972 | * The "local" command. |
| 8973 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8974 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8975 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8976 | { |
| 8977 | char *name; |
| 8978 | |
| 8979 | argv = argptr; |
| 8980 | while ((name = *argv++) != NULL) { |
| 8981 | mklocal(name); |
| 8982 | } |
| 8983 | return 0; |
| 8984 | } |
| 8985 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8986 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8987 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8988 | { |
| 8989 | return 1; |
| 8990 | } |
| 8991 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8992 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8993 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8994 | { |
| 8995 | return 0; |
| 8996 | } |
| 8997 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8998 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8999 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9000 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9001 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9002 | iflag = 0; /* exit on error */ |
| 9003 | mflag = 0; |
| 9004 | optschanged(); |
| 9005 | shellexec(argv + 1, pathval(), 0); |
| 9006 | } |
| 9007 | return 0; |
| 9008 | } |
| 9009 | |
| 9010 | /* |
| 9011 | * The return command. |
| 9012 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9013 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9014 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9015 | { |
| 9016 | /* |
| 9017 | * If called outside a function, do what ksh does; |
| 9018 | * skip the rest of the file. |
| 9019 | */ |
| 9020 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 9021 | return argv[1] ? number(argv[1]) : exitstatus; |
| 9022 | } |
| 9023 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9024 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9025 | static int breakcmd(int, char **) FAST_FUNC; |
| 9026 | static int dotcmd(int, char **) FAST_FUNC; |
| 9027 | static int evalcmd(int, char **) FAST_FUNC; |
| 9028 | static int exitcmd(int, char **) FAST_FUNC; |
| 9029 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9030 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9031 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9032 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 9033 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9034 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 9035 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9036 | #if MAX_HISTORY |
| 9037 | static int historycmd(int, char **) FAST_FUNC; |
| 9038 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9039 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9040 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9041 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9042 | static int readcmd(int, char **) FAST_FUNC; |
| 9043 | static int setcmd(int, char **) FAST_FUNC; |
| 9044 | static int shiftcmd(int, char **) FAST_FUNC; |
| 9045 | static int timescmd(int, char **) FAST_FUNC; |
| 9046 | static int trapcmd(int, char **) FAST_FUNC; |
| 9047 | static int umaskcmd(int, char **) FAST_FUNC; |
| 9048 | static int unsetcmd(int, char **) FAST_FUNC; |
| 9049 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9050 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9051 | #define BUILTIN_NOSPEC "0" |
| 9052 | #define BUILTIN_SPECIAL "1" |
| 9053 | #define BUILTIN_REGULAR "2" |
| 9054 | #define BUILTIN_SPEC_REG "3" |
| 9055 | #define BUILTIN_ASSIGN "4" |
| 9056 | #define BUILTIN_SPEC_ASSG "5" |
| 9057 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9058 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 9059 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9060 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 9061 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9062 | 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] | 9063 | #endif |
| 9064 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9065 | 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] | 9066 | #endif |
| 9067 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9068 | 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] | 9069 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 9070 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9071 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9072 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9073 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 9074 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9075 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9076 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9077 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9078 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9079 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9080 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9081 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9082 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9083 | #endif |
| 9084 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9085 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9086 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9087 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 9088 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 9089 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9090 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9091 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9092 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9093 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9094 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9095 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9096 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9097 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 9098 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 9099 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 9100 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 9101 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9102 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9103 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9104 | #endif |
| 9105 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9106 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9107 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9108 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9109 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9110 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9111 | #endif |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 9112 | #if MAX_HISTORY |
| 9113 | { BUILTIN_NOSPEC "history" , historycmd }, |
| 9114 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9115 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9116 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 9117 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9118 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9119 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9120 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9121 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9122 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9123 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9124 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9125 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9126 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 9127 | { BUILTIN_REGULAR "read" , readcmd }, |
| 9128 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 9129 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 9130 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 9131 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9132 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9133 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9134 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9135 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9136 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9137 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9138 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 9139 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 9140 | { BUILTIN_REGULAR "true" , truecmd }, |
| 9141 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 9142 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 9143 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9144 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9145 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9146 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9147 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 9148 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9149 | }; |
| 9150 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9151 | /* Should match the above table! */ |
| 9152 | #define COMMANDCMD (builtintab + \ |
| 9153 | 2 + \ |
| 9154 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9155 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9156 | 1 * ENABLE_ASH_ALIAS + \ |
| 9157 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9158 | 3) |
| 9159 | #define EXECCMD (builtintab + \ |
| 9160 | 2 + \ |
| 9161 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9162 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9163 | 1 * ENABLE_ASH_ALIAS + \ |
| 9164 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9165 | 3 + \ |
| 9166 | 1 * ENABLE_ASH_CMDCMD + \ |
| 9167 | 1 + \ |
| 9168 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 9169 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9170 | |
| 9171 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9172 | * Search the table of builtin commands. |
| 9173 | */ |
| 9174 | static struct builtincmd * |
| 9175 | find_builtin(const char *name) |
| 9176 | { |
| 9177 | struct builtincmd *bp; |
| 9178 | |
| 9179 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 9180 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9181 | pstrcmp |
| 9182 | ); |
| 9183 | return bp; |
| 9184 | } |
| 9185 | |
| 9186 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9187 | * Execute a simple command. |
| 9188 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9189 | static int |
| 9190 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9191 | { |
| 9192 | const char *q = endofname(p); |
| 9193 | if (p == q) |
| 9194 | return 0; |
| 9195 | return *q == '='; |
| 9196 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9197 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9198 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9199 | { |
| 9200 | /* Preserve exitstatus of a previous possible redirection |
| 9201 | * as POSIX mandates */ |
| 9202 | return back_exitstatus; |
| 9203 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 9204 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9205 | evalcommand(union node *cmd, int flags) |
| 9206 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9207 | static const struct builtincmd null_bltin = { |
| 9208 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9209 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9210 | struct stackmark smark; |
| 9211 | union node *argp; |
| 9212 | struct arglist arglist; |
| 9213 | struct arglist varlist; |
| 9214 | char **argv; |
| 9215 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9216 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9217 | struct cmdentry cmdentry; |
| 9218 | struct job *jp; |
| 9219 | char *lastarg; |
| 9220 | const char *path; |
| 9221 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9222 | int status; |
| 9223 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9224 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9225 | smallint cmd_is_exec; |
| 9226 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9227 | |
| 9228 | /* First expand the arguments. */ |
| 9229 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9230 | setstackmark(&smark); |
| 9231 | back_exitstatus = 0; |
| 9232 | |
| 9233 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9234 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9235 | varlist.lastp = &varlist.list; |
| 9236 | *varlist.lastp = NULL; |
| 9237 | arglist.lastp = &arglist.list; |
| 9238 | *arglist.lastp = NULL; |
| 9239 | |
| 9240 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9241 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9242 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9243 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9244 | } |
| 9245 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9246 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9247 | struct strlist **spp; |
| 9248 | |
| 9249 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9250 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9251 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9252 | else |
| 9253 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9254 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9255 | for (sp = *spp; sp; sp = sp->next) |
| 9256 | argc++; |
| 9257 | } |
| 9258 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9259 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9260 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9261 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9262 | *nargv++ = sp->text; |
| 9263 | } |
| 9264 | *nargv = NULL; |
| 9265 | |
| 9266 | lastarg = NULL; |
| 9267 | if (iflag && funcnest == 0 && argc > 0) |
| 9268 | lastarg = nargv[-1]; |
| 9269 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9270 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9271 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9272 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9273 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9274 | path = vpath.var_text; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9275 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9276 | struct strlist **spp; |
| 9277 | char *p; |
| 9278 | |
| 9279 | spp = varlist.lastp; |
| 9280 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9281 | |
| 9282 | /* |
| 9283 | * Modify the command lookup path, if a PATH= assignment |
| 9284 | * is present |
| 9285 | */ |
| 9286 | p = (*spp)->text; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9287 | if (varcmp(p, path) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9288 | path = p; |
| 9289 | } |
| 9290 | |
| 9291 | /* Print the command if xflag is set. */ |
| 9292 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9293 | int n; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9294 | const char *p = " %s" + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9295 | |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9296 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9297 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9298 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9299 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9300 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9301 | sp = sp->next; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9302 | p = " %s"; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9303 | } |
| 9304 | sp = arglist.list; |
| 9305 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9306 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9307 | } |
| 9308 | |
| 9309 | cmd_is_exec = 0; |
| 9310 | spclbltin = -1; |
| 9311 | |
| 9312 | /* Now locate the command. */ |
| 9313 | if (argc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9314 | int cmd_flag = DO_ERR; |
Denys Vlasenko | 0b4980c | 2012-09-25 12:49:29 +0200 | [diff] [blame] | 9315 | #if ENABLE_ASH_CMDCMD |
| 9316 | const char *oldpath = path + 5; |
| 9317 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9318 | path += 5; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9319 | for (;;) { |
| 9320 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9321 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9322 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9323 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9324 | goto bail; |
| 9325 | } |
| 9326 | |
| 9327 | /* implement bltin and command here */ |
| 9328 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9329 | break; |
| 9330 | if (spclbltin < 0) |
| 9331 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9332 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9333 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9334 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9335 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9336 | path = oldpath; |
| 9337 | nargv = parse_command_args(argv, &path); |
| 9338 | if (!nargv) |
| 9339 | break; |
| 9340 | argc -= nargv - argv; |
| 9341 | argv = nargv; |
| 9342 | cmd_flag |= DO_NOFUNC; |
| 9343 | } else |
| 9344 | #endif |
| 9345 | break; |
| 9346 | } |
| 9347 | } |
| 9348 | |
| 9349 | if (status) { |
| 9350 | /* We have a redirection error. */ |
| 9351 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9352 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9353 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9354 | exitstatus = status; |
| 9355 | goto out; |
| 9356 | } |
| 9357 | |
| 9358 | /* Execute the command. */ |
| 9359 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9360 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9361 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9362 | #if ENABLE_FEATURE_SH_NOFORK |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9363 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 9364 | * around run_nofork_applet() call. |
| 9365 | * (2) Should this check also be done in forkshell()? |
| 9366 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 9367 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9368 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9369 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9370 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9371 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9372 | /* run <applet>_main() */ |
| 9373 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9374 | break; |
| 9375 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9376 | #endif |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9377 | /* Can we avoid forking off? For example, very last command |
| 9378 | * in a script or a subshell does not need forking, |
| 9379 | * we can just exec it. |
| 9380 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9381 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9382 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9383 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9384 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9385 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9386 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9387 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9388 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9389 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9390 | break; |
| 9391 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9392 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9393 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 9394 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9395 | } |
| 9396 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9397 | shellexec(argv, path, cmdentry.u.index); |
| 9398 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9399 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9400 | case CMDBUILTIN: |
| 9401 | cmdenviron = varlist.list; |
| 9402 | if (cmdenviron) { |
| 9403 | struct strlist *list = cmdenviron; |
| 9404 | int i = VNOSET; |
| 9405 | if (spclbltin > 0 || argc == 0) { |
| 9406 | i = 0; |
| 9407 | if (cmd_is_exec && argc > 1) |
| 9408 | i = VEXPORT; |
| 9409 | } |
| 9410 | listsetvar(list, i); |
| 9411 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9412 | /* Tight loop with builtins only: |
| 9413 | * "while kill -0 $child; do true; done" |
| 9414 | * will never exit even if $child died, unless we do this |
| 9415 | * to reap the zombie and make kill detect that it's gone: */ |
| 9416 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9417 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9418 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9419 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9420 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9421 | if (i == EXEXIT) |
| 9422 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9423 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9424 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9425 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9426 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9427 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9428 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9429 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9430 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9431 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9432 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9433 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9434 | } |
| 9435 | break; |
| 9436 | |
| 9437 | case CMDFUNCTION: |
| 9438 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9439 | /* See above for the rationale */ |
| 9440 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9441 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9442 | goto raise; |
| 9443 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9444 | |
| 9445 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9446 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9447 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9448 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9449 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9450 | /* dsl: I think this is intended to be used to support |
| 9451 | * '_' in 'vi' command mode during line editing... |
| 9452 | * However I implemented that within libedit itself. |
| 9453 | */ |
| 9454 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9455 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9456 | popstackmark(&smark); |
| 9457 | } |
| 9458 | |
| 9459 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9460 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9461 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9462 | char *volatile savecmdname; |
| 9463 | struct jmploc *volatile savehandler; |
| 9464 | struct jmploc jmploc; |
| 9465 | int i; |
| 9466 | |
| 9467 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9468 | i = setjmp(jmploc.loc); |
| 9469 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9470 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9471 | savehandler = exception_handler; |
| 9472 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9473 | commandname = argv[0]; |
| 9474 | argptr = argv + 1; |
| 9475 | optptr = NULL; /* initialize nextopt */ |
| 9476 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9477 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9478 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9479 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9480 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9481 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9482 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9483 | |
| 9484 | return i; |
| 9485 | } |
| 9486 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9487 | static int |
| 9488 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9489 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 9490 | return endofname(p)[0] == '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9491 | } |
| 9492 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9493 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9494 | /* |
| 9495 | * Search for a command. This is called before we fork so that the |
| 9496 | * 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] | 9497 | * the child. The check for "goodname" is an overly conservative |
| 9498 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9499 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9500 | static void |
| 9501 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9502 | { |
| 9503 | struct cmdentry entry; |
| 9504 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9505 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9506 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9507 | } |
| 9508 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9509 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9510 | /* ============ Builtin commands |
| 9511 | * |
| 9512 | * Builtin commands whose functions are closely tied to evaluation |
| 9513 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9514 | */ |
| 9515 | |
| 9516 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9517 | * Handle break and continue commands. Break, continue, and return are |
| 9518 | * all handled by setting the evalskip flag. The evaluation routines |
| 9519 | * above all check this flag, and if it is set they start skipping |
| 9520 | * commands rather than executing them. The variable skipcount is |
| 9521 | * the number of loops to break/continue, or the number of function |
| 9522 | * levels to return. (The latter is always 1.) It should probably |
| 9523 | * be an error to break out of more loops than exist, but it isn't |
| 9524 | * in the standard shell so we don't make it one here. |
| 9525 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9526 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9527 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9528 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9529 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9530 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9531 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9532 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9533 | if (n > loopnest) |
| 9534 | n = loopnest; |
| 9535 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9536 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9537 | skipcount = n; |
| 9538 | } |
| 9539 | return 0; |
| 9540 | } |
| 9541 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9542 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9543 | /* ============ input.c |
| 9544 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9545 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9546 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9547 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9548 | enum { |
| 9549 | INPUT_PUSH_FILE = 1, |
| 9550 | INPUT_NOFILE_OK = 2, |
| 9551 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9552 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9553 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9554 | /* values of checkkwd variable */ |
| 9555 | #define CHKALIAS 0x1 |
| 9556 | #define CHKKWD 0x2 |
| 9557 | #define CHKNL 0x4 |
| 9558 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9559 | /* |
| 9560 | * Push a string back onto the input at this current parsefile level. |
| 9561 | * We handle aliases this way. |
| 9562 | */ |
| 9563 | #if !ENABLE_ASH_ALIAS |
| 9564 | #define pushstring(s, ap) pushstring(s) |
| 9565 | #endif |
| 9566 | static void |
| 9567 | pushstring(char *s, struct alias *ap) |
| 9568 | { |
| 9569 | struct strpush *sp; |
| 9570 | int len; |
| 9571 | |
| 9572 | len = strlen(s); |
| 9573 | INT_OFF; |
| 9574 | if (g_parsefile->strpush) { |
| 9575 | sp = ckzalloc(sizeof(*sp)); |
| 9576 | sp->prev = g_parsefile->strpush; |
| 9577 | } else { |
| 9578 | sp = &(g_parsefile->basestrpush); |
| 9579 | } |
| 9580 | g_parsefile->strpush = sp; |
| 9581 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9582 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9583 | #if ENABLE_ASH_ALIAS |
| 9584 | sp->ap = ap; |
| 9585 | if (ap) { |
| 9586 | ap->flag |= ALIASINUSE; |
| 9587 | sp->string = s; |
| 9588 | } |
| 9589 | #endif |
| 9590 | g_parsefile->next_to_pgetc = s; |
| 9591 | g_parsefile->left_in_line = len; |
| 9592 | INT_ON; |
| 9593 | } |
| 9594 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9595 | static void |
| 9596 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9597 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9598 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9599 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9600 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9601 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9602 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9603 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9604 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9605 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9606 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9607 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9608 | if (sp->string != sp->ap->val) { |
| 9609 | free(sp->string); |
| 9610 | } |
| 9611 | sp->ap->flag &= ~ALIASINUSE; |
| 9612 | if (sp->ap->flag & ALIASDEAD) { |
| 9613 | unalias(sp->ap->name); |
| 9614 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9615 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9616 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9617 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9618 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9619 | g_parsefile->strpush = sp->prev; |
| 9620 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9621 | free(sp); |
| 9622 | INT_ON; |
| 9623 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9624 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9625 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9626 | //it peeks whether it is &>, and then pushes back both chars. |
| 9627 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9628 | //to make two pungetc() reliable. Currently, |
| 9629 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9630 | static int |
| 9631 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9632 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9633 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9634 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9635 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9636 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9637 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9638 | retry: |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9639 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 9640 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1, /*loop_on_EINTR:*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9641 | else { |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9642 | int timeout = -1; |
| 9643 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 9644 | if (iflag) { |
| 9645 | const char *tmout_var = lookupvar("TMOUT"); |
| 9646 | if (tmout_var) { |
| 9647 | timeout = atoi(tmout_var) * 1000; |
| 9648 | if (timeout <= 0) |
| 9649 | timeout = -1; |
| 9650 | } |
| 9651 | } |
| 9652 | # endif |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 9653 | # if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9654 | line_input_state->path_lookup = pathval(); |
Denys Vlasenko | 8c52f80 | 2011-02-04 17:36:21 +0100 | [diff] [blame] | 9655 | # endif |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 9656 | /* Unicode support should be activated even if LANG is set |
| 9657 | * _during_ shell execution, not only if it was set when |
| 9658 | * shell was started. Therefore, re-check LANG every time: |
| 9659 | */ |
Denys Vlasenko | 3e7ecb1 | 2013-07-02 17:30:23 +0200 | [diff] [blame] | 9660 | { |
| 9661 | const char *s = lookupvar("LC_ALL"); |
| 9662 | if (!s) s = lookupvar("LANG"); |
| 9663 | reinit_unicode(s); |
| 9664 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9665 | nr = read_line_input(line_input_state, cmdedit_prompt, buf, IBUFSIZ, timeout); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9666 | if (nr == 0) { |
| 9667 | /* Ctrl+C pressed */ |
| 9668 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9669 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9670 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9671 | raise(SIGINT); |
| 9672 | return 1; |
| 9673 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9674 | goto retry; |
| 9675 | } |
Denys Vlasenko | 66c5b12 | 2011-02-08 05:07:02 +0100 | [diff] [blame] | 9676 | if (nr < 0) { |
| 9677 | if (errno == 0) { |
| 9678 | /* Ctrl+D pressed */ |
| 9679 | nr = 0; |
| 9680 | } |
| 9681 | # if ENABLE_ASH_IDLE_TIMEOUT |
| 9682 | else if (errno == EAGAIN && timeout > 0) { |
| 9683 | printf("\007timed out waiting for input: auto-logout\n"); |
| 9684 | exitshell(); |
| 9685 | } |
| 9686 | # endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9687 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9688 | } |
| 9689 | #else |
Denys Vlasenko | 80542ba | 2011-05-08 21:23:43 +0200 | [diff] [blame] | 9690 | nr = nonblock_immune_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1, /*loop_on_EINTR:*/ 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9691 | #endif |
| 9692 | |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 9693 | #if 0 /* disabled: nonblock_immune_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9694 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9695 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9696 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9697 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9698 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9699 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9700 | out2str("sh: turning off NDELAY mode\n"); |
| 9701 | goto retry; |
| 9702 | } |
| 9703 | } |
| 9704 | } |
| 9705 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9706 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9707 | return nr; |
| 9708 | } |
| 9709 | |
| 9710 | /* |
| 9711 | * Refill the input buffer and return the next input character: |
| 9712 | * |
| 9713 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9714 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9715 | * or we are reading from a string so we can't refill the buffer, |
| 9716 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9717 | * 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] | 9718 | * 4) Process input up to the next newline, deleting nul characters. |
| 9719 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9720 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9721 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9722 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9723 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9724 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9725 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9726 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9727 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9728 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9729 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9730 | if (g_parsefile->left_in_line == -1 |
| 9731 | && g_parsefile->strpush->ap |
| 9732 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9733 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9734 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9735 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9736 | return PEOA; |
| 9737 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9738 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9739 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9740 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9741 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9742 | g_parsefile->left_in_line, |
| 9743 | g_parsefile->next_to_pgetc, |
| 9744 | g_parsefile->next_to_pgetc); |
| 9745 | if (--g_parsefile->left_in_line >= 0) |
| 9746 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9747 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9748 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9749 | * "pgetc" needs refilling. |
| 9750 | */ |
| 9751 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9752 | /* -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] | 9753 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9754 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9755 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9756 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9757 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9758 | /* even in failure keep left_in_line and next_to_pgetc |
| 9759 | * in lock step, for correct multi-layer pungetc. |
| 9760 | * left_in_line was decremented before preadbuffer(), |
| 9761 | * must inc next_to_pgetc: */ |
| 9762 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9763 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9764 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9765 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9766 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9767 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9768 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9769 | again: |
| 9770 | more = preadfd(); |
| 9771 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9772 | /* don't try reading again */ |
| 9773 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9774 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9775 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9776 | return PEOF; |
| 9777 | } |
| 9778 | } |
| 9779 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9780 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9781 | * Set g_parsefile->left_in_line |
| 9782 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9783 | * NUL chars are deleted. |
| 9784 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9785 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9786 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9787 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9788 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9789 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9790 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9791 | c = *q; |
| 9792 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9793 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9794 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9795 | q++; |
| 9796 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9797 | 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] | 9798 | break; |
| 9799 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9800 | } |
| 9801 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9802 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9803 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9804 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9805 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9806 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9807 | } |
| 9808 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9809 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9810 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9811 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9812 | char save = *q; |
| 9813 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9814 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9815 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9816 | } |
| 9817 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9818 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9819 | g_parsefile->left_in_line, |
| 9820 | g_parsefile->next_to_pgetc, |
| 9821 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9822 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9825 | #define pgetc_as_macro() \ |
| 9826 | (--g_parsefile->left_in_line >= 0 \ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9827 | ? (unsigned char)*g_parsefile->next_to_pgetc++ \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9828 | : preadbuffer() \ |
| 9829 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9830 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9831 | static int |
| 9832 | pgetc(void) |
| 9833 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9834 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9835 | g_parsefile->left_in_line, |
| 9836 | g_parsefile->next_to_pgetc, |
| 9837 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9838 | return pgetc_as_macro(); |
| 9839 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9840 | |
| 9841 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9842 | # define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9843 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9844 | # define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9845 | #endif |
| 9846 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9847 | #if ENABLE_ASH_ALIAS |
| 9848 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9849 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9850 | { |
| 9851 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9852 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9853 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9854 | g_parsefile->left_in_line, |
| 9855 | g_parsefile->next_to_pgetc, |
| 9856 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9857 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9858 | } while (c == PEOA); |
| 9859 | return c; |
| 9860 | } |
| 9861 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9862 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9863 | #endif |
| 9864 | |
| 9865 | /* |
| 9866 | * Read a line from the script. |
| 9867 | */ |
| 9868 | static char * |
| 9869 | pfgets(char *line, int len) |
| 9870 | { |
| 9871 | char *p = line; |
| 9872 | int nleft = len; |
| 9873 | int c; |
| 9874 | |
| 9875 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9876 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9877 | if (c == PEOF) { |
| 9878 | if (p == line) |
| 9879 | return NULL; |
| 9880 | break; |
| 9881 | } |
| 9882 | *p++ = c; |
| 9883 | if (c == '\n') |
| 9884 | break; |
| 9885 | } |
| 9886 | *p = '\0'; |
| 9887 | return line; |
| 9888 | } |
| 9889 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9890 | /* |
| 9891 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9892 | * PEOF may be pushed back. |
| 9893 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9894 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9895 | pungetc(void) |
| 9896 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9897 | g_parsefile->left_in_line++; |
| 9898 | g_parsefile->next_to_pgetc--; |
| 9899 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9900 | g_parsefile->left_in_line, |
| 9901 | g_parsefile->next_to_pgetc, |
| 9902 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9903 | } |
| 9904 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9905 | /* |
| 9906 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9907 | * adds a new entry to the stack and popfile restores the previous level. |
| 9908 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9909 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9910 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9911 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9912 | struct parsefile *pf; |
| 9913 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9914 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9915 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9916 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9917 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9918 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9919 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9920 | } |
| 9921 | |
| 9922 | static void |
| 9923 | popfile(void) |
| 9924 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9925 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9926 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9927 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9928 | if (pf->pf_fd >= 0) |
| 9929 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9930 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9931 | while (pf->strpush) |
| 9932 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9933 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9934 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9935 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9936 | } |
| 9937 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9938 | /* |
| 9939 | * Return to top level. |
| 9940 | */ |
| 9941 | static void |
| 9942 | popallfiles(void) |
| 9943 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9944 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9945 | popfile(); |
| 9946 | } |
| 9947 | |
| 9948 | /* |
| 9949 | * Close the file(s) that the shell is reading commands from. Called |
| 9950 | * after a fork is done. |
| 9951 | */ |
| 9952 | static void |
| 9953 | closescript(void) |
| 9954 | { |
| 9955 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9956 | if (g_parsefile->pf_fd > 0) { |
| 9957 | close(g_parsefile->pf_fd); |
| 9958 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9959 | } |
| 9960 | } |
| 9961 | |
| 9962 | /* |
| 9963 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9964 | * interrupts off. |
| 9965 | */ |
| 9966 | static void |
| 9967 | setinputfd(int fd, int push) |
| 9968 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9969 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9970 | if (push) { |
| 9971 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9972 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9973 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9974 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9975 | if (g_parsefile->buf == NULL) |
| 9976 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9977 | g_parsefile->left_in_buffer = 0; |
| 9978 | g_parsefile->left_in_line = 0; |
| 9979 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9980 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9981 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9982 | /* |
| 9983 | * Set the input to take input from a file. If push is set, push the |
| 9984 | * old input onto the stack first. |
| 9985 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9986 | static int |
| 9987 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9988 | { |
| 9989 | int fd; |
| 9990 | int fd2; |
| 9991 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9992 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9993 | fd = open(fname, O_RDONLY); |
| 9994 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9995 | if (flags & INPUT_NOFILE_OK) |
| 9996 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9997 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9998 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9999 | if (fd < 10) { |
| 10000 | fd2 = copyfd(fd, 10); |
| 10001 | close(fd); |
| 10002 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10003 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10004 | fd = fd2; |
| 10005 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10006 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10007 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10008 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 10009 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10010 | } |
| 10011 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10012 | /* |
| 10013 | * Like setinputfile, but takes input from a string. |
| 10014 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10015 | static void |
| 10016 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10017 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10018 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10019 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10020 | g_parsefile->next_to_pgetc = string; |
| 10021 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10022 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 10023 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10024 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10025 | } |
| 10026 | |
| 10027 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10028 | /* ============ mail.c |
| 10029 | * |
| 10030 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10031 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10032 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10033 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10034 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10035 | #define MAXMBOXES 10 |
| 10036 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10037 | /* times of mailboxes */ |
| 10038 | static time_t mailtime[MAXMBOXES]; |
| 10039 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10040 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10041 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10042 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10043 | * Print appropriate message(s) if mail has arrived. |
| 10044 | * If mail_var_path_changed is set, |
| 10045 | * then the value of MAIL has mail_var_path_changed, |
| 10046 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10047 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10048 | static void |
| 10049 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10050 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10051 | const char *mpath; |
| 10052 | char *p; |
| 10053 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10054 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10055 | struct stackmark smark; |
| 10056 | struct stat statb; |
| 10057 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10058 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10059 | mpath = mpathset() ? mpathval() : mailval(); |
| 10060 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 10061 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10062 | if (p == NULL) |
| 10063 | break; |
| 10064 | if (*p == '\0') |
| 10065 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10066 | for (q = p; *q; q++) |
| 10067 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 10068 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10069 | if (q[-1] != '/') |
| 10070 | abort(); |
| 10071 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10072 | q[-1] = '\0'; /* delete trailing '/' */ |
| 10073 | if (stat(p, &statb) < 0) { |
| 10074 | *mtp = 0; |
| 10075 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10076 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10077 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 10078 | fprintf( |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 10079 | stderr, "%s\n", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10080 | pathopt ? pathopt : "you have mail" |
| 10081 | ); |
| 10082 | } |
| 10083 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10084 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10085 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10086 | popstackmark(&smark); |
| 10087 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10088 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10089 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10090 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10091 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10092 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10093 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10094 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10095 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10096 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10097 | |
| 10098 | /* ============ ??? */ |
| 10099 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10100 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10101 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10102 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10103 | static void |
| 10104 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10105 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10106 | char **newparam; |
| 10107 | char **ap; |
| 10108 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10109 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10110 | for (nparam = 0; argv[nparam]; nparam++) |
| 10111 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10112 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 10113 | while (*argv) { |
| 10114 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10115 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10116 | *ap = NULL; |
| 10117 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10118 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10119 | shellparam.nparam = nparam; |
| 10120 | shellparam.p = newparam; |
| 10121 | #if ENABLE_ASH_GETOPTS |
| 10122 | shellparam.optind = 1; |
| 10123 | shellparam.optoff = -1; |
| 10124 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10125 | } |
| 10126 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10127 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10128 | * Process shell options. The global variable argptr contains a pointer |
| 10129 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10130 | * |
| 10131 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 10132 | * For a non-interactive shell, an error condition encountered |
| 10133 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 10134 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 10135 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10136 | * ... |
| 10137 | * Utility syntax error (option or operand error) Shall exit |
| 10138 | * ... |
| 10139 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 10140 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 10141 | * and shell continues), and people rely on this behavior! |
| 10142 | * Testcase: |
| 10143 | * set -o barfoo 2>/dev/null |
| 10144 | * echo $? |
| 10145 | * |
| 10146 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10147 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10148 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10149 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10150 | { |
| 10151 | int i; |
| 10152 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10153 | if (name) { |
| 10154 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10155 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10156 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10157 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10158 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10159 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10160 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10161 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10162 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10163 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10164 | if (val) { |
| 10165 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 10166 | } else { |
| 10167 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 10168 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10169 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10170 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10171 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10172 | static void |
| 10173 | setoption(int flag, int val) |
| 10174 | { |
| 10175 | int i; |
| 10176 | |
| 10177 | for (i = 0; i < NOPTS; i++) { |
| 10178 | if (optletters(i) == flag) { |
| 10179 | optlist[i] = val; |
| 10180 | return; |
| 10181 | } |
| 10182 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10183 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10184 | /* NOTREACHED */ |
| 10185 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10186 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10187 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10188 | { |
| 10189 | char *p; |
| 10190 | int val; |
| 10191 | int c; |
| 10192 | |
| 10193 | if (cmdline) |
| 10194 | minusc = NULL; |
| 10195 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10196 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10197 | if (c != '-' && c != '+') |
| 10198 | break; |
| 10199 | argptr++; |
| 10200 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10201 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10202 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10203 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10204 | if (!cmdline) { |
| 10205 | /* "-" means turn off -x and -v */ |
| 10206 | if (p[0] == '\0') |
| 10207 | xflag = vflag = 0; |
| 10208 | /* "--" means reset params */ |
| 10209 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10210 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10211 | } |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 10212 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10213 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10214 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10215 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10216 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10217 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10218 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10219 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10220 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10221 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10222 | /* it already printed err message */ |
| 10223 | return 1; /* error */ |
| 10224 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10225 | if (*argptr) |
| 10226 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10227 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10228 | isloginsh = 1; |
| 10229 | /* bash does not accept +-login, we also won't */ |
| 10230 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10231 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10232 | isloginsh = 1; |
| 10233 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10234 | } else { |
| 10235 | setoption(c, val); |
| 10236 | } |
| 10237 | } |
| 10238 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10239 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10240 | } |
| 10241 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10242 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10243 | * The shift builtin command. |
| 10244 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10245 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10246 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10247 | { |
| 10248 | int n; |
| 10249 | char **ap1, **ap2; |
| 10250 | |
| 10251 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10252 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10253 | n = number(argv[1]); |
| 10254 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10255 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10256 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10257 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10258 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10259 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10260 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10261 | } |
| 10262 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10263 | while ((*ap2++ = *ap1++) != NULL) |
| 10264 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10265 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10266 | shellparam.optind = 1; |
| 10267 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10268 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10269 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10270 | return 0; |
| 10271 | } |
| 10272 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10273 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10274 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10275 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10276 | * Maybe we could keep them in an ordered balanced binary tree |
| 10277 | * instead of hashed lists. |
| 10278 | * For now just roll 'em through qsort for printing... |
| 10279 | */ |
| 10280 | static int |
| 10281 | showvars(const char *sep_prefix, int on, int off) |
| 10282 | { |
| 10283 | const char *sep; |
| 10284 | char **ep, **epend; |
| 10285 | |
| 10286 | ep = listvars(on, off, &epend); |
| 10287 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10288 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10289 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10290 | |
| 10291 | for (; ep < epend; ep++) { |
| 10292 | const char *p; |
| 10293 | const char *q; |
| 10294 | |
| 10295 | p = strchrnul(*ep, '='); |
| 10296 | q = nullstr; |
| 10297 | if (*p) |
| 10298 | q = single_quote(++p); |
| 10299 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10300 | } |
| 10301 | return 0; |
| 10302 | } |
| 10303 | |
| 10304 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10305 | * The set command builtin. |
| 10306 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10307 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10308 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10309 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10310 | int retval; |
| 10311 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10312 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10313 | return showvars(nullstr, 0, VUNSET); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 10314 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10315 | INT_OFF; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 10316 | retval = options(/*cmdline:*/ 0); |
| 10317 | if (retval == 0) { /* if no parse error... */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10318 | optschanged(); |
| 10319 | if (*argptr != NULL) { |
| 10320 | setparam(argptr); |
| 10321 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10322 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10323 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10324 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10325 | } |
| 10326 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10327 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10328 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10329 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10330 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10331 | uint32_t t; |
| 10332 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10333 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10334 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10335 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10336 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 10337 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10338 | vrandom.flags &= ~VNOFUNC; |
| 10339 | } else { |
| 10340 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10341 | t = strtoul(value, NULL, 10); |
| 10342 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10343 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10344 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10345 | #endif |
| 10346 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10347 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10348 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10349 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10350 | { |
| 10351 | char *p, *q; |
| 10352 | char c = '?'; |
| 10353 | int done = 0; |
| 10354 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10355 | char s[12]; |
| 10356 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10357 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10358 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10359 | return 1; |
| 10360 | optnext = optfirst + *param_optind - 1; |
| 10361 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10362 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10363 | p = NULL; |
| 10364 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10365 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10366 | if (p == NULL || *p == '\0') { |
| 10367 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10368 | p = *optnext; |
| 10369 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10370 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10371 | p = NULL; |
| 10372 | done = 1; |
| 10373 | goto out; |
| 10374 | } |
| 10375 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10376 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10377 | goto atend; |
| 10378 | } |
| 10379 | |
| 10380 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10381 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10382 | if (*q == '\0') { |
| 10383 | if (optstr[0] == ':') { |
| 10384 | s[0] = c; |
| 10385 | s[1] = '\0'; |
| 10386 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10387 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10388 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10389 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10390 | } |
| 10391 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10392 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10393 | } |
| 10394 | if (*++q == ':') |
| 10395 | q++; |
| 10396 | } |
| 10397 | |
| 10398 | if (*++q == ':') { |
| 10399 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10400 | if (optstr[0] == ':') { |
| 10401 | s[0] = c; |
| 10402 | s[1] = '\0'; |
| 10403 | err |= setvarsafe("OPTARG", s, 0); |
| 10404 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10405 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10406 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10407 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10408 | c = '?'; |
| 10409 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10410 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10411 | } |
| 10412 | |
| 10413 | if (p == *optnext) |
| 10414 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10415 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10416 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10417 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10418 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10419 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10420 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10421 | *param_optind = optnext - optfirst + 1; |
| 10422 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10423 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10424 | s[0] = c; |
| 10425 | s[1] = '\0'; |
| 10426 | err |= setvarsafe(optvar, s, 0); |
| 10427 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10428 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10429 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10430 | flush_stdout_stderr(); |
| 10431 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10432 | } |
| 10433 | return done; |
| 10434 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10435 | |
| 10436 | /* |
| 10437 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10438 | * to be processed. Shellparam.optptr points to the next character to |
| 10439 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10440 | * then it's the first time getopts has been called. |
| 10441 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10442 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10443 | getoptscmd(int argc, char **argv) |
| 10444 | { |
| 10445 | char **optbase; |
| 10446 | |
| 10447 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10448 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10449 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10450 | optbase = shellparam.p; |
| 10451 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10452 | shellparam.optind = 1; |
| 10453 | shellparam.optoff = -1; |
| 10454 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10455 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10456 | optbase = &argv[3]; |
| 10457 | if (shellparam.optind > argc - 2) { |
| 10458 | shellparam.optind = 1; |
| 10459 | shellparam.optoff = -1; |
| 10460 | } |
| 10461 | } |
| 10462 | |
| 10463 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10464 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10465 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10466 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10467 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10468 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10469 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10470 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10471 | struct heredoc { |
| 10472 | struct heredoc *next; /* next here document in list */ |
| 10473 | union node *here; /* redirection node */ |
| 10474 | char *eofmark; /* string indicating end of input */ |
| 10475 | smallint striptabs; /* if set, strip leading tabs */ |
| 10476 | }; |
| 10477 | |
| 10478 | static smallint tokpushback; /* last token pushed back */ |
| 10479 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10480 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10481 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10482 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10483 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10484 | static struct nodelist *backquotelist; |
| 10485 | static union node *redirnode; |
| 10486 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10487 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10488 | static const char * |
| 10489 | tokname(char *buf, int tok) |
| 10490 | { |
| 10491 | if (tok < TSEMI) |
| 10492 | return tokname_array[tok] + 1; |
| 10493 | sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 10494 | return buf; |
| 10495 | } |
| 10496 | |
| 10497 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10498 | * Called when an unexpected token is read during the parse. The argument |
| 10499 | * is the token that is expected, or -1 if more than one type of token can |
| 10500 | * occur at this point. |
| 10501 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10502 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10503 | static void |
| 10504 | raise_error_unexpected_syntax(int token) |
| 10505 | { |
| 10506 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10507 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10508 | int l; |
| 10509 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10510 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10511 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10512 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10513 | raise_error_syntax(msg); |
| 10514 | /* NOTREACHED */ |
| 10515 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10516 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10517 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10518 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10519 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10520 | static union node *andor(void); |
| 10521 | static union node *pipeline(void); |
| 10522 | static union node *parse_command(void); |
| 10523 | static void parseheredoc(void); |
| 10524 | static char peektoken(void); |
| 10525 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10526 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10527 | static union node * |
| 10528 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10529 | { |
| 10530 | union node *n1, *n2, *n3; |
| 10531 | int tok; |
| 10532 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10533 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10534 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10535 | return NULL; |
| 10536 | n1 = NULL; |
| 10537 | for (;;) { |
| 10538 | n2 = andor(); |
| 10539 | tok = readtoken(); |
| 10540 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10541 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10542 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10543 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10544 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10545 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10546 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10547 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10548 | n2 = n3; |
| 10549 | } |
| 10550 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10551 | } |
| 10552 | } |
| 10553 | if (n1 == NULL) { |
| 10554 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10555 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10556 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10557 | n3->type = NSEMI; |
| 10558 | n3->nbinary.ch1 = n1; |
| 10559 | n3->nbinary.ch2 = n2; |
| 10560 | n1 = n3; |
| 10561 | } |
| 10562 | switch (tok) { |
| 10563 | case TBACKGND: |
| 10564 | case TSEMI: |
| 10565 | tok = readtoken(); |
| 10566 | /* fall through */ |
| 10567 | case TNL: |
| 10568 | if (tok == TNL) { |
| 10569 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10570 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10571 | return n1; |
| 10572 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10573 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10574 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10575 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10576 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10577 | return n1; |
| 10578 | break; |
| 10579 | case TEOF: |
| 10580 | if (heredoclist) |
| 10581 | parseheredoc(); |
| 10582 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10583 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10584 | return n1; |
| 10585 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10586 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10587 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10588 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10589 | return n1; |
| 10590 | } |
| 10591 | } |
| 10592 | } |
| 10593 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10594 | static union node * |
| 10595 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10596 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10597 | union node *n1, *n2, *n3; |
| 10598 | int t; |
| 10599 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10600 | n1 = pipeline(); |
| 10601 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10602 | t = readtoken(); |
| 10603 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10604 | t = NAND; |
| 10605 | } else if (t == TOR) { |
| 10606 | t = NOR; |
| 10607 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10608 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10609 | return n1; |
| 10610 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10611 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10612 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10613 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10614 | n3->type = t; |
| 10615 | n3->nbinary.ch1 = n1; |
| 10616 | n3->nbinary.ch2 = n2; |
| 10617 | n1 = n3; |
| 10618 | } |
| 10619 | } |
| 10620 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10621 | static union node * |
| 10622 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10623 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10624 | union node *n1, *n2, *pipenode; |
| 10625 | struct nodelist *lp, *prev; |
| 10626 | int negate; |
| 10627 | |
| 10628 | negate = 0; |
| 10629 | TRACE(("pipeline: entered\n")); |
| 10630 | if (readtoken() == TNOT) { |
| 10631 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10632 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10633 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10634 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10635 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10636 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10637 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10638 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10639 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10640 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10641 | pipenode->npipe.cmdlist = lp; |
| 10642 | lp->n = n1; |
| 10643 | do { |
| 10644 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10645 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10646 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10647 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10648 | prev->next = lp; |
| 10649 | } while (readtoken() == TPIPE); |
| 10650 | lp->next = NULL; |
| 10651 | n1 = pipenode; |
| 10652 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10653 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10654 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10655 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10656 | n2->type = NNOT; |
| 10657 | n2->nnot.com = n1; |
| 10658 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10659 | } |
| 10660 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10661 | } |
| 10662 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10663 | static union node * |
| 10664 | makename(void) |
| 10665 | { |
| 10666 | union node *n; |
| 10667 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10668 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10669 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10670 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10671 | n->narg.text = wordtext; |
| 10672 | n->narg.backquote = backquotelist; |
| 10673 | return n; |
| 10674 | } |
| 10675 | |
| 10676 | static void |
| 10677 | fixredir(union node *n, const char *text, int err) |
| 10678 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10679 | int fd; |
| 10680 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10681 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10682 | if (!err) |
| 10683 | n->ndup.vname = NULL; |
| 10684 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10685 | fd = bb_strtou(text, NULL, 10); |
| 10686 | if (!errno && fd >= 0) |
| 10687 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10688 | else if (LONE_DASH(text)) |
| 10689 | n->ndup.dupfd = -1; |
| 10690 | else { |
| 10691 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10692 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10693 | n->ndup.vname = makename(); |
| 10694 | } |
| 10695 | } |
| 10696 | |
| 10697 | /* |
| 10698 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10699 | * or backquotes). |
| 10700 | */ |
| 10701 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10702 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10703 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10704 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10705 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10706 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10707 | if (c == CTLQUOTEMARK) |
| 10708 | continue; |
| 10709 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10710 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10711 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10712 | return 0; |
| 10713 | } |
| 10714 | return 1; |
| 10715 | } |
| 10716 | |
| 10717 | static void |
| 10718 | parsefname(void) |
| 10719 | { |
| 10720 | union node *n = redirnode; |
| 10721 | |
| 10722 | if (readtoken() != TWORD) |
| 10723 | raise_error_unexpected_syntax(-1); |
| 10724 | if (n->type == NHERE) { |
| 10725 | struct heredoc *here = heredoc; |
| 10726 | struct heredoc *p; |
| 10727 | int i; |
| 10728 | |
| 10729 | if (quoteflag == 0) |
| 10730 | n->type = NXHERE; |
| 10731 | TRACE(("Here document %d\n", n->type)); |
| 10732 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10733 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10734 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10735 | here->eofmark = wordtext; |
| 10736 | here->next = NULL; |
| 10737 | if (heredoclist == NULL) |
| 10738 | heredoclist = here; |
| 10739 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10740 | for (p = heredoclist; p->next; p = p->next) |
| 10741 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10742 | p->next = here; |
| 10743 | } |
| 10744 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10745 | fixredir(n, wordtext, 0); |
| 10746 | } else { |
| 10747 | n->nfile.fname = makename(); |
| 10748 | } |
| 10749 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10750 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10751 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10752 | simplecmd(void) |
| 10753 | { |
| 10754 | union node *args, **app; |
| 10755 | union node *n = NULL; |
| 10756 | union node *vars, **vpp; |
| 10757 | union node **rpp, *redir; |
| 10758 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10759 | #if ENABLE_ASH_BASH_COMPAT |
| 10760 | smallint double_brackets_flag = 0; |
| 10761 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10762 | |
| 10763 | args = NULL; |
| 10764 | app = &args; |
| 10765 | vars = NULL; |
| 10766 | vpp = &vars; |
| 10767 | redir = NULL; |
| 10768 | rpp = &redir; |
| 10769 | |
| 10770 | savecheckkwd = CHKALIAS; |
| 10771 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10772 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10773 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10774 | t = readtoken(); |
| 10775 | switch (t) { |
| 10776 | #if ENABLE_ASH_BASH_COMPAT |
| 10777 | case TAND: /* "&&" */ |
| 10778 | case TOR: /* "||" */ |
| 10779 | if (!double_brackets_flag) { |
| 10780 | tokpushback = 1; |
| 10781 | goto out; |
| 10782 | } |
| 10783 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10784 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10785 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10786 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10787 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10788 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10789 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10790 | #if ENABLE_ASH_BASH_COMPAT |
| 10791 | if (strcmp("[[", wordtext) == 0) |
| 10792 | double_brackets_flag = 1; |
| 10793 | else if (strcmp("]]", wordtext) == 0) |
| 10794 | double_brackets_flag = 0; |
| 10795 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10796 | n->narg.backquote = backquotelist; |
| 10797 | if (savecheckkwd && isassignment(wordtext)) { |
| 10798 | *vpp = n; |
| 10799 | vpp = &n->narg.next; |
| 10800 | } else { |
| 10801 | *app = n; |
| 10802 | app = &n->narg.next; |
| 10803 | savecheckkwd = 0; |
| 10804 | } |
| 10805 | break; |
| 10806 | case TREDIR: |
| 10807 | *rpp = n = redirnode; |
| 10808 | rpp = &n->nfile.next; |
| 10809 | parsefname(); /* read name of redirection file */ |
| 10810 | break; |
| 10811 | case TLP: |
| 10812 | if (args && app == &args->narg.next |
| 10813 | && !vars && !redir |
| 10814 | ) { |
| 10815 | struct builtincmd *bcmd; |
| 10816 | const char *name; |
| 10817 | |
| 10818 | /* We have a function */ |
| 10819 | if (readtoken() != TRP) |
| 10820 | raise_error_unexpected_syntax(TRP); |
| 10821 | name = n->narg.text; |
| 10822 | if (!goodname(name) |
| 10823 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10824 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10825 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10826 | } |
| 10827 | n->type = NDEFUN; |
| 10828 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10829 | n->narg.next = parse_command(); |
| 10830 | return n; |
| 10831 | } |
| 10832 | /* fall through */ |
| 10833 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10834 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10835 | goto out; |
| 10836 | } |
| 10837 | } |
| 10838 | out: |
| 10839 | *app = NULL; |
| 10840 | *vpp = NULL; |
| 10841 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10842 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10843 | n->type = NCMD; |
| 10844 | n->ncmd.args = args; |
| 10845 | n->ncmd.assign = vars; |
| 10846 | n->ncmd.redirect = redir; |
| 10847 | return n; |
| 10848 | } |
| 10849 | |
| 10850 | static union node * |
| 10851 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10852 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10853 | union node *n1, *n2; |
| 10854 | union node *ap, **app; |
| 10855 | union node *cp, **cpp; |
| 10856 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10857 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10858 | int t; |
| 10859 | |
| 10860 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10861 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10862 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10863 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10864 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10865 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10866 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10867 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10868 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10869 | n1->type = NIF; |
| 10870 | n1->nif.test = list(0); |
| 10871 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10872 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10873 | n1->nif.ifpart = list(0); |
| 10874 | n2 = n1; |
| 10875 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10876 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10877 | n2 = n2->nif.elsepart; |
| 10878 | n2->type = NIF; |
| 10879 | n2->nif.test = list(0); |
| 10880 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10881 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10882 | n2->nif.ifpart = list(0); |
| 10883 | } |
| 10884 | if (lasttoken == TELSE) |
| 10885 | n2->nif.elsepart = list(0); |
| 10886 | else { |
| 10887 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10888 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10889 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10890 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10891 | break; |
| 10892 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10893 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10894 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10895 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10896 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10897 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10898 | got = readtoken(); |
| 10899 | if (got != TDO) { |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10900 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got] + 1, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10901 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10902 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10903 | } |
| 10904 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10905 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10906 | break; |
| 10907 | } |
| 10908 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10909 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10910 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10911 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10912 | n1->type = NFOR; |
| 10913 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10914 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10915 | if (readtoken() == TIN) { |
| 10916 | app = ≈ |
| 10917 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10918 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10919 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10920 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10921 | n2->narg.text = wordtext; |
| 10922 | n2->narg.backquote = backquotelist; |
| 10923 | *app = n2; |
| 10924 | app = &n2->narg.next; |
| 10925 | } |
| 10926 | *app = NULL; |
| 10927 | n1->nfor.args = ap; |
| 10928 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10929 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10930 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10931 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10932 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10933 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10934 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10935 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10936 | n1->nfor.args = n2; |
| 10937 | /* |
| 10938 | * Newline or semicolon here is optional (but note |
| 10939 | * that the original Bourne shell only allowed NL). |
| 10940 | */ |
| 10941 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10942 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10943 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10944 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10945 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10946 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10947 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10948 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10949 | break; |
| 10950 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10951 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10952 | n1->type = NCASE; |
| 10953 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10954 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10955 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10956 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10957 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10958 | n2->narg.text = wordtext; |
| 10959 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10960 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10961 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10962 | } while (readtoken() == TNL); |
| 10963 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10964 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10965 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10966 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10967 | checkkwd = CHKNL | CHKKWD; |
| 10968 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10969 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10970 | if (lasttoken == TLP) |
| 10971 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10972 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10973 | cp->type = NCLIST; |
| 10974 | app = &cp->nclist.pattern; |
| 10975 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10976 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10977 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10978 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10979 | ap->narg.text = wordtext; |
| 10980 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10981 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10982 | break; |
| 10983 | app = &ap->narg.next; |
| 10984 | readtoken(); |
| 10985 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10986 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10987 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10988 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10989 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10990 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10991 | cpp = &cp->nclist.next; |
| 10992 | |
| 10993 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10994 | t = readtoken(); |
| 10995 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10996 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10997 | raise_error_unexpected_syntax(TENDCASE); |
| 10998 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10999 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11000 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11001 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11002 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11003 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11004 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11005 | n1->type = NSUBSHELL; |
| 11006 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11007 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11008 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11009 | break; |
| 11010 | case TBEGIN: |
| 11011 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11012 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11013 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11014 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11015 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11016 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11017 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11018 | } |
| 11019 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11020 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11021 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11022 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11023 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11024 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11025 | checkkwd = CHKKWD | CHKALIAS; |
| 11026 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11027 | while (readtoken() == TREDIR) { |
| 11028 | *rpp = n2 = redirnode; |
| 11029 | rpp = &n2->nfile.next; |
| 11030 | parsefname(); |
| 11031 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11032 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11033 | *rpp = NULL; |
| 11034 | if (redir) { |
| 11035 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11036 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11037 | n2->type = NREDIR; |
| 11038 | n2->nredir.n = n1; |
| 11039 | n1 = n2; |
| 11040 | } |
| 11041 | n1->nredir.redirect = redir; |
| 11042 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11043 | return n1; |
| 11044 | } |
| 11045 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11046 | #if ENABLE_ASH_BASH_COMPAT |
| 11047 | static int decode_dollar_squote(void) |
| 11048 | { |
| 11049 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 11050 | int c, cnt; |
| 11051 | char *p; |
| 11052 | char buf[4]; |
| 11053 | |
| 11054 | c = pgetc(); |
| 11055 | p = strchr(C_escapes, c); |
| 11056 | if (p) { |
| 11057 | buf[0] = c; |
| 11058 | p = buf; |
| 11059 | cnt = 3; |
| 11060 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 11061 | do { |
| 11062 | c = pgetc(); |
| 11063 | *++p = c; |
| 11064 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 11065 | pungetc(); |
| 11066 | } else if (c == 'x') { /* \xHH */ |
| 11067 | do { |
| 11068 | c = pgetc(); |
| 11069 | *++p = c; |
| 11070 | } while (isxdigit(c) && --cnt); |
| 11071 | pungetc(); |
| 11072 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 11073 | c = 'x'; |
| 11074 | goto unrecognized; |
| 11075 | } |
| 11076 | } else { /* simple seq like \\ or \t */ |
| 11077 | p++; |
| 11078 | } |
| 11079 | *p = '\0'; |
| 11080 | p = buf; |
| 11081 | c = bb_process_escape_sequence((void*)&p); |
| 11082 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 11083 | if (c != '\'' && c != '"') { |
| 11084 | unrecognized: |
| 11085 | c |= 0x100; /* "please encode \, then me" */ |
| 11086 | } |
| 11087 | } |
| 11088 | return c; |
| 11089 | } |
| 11090 | #endif |
| 11091 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11092 | /* |
| 11093 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 11094 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 11095 | * 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] | 11096 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11097 | * is the first character of the input token or document. |
| 11098 | * |
| 11099 | * Because C does not have internal subroutines, I have simulated them |
| 11100 | * using goto's to implement the subroutine linkage. The following macros |
| 11101 | * will run code that appears at the end of readtoken1. |
| 11102 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11103 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 11104 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 11105 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 11106 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 11107 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 11108 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11110 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 11111 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11112 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11113 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11114 | char *out; |
| 11115 | int len; |
| 11116 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11117 | struct nodelist *bqlist; |
| 11118 | smallint quotef; |
| 11119 | smallint dblquote; |
| 11120 | smallint oldstyle; |
| 11121 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11122 | #if ENABLE_ASH_EXPAND_PRMT |
| 11123 | smallint pssyntax; /* we are expanding a prompt string */ |
| 11124 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11125 | int varnest; /* levels of variables expansion */ |
| 11126 | int arinest; /* levels of arithmetic expansion */ |
| 11127 | int parenlevel; /* levels of parens in arithmetic */ |
| 11128 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 11129 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11130 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11131 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11132 | #if __GNUC__ |
| 11133 | /* Avoid longjmp clobbering */ |
| 11134 | (void) &out; |
| 11135 | (void) "ef; |
| 11136 | (void) &dblquote; |
| 11137 | (void) &varnest; |
| 11138 | (void) &arinest; |
| 11139 | (void) &parenlevel; |
| 11140 | (void) &dqvarnest; |
| 11141 | (void) &oldstyle; |
| 11142 | (void) &prevsyntax; |
| 11143 | (void) &syntax; |
| 11144 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11145 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11146 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11147 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11148 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11149 | #if ENABLE_ASH_EXPAND_PRMT |
| 11150 | pssyntax = (syntax == PSSYNTAX); |
| 11151 | if (pssyntax) |
| 11152 | syntax = DQSYNTAX; |
| 11153 | #endif |
| 11154 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11155 | varnest = 0; |
| 11156 | arinest = 0; |
| 11157 | parenlevel = 0; |
| 11158 | dqvarnest = 0; |
| 11159 | |
| 11160 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11161 | loop: |
| 11162 | /* For each line, until end of word */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11163 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 11164 | for (;;) { /* until end of line or end of word */ |
| 11165 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
| 11166 | switch (SIT(c, syntax)) { |
| 11167 | case CNL: /* '\n' */ |
| 11168 | if (syntax == BASESYNTAX) |
| 11169 | goto endword; /* exit outer loop */ |
| 11170 | USTPUTC(c, out); |
| 11171 | g_parsefile->linno++; |
| 11172 | setprompt_if(doprompt, 2); |
| 11173 | c = pgetc(); |
| 11174 | goto loop; /* continue outer loop */ |
| 11175 | case CWORD: |
| 11176 | USTPUTC(c, out); |
| 11177 | break; |
| 11178 | case CCTL: |
| 11179 | if (eofmark == NULL || dblquote) |
| 11180 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11181 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11182 | if (c == '\\' && bash_dollar_squote) { |
| 11183 | c = decode_dollar_squote(); |
| 11184 | if (c & 0x100) { |
| 11185 | USTPUTC('\\', out); |
| 11186 | c = (unsigned char)c; |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11187 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11188 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11189 | #endif |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11190 | USTPUTC(c, out); |
| 11191 | break; |
| 11192 | case CBACK: /* backslash */ |
| 11193 | c = pgetc_without_PEOA(); |
| 11194 | if (c == PEOF) { |
| 11195 | USTPUTC(CTLESC, out); |
| 11196 | USTPUTC('\\', out); |
| 11197 | pungetc(); |
| 11198 | } else if (c == '\n') { |
| 11199 | setprompt_if(doprompt, 2); |
| 11200 | } else { |
| 11201 | #if ENABLE_ASH_EXPAND_PRMT |
| 11202 | if (c == '$' && pssyntax) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11203 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11204 | USTPUTC('\\', out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11205 | } |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11206 | #endif |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11207 | /* Backslash is retained if we are in "str" and next char isn't special */ |
| 11208 | if (dblquote |
| 11209 | && c != '\\' |
| 11210 | && c != '`' |
| 11211 | && c != '$' |
| 11212 | && (c != '"' || eofmark != NULL) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11213 | ) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11214 | USTPUTC(CTLESC, out); |
| 11215 | USTPUTC('\\', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11216 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11217 | if (SIT(c, SQSYNTAX) == CCTL) |
| 11218 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 0ff78a0 | 2010-08-30 15:20:07 +0200 | [diff] [blame] | 11219 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11220 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11221 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11222 | break; |
| 11223 | case CSQUOTE: |
| 11224 | syntax = SQSYNTAX; |
| 11225 | quotemark: |
| 11226 | if (eofmark == NULL) { |
| 11227 | USTPUTC(CTLQUOTEMARK, out); |
| 11228 | } |
| 11229 | break; |
| 11230 | case CDQUOTE: |
| 11231 | syntax = DQSYNTAX; |
| 11232 | dblquote = 1; |
| 11233 | goto quotemark; |
| 11234 | case CENDQUOTE: |
| 11235 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
| 11236 | if (eofmark != NULL && arinest == 0 |
| 11237 | && varnest == 0 |
| 11238 | ) { |
| 11239 | USTPUTC(c, out); |
| 11240 | } else { |
| 11241 | if (dqvarnest == 0) { |
| 11242 | syntax = BASESYNTAX; |
| 11243 | dblquote = 0; |
| 11244 | } |
| 11245 | quotef = 1; |
| 11246 | goto quotemark; |
| 11247 | } |
| 11248 | break; |
| 11249 | case CVAR: /* '$' */ |
| 11250 | PARSESUB(); /* parse substitution */ |
| 11251 | break; |
| 11252 | case CENDVAR: /* '}' */ |
| 11253 | if (varnest > 0) { |
| 11254 | varnest--; |
| 11255 | if (dqvarnest > 0) { |
| 11256 | dqvarnest--; |
| 11257 | } |
| 11258 | c = CTLENDVAR; |
| 11259 | } |
| 11260 | USTPUTC(c, out); |
| 11261 | break; |
| 11262 | #if ENABLE_SH_MATH_SUPPORT |
| 11263 | case CLP: /* '(' in arithmetic */ |
| 11264 | parenlevel++; |
| 11265 | USTPUTC(c, out); |
| 11266 | break; |
| 11267 | case CRP: /* ')' in arithmetic */ |
| 11268 | if (parenlevel > 0) { |
| 11269 | parenlevel--; |
| 11270 | } else { |
| 11271 | if (pgetc() == ')') { |
| 11272 | if (--arinest == 0) { |
| 11273 | syntax = prevsyntax; |
| 11274 | dblquote = (syntax == DQSYNTAX); |
| 11275 | c = CTLENDARI; |
| 11276 | } |
| 11277 | } else { |
| 11278 | /* |
| 11279 | * unbalanced parens |
| 11280 | * (don't 2nd guess - no error) |
| 11281 | */ |
| 11282 | pungetc(); |
| 11283 | } |
| 11284 | } |
| 11285 | USTPUTC(c, out); |
| 11286 | break; |
| 11287 | #endif |
| 11288 | case CBQUOTE: /* '`' */ |
| 11289 | PARSEBACKQOLD(); |
| 11290 | break; |
| 11291 | case CENDFILE: |
| 11292 | goto endword; /* exit outer loop */ |
| 11293 | case CIGN: |
| 11294 | break; |
| 11295 | default: |
| 11296 | if (varnest == 0) { |
| 11297 | #if ENABLE_ASH_BASH_COMPAT |
| 11298 | if (c == '&') { |
| 11299 | if (pgetc() == '>') |
| 11300 | c = 0x100 + '>'; /* flag &> */ |
| 11301 | pungetc(); |
| 11302 | } |
| 11303 | #endif |
| 11304 | goto endword; /* exit outer loop */ |
| 11305 | } |
| 11306 | IF_ASH_ALIAS(if (c != PEOA)) |
| 11307 | USTPUTC(c, out); |
| 11308 | } |
| 11309 | c = pgetc_fast(); |
| 11310 | } /* for (;;) */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11311 | endword: |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11312 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11313 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11314 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11315 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11316 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11317 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11318 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11319 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11320 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11321 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11322 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11323 | } |
| 11324 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11325 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11326 | out = stackblock(); |
| 11327 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11328 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11329 | && quotef == 0 |
| 11330 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11331 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11332 | PARSEREDIR(); /* passed as params: out, c */ |
| 11333 | lasttoken = TREDIR; |
| 11334 | return lasttoken; |
| 11335 | } |
| 11336 | /* else: non-number X seen, interpret it |
| 11337 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11338 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11339 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11340 | } |
| 11341 | quoteflag = quotef; |
| 11342 | backquotelist = bqlist; |
| 11343 | grabstackblock(len); |
| 11344 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11345 | lasttoken = TWORD; |
| 11346 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11347 | /* end of readtoken routine */ |
| 11348 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11349 | /* |
| 11350 | * Check to see whether we are at the end of the here document. When this |
| 11351 | * is called, c is set to the first character of the next input line. If |
| 11352 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11353 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11354 | checkend: { |
| 11355 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11356 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11357 | if (c == PEOA) |
| 11358 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11359 | #endif |
| 11360 | if (striptabs) { |
| 11361 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11362 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11363 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11364 | } |
| 11365 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11366 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11367 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11368 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11369 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11370 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11371 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11372 | if (*p == '\n' && *q == '\0') { |
| 11373 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11374 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11375 | needprompt = doprompt; |
| 11376 | } else { |
| 11377 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11378 | } |
| 11379 | } |
| 11380 | } |
| 11381 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11382 | goto checkend_return; |
| 11383 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11384 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11385 | /* |
| 11386 | * Parse a redirection operator. The variable "out" points to a string |
| 11387 | * specifying the fd to be redirected. The variable "c" contains the |
| 11388 | * first character of the redirection operator. |
| 11389 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11390 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11391 | /* out is already checked to be a valid number or "" */ |
| 11392 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11393 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11394 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11395 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11396 | if (c == '>') { |
| 11397 | np->nfile.fd = 1; |
| 11398 | c = pgetc(); |
| 11399 | if (c == '>') |
| 11400 | np->type = NAPPEND; |
| 11401 | else if (c == '|') |
| 11402 | np->type = NCLOBBER; |
| 11403 | else if (c == '&') |
| 11404 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11405 | /* 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] | 11406 | else { |
| 11407 | np->type = NTO; |
| 11408 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11409 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11410 | } |
| 11411 | #if ENABLE_ASH_BASH_COMPAT |
| 11412 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11413 | np->nfile.fd = 1; |
| 11414 | pgetc(); /* this is '>', no need to check */ |
| 11415 | np->type = NTO2; |
| 11416 | } |
| 11417 | #endif |
| 11418 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11419 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11420 | c = pgetc(); |
| 11421 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11422 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11423 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11424 | np = stzalloc(sizeof(struct nhere)); |
| 11425 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11426 | } |
| 11427 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11428 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11429 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11430 | c = pgetc(); |
| 11431 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11432 | heredoc->striptabs = 1; |
| 11433 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11434 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11435 | pungetc(); |
| 11436 | } |
| 11437 | break; |
| 11438 | |
| 11439 | case '&': |
| 11440 | np->type = NFROMFD; |
| 11441 | break; |
| 11442 | |
| 11443 | case '>': |
| 11444 | np->type = NFROMTO; |
| 11445 | break; |
| 11446 | |
| 11447 | default: |
| 11448 | np->type = NFROM; |
| 11449 | pungetc(); |
| 11450 | break; |
| 11451 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11452 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11453 | if (fd >= 0) |
| 11454 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11455 | redirnode = np; |
| 11456 | goto parseredir_return; |
| 11457 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11458 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11459 | /* |
| 11460 | * Parse a substitution. At this point, we have read the dollar sign |
| 11461 | * and nothing else. |
| 11462 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11463 | |
| 11464 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11465 | * (assuming ascii char codes, as the original implementation did) */ |
| 11466 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11467 | (((unsigned)(c) - 33 < 32) \ |
| 11468 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11469 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11470 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11471 | int typeloc; |
| 11472 | int flags; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11473 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11474 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11475 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11476 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11477 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11478 | #if ENABLE_ASH_BASH_COMPAT |
| 11479 | if (c == '\'') |
| 11480 | bash_dollar_squote = 1; |
| 11481 | else |
| 11482 | #endif |
| 11483 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11484 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11485 | } else if (c == '(') { |
| 11486 | /* $(command) or $((arith)) */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11487 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11488 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11489 | PARSEARITH(); |
| 11490 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11491 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11492 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11493 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11494 | pungetc(); |
| 11495 | PARSEBACKQNEW(); |
| 11496 | } |
| 11497 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11498 | /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11499 | USTPUTC(CTLVAR, out); |
| 11500 | typeloc = out - (char *)stackblock(); |
| 11501 | USTPUTC(VSNORMAL, out); |
| 11502 | subtype = VSNORMAL; |
| 11503 | if (c == '{') { |
| 11504 | c = pgetc(); |
| 11505 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11506 | c = pgetc(); |
| 11507 | if (c == '}') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11508 | c = '#'; /* ${#} - same as $# */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11509 | else |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11510 | subtype = VSLENGTH; /* ${#VAR} */ |
| 11511 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11512 | subtype = 0; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11513 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11514 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11515 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11516 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11517 | do { |
| 11518 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11519 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11520 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11521 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11522 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11523 | do { |
| 11524 | STPUTC(c, out); |
| 11525 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11526 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11527 | } else if (is_special(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11528 | /* $[{[#]]<specialchar>[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11529 | USTPUTC(c, out); |
| 11530 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11531 | } else { |
| 11532 | badsub: |
| 11533 | raise_error_syntax("bad substitution"); |
| 11534 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11535 | if (c != '}' && subtype == VSLENGTH) { |
| 11536 | /* ${#VAR didn't end with } */ |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11537 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11538 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11539 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11540 | STPUTC('=', out); |
| 11541 | flags = 0; |
| 11542 | if (subtype == 0) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11543 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 11544 | /* c == first char after VAR */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11545 | switch (c) { |
| 11546 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11547 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11548 | #if ENABLE_ASH_BASH_COMPAT |
| 11549 | if (c == ':' || c == '$' || isdigit(c)) { |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 11550 | //TODO: support more general format ${v:EXPR:EXPR}, |
| 11551 | // where EXPR follows $(()) rules |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11552 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11553 | pungetc(); |
| 11554 | break; /* "goto do_pungetc" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11555 | } |
| 11556 | #endif |
| 11557 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11558 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11559 | default: { |
| 11560 | static const char types[] ALIGN1 = "}-+?="; |
| 11561 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11562 | if (p == NULL) |
| 11563 | goto badsub; |
| 11564 | subtype = p - types + VSNORMAL; |
| 11565 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11566 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11567 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11568 | case '#': { |
| 11569 | int cc = c; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11570 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11571 | c = pgetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11572 | if (c != cc) |
| 11573 | goto do_pungetc; |
| 11574 | subtype++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11575 | break; |
| 11576 | } |
| 11577 | #if ENABLE_ASH_BASH_COMPAT |
| 11578 | case '/': |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 11579 | /* ${v/[/]pattern/repl} */ |
| 11580 | //TODO: encode pattern and repl separately. |
| 11581 | // Currently ${v/$var_with_slash/repl} is horribly broken |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11582 | subtype = VSREPLACE; |
| 11583 | c = pgetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11584 | if (c != '/') |
| 11585 | goto do_pungetc; |
| 11586 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11587 | break; |
| 11588 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11589 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11590 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11591 | do_pungetc: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11592 | pungetc(); |
| 11593 | } |
| 11594 | if (dblquote || arinest) |
| 11595 | flags |= VSQUOTE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11596 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11597 | if (subtype != VSNORMAL) { |
| 11598 | varnest++; |
| 11599 | if (dblquote || arinest) { |
| 11600 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11601 | } |
| 11602 | } |
| 11603 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11604 | goto parsesub_return; |
| 11605 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11606 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11607 | /* |
| 11608 | * Called to parse command substitutions. Newstyle is set if the command |
| 11609 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11610 | * list of commands (passed by reference), and savelen is the number of |
| 11611 | * characters on the top of the stack which must be preserved. |
| 11612 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11613 | parsebackq: { |
| 11614 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11615 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11616 | union node *n; |
| 11617 | char *volatile str; |
| 11618 | struct jmploc jmploc; |
| 11619 | struct jmploc *volatile savehandler; |
| 11620 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11621 | smallint saveprompt = 0; |
| 11622 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11623 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11624 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11625 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11626 | savepbq = parsebackquote; |
| 11627 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11628 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11629 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11630 | exception_handler = savehandler; |
| 11631 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11632 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11633 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11634 | str = NULL; |
| 11635 | savelen = out - (char *)stackblock(); |
| 11636 | if (savelen > 0) { |
| 11637 | str = ckmalloc(savelen); |
| 11638 | memcpy(str, stackblock(), savelen); |
| 11639 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11640 | savehandler = exception_handler; |
| 11641 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11642 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11643 | if (oldstyle) { |
| 11644 | /* We must read until the closing backquote, giving special |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 11645 | * treatment to some slashes, and then push the string and |
| 11646 | * reread it as input, interpreting it normally. |
| 11647 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11648 | char *pout; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11649 | size_t psavelen; |
| 11650 | char *pstr; |
| 11651 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11652 | STARTSTACKSTR(pout); |
| 11653 | for (;;) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11654 | int pc; |
| 11655 | |
| 11656 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11657 | pc = pgetc(); |
| 11658 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11659 | case '`': |
| 11660 | goto done; |
| 11661 | |
| 11662 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11663 | pc = pgetc(); |
| 11664 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11665 | g_parsefile->linno++; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11666 | setprompt_if(doprompt, 2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11667 | /* |
| 11668 | * If eating a newline, avoid putting |
| 11669 | * the newline into the new character |
| 11670 | * stream (via the STPUTC after the |
| 11671 | * switch). |
| 11672 | */ |
| 11673 | continue; |
| 11674 | } |
| 11675 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11676 | && (!dblquote || pc != '"') |
| 11677 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11678 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11679 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11680 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11681 | break; |
| 11682 | } |
| 11683 | /* fall through */ |
| 11684 | |
| 11685 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11686 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11687 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11688 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11689 | |
| 11690 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11691 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11692 | needprompt = doprompt; |
| 11693 | break; |
| 11694 | |
| 11695 | default: |
| 11696 | break; |
| 11697 | } |
| 11698 | STPUTC(pc, pout); |
| 11699 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11700 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11701 | STPUTC('\0', pout); |
| 11702 | psavelen = pout - (char *)stackblock(); |
| 11703 | if (psavelen > 0) { |
| 11704 | pstr = grabstackstr(pout); |
| 11705 | setinputstring(pstr); |
| 11706 | } |
| 11707 | } |
| 11708 | nlpp = &bqlist; |
| 11709 | while (*nlpp) |
| 11710 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11711 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11712 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11713 | parsebackquote = oldstyle; |
| 11714 | |
| 11715 | if (oldstyle) { |
| 11716 | saveprompt = doprompt; |
| 11717 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11718 | } |
| 11719 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11720 | n = list(2); |
| 11721 | |
| 11722 | if (oldstyle) |
| 11723 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11724 | else if (readtoken() != TRP) |
| 11725 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11726 | |
| 11727 | (*nlpp)->n = n; |
| 11728 | if (oldstyle) { |
| 11729 | /* |
| 11730 | * Start reading from old file again, ignoring any pushed back |
| 11731 | * tokens left from the backquote parsing |
| 11732 | */ |
| 11733 | popfile(); |
| 11734 | tokpushback = 0; |
| 11735 | } |
| 11736 | while (stackblocksize() <= savelen) |
| 11737 | growstackblock(); |
| 11738 | STARTSTACKSTR(out); |
| 11739 | if (str) { |
| 11740 | memcpy(out, str, savelen); |
| 11741 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11742 | INT_OFF; |
| 11743 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11744 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11745 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11746 | } |
| 11747 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11748 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11749 | if (arinest || dblquote) |
| 11750 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11751 | else |
| 11752 | USTPUTC(CTLBACKQ, out); |
| 11753 | if (oldstyle) |
| 11754 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11755 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11756 | } |
| 11757 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11758 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11759 | /* |
| 11760 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11761 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11762 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11763 | if (++arinest == 1) { |
| 11764 | prevsyntax = syntax; |
| 11765 | syntax = ARISYNTAX; |
| 11766 | USTPUTC(CTLARI, out); |
| 11767 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11768 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11769 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11770 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11771 | } else { |
| 11772 | /* |
| 11773 | * we collapse embedded arithmetic expansion to |
| 11774 | * parenthesis, which should be equivalent |
| 11775 | */ |
| 11776 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11777 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11778 | goto parsearith_return; |
| 11779 | } |
| 11780 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11781 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11782 | } /* end of readtoken */ |
| 11783 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11784 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11785 | * Read the next input token. |
| 11786 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11787 | * backquotes. We set quoteflag to true if any part of the word was |
| 11788 | * quoted. |
| 11789 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11790 | * the redirection. |
| 11791 | * In all cases, the variable startlinno is set to the number of the line |
| 11792 | * on which the token starts. |
| 11793 | * |
| 11794 | * [Change comment: here documents and internal procedures] |
| 11795 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11796 | * word parsing code into a separate routine. In this case, readtoken |
| 11797 | * doesn't need to have any internal procedures, but parseword does. |
| 11798 | * We could also make parseoperator in essence the main routine, and |
| 11799 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11800 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11801 | #define NEW_xxreadtoken |
| 11802 | #ifdef NEW_xxreadtoken |
| 11803 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11804 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11805 | '\n', '(', ')', /* singles */ |
| 11806 | '&', '|', ';', /* doubles */ |
| 11807 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11808 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11809 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11810 | #define xxreadtoken_singles 3 |
| 11811 | #define xxreadtoken_doubles 3 |
| 11812 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11813 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11814 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11815 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11816 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11817 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11818 | }; |
| 11819 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11820 | static int |
| 11821 | xxreadtoken(void) |
| 11822 | { |
| 11823 | int c; |
| 11824 | |
| 11825 | if (tokpushback) { |
| 11826 | tokpushback = 0; |
| 11827 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11828 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11829 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11830 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11831 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11832 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11833 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11834 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11835 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11836 | if (c == '#') { |
| 11837 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11838 | continue; |
| 11839 | pungetc(); |
| 11840 | } else if (c == '\\') { |
| 11841 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11842 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11843 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11844 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11845 | startlinno = ++g_parsefile->linno; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11846 | setprompt_if(doprompt, 2); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11847 | } else { |
| 11848 | const char *p; |
| 11849 | |
| 11850 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11851 | if (c != PEOF) { |
| 11852 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11853 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11854 | needprompt = doprompt; |
| 11855 | } |
| 11856 | |
| 11857 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11858 | if (p == NULL) |
| 11859 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11860 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11861 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11862 | int cc = pgetc(); |
| 11863 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11864 | p += xxreadtoken_doubles + 1; |
| 11865 | } else { |
| 11866 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11867 | #if ENABLE_ASH_BASH_COMPAT |
| 11868 | if (c == '&' && cc == '>') /* &> */ |
| 11869 | break; /* return readtoken1(...) */ |
| 11870 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11871 | } |
| 11872 | } |
| 11873 | } |
| 11874 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11875 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11876 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11877 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11878 | |
| 11879 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11880 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11881 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11882 | #define RETURN(token) return lasttoken = token |
| 11883 | static int |
| 11884 | xxreadtoken(void) |
| 11885 | { |
| 11886 | int c; |
| 11887 | |
| 11888 | if (tokpushback) { |
| 11889 | tokpushback = 0; |
| 11890 | return lasttoken; |
| 11891 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11892 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11893 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11894 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11895 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11896 | switch (c) { |
| 11897 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11898 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11899 | continue; |
| 11900 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11901 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11902 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11903 | pungetc(); |
| 11904 | continue; |
| 11905 | case '\\': |
| 11906 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11907 | startlinno = ++g_parsefile->linno; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11908 | setprompt_if(doprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11909 | continue; |
| 11910 | } |
| 11911 | pungetc(); |
| 11912 | goto breakloop; |
| 11913 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11914 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11915 | needprompt = doprompt; |
| 11916 | RETURN(TNL); |
| 11917 | case PEOF: |
| 11918 | RETURN(TEOF); |
| 11919 | case '&': |
| 11920 | if (pgetc() == '&') |
| 11921 | RETURN(TAND); |
| 11922 | pungetc(); |
| 11923 | RETURN(TBACKGND); |
| 11924 | case '|': |
| 11925 | if (pgetc() == '|') |
| 11926 | RETURN(TOR); |
| 11927 | pungetc(); |
| 11928 | RETURN(TPIPE); |
| 11929 | case ';': |
| 11930 | if (pgetc() == ';') |
| 11931 | RETURN(TENDCASE); |
| 11932 | pungetc(); |
| 11933 | RETURN(TSEMI); |
| 11934 | case '(': |
| 11935 | RETURN(TLP); |
| 11936 | case ')': |
| 11937 | RETURN(TRP); |
| 11938 | default: |
| 11939 | goto breakloop; |
| 11940 | } |
| 11941 | } |
| 11942 | breakloop: |
| 11943 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11944 | #undef RETURN |
| 11945 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11946 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11947 | |
| 11948 | static int |
| 11949 | readtoken(void) |
| 11950 | { |
| 11951 | int t; |
| 11952 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11953 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11954 | #endif |
| 11955 | |
| 11956 | #if ENABLE_ASH_ALIAS |
| 11957 | top: |
| 11958 | #endif |
| 11959 | |
| 11960 | t = xxreadtoken(); |
| 11961 | |
| 11962 | /* |
| 11963 | * eat newlines |
| 11964 | */ |
| 11965 | if (checkkwd & CHKNL) { |
| 11966 | while (t == TNL) { |
| 11967 | parseheredoc(); |
| 11968 | t = xxreadtoken(); |
| 11969 | } |
| 11970 | } |
| 11971 | |
| 11972 | if (t != TWORD || quoteflag) { |
| 11973 | goto out; |
| 11974 | } |
| 11975 | |
| 11976 | /* |
| 11977 | * check for keywords |
| 11978 | */ |
| 11979 | if (checkkwd & CHKKWD) { |
| 11980 | const char *const *pp; |
| 11981 | |
| 11982 | pp = findkwd(wordtext); |
| 11983 | if (pp) { |
| 11984 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11985 | TRACE(("keyword '%s' recognized\n", tokname_array[t] + 1)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11986 | goto out; |
| 11987 | } |
| 11988 | } |
| 11989 | |
| 11990 | if (checkkwd & CHKALIAS) { |
| 11991 | #if ENABLE_ASH_ALIAS |
| 11992 | struct alias *ap; |
| 11993 | ap = lookupalias(wordtext, 1); |
| 11994 | if (ap != NULL) { |
| 11995 | if (*ap->val) { |
| 11996 | pushstring(ap->val, ap); |
| 11997 | } |
| 11998 | goto top; |
| 11999 | } |
| 12000 | #endif |
| 12001 | } |
| 12002 | out: |
| 12003 | checkkwd = 0; |
| 12004 | #if DEBUG |
| 12005 | if (!alreadyseen) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 12006 | TRACE(("token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12007 | else |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 12008 | 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] | 12009 | #endif |
| 12010 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12011 | } |
| 12012 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12013 | static char |
| 12014 | peektoken(void) |
| 12015 | { |
| 12016 | int t; |
| 12017 | |
| 12018 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12019 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12020 | return tokname_array[t][0]; |
| 12021 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12022 | |
| 12023 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12024 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 12025 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12026 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12027 | static union node * |
| 12028 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12029 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12030 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12031 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12032 | tokpushback = 0; |
| 12033 | doprompt = interact; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12034 | setprompt_if(doprompt, doprompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12035 | needprompt = 0; |
| 12036 | t = readtoken(); |
| 12037 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12038 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12039 | if (t == TNL) |
| 12040 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 12041 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12042 | return list(1); |
| 12043 | } |
| 12044 | |
| 12045 | /* |
| 12046 | * Input any here documents. |
| 12047 | */ |
| 12048 | static void |
| 12049 | parseheredoc(void) |
| 12050 | { |
| 12051 | struct heredoc *here; |
| 12052 | union node *n; |
| 12053 | |
| 12054 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 12055 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12056 | |
| 12057 | while (here) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 12058 | setprompt_if(needprompt, 2); |
| 12059 | readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12060 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12061 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12062 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 12063 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12064 | n->narg.text = wordtext; |
| 12065 | n->narg.backquote = backquotelist; |
| 12066 | here->here->nhere.doc = n; |
| 12067 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12068 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12069 | } |
| 12070 | |
| 12071 | |
| 12072 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12073 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12074 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12075 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12076 | static const char * |
| 12077 | expandstr(const char *ps) |
| 12078 | { |
| 12079 | union node n; |
| 12080 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 12081 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 12082 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12083 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 12084 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 12085 | popfile(); |
| 12086 | |
| 12087 | n.narg.type = NARG; |
| 12088 | n.narg.next = NULL; |
| 12089 | n.narg.text = wordtext; |
| 12090 | n.narg.backquote = backquotelist; |
| 12091 | |
| 12092 | expandarg(&n, NULL, 0); |
| 12093 | return stackblock(); |
| 12094 | } |
| 12095 | #endif |
| 12096 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12097 | /* |
| 12098 | * Execute a command or commands contained in a string. |
| 12099 | */ |
| 12100 | static int |
| 12101 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12102 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12103 | union node *n; |
| 12104 | struct stackmark smark; |
| 12105 | int skip; |
| 12106 | |
| 12107 | setinputstring(s); |
| 12108 | setstackmark(&smark); |
| 12109 | |
| 12110 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12111 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12112 | evaltree(n, 0); |
| 12113 | popstackmark(&smark); |
| 12114 | skip = evalskip; |
| 12115 | if (skip) |
| 12116 | break; |
| 12117 | } |
| 12118 | popfile(); |
| 12119 | |
| 12120 | skip &= mask; |
| 12121 | evalskip = skip; |
| 12122 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12123 | } |
| 12124 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12125 | /* |
| 12126 | * The eval command. |
| 12127 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12128 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12129 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12130 | { |
| 12131 | char *p; |
| 12132 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12133 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12134 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12135 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12136 | argv += 2; |
| 12137 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12138 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12139 | for (;;) { |
| 12140 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12141 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12142 | if (p == NULL) |
| 12143 | break; |
| 12144 | STPUTC(' ', concat); |
| 12145 | } |
| 12146 | STPUTC('\0', concat); |
| 12147 | p = grabstackstr(concat); |
| 12148 | } |
| 12149 | evalstring(p, ~SKIPEVAL); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12150 | } |
| 12151 | return exitstatus; |
| 12152 | } |
| 12153 | |
| 12154 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 12155 | * Read and execute commands. |
| 12156 | * "Top" is nonzero for the top level command loop; |
| 12157 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12158 | */ |
| 12159 | static int |
| 12160 | cmdloop(int top) |
| 12161 | { |
| 12162 | union node *n; |
| 12163 | struct stackmark smark; |
| 12164 | int inter; |
| 12165 | int numeof = 0; |
| 12166 | |
| 12167 | TRACE(("cmdloop(%d) called\n", top)); |
| 12168 | for (;;) { |
| 12169 | int skip; |
| 12170 | |
| 12171 | setstackmark(&smark); |
| 12172 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 12173 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12174 | showjobs(stderr, SHOW_CHANGED); |
| 12175 | #endif |
| 12176 | inter = 0; |
| 12177 | if (iflag && top) { |
| 12178 | inter++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12179 | chkmail(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12180 | } |
| 12181 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 12182 | #if DEBUG |
| 12183 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 12184 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 12185 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12186 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12187 | if (!top || numeof >= 50) |
| 12188 | break; |
| 12189 | if (!stoppedjobs()) { |
| 12190 | if (!Iflag) |
| 12191 | break; |
| 12192 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 12193 | } |
| 12194 | numeof++; |
| 12195 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 12196 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 12197 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12198 | numeof = 0; |
| 12199 | evaltree(n, 0); |
| 12200 | } |
| 12201 | popstackmark(&smark); |
| 12202 | skip = evalskip; |
| 12203 | |
| 12204 | if (skip) { |
| 12205 | evalskip = 0; |
| 12206 | return skip & SKIPEVAL; |
| 12207 | } |
| 12208 | } |
| 12209 | return 0; |
| 12210 | } |
| 12211 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12212 | /* |
| 12213 | * Take commands from a file. To be compatible we should do a path |
| 12214 | * search for the file, which is necessary to find sub-commands. |
| 12215 | */ |
| 12216 | static char * |
| 12217 | find_dot_file(char *name) |
| 12218 | { |
| 12219 | char *fullname; |
| 12220 | const char *path = pathval(); |
| 12221 | struct stat statb; |
| 12222 | |
| 12223 | /* don't try this for absolute or relative paths */ |
| 12224 | if (strchr(name, '/')) |
| 12225 | return name; |
| 12226 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12227 | /* IIRC standards do not say whether . is to be searched. |
| 12228 | * And it is even smaller this way, making it unconditional for now: |
| 12229 | */ |
| 12230 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12231 | fullname = name; |
| 12232 | goto try_cur_dir; |
| 12233 | } |
| 12234 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12235 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12236 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12237 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12238 | /* |
| 12239 | * Don't bother freeing here, since it will |
| 12240 | * be freed by the caller. |
| 12241 | */ |
| 12242 | return fullname; |
| 12243 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12244 | if (fullname != name) |
| 12245 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12246 | } |
| 12247 | |
| 12248 | /* not found in the PATH */ |
| 12249 | ash_msg_and_raise_error("%s: not found", name); |
| 12250 | /* NOTREACHED */ |
| 12251 | } |
| 12252 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12253 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12254 | dotcmd(int argc, char **argv) |
| 12255 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12256 | char *fullname; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12257 | struct strlist *sp; |
| 12258 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12259 | |
| 12260 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12261 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12262 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12263 | if (!argv[1]) { |
| 12264 | /* bash says: "bash: .: filename argument required" */ |
| 12265 | return 2; /* bash compat */ |
| 12266 | } |
| 12267 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12268 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 12269 | exitstatus = 0; |
| 12270 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 12271 | /* This aborts if file isn't found, which is POSIXly correct. |
| 12272 | * bash returns exitcode 1 instead. |
| 12273 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12274 | fullname = find_dot_file(argv[1]); |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12275 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12276 | argv += 2; |
| 12277 | argc -= 2; |
| 12278 | if (argc) { /* argc > 0, argv[0] != NULL */ |
| 12279 | saveparam = shellparam; |
| 12280 | shellparam.malloced = 0; |
| 12281 | shellparam.nparam = argc; |
| 12282 | shellparam.p = argv; |
| 12283 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12284 | |
Denys Vlasenko | 091f831 | 2013-03-17 14:25:22 +0100 | [diff] [blame] | 12285 | /* This aborts if file can't be opened, which is POSIXly correct. |
| 12286 | * bash returns exitcode 1 instead. |
| 12287 | */ |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12288 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12289 | commandname = fullname; |
| 12290 | cmdloop(0); |
| 12291 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12292 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12293 | if (argc) { |
| 12294 | freeparam(&shellparam); |
| 12295 | shellparam = saveparam; |
| 12296 | }; |
| 12297 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12298 | return exitstatus; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12299 | } |
| 12300 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12301 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12302 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12303 | { |
| 12304 | if (stoppedjobs()) |
| 12305 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12306 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12307 | exitstatus = number(argv[1]); |
| 12308 | raise_exception(EXEXIT); |
| 12309 | /* NOTREACHED */ |
| 12310 | } |
| 12311 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12312 | /* |
| 12313 | * Read a file containing shell functions. |
| 12314 | */ |
| 12315 | static void |
| 12316 | readcmdfile(char *name) |
| 12317 | { |
| 12318 | setinputfile(name, INPUT_PUSH_FILE); |
| 12319 | cmdloop(0); |
| 12320 | popfile(); |
| 12321 | } |
| 12322 | |
| 12323 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12324 | /* ============ find_command inplementation */ |
| 12325 | |
| 12326 | /* |
| 12327 | * Resolve a command name. If you change this routine, you may have to |
| 12328 | * change the shellexec routine as well. |
| 12329 | */ |
| 12330 | static void |
| 12331 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12332 | { |
| 12333 | struct tblentry *cmdp; |
| 12334 | int idx; |
| 12335 | int prev; |
| 12336 | char *fullname; |
| 12337 | struct stat statb; |
| 12338 | int e; |
| 12339 | int updatetbl; |
| 12340 | struct builtincmd *bcmd; |
| 12341 | |
| 12342 | /* If name contains a slash, don't use PATH or hash table */ |
| 12343 | if (strchr(name, '/') != NULL) { |
| 12344 | entry->u.index = -1; |
| 12345 | if (act & DO_ABS) { |
| 12346 | while (stat(name, &statb) < 0) { |
| 12347 | #ifdef SYSV |
| 12348 | if (errno == EINTR) |
| 12349 | continue; |
| 12350 | #endif |
| 12351 | entry->cmdtype = CMDUNKNOWN; |
| 12352 | return; |
| 12353 | } |
| 12354 | } |
| 12355 | entry->cmdtype = CMDNORMAL; |
| 12356 | return; |
| 12357 | } |
| 12358 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12359 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12360 | |
| 12361 | updatetbl = (path == pathval()); |
| 12362 | if (!updatetbl) { |
| 12363 | act |= DO_ALTPATH; |
| 12364 | if (strstr(path, "%builtin") != NULL) |
| 12365 | act |= DO_ALTBLTIN; |
| 12366 | } |
| 12367 | |
| 12368 | /* If name is in the table, check answer will be ok */ |
| 12369 | cmdp = cmdlookup(name, 0); |
| 12370 | if (cmdp != NULL) { |
| 12371 | int bit; |
| 12372 | |
| 12373 | switch (cmdp->cmdtype) { |
| 12374 | default: |
| 12375 | #if DEBUG |
| 12376 | abort(); |
| 12377 | #endif |
| 12378 | case CMDNORMAL: |
| 12379 | bit = DO_ALTPATH; |
| 12380 | break; |
| 12381 | case CMDFUNCTION: |
| 12382 | bit = DO_NOFUNC; |
| 12383 | break; |
| 12384 | case CMDBUILTIN: |
| 12385 | bit = DO_ALTBLTIN; |
| 12386 | break; |
| 12387 | } |
| 12388 | if (act & bit) { |
| 12389 | updatetbl = 0; |
| 12390 | cmdp = NULL; |
| 12391 | } else if (cmdp->rehash == 0) |
| 12392 | /* if not invalidated by cd, we're done */ |
| 12393 | goto success; |
| 12394 | } |
| 12395 | |
| 12396 | /* If %builtin not in path, check for builtin next */ |
| 12397 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12398 | if (bcmd) { |
| 12399 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12400 | goto builtin_success; |
| 12401 | if (act & DO_ALTPATH) { |
| 12402 | if (!(act & DO_ALTBLTIN)) |
| 12403 | goto builtin_success; |
| 12404 | } else if (builtinloc <= 0) { |
| 12405 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12406 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12407 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12408 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12409 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12410 | { |
| 12411 | int applet_no = find_applet_by_name(name); |
| 12412 | if (applet_no >= 0) { |
| 12413 | entry->cmdtype = CMDNORMAL; |
| 12414 | entry->u.index = -2 - applet_no; |
| 12415 | return; |
| 12416 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12417 | } |
| 12418 | #endif |
| 12419 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12420 | /* We have to search path. */ |
| 12421 | prev = -1; /* where to start */ |
| 12422 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12423 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12424 | prev = builtinloc; |
| 12425 | else |
| 12426 | prev = cmdp->param.index; |
| 12427 | } |
| 12428 | |
| 12429 | e = ENOENT; |
| 12430 | idx = -1; |
| 12431 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12432 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12433 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12434 | /* NB: code below will still use fullname |
| 12435 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12436 | idx++; |
| 12437 | if (pathopt) { |
| 12438 | if (prefix(pathopt, "builtin")) { |
| 12439 | if (bcmd) |
| 12440 | goto builtin_success; |
| 12441 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12442 | } |
| 12443 | if ((act & DO_NOFUNC) |
| 12444 | || !prefix(pathopt, "func") |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 12445 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12446 | continue; |
| 12447 | } |
| 12448 | } |
| 12449 | /* if rehash, don't redo absolute path names */ |
| 12450 | if (fullname[0] == '/' && idx <= prev) { |
| 12451 | if (idx < prev) |
| 12452 | continue; |
| 12453 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12454 | goto success; |
| 12455 | } |
| 12456 | while (stat(fullname, &statb) < 0) { |
| 12457 | #ifdef SYSV |
| 12458 | if (errno == EINTR) |
| 12459 | continue; |
| 12460 | #endif |
| 12461 | if (errno != ENOENT && errno != ENOTDIR) |
| 12462 | e = errno; |
| 12463 | goto loop; |
| 12464 | } |
| 12465 | e = EACCES; /* if we fail, this will be the error */ |
| 12466 | if (!S_ISREG(statb.st_mode)) |
| 12467 | continue; |
| 12468 | if (pathopt) { /* this is a %func directory */ |
| 12469 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12470 | /* NB: stalloc will return space pointed by fullname |
| 12471 | * (because we don't have any intervening allocations |
| 12472 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12473 | readcmdfile(fullname); |
| 12474 | cmdp = cmdlookup(name, 0); |
| 12475 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12476 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12477 | stunalloc(fullname); |
| 12478 | goto success; |
| 12479 | } |
| 12480 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12481 | if (!updatetbl) { |
| 12482 | entry->cmdtype = CMDNORMAL; |
| 12483 | entry->u.index = idx; |
| 12484 | return; |
| 12485 | } |
| 12486 | INT_OFF; |
| 12487 | cmdp = cmdlookup(name, 1); |
| 12488 | cmdp->cmdtype = CMDNORMAL; |
| 12489 | cmdp->param.index = idx; |
| 12490 | INT_ON; |
| 12491 | goto success; |
| 12492 | } |
| 12493 | |
| 12494 | /* We failed. If there was an entry for this command, delete it */ |
| 12495 | if (cmdp && updatetbl) |
| 12496 | delete_cmd_entry(); |
| 12497 | if (act & DO_ERR) |
| 12498 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12499 | entry->cmdtype = CMDUNKNOWN; |
| 12500 | return; |
| 12501 | |
| 12502 | builtin_success: |
| 12503 | if (!updatetbl) { |
| 12504 | entry->cmdtype = CMDBUILTIN; |
| 12505 | entry->u.cmd = bcmd; |
| 12506 | return; |
| 12507 | } |
| 12508 | INT_OFF; |
| 12509 | cmdp = cmdlookup(name, 1); |
| 12510 | cmdp->cmdtype = CMDBUILTIN; |
| 12511 | cmdp->param.cmd = bcmd; |
| 12512 | INT_ON; |
| 12513 | success: |
| 12514 | cmdp->rehash = 0; |
| 12515 | entry->cmdtype = cmdp->cmdtype; |
| 12516 | entry->u = cmdp->param; |
| 12517 | } |
| 12518 | |
| 12519 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12520 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12521 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12522 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12523 | * The trap builtin. |
| 12524 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12525 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12526 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12527 | { |
| 12528 | char *action; |
| 12529 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12530 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12531 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12532 | nextopt(nullstr); |
| 12533 | ap = argptr; |
| 12534 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12535 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12536 | char *tr = trap_ptr[signo]; |
| 12537 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12538 | /* note: bash adds "SIG", but only if invoked |
| 12539 | * as "bash". If called as "sh", or if set -o posix, |
| 12540 | * then it prints short signal names. |
| 12541 | * We are printing short names: */ |
| 12542 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12543 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12544 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12545 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12546 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12547 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12548 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12549 | } |
| 12550 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12551 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12552 | if (trap_ptr != trap) { |
| 12553 | free(trap_ptr); |
| 12554 | trap_ptr = trap; |
| 12555 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12556 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12557 | return 0; |
| 12558 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12559 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12560 | action = NULL; |
| 12561 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12562 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12563 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12564 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12565 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12566 | if (signo < 0) { |
| 12567 | /* Mimic bash message exactly */ |
| 12568 | ash_msg("%s: invalid signal specification", *ap); |
| 12569 | exitcode = 1; |
| 12570 | goto next; |
| 12571 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12572 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12573 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12574 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12575 | action = NULL; |
| 12576 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12577 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12578 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12579 | free(trap[signo]); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 12580 | if (action) |
| 12581 | may_have_traps = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12582 | trap[signo] = action; |
| 12583 | if (signo != 0) |
| 12584 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12585 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12586 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12587 | ap++; |
| 12588 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12589 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12590 | } |
| 12591 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12592 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12593 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12594 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12595 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12596 | /* |
| 12597 | * Lists available builtins |
| 12598 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12599 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12600 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12601 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12602 | unsigned col; |
| 12603 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12604 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12605 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12606 | "Built-in commands:\n" |
| 12607 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12608 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12609 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12610 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12611 | if (col > 60) { |
| 12612 | out1fmt("\n"); |
| 12613 | col = 0; |
| 12614 | } |
| 12615 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12616 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12617 | { |
| 12618 | const char *a = applet_names; |
| 12619 | while (*a) { |
| 12620 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12621 | if (col > 60) { |
| 12622 | out1fmt("\n"); |
| 12623 | col = 0; |
| 12624 | } |
| 12625 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12626 | } |
| 12627 | } |
| 12628 | #endif |
| 12629 | out1fmt("\n\n"); |
| 12630 | return EXIT_SUCCESS; |
| 12631 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12632 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12633 | |
Flemming Madsen | d96ffda | 2013-04-07 18:47:24 +0200 | [diff] [blame] | 12634 | #if MAX_HISTORY |
| 12635 | static int FAST_FUNC |
| 12636 | historycmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
| 12637 | { |
| 12638 | show_history(line_input_state); |
| 12639 | return EXIT_SUCCESS; |
| 12640 | } |
| 12641 | #endif |
| 12642 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12643 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12644 | * The export and readonly commands. |
| 12645 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12646 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12647 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12648 | { |
| 12649 | struct var *vp; |
| 12650 | char *name; |
| 12651 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12652 | char **aptr; |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12653 | char opt; |
| 12654 | int flag; |
| 12655 | int flag_off; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12656 | |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12657 | /* "readonly" in bash accepts, but ignores -n. |
| 12658 | * We do the same: it saves a conditional in nextopt's param. |
| 12659 | */ |
| 12660 | flag_off = 0; |
| 12661 | while ((opt = nextopt("np")) != '\0') { |
| 12662 | if (opt == 'n') |
| 12663 | flag_off = VEXPORT; |
| 12664 | } |
| 12665 | flag = VEXPORT; |
| 12666 | if (argv[0][0] == 'r') { |
| 12667 | flag = VREADONLY; |
| 12668 | flag_off = 0; /* readonly ignores -n */ |
| 12669 | } |
| 12670 | flag_off = ~flag_off; |
| 12671 | |
| 12672 | /*if (opt_p_not_specified) - bash doesnt check this. Try "export -p NAME" */ |
| 12673 | { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12674 | aptr = argptr; |
| 12675 | name = *aptr; |
| 12676 | if (name) { |
| 12677 | do { |
| 12678 | p = strchr(name, '='); |
| 12679 | if (p != NULL) { |
| 12680 | p++; |
| 12681 | } else { |
| 12682 | vp = *findvar(hashvar(name), name); |
| 12683 | if (vp) { |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12684 | vp->flags = ((vp->flags | flag) & flag_off); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12685 | continue; |
| 12686 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12687 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12688 | setvar(name, p, (flag & flag_off)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12689 | } while ((name = *++aptr) != NULL); |
| 12690 | return 0; |
| 12691 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12692 | } |
Denys Vlasenko | d527588 | 2012-10-01 13:41:17 +0200 | [diff] [blame] | 12693 | |
| 12694 | /* No arguments. Show the list of exported or readonly vars. |
| 12695 | * -n is ignored. |
| 12696 | */ |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12697 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12698 | return 0; |
| 12699 | } |
| 12700 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12701 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12702 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12703 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12704 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12705 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12706 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12707 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12708 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12709 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12710 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12711 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12712 | } |
| 12713 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12714 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12715 | * The unset builtin command. We unset the function before we unset the |
| 12716 | * variable to allow a function to be unset when there is a readonly variable |
| 12717 | * with the same name. |
| 12718 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12719 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12720 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12721 | { |
| 12722 | char **ap; |
| 12723 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12724 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12725 | int ret = 0; |
| 12726 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12727 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12728 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12729 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12730 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12731 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12732 | if (flag != 'f') { |
| 12733 | i = unsetvar(*ap); |
| 12734 | ret |= i; |
| 12735 | if (!(i & 2)) |
| 12736 | continue; |
| 12737 | } |
| 12738 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12739 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12740 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12741 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12742 | } |
| 12743 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12744 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12745 | ' ', offsetof(struct tms, tms_utime), |
| 12746 | '\n', offsetof(struct tms, tms_stime), |
| 12747 | ' ', offsetof(struct tms, tms_cutime), |
| 12748 | '\n', offsetof(struct tms, tms_cstime), |
| 12749 | 0 |
| 12750 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12751 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12752 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12753 | { |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12754 | unsigned long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12755 | const unsigned char *p; |
| 12756 | struct tms buf; |
| 12757 | |
| 12758 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12759 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12760 | |
| 12761 | p = timescmd_str; |
| 12762 | do { |
| 12763 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12764 | s = t / clk_tck; |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12765 | t = t % clk_tck; |
| 12766 | out1fmt("%lum%lu.%03lus%c", |
| 12767 | s / 60, s % 60, |
| 12768 | (t * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12769 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12770 | p += 2; |
| 12771 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12772 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12773 | return 0; |
| 12774 | } |
| 12775 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12776 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12777 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12778 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12779 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12780 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12781 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12782 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12783 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12784 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12785 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12786 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12787 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12788 | argv++; |
| 12789 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12790 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12791 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12792 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12793 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12794 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12795 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12796 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12797 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12798 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12799 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12800 | * The read builtin. Options: |
| 12801 | * -r Do not interpret '\' specially |
| 12802 | * -s Turn off echo (tty only) |
| 12803 | * -n NCHARS Read NCHARS max |
| 12804 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12805 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12806 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12807 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12808 | * TODO: bash also has: |
| 12809 | * -a ARRAY Read into array[0],[1],etc |
| 12810 | * -d DELIM End on DELIM char, not newline |
| 12811 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12812 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12813 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12814 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12815 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12816 | char *opt_n = NULL; |
| 12817 | char *opt_p = NULL; |
| 12818 | char *opt_t = NULL; |
| 12819 | char *opt_u = NULL; |
| 12820 | int read_flags = 0; |
| 12821 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12822 | int i; |
| 12823 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12824 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12825 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12826 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12827 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12828 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12829 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12830 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12831 | break; |
| 12832 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12833 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12834 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12835 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12836 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12837 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12838 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12839 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12840 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12841 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12842 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12843 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12844 | default: |
| 12845 | break; |
| 12846 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12847 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12848 | |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 12849 | /* "read -s" needs to save/restore termios, can't allow ^C |
| 12850 | * to jump out of it. |
| 12851 | */ |
| 12852 | INT_OFF; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 12853 | r = shell_builtin_read(setvar2, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12854 | argptr, |
| 12855 | bltinlookup("IFS"), /* can be NULL */ |
| 12856 | read_flags, |
| 12857 | opt_n, |
| 12858 | opt_p, |
| 12859 | opt_t, |
| 12860 | opt_u |
| 12861 | ); |
Denys Vlasenko | 9e71e3c | 2012-09-06 13:28:10 +0200 | [diff] [blame] | 12862 | INT_ON; |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12863 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12864 | if ((uintptr_t)r > 1) |
| 12865 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12866 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12867 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12868 | } |
| 12869 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12870 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12871 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12872 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12873 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12874 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12875 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12876 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12877 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12878 | S_IROTH, S_IWOTH, S_IXOTH |
| 12879 | }; |
| 12880 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12881 | /* TODO: use bb_parse_mode() instead */ |
| 12882 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12883 | char *ap; |
| 12884 | mode_t mask; |
| 12885 | int i; |
| 12886 | int symbolic_mode = 0; |
| 12887 | |
| 12888 | while (nextopt("S") != '\0') { |
| 12889 | symbolic_mode = 1; |
| 12890 | } |
| 12891 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12892 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12893 | mask = umask(0); |
| 12894 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12895 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12896 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12897 | ap = *argptr; |
| 12898 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12899 | if (symbolic_mode) { |
| 12900 | char buf[18]; |
| 12901 | char *p = buf; |
| 12902 | |
| 12903 | for (i = 0; i < 3; i++) { |
| 12904 | int j; |
| 12905 | |
| 12906 | *p++ = permuser[i]; |
| 12907 | *p++ = '='; |
| 12908 | for (j = 0; j < 3; j++) { |
| 12909 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12910 | *p++ = permmode[j]; |
| 12911 | } |
| 12912 | } |
| 12913 | *p++ = ','; |
| 12914 | } |
| 12915 | *--p = 0; |
| 12916 | puts(buf); |
| 12917 | } else { |
| 12918 | out1fmt("%.4o\n", mask); |
| 12919 | } |
| 12920 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12921 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12922 | mask = 0; |
| 12923 | do { |
| 12924 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12925 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12926 | mask = (mask << 3) + (*ap - '0'); |
| 12927 | } while (*++ap != '\0'); |
| 12928 | umask(mask); |
| 12929 | } else { |
| 12930 | mask = ~mask & 0777; |
| 12931 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12932 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12933 | } |
| 12934 | umask(~mask & 0777); |
| 12935 | } |
| 12936 | } |
| 12937 | return 0; |
| 12938 | } |
| 12939 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12940 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12941 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12942 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12943 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12944 | } |
| 12945 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12946 | /* ============ main() and helpers */ |
| 12947 | |
| 12948 | /* |
| 12949 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12950 | */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12951 | static void |
| 12952 | exitshell(void) |
| 12953 | { |
| 12954 | struct jmploc loc; |
| 12955 | char *p; |
| 12956 | int status; |
| 12957 | |
Denys Vlasenko | bede215 | 2011-09-04 16:12:33 +0200 | [diff] [blame] | 12958 | #if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT |
| 12959 | save_history(line_input_state); |
| 12960 | #endif |
| 12961 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12962 | status = exitstatus; |
| 12963 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12964 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12965 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12966 | /* dash bug: it just does _exit(exitstatus) here |
| 12967 | * but we have to do setjobctl(0) first! |
| 12968 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12969 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12970 | status = exitstatus; |
| 12971 | goto out; |
| 12972 | } |
| 12973 | exception_handler = &loc; |
| 12974 | p = trap[0]; |
| 12975 | if (p) { |
| 12976 | trap[0] = NULL; |
| 12977 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 12978 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12979 | } |
| 12980 | flush_stdout_stderr(); |
| 12981 | out: |
| 12982 | setjobctl(0); |
| 12983 | _exit(status); |
| 12984 | /* NOTREACHED */ |
| 12985 | } |
| 12986 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12987 | static void |
| 12988 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12989 | { |
| 12990 | /* from input.c: */ |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 12991 | /* we will never free this */ |
| 12992 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12993 | |
| 12994 | /* from trap.c: */ |
| 12995 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 12996 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 12997 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 12998 | */ |
Denys Vlasenko | cacb2cd | 2010-10-05 00:13:02 +0200 | [diff] [blame] | 12999 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13000 | |
| 13001 | /* from var.c: */ |
| 13002 | { |
| 13003 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13004 | const char *p; |
| 13005 | struct stat st1, st2; |
| 13006 | |
| 13007 | initvar(); |
| 13008 | for (envp = environ; envp && *envp; envp++) { |
| 13009 | if (strchr(*envp, '=')) { |
| 13010 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 13011 | } |
| 13012 | } |
| 13013 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 13014 | setvar("PPID", utoa(getppid()), 0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13015 | |
| 13016 | p = lookupvar("PWD"); |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13017 | if (p) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13018 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13019 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino |
| 13020 | ) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13021 | p = '\0'; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13022 | } |
| 13023 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13024 | setpwd(p, 0); |
| 13025 | } |
| 13026 | } |
| 13027 | |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13028 | |
| 13029 | //usage:#define ash_trivial_usage |
Denys Vlasenko | 6b6af53 | 2011-03-08 10:24:17 +0100 | [diff] [blame] | 13030 | //usage: "[-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]" |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13031 | //usage:#define ash_full_usage "\n\n" |
| 13032 | //usage: "Unix shell interpreter" |
| 13033 | |
| 13034 | //usage:#if ENABLE_FEATURE_SH_IS_ASH |
| 13035 | //usage:# define sh_trivial_usage ash_trivial_usage |
| 13036 | //usage:# define sh_full_usage ash_full_usage |
| 13037 | //usage:#endif |
| 13038 | //usage:#if ENABLE_FEATURE_BASH_IS_ASH |
| 13039 | //usage:# define bash_trivial_usage ash_trivial_usage |
| 13040 | //usage:# define bash_full_usage ash_full_usage |
| 13041 | //usage:#endif |
| 13042 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13043 | /* |
| 13044 | * Process the shell command line arguments. |
| 13045 | */ |
| 13046 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13047 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13048 | { |
| 13049 | int i; |
| 13050 | const char *xminusc; |
| 13051 | char **xargv; |
| 13052 | |
| 13053 | xargv = argv; |
| 13054 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13055 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13056 | xargv++; |
| 13057 | for (i = 0; i < NOPTS; i++) |
| 13058 | optlist[i] = 2; |
| 13059 | argptr = xargv; |
Denys Vlasenko | b0b8343 | 2011-03-07 12:34:59 +0100 | [diff] [blame] | 13060 | if (options(/*cmdline:*/ 1)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 13061 | /* it already printed err message */ |
| 13062 | raise_exception(EXERROR); |
| 13063 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13064 | xargv = argptr; |
| 13065 | xminusc = minusc; |
| 13066 | if (*xargv == NULL) { |
| 13067 | if (xminusc) |
| 13068 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 13069 | sflag = 1; |
| 13070 | } |
| 13071 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 13072 | iflag = 1; |
| 13073 | if (mflag == 2) |
| 13074 | mflag = iflag; |
| 13075 | for (i = 0; i < NOPTS; i++) |
| 13076 | if (optlist[i] == 2) |
| 13077 | optlist[i] = 0; |
| 13078 | #if DEBUG == 2 |
| 13079 | debug = 1; |
| 13080 | #endif |
| 13081 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 13082 | if (xminusc) { |
| 13083 | minusc = *xargv++; |
| 13084 | if (*xargv) |
| 13085 | goto setarg0; |
| 13086 | } else if (!sflag) { |
| 13087 | setinputfile(*xargv, 0); |
| 13088 | setarg0: |
| 13089 | arg0 = *xargv++; |
| 13090 | commandname = arg0; |
| 13091 | } |
| 13092 | |
| 13093 | shellparam.p = xargv; |
| 13094 | #if ENABLE_ASH_GETOPTS |
| 13095 | shellparam.optind = 1; |
| 13096 | shellparam.optoff = -1; |
| 13097 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13098 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13099 | while (*xargv) { |
| 13100 | shellparam.nparam++; |
| 13101 | xargv++; |
| 13102 | } |
| 13103 | optschanged(); |
| 13104 | } |
| 13105 | |
| 13106 | /* |
| 13107 | * Read /etc/profile or .profile. |
| 13108 | */ |
| 13109 | static void |
| 13110 | read_profile(const char *name) |
| 13111 | { |
| 13112 | int skip; |
| 13113 | |
| 13114 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 13115 | return; |
| 13116 | skip = cmdloop(0); |
| 13117 | popfile(); |
| 13118 | if (skip) |
| 13119 | exitshell(); |
| 13120 | } |
| 13121 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13122 | /* |
| 13123 | * This routine is called when an error or an interrupt occurs in an |
| 13124 | * interactive shell and control is returned to the main command loop. |
| 13125 | */ |
| 13126 | static void |
| 13127 | reset(void) |
| 13128 | { |
| 13129 | /* from eval.c: */ |
| 13130 | evalskip = 0; |
| 13131 | loopnest = 0; |
| 13132 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 13133 | g_parsefile->left_in_buffer = 0; |
| 13134 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13135 | popallfiles(); |
| 13136 | /* from parser.c: */ |
| 13137 | tokpushback = 0; |
| 13138 | checkkwd = 0; |
| 13139 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 13140 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13141 | } |
| 13142 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13143 | #if PROFILE |
| 13144 | static short profile_buf[16384]; |
| 13145 | extern int etext(); |
| 13146 | #endif |
| 13147 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 13148 | /* |
| 13149 | * Main routine. We initialize things, parse the arguments, execute |
| 13150 | * profiles if we're a login shell, and then call cmdloop to execute |
| 13151 | * commands. The setjmp call sets up the location to jump to when an |
| 13152 | * exception occurs. When an exception occurs the variable "state" |
| 13153 | * is used to figure out how far we had gotten. |
| 13154 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 13155 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 13156 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13157 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 13158 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13159 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13160 | struct jmploc jmploc; |
| 13161 | struct stackmark smark; |
| 13162 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13163 | /* Initialize global data */ |
| 13164 | INIT_G_misc(); |
| 13165 | INIT_G_memstack(); |
| 13166 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13167 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13168 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 13169 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 13170 | INIT_G_cmdtable(); |
| 13171 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13172 | #if PROFILE |
| 13173 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13174 | #endif |
| 13175 | |
| 13176 | #if ENABLE_FEATURE_EDITING |
| 13177 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13178 | #endif |
| 13179 | state = 0; |
| 13180 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13181 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13182 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13183 | |
| 13184 | reset(); |
| 13185 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13186 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13187 | if (e == EXERROR) |
| 13188 | exitstatus = 2; |
| 13189 | s = state; |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13190 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13191 | exitshell(); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13192 | } |
| 13193 | if (e == EXINT) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13194 | outcslow('\n', stderr); |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13195 | } |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13196 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13197 | popstackmark(&smark); |
| 13198 | FORCE_INT_ON; /* enable interrupts */ |
| 13199 | if (s == 1) |
| 13200 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13201 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13202 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13203 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13204 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13205 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13206 | } |
| 13207 | exception_handler = &jmploc; |
| 13208 | #if DEBUG |
| 13209 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 13210 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13211 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13212 | #endif |
| 13213 | rootpid = getpid(); |
| 13214 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13215 | init(); |
| 13216 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13217 | procargs(argv); |
| 13218 | |
Denys Vlasenko | 6088e13 | 2010-12-25 23:58:42 +0100 | [diff] [blame] | 13219 | if (argv[0] && argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13220 | isloginsh = 1; |
| 13221 | if (isloginsh) { |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 13222 | const char *hp; |
| 13223 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13224 | state = 1; |
| 13225 | read_profile("/etc/profile"); |
| 13226 | state1: |
| 13227 | state = 2; |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 13228 | hp = lookupvar("HOME"); |
| 13229 | if (hp) { |
| 13230 | hp = concat_path_file(hp, ".profile"); |
| 13231 | read_profile(hp); |
| 13232 | free((char*)hp); |
| 13233 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13234 | } |
| 13235 | state2: |
| 13236 | state = 3; |
| 13237 | if ( |
| 13238 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13239 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13240 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13241 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13242 | ) { |
| 13243 | shinit = lookupvar("ENV"); |
| 13244 | if (shinit != NULL && *shinit != '\0') { |
| 13245 | read_profile(shinit); |
| 13246 | } |
| 13247 | } |
| 13248 | state3: |
| 13249 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13250 | if (minusc) { |
| 13251 | /* evalstring pushes parsefile stack. |
| 13252 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13253 | * is one of stacked source fds. |
| 13254 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 13255 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 13256 | // ^^ not necessary since now we special-case fd 0 |
| 13257 | // in is_hidden_fd() to not be considered "hidden fd" |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13258 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13259 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13260 | |
| 13261 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 4840ae8 | 2011-09-04 15:28:03 +0200 | [diff] [blame] | 13262 | #if MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13263 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13264 | const char *hp = lookupvar("HISTFILE"); |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 13265 | if (!hp) { |
| 13266 | hp = lookupvar("HOME"); |
Stefan Hellermann | 4ef1439 | 2013-03-15 02:45:50 +0100 | [diff] [blame] | 13267 | if (hp) { |
Stefan Hellermann | aeb717a | 2013-03-03 15:29:32 +0100 | [diff] [blame] | 13268 | hp = concat_path_file(hp, ".ash_history"); |
| 13269 | setvar("HISTFILE", hp, 0); |
| 13270 | free((char*)hp); |
| 13271 | hp = lookupvar("HISTFILE"); |
| 13272 | } |
| 13273 | } |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13274 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13275 | line_input_state->hist_file = hp; |
Denys Vlasenko | 2c4de5b | 2011-03-31 13:16:52 +0200 | [diff] [blame] | 13276 | # if ENABLE_FEATURE_SH_HISTFILESIZE |
| 13277 | hp = lookupvar("HISTFILESIZE"); |
| 13278 | line_input_state->max_history = size_from_HISTFILESIZE(hp); |
| 13279 | # endif |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13280 | } |
| 13281 | #endif |
| 13282 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13283 | cmdloop(1); |
| 13284 | } |
| 13285 | #if PROFILE |
| 13286 | monitor(0); |
| 13287 | #endif |
| 13288 | #ifdef GPROF |
| 13289 | { |
| 13290 | extern void _mcleanup(void); |
| 13291 | _mcleanup(); |
| 13292 | } |
| 13293 | #endif |
Denys Vlasenko | b563f62 | 2010-09-25 17:15:13 +0200 | [diff] [blame] | 13294 | TRACE(("End of main reached\n")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13295 | exitshell(); |
| 13296 | /* NOTREACHED */ |
| 13297 | } |
| 13298 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13299 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13300 | /*- |
| 13301 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13302 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13303 | * |
| 13304 | * This code is derived from software contributed to Berkeley by |
| 13305 | * Kenneth Almquist. |
| 13306 | * |
| 13307 | * Redistribution and use in source and binary forms, with or without |
| 13308 | * modification, are permitted provided that the following conditions |
| 13309 | * are met: |
| 13310 | * 1. Redistributions of source code must retain the above copyright |
| 13311 | * notice, this list of conditions and the following disclaimer. |
| 13312 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13313 | * notice, this list of conditions and the following disclaimer in the |
| 13314 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13315 | * 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] | 13316 | * may be used to endorse or promote products derived from this software |
| 13317 | * without specific prior written permission. |
| 13318 | * |
| 13319 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13320 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13321 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13322 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13323 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13324 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13325 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13326 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13327 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13328 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13329 | * SUCH DAMAGE. |
| 13330 | */ |