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 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 16 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
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 | * |
| 26 | * When debugging is on, debugging info will be written to ./trace and |
| 27 | * a quit signal will generate a core dump. |
| 28 | */ |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 29 | #define DEBUG 0 |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 30 | /* Tweak debug output verbosity here */ |
| 31 | #define DEBUG_TIME 0 |
| 32 | #define DEBUG_PID 1 |
| 33 | #define DEBUG_SIG 1 |
| 34 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 35 | #define PROFILE 0 |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 37 | #define JOBS ENABLE_ASH_JOB_CONTROL |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 39 | #include "busybox.h" /* for applet_names */ |
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 | |
| 45 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 46 | #include "math.h" |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 47 | #if ENABLE_ASH_RANDOM_SUPPORT |
| 48 | # include "random.h" |
Denys Vlasenko | 36df048 | 2009-10-19 16:07:28 +0200 | [diff] [blame] | 49 | #else |
| 50 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 51 | #endif |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 52 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 53 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 54 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 55 | /* STANDALONE does not make sense, and won't compile */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 56 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 57 | # undef ENABLE_FEATURE_SH_STANDALONE |
| 58 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 59 | # undef IF_NOT_FEATURE_SH_STANDALONE |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 60 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
| 61 | # define IF_FEATURE_SH_STANDALONE(...) |
| 62 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 65 | #ifndef PIPE_BUF |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 66 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Denys Vlasenko | 153fcaa | 2010-02-21 05:17:41 +0100 | [diff] [blame] | 69 | #if !BB_MMU |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 70 | # error "Do not even bother, ash will not run on NOMMU machine" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 73 | //applet:IF_ASH(APPLET(ash, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 74 | //applet:IF_FEATURE_SH_IS_ASH(APPLET_ODDNAME(sh, ash, _BB_DIR_BIN, _BB_SUID_DROP, sh)) |
| 75 | //applet:IF_FEATURE_BASH_IS_ASH(APPLET_ODDNAME(bash, ash, _BB_DIR_BIN, _BB_SUID_DROP, bash)) |
| 76 | |
| 77 | //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o |
| 78 | //kbuild:lib-$(CONFIG_ASH_RANDOM_SUPPORT) += random.o |
| 79 | |
| 80 | //config:config ASH |
| 81 | //config: bool "ash" |
| 82 | //config: default y |
| 83 | //config: depends on !NOMMU |
| 84 | //config: help |
| 85 | //config: Tha 'ash' shell adds about 60k in the default configuration and is |
| 86 | //config: the most complete and most pedantically correct shell included with |
| 87 | //config: busybox. This shell is actually a derivative of the Debian 'dash' |
| 88 | //config: shell (by Herbert Xu), which was created by porting the 'ash' shell |
| 89 | //config: (written by Kenneth Almquist) from NetBSD. |
| 90 | //config: |
| 91 | //config:config ASH_BASH_COMPAT |
| 92 | //config: bool "bash-compatible extensions" |
| 93 | //config: default y |
| 94 | //config: depends on ASH |
| 95 | //config: help |
| 96 | //config: Enable bash-compatible extensions. |
| 97 | //config: |
| 98 | //config:config ASH_JOB_CONTROL |
| 99 | //config: bool "Job control" |
| 100 | //config: default y |
| 101 | //config: depends on ASH |
| 102 | //config: help |
| 103 | //config: Enable job control in the ash shell. |
| 104 | //config: |
| 105 | //config:config ASH_ALIAS |
| 106 | //config: bool "alias support" |
| 107 | //config: default y |
| 108 | //config: depends on ASH |
| 109 | //config: help |
| 110 | //config: Enable alias support in the ash shell. |
| 111 | //config: |
| 112 | //config:config ASH_GETOPTS |
| 113 | //config: bool "Builtin getopt to parse positional parameters" |
| 114 | //config: default y |
| 115 | //config: depends on ASH |
| 116 | //config: help |
| 117 | //config: Enable getopts builtin in the ash shell. |
| 118 | //config: |
| 119 | //config:config ASH_BUILTIN_ECHO |
| 120 | //config: bool "Builtin version of 'echo'" |
| 121 | //config: default y |
| 122 | //config: depends on ASH |
| 123 | //config: help |
| 124 | //config: Enable support for echo, builtin to ash. |
| 125 | //config: |
| 126 | //config:config ASH_BUILTIN_PRINTF |
| 127 | //config: bool "Builtin version of 'printf'" |
| 128 | //config: default y |
| 129 | //config: depends on ASH |
| 130 | //config: help |
| 131 | //config: Enable support for printf, builtin to ash. |
| 132 | //config: |
| 133 | //config:config ASH_BUILTIN_TEST |
| 134 | //config: bool "Builtin version of 'test'" |
| 135 | //config: default y |
| 136 | //config: depends on ASH |
| 137 | //config: help |
| 138 | //config: Enable support for test, builtin to ash. |
| 139 | //config: |
| 140 | //config:config ASH_CMDCMD |
| 141 | //config: bool "'command' command to override shell builtins" |
| 142 | //config: default y |
| 143 | //config: depends on ASH |
| 144 | //config: help |
| 145 | //config: Enable support for the ash 'command' builtin, which allows |
| 146 | //config: you to run the specified command with the specified arguments, |
| 147 | //config: even when there is an ash builtin command with the same name. |
| 148 | //config: |
| 149 | //config:config ASH_MAIL |
| 150 | //config: bool "Check for new mail on interactive shells" |
| 151 | //config: default n |
| 152 | //config: depends on ASH |
| 153 | //config: help |
| 154 | //config: Enable "check for new mail" in the ash shell. |
| 155 | //config: |
| 156 | //config:config ASH_OPTIMIZE_FOR_SIZE |
| 157 | //config: bool "Optimize for size instead of speed" |
| 158 | //config: default y |
| 159 | //config: depends on ASH |
| 160 | //config: help |
| 161 | //config: Compile ash for reduced size at the price of speed. |
| 162 | //config: |
| 163 | //config:config ASH_RANDOM_SUPPORT |
| 164 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 165 | //config: default y |
| 166 | //config: depends on ASH |
| 167 | //config: help |
| 168 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 169 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 170 | //config: You can reset the generator by using a specified start value. |
| 171 | //config: After "unset RANDOM" the generator will switch off and this |
| 172 | //config: variable will no longer have special treatment. |
| 173 | //config: |
| 174 | //config:config ASH_EXPAND_PRMT |
| 175 | //config: bool "Expand prompt string" |
| 176 | //config: default y |
| 177 | //config: depends on ASH |
| 178 | //config: help |
| 179 | //config: "PS#" may contain volatile content, such as backquote commands. |
| 180 | //config: This option recreates the prompt string from the environment |
| 181 | //config: variable each time it is displayed. |
Denys Vlasenko | 51ca776 | 2010-07-16 17:16:40 +0200 | [diff] [blame] | 182 | //config: |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 183 | |
| 184 | //usage:#define ash_trivial_usage NOUSAGE_STR |
| 185 | //usage:#define ash_full_usage "" |
| 186 | //usage:#define sh_trivial_usage NOUSAGE_STR |
| 187 | //usage:#define sh_full_usage "" |
| 188 | //usage:#define bash_trivial_usage NOUSAGE_STR |
| 189 | //usage:#define bash_full_usage "" |
| 190 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 191 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 192 | /* ============ Hash table sizes. Configurable. */ |
| 193 | |
| 194 | #define VTABSIZE 39 |
| 195 | #define ATABSIZE 39 |
| 196 | #define CMDTABLESIZE 31 /* should be prime */ |
| 197 | |
| 198 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 199 | /* ============ Shell options */ |
| 200 | |
| 201 | static const char *const optletters_optnames[] = { |
| 202 | "e" "errexit", |
| 203 | "f" "noglob", |
| 204 | "I" "ignoreeof", |
| 205 | "i" "interactive", |
| 206 | "m" "monitor", |
| 207 | "n" "noexec", |
| 208 | "s" "stdin", |
| 209 | "x" "xtrace", |
| 210 | "v" "verbose", |
| 211 | "C" "noclobber", |
| 212 | "a" "allexport", |
| 213 | "b" "notify", |
| 214 | "u" "nounset", |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 215 | "\0" "vi" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 216 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | e9ac32a | 2009-12-05 02:01:25 +0100 | [diff] [blame] | 217 | ,"\0" "pipefail" |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 218 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 219 | #if DEBUG |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 220 | ,"\0" "nolog" |
| 221 | ,"\0" "debug" |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 222 | #endif |
| 223 | }; |
| 224 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 225 | #define optletters(n) optletters_optnames[n][0] |
| 226 | #define optnames(n) (optletters_optnames[n] + 1) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 227 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 228 | enum { NOPTS = ARRAY_SIZE(optletters_optnames) }; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 229 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 230 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 231 | /* ============ Misc data */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 232 | |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 233 | #define msg_illnum "Illegal number: %s" |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 234 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 235 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 236 | * We enclose jmp_buf in a structure so that we can declare pointers to |
| 237 | * jump locations. The global variable handler contains the location to |
Denis Vlasenko | f173395 | 2009-03-19 23:21:55 +0000 | [diff] [blame] | 238 | * jump to when an exception occurs, and the global variable exception_type |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 239 | * contains a code identifying the exception. To implement nested |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 240 | * exception handlers, the user should save the value of handler on entry |
| 241 | * to an inner scope, set handler to point to a jmploc structure for the |
| 242 | * inner scope, and restore handler on exit from the scope. |
| 243 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 244 | struct jmploc { |
| 245 | jmp_buf loc; |
| 246 | }; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 247 | |
| 248 | struct globals_misc { |
| 249 | /* pid of main shell */ |
| 250 | int rootpid; |
| 251 | /* shell level: 0 for the main shell, 1 for its children, and so on */ |
| 252 | int shlvl; |
| 253 | #define rootshell (!shlvl) |
| 254 | char *minusc; /* argument to -c option */ |
| 255 | |
| 256 | char *curdir; // = nullstr; /* current working directory */ |
| 257 | char *physdir; // = nullstr; /* physical working directory */ |
| 258 | |
| 259 | char *arg0; /* value of $0 */ |
| 260 | |
| 261 | struct jmploc *exception_handler; |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 262 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 263 | volatile int suppress_int; /* counter */ |
| 264 | volatile /*sig_atomic_t*/ smallint pending_int; /* 1 = got SIGINT */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 265 | /* last pending signal */ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 266 | volatile /*sig_atomic_t*/ smallint pending_sig; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 267 | smallint exception_type; /* kind of exception (0..5) */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 268 | /* exceptions */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 269 | #define EXINT 0 /* SIGINT received */ |
| 270 | #define EXERROR 1 /* a generic error */ |
| 271 | #define EXSHELLPROC 2 /* execute a shell procedure */ |
| 272 | #define EXEXEC 3 /* command execution failed */ |
| 273 | #define EXEXIT 4 /* exit the shell */ |
| 274 | #define EXSIG 5 /* trapped signal in wait(1) */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 275 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 276 | smallint isloginsh; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 277 | char nullstr[1]; /* zero length string */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 278 | |
| 279 | char optlist[NOPTS]; |
| 280 | #define eflag optlist[0] |
| 281 | #define fflag optlist[1] |
| 282 | #define Iflag optlist[2] |
| 283 | #define iflag optlist[3] |
| 284 | #define mflag optlist[4] |
| 285 | #define nflag optlist[5] |
| 286 | #define sflag optlist[6] |
| 287 | #define xflag optlist[7] |
| 288 | #define vflag optlist[8] |
| 289 | #define Cflag optlist[9] |
| 290 | #define aflag optlist[10] |
| 291 | #define bflag optlist[11] |
| 292 | #define uflag optlist[12] |
| 293 | #define viflag optlist[13] |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 294 | #if ENABLE_ASH_BASH_COMPAT |
| 295 | # define pipefail optlist[14] |
| 296 | #else |
| 297 | # define pipefail 0 |
| 298 | #endif |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 299 | #if DEBUG |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 300 | # define nolog optlist[14 + ENABLE_ASH_BASH_COMPAT] |
| 301 | # define debug optlist[15 + ENABLE_ASH_BASH_COMPAT] |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 302 | #endif |
| 303 | |
| 304 | /* trap handler commands */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 305 | /* |
| 306 | * Sigmode records the current value of the signal handlers for the various |
| 307 | * 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] | 308 | * 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] | 309 | */ |
| 310 | char sigmode[NSIG - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 311 | #define S_DFL 1 /* default signal handling (SIG_DFL) */ |
| 312 | #define S_CATCH 2 /* signal is caught */ |
| 313 | #define S_IGN 3 /* signal is ignored (SIG_IGN) */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 314 | #define S_HARD_IGN 4 /* signal is ignored permenantly */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 315 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 316 | /* indicates specified signal received */ |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 317 | uint8_t gotsig[NSIG - 1]; /* offset by 1: "signal" 0 is meaningless */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 318 | 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] | 319 | char *trap[NSIG]; |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 320 | char **trap_ptr; /* used only by "trap hack" */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 321 | |
| 322 | /* Rarely referenced stuff */ |
| 323 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 324 | random_t random_gen; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 325 | #endif |
| 326 | pid_t backgndpid; /* pid of last background process */ |
| 327 | 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] | 328 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 329 | extern struct globals_misc *const ash_ptr_to_globals_misc; |
| 330 | #define G_misc (*ash_ptr_to_globals_misc) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 331 | #define rootpid (G_misc.rootpid ) |
| 332 | #define shlvl (G_misc.shlvl ) |
| 333 | #define minusc (G_misc.minusc ) |
| 334 | #define curdir (G_misc.curdir ) |
| 335 | #define physdir (G_misc.physdir ) |
| 336 | #define arg0 (G_misc.arg0 ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 337 | #define exception_handler (G_misc.exception_handler) |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 338 | #define exception_type (G_misc.exception_type ) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 339 | #define suppress_int (G_misc.suppress_int ) |
| 340 | #define pending_int (G_misc.pending_int ) |
| 341 | #define pending_sig (G_misc.pending_sig ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 342 | #define isloginsh (G_misc.isloginsh ) |
| 343 | #define nullstr (G_misc.nullstr ) |
| 344 | #define optlist (G_misc.optlist ) |
| 345 | #define sigmode (G_misc.sigmode ) |
| 346 | #define gotsig (G_misc.gotsig ) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 347 | #define may_have_traps (G_misc.may_have_traps ) |
Denis Vlasenko | 26bc57d | 2008-06-27 00:29:34 +0000 | [diff] [blame] | 348 | #define trap (G_misc.trap ) |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 349 | #define trap_ptr (G_misc.trap_ptr ) |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 350 | #define random_gen (G_misc.random_gen ) |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 351 | #define backgndpid (G_misc.backgndpid ) |
| 352 | #define job_warning (G_misc.job_warning) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 353 | #define INIT_G_misc() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 354 | (*(struct globals_misc**)&ash_ptr_to_globals_misc) = xzalloc(sizeof(G_misc)); \ |
| 355 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 356 | curdir = nullstr; \ |
| 357 | physdir = nullstr; \ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 358 | trap_ptr = trap; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 359 | } while (0) |
| 360 | |
| 361 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 362 | /* ============ DEBUG */ |
| 363 | #if DEBUG |
| 364 | static void trace_printf(const char *fmt, ...); |
| 365 | static void trace_vprintf(const char *fmt, va_list va); |
| 366 | # define TRACE(param) trace_printf param |
| 367 | # define TRACEV(param) trace_vprintf param |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 368 | # define close(fd) do { \ |
| 369 | int dfd = (fd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 370 | if (close(dfd) < 0) \ |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 371 | bb_error_msg("bug on %d: closing %d(0x%x)", \ |
Denis Vlasenko | 1bb3d7e | 2009-03-20 07:45:36 +0000 | [diff] [blame] | 372 | __LINE__, dfd, dfd); \ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 373 | } while (0) |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 374 | #else |
| 375 | # define TRACE(param) |
| 376 | # define TRACEV(param) |
| 377 | #endif |
| 378 | |
| 379 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 380 | /* ============ Utility functions */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 381 | #define xbarrier() do { __asm__ __volatile__ ("": : :"memory"); } while (0) |
| 382 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 383 | static int isdigit_str9(const char *str) |
| 384 | { |
| 385 | int maxlen = 9 + 1; /* max 9 digits: 999999999 */ |
| 386 | while (--maxlen && isdigit(*str)) |
| 387 | str++; |
| 388 | return (*str == '\0'); |
| 389 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 390 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 391 | static const char *var_end(const char *var) |
| 392 | { |
| 393 | while (*var) |
| 394 | if (*var++ == '=') |
| 395 | break; |
| 396 | return var; |
| 397 | } |
| 398 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 399 | |
| 400 | /* ============ Interrupts / exceptions */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 401 | /* |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 402 | * These macros allow the user to suspend the handling of interrupt signals |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 403 | * 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] | 404 | * much more efficient and portable. (But hacking the kernel is so much |
| 405 | * more fun than worrying about efficiency and portability. :-)) |
| 406 | */ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 407 | #define INT_OFF do { \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 408 | suppress_int++; \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 409 | xbarrier(); \ |
| 410 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 411 | |
| 412 | /* |
| 413 | * Called to raise an exception. Since C doesn't include exceptions, we |
| 414 | * 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] | 415 | * stored in the global variable "exception_type". |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 416 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 417 | static void raise_exception(int) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 418 | static void |
| 419 | raise_exception(int e) |
| 420 | { |
| 421 | #if DEBUG |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 422 | if (exception_handler == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 423 | abort(); |
| 424 | #endif |
| 425 | INT_OFF; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 426 | exception_type = e; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 427 | longjmp(exception_handler->loc, 1); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 428 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 429 | #if DEBUG |
| 430 | #define raise_exception(e) do { \ |
| 431 | TRACE(("raising exception %d on line %d\n", (e), __LINE__)); \ |
| 432 | raise_exception(e); \ |
| 433 | } while (0) |
| 434 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * Called from trap.c when a SIGINT is received. (If the user specifies |
| 438 | * that SIGINT is to be trapped or ignored using the trap builtin, then |
| 439 | * this routine is not called.) Suppressint is nonzero when interrupts |
| 440 | * are held using the INT_OFF macro. (The test for iflag is just |
| 441 | * defensive programming.) |
| 442 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 443 | static void raise_interrupt(void) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 444 | static void |
| 445 | raise_interrupt(void) |
| 446 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 447 | int ex_type; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 448 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 449 | pending_int = 0; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 450 | /* Signal is not automatically unmasked after it is raised, |
| 451 | * do it ourself - unmask all signals */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 452 | sigprocmask_allsigs(SIG_UNBLOCK); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 453 | /* pending_sig = 0; - now done in signal_handler() */ |
Denis Vlasenko | 7c139b4 | 2007-03-21 20:17:27 +0000 | [diff] [blame] | 454 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 455 | ex_type = EXSIG; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 456 | if (gotsig[SIGINT - 1] && !trap[SIGINT]) { |
| 457 | if (!(rootshell && iflag)) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 458 | /* Kill ourself with SIGINT */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 459 | signal(SIGINT, SIG_DFL); |
| 460 | raise(SIGINT); |
| 461 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 462 | ex_type = EXINT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 463 | } |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 464 | raise_exception(ex_type); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 465 | /* NOTREACHED */ |
| 466 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 467 | #if DEBUG |
| 468 | #define raise_interrupt() do { \ |
| 469 | TRACE(("raising interrupt on line %d\n", __LINE__)); \ |
| 470 | raise_interrupt(); \ |
| 471 | } while (0) |
| 472 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 473 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 474 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 475 | int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 476 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 477 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 478 | if (--suppress_int == 0 && pending_int) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 479 | raise_interrupt(); |
| 480 | } |
| 481 | } |
| 482 | #define INT_ON int_on() |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 483 | static IF_ASH_OPTIMIZE_FOR_SIZE(inline) void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 484 | force_int_on(void) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 485 | { |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 486 | xbarrier(); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 487 | suppress_int = 0; |
| 488 | if (pending_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 489 | raise_interrupt(); |
| 490 | } |
| 491 | #define FORCE_INT_ON force_int_on() |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 492 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 493 | #define SAVE_INT(v) ((v) = suppress_int) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 494 | |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 495 | #define RESTORE_INT(v) do { \ |
| 496 | xbarrier(); \ |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 497 | suppress_int = (v); \ |
| 498 | if (suppress_int == 0 && pending_int) \ |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 499 | raise_interrupt(); \ |
| 500 | } while (0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 501 | |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 502 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 503 | /* ============ Stdout/stderr output */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 504 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 505 | static void |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 506 | outstr(const char *p, FILE *file) |
Denis Vlasenko | e5570da | 2007-02-19 22:41:55 +0000 | [diff] [blame] | 507 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 508 | INT_OFF; |
| 509 | fputs(p, file); |
| 510 | INT_ON; |
| 511 | } |
| 512 | |
| 513 | static void |
| 514 | flush_stdout_stderr(void) |
| 515 | { |
| 516 | INT_OFF; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 517 | fflush_all(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 518 | INT_ON; |
| 519 | } |
| 520 | |
| 521 | static void |
| 522 | outcslow(int c, FILE *dest) |
| 523 | { |
| 524 | INT_OFF; |
| 525 | putc(c, dest); |
| 526 | fflush(dest); |
| 527 | INT_ON; |
| 528 | } |
| 529 | |
| 530 | static int out1fmt(const char *, ...) __attribute__((__format__(__printf__,1,2))); |
| 531 | static int |
| 532 | out1fmt(const char *fmt, ...) |
| 533 | { |
| 534 | va_list ap; |
| 535 | int r; |
| 536 | |
| 537 | INT_OFF; |
| 538 | va_start(ap, fmt); |
| 539 | r = vprintf(fmt, ap); |
| 540 | va_end(ap); |
| 541 | INT_ON; |
| 542 | return r; |
| 543 | } |
| 544 | |
| 545 | static int fmtstr(char *, size_t, const char *, ...) __attribute__((__format__(__printf__,3,4))); |
| 546 | static int |
| 547 | fmtstr(char *outbuf, size_t length, const char *fmt, ...) |
| 548 | { |
| 549 | va_list ap; |
| 550 | int ret; |
| 551 | |
| 552 | va_start(ap, fmt); |
| 553 | INT_OFF; |
| 554 | ret = vsnprintf(outbuf, length, fmt, ap); |
| 555 | va_end(ap); |
| 556 | INT_ON; |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | out1str(const char *p) |
| 562 | { |
| 563 | outstr(p, stdout); |
| 564 | } |
| 565 | |
| 566 | static void |
| 567 | out2str(const char *p) |
| 568 | { |
| 569 | outstr(p, stderr); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 570 | flush_stdout_stderr(); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 574 | /* ============ Parser structures */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 575 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 576 | /* control characters in argument strings */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 577 | #define CTL_FIRST CTLESC |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 578 | #define CTLESC ((unsigned char)'\201') /* escape next character */ |
| 579 | #define CTLVAR ((unsigned char)'\202') /* variable defn */ |
| 580 | #define CTLENDVAR ((unsigned char)'\203') |
| 581 | #define CTLBACKQ ((unsigned char)'\204') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 582 | #define CTLQUOTE 01 /* ored with CTLBACKQ code if in quotes */ |
| 583 | /* CTLBACKQ | CTLQUOTE == '\205' */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 584 | #define CTLARI ((unsigned char)'\206') /* arithmetic expression */ |
| 585 | #define CTLENDARI ((unsigned char)'\207') |
| 586 | #define CTLQUOTEMARK ((unsigned char)'\210') |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 587 | #define CTL_LAST CTLQUOTEMARK |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 588 | |
| 589 | /* variable substitution byte (follows CTLVAR) */ |
| 590 | #define VSTYPE 0x0f /* type of variable substitution */ |
| 591 | #define VSNUL 0x10 /* colon--treat the empty string as unset */ |
| 592 | #define VSQUOTE 0x80 /* inside double quotes--suppress splitting */ |
| 593 | |
| 594 | /* values of VSTYPE field */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 595 | #define VSNORMAL 0x1 /* normal variable: $var or ${var} */ |
| 596 | #define VSMINUS 0x2 /* ${var-text} */ |
| 597 | #define VSPLUS 0x3 /* ${var+text} */ |
| 598 | #define VSQUESTION 0x4 /* ${var?message} */ |
| 599 | #define VSASSIGN 0x5 /* ${var=text} */ |
| 600 | #define VSTRIMRIGHT 0x6 /* ${var%pattern} */ |
| 601 | #define VSTRIMRIGHTMAX 0x7 /* ${var%%pattern} */ |
| 602 | #define VSTRIMLEFT 0x8 /* ${var#pattern} */ |
| 603 | #define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */ |
| 604 | #define VSLENGTH 0xa /* ${#var} */ |
| 605 | #if ENABLE_ASH_BASH_COMPAT |
| 606 | #define VSSUBSTR 0xc /* ${var:position:length} */ |
| 607 | #define VSREPLACE 0xd /* ${var/pattern/replacement} */ |
| 608 | #define VSREPLACEALL 0xe /* ${var//pattern/replacement} */ |
| 609 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 611 | static const char dolatstr[] ALIGN1 = { |
| 612 | CTLVAR, VSNORMAL|VSQUOTE, '@', '=', '\0' |
| 613 | }; |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 614 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 615 | #define NCMD 0 |
| 616 | #define NPIPE 1 |
| 617 | #define NREDIR 2 |
| 618 | #define NBACKGND 3 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 619 | #define NSUBSHELL 4 |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 620 | #define NAND 5 |
| 621 | #define NOR 6 |
| 622 | #define NSEMI 7 |
| 623 | #define NIF 8 |
| 624 | #define NWHILE 9 |
| 625 | #define NUNTIL 10 |
| 626 | #define NFOR 11 |
| 627 | #define NCASE 12 |
| 628 | #define NCLIST 13 |
| 629 | #define NDEFUN 14 |
| 630 | #define NARG 15 |
| 631 | #define NTO 16 |
| 632 | #if ENABLE_ASH_BASH_COMPAT |
| 633 | #define NTO2 17 |
| 634 | #endif |
| 635 | #define NCLOBBER 18 |
| 636 | #define NFROM 19 |
| 637 | #define NFROMTO 20 |
| 638 | #define NAPPEND 21 |
| 639 | #define NTOFD 22 |
| 640 | #define NFROMFD 23 |
| 641 | #define NHERE 24 |
| 642 | #define NXHERE 25 |
| 643 | #define NNOT 26 |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 644 | #define N_NUMBER 27 |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 645 | |
| 646 | union node; |
| 647 | |
| 648 | struct ncmd { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 649 | smallint type; /* Nxxxx */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 650 | union node *assign; |
| 651 | union node *args; |
| 652 | union node *redirect; |
| 653 | }; |
| 654 | |
| 655 | struct npipe { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 656 | smallint type; |
| 657 | smallint pipe_backgnd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 658 | struct nodelist *cmdlist; |
| 659 | }; |
| 660 | |
| 661 | struct nredir { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 662 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 663 | union node *n; |
| 664 | union node *redirect; |
| 665 | }; |
| 666 | |
| 667 | struct nbinary { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 668 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 669 | union node *ch1; |
| 670 | union node *ch2; |
| 671 | }; |
| 672 | |
| 673 | struct nif { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 674 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 675 | union node *test; |
| 676 | union node *ifpart; |
| 677 | union node *elsepart; |
| 678 | }; |
| 679 | |
| 680 | struct nfor { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 681 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 682 | union node *args; |
| 683 | union node *body; |
| 684 | char *var; |
| 685 | }; |
| 686 | |
| 687 | struct ncase { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 688 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 689 | union node *expr; |
| 690 | union node *cases; |
| 691 | }; |
| 692 | |
| 693 | struct nclist { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 694 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 695 | union node *next; |
| 696 | union node *pattern; |
| 697 | union node *body; |
| 698 | }; |
| 699 | |
| 700 | struct narg { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 701 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 702 | union node *next; |
| 703 | char *text; |
| 704 | struct nodelist *backquote; |
| 705 | }; |
| 706 | |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 707 | /* nfile and ndup layout must match! |
| 708 | * NTOFD (>&fdnum) uses ndup structure, but we may discover mid-flight |
| 709 | * that it is actually NTO2 (>&file), and change its type. |
| 710 | */ |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 711 | struct nfile { |
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 | int fd; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 715 | int _unused_dupfd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 716 | union node *fname; |
| 717 | char *expfname; |
| 718 | }; |
| 719 | |
| 720 | struct ndup { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 721 | smallint type; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 722 | union node *next; |
| 723 | int fd; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 724 | int dupfd; |
| 725 | union node *vname; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 726 | char *_unused_expfname; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 727 | }; |
| 728 | |
| 729 | struct nhere { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 730 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 731 | union node *next; |
| 732 | int fd; |
| 733 | union node *doc; |
| 734 | }; |
| 735 | |
| 736 | struct nnot { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 737 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 738 | union node *com; |
| 739 | }; |
| 740 | |
| 741 | union node { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 742 | smallint type; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 743 | struct ncmd ncmd; |
| 744 | struct npipe npipe; |
| 745 | struct nredir nredir; |
| 746 | struct nbinary nbinary; |
| 747 | struct nif nif; |
| 748 | struct nfor nfor; |
| 749 | struct ncase ncase; |
| 750 | struct nclist nclist; |
| 751 | struct narg narg; |
| 752 | struct nfile nfile; |
| 753 | struct ndup ndup; |
| 754 | struct nhere nhere; |
| 755 | struct nnot nnot; |
| 756 | }; |
| 757 | |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 758 | /* |
| 759 | * NODE_EOF is returned by parsecmd when it encounters an end of file. |
| 760 | * It must be distinct from NULL. |
| 761 | */ |
| 762 | #define NODE_EOF ((union node *) -1L) |
| 763 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 764 | struct nodelist { |
| 765 | struct nodelist *next; |
| 766 | union node *n; |
| 767 | }; |
| 768 | |
| 769 | struct funcnode { |
| 770 | int count; |
| 771 | union node n; |
| 772 | }; |
| 773 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 774 | /* |
| 775 | * Free a parse tree. |
| 776 | */ |
| 777 | static void |
| 778 | freefunc(struct funcnode *f) |
| 779 | { |
| 780 | if (f && --f->count < 0) |
| 781 | free(f); |
| 782 | } |
| 783 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 784 | |
| 785 | /* ============ Debugging output */ |
| 786 | |
| 787 | #if DEBUG |
| 788 | |
| 789 | static FILE *tracefile; |
| 790 | |
| 791 | static void |
| 792 | trace_printf(const char *fmt, ...) |
| 793 | { |
| 794 | va_list va; |
| 795 | |
| 796 | if (debug != 1) |
| 797 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 798 | if (DEBUG_TIME) |
| 799 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 800 | if (DEBUG_PID) |
| 801 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 802 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 803 | 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] | 804 | va_start(va, fmt); |
| 805 | vfprintf(tracefile, fmt, va); |
| 806 | va_end(va); |
| 807 | } |
| 808 | |
| 809 | static void |
| 810 | trace_vprintf(const char *fmt, va_list va) |
| 811 | { |
| 812 | if (debug != 1) |
| 813 | return; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 814 | if (DEBUG_TIME) |
| 815 | fprintf(tracefile, "%u ", (int) time(NULL)); |
| 816 | if (DEBUG_PID) |
| 817 | fprintf(tracefile, "[%u] ", (int) getpid()); |
| 818 | if (DEBUG_SIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 819 | 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] | 820 | vfprintf(tracefile, fmt, va); |
| 821 | } |
| 822 | |
| 823 | static void |
| 824 | trace_puts(const char *s) |
| 825 | { |
| 826 | if (debug != 1) |
| 827 | return; |
| 828 | fputs(s, tracefile); |
| 829 | } |
| 830 | |
| 831 | static void |
| 832 | trace_puts_quoted(char *s) |
| 833 | { |
| 834 | char *p; |
| 835 | char c; |
| 836 | |
| 837 | if (debug != 1) |
| 838 | return; |
| 839 | putc('"', tracefile); |
| 840 | for (p = s; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 841 | switch ((unsigned char)*p) { |
| 842 | case '\n': c = 'n'; goto backslash; |
| 843 | case '\t': c = 't'; goto backslash; |
| 844 | case '\r': c = 'r'; goto backslash; |
| 845 | case '\"': c = '\"'; goto backslash; |
| 846 | case '\\': c = '\\'; goto backslash; |
| 847 | case CTLESC: c = 'e'; goto backslash; |
| 848 | case CTLVAR: c = 'v'; goto backslash; |
| 849 | case CTLVAR+CTLQUOTE: c = 'V'; goto backslash; |
| 850 | case CTLBACKQ: c = 'q'; goto backslash; |
| 851 | case CTLBACKQ+CTLQUOTE: c = 'Q'; goto backslash; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 852 | backslash: |
| 853 | putc('\\', tracefile); |
| 854 | putc(c, tracefile); |
| 855 | break; |
| 856 | default: |
| 857 | if (*p >= ' ' && *p <= '~') |
| 858 | putc(*p, tracefile); |
| 859 | else { |
| 860 | putc('\\', tracefile); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 861 | putc((*p >> 6) & 03, tracefile); |
| 862 | putc((*p >> 3) & 07, tracefile); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 863 | putc(*p & 07, tracefile); |
| 864 | } |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | putc('"', tracefile); |
| 869 | } |
| 870 | |
| 871 | static void |
| 872 | trace_puts_args(char **ap) |
| 873 | { |
| 874 | if (debug != 1) |
| 875 | return; |
| 876 | if (!*ap) |
| 877 | return; |
| 878 | while (1) { |
| 879 | trace_puts_quoted(*ap); |
| 880 | if (!*++ap) { |
| 881 | putc('\n', tracefile); |
| 882 | break; |
| 883 | } |
| 884 | putc(' ', tracefile); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | static void |
| 889 | opentrace(void) |
| 890 | { |
| 891 | char s[100]; |
| 892 | #ifdef O_APPEND |
| 893 | int flags; |
| 894 | #endif |
| 895 | |
| 896 | if (debug != 1) { |
| 897 | if (tracefile) |
| 898 | fflush(tracefile); |
| 899 | /* leave open because libedit might be using it */ |
| 900 | return; |
| 901 | } |
| 902 | strcpy(s, "./trace"); |
| 903 | if (tracefile) { |
| 904 | if (!freopen(s, "a", tracefile)) { |
| 905 | fprintf(stderr, "Can't re-open %s\n", s); |
| 906 | debug = 0; |
| 907 | return; |
| 908 | } |
| 909 | } else { |
| 910 | tracefile = fopen(s, "a"); |
| 911 | if (tracefile == NULL) { |
| 912 | fprintf(stderr, "Can't open %s\n", s); |
| 913 | debug = 0; |
| 914 | return; |
| 915 | } |
| 916 | } |
| 917 | #ifdef O_APPEND |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 918 | flags = fcntl(fileno(tracefile), F_GETFL); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 919 | if (flags >= 0) |
| 920 | fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND); |
| 921 | #endif |
| 922 | setlinebuf(tracefile); |
| 923 | fputs("\nTracing started.\n", tracefile); |
| 924 | } |
| 925 | |
| 926 | static void |
| 927 | indent(int amount, char *pfx, FILE *fp) |
| 928 | { |
| 929 | int i; |
| 930 | |
| 931 | for (i = 0; i < amount; i++) { |
| 932 | if (pfx && i == amount - 1) |
| 933 | fputs(pfx, fp); |
| 934 | putc('\t', fp); |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /* little circular references here... */ |
| 939 | static void shtree(union node *n, int ind, char *pfx, FILE *fp); |
| 940 | |
| 941 | static void |
| 942 | sharg(union node *arg, FILE *fp) |
| 943 | { |
| 944 | char *p; |
| 945 | struct nodelist *bqlist; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 946 | unsigned char subtype; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 947 | |
| 948 | if (arg->type != NARG) { |
| 949 | out1fmt("<node type %d>\n", arg->type); |
| 950 | abort(); |
| 951 | } |
| 952 | bqlist = arg->narg.backquote; |
| 953 | for (p = arg->narg.text; *p; p++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 954 | switch ((unsigned char)*p) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 955 | case CTLESC: |
| 956 | putc(*++p, fp); |
| 957 | break; |
| 958 | case CTLVAR: |
| 959 | putc('$', fp); |
| 960 | putc('{', fp); |
| 961 | subtype = *++p; |
| 962 | if (subtype == VSLENGTH) |
| 963 | putc('#', fp); |
| 964 | |
| 965 | while (*p != '=') |
| 966 | putc(*p++, fp); |
| 967 | |
| 968 | if (subtype & VSNUL) |
| 969 | putc(':', fp); |
| 970 | |
| 971 | switch (subtype & VSTYPE) { |
| 972 | case VSNORMAL: |
| 973 | putc('}', fp); |
| 974 | break; |
| 975 | case VSMINUS: |
| 976 | putc('-', fp); |
| 977 | break; |
| 978 | case VSPLUS: |
| 979 | putc('+', fp); |
| 980 | break; |
| 981 | case VSQUESTION: |
| 982 | putc('?', fp); |
| 983 | break; |
| 984 | case VSASSIGN: |
| 985 | putc('=', fp); |
| 986 | break; |
| 987 | case VSTRIMLEFT: |
| 988 | putc('#', fp); |
| 989 | break; |
| 990 | case VSTRIMLEFTMAX: |
| 991 | putc('#', fp); |
| 992 | putc('#', fp); |
| 993 | break; |
| 994 | case VSTRIMRIGHT: |
| 995 | putc('%', fp); |
| 996 | break; |
| 997 | case VSTRIMRIGHTMAX: |
| 998 | putc('%', fp); |
| 999 | putc('%', fp); |
| 1000 | break; |
| 1001 | case VSLENGTH: |
| 1002 | break; |
| 1003 | default: |
| 1004 | out1fmt("<subtype %d>", subtype); |
| 1005 | } |
| 1006 | break; |
| 1007 | case CTLENDVAR: |
| 1008 | putc('}', fp); |
| 1009 | break; |
| 1010 | case CTLBACKQ: |
| 1011 | case CTLBACKQ|CTLQUOTE: |
| 1012 | putc('$', fp); |
| 1013 | putc('(', fp); |
| 1014 | shtree(bqlist->n, -1, NULL, fp); |
| 1015 | putc(')', fp); |
| 1016 | break; |
| 1017 | default: |
| 1018 | putc(*p, fp); |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | } |
| 1023 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1024 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1025 | shcmd(union node *cmd, FILE *fp) |
| 1026 | { |
| 1027 | union node *np; |
| 1028 | int first; |
| 1029 | const char *s; |
| 1030 | int dftfd; |
| 1031 | |
| 1032 | first = 1; |
| 1033 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1034 | if (!first) |
| 1035 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1036 | sharg(np, fp); |
| 1037 | first = 0; |
| 1038 | } |
| 1039 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1040 | if (!first) |
| 1041 | putc(' ', fp); |
| 1042 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1043 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1044 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1045 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1046 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1047 | #if ENABLE_ASH_BASH_COMPAT |
| 1048 | case NTO2: |
| 1049 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1050 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1051 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1052 | case NFROMFD: s = "<&"; break; |
| 1053 | case NFROMTO: s = "<>"; break; |
| 1054 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1055 | } |
| 1056 | if (np->nfile.fd != dftfd) |
| 1057 | fprintf(fp, "%d", np->nfile.fd); |
| 1058 | fputs(s, fp); |
| 1059 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1060 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1061 | } else { |
| 1062 | sharg(np->nfile.fname, fp); |
| 1063 | } |
| 1064 | first = 0; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | static void |
| 1069 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1070 | { |
| 1071 | struct nodelist *lp; |
| 1072 | const char *s; |
| 1073 | |
| 1074 | if (n == NULL) |
| 1075 | return; |
| 1076 | |
| 1077 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1078 | |
| 1079 | if (n == NODE_EOF) { |
| 1080 | fputs("<EOF>", fp); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1084 | switch (n->type) { |
| 1085 | case NSEMI: |
| 1086 | s = "; "; |
| 1087 | goto binop; |
| 1088 | case NAND: |
| 1089 | s = " && "; |
| 1090 | goto binop; |
| 1091 | case NOR: |
| 1092 | s = " || "; |
| 1093 | binop: |
| 1094 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1095 | /* if (ind < 0) */ |
| 1096 | fputs(s, fp); |
| 1097 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1098 | break; |
| 1099 | case NCMD: |
| 1100 | shcmd(n, fp); |
| 1101 | if (ind >= 0) |
| 1102 | putc('\n', fp); |
| 1103 | break; |
| 1104 | case NPIPE: |
| 1105 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1106 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1107 | if (lp->next) |
| 1108 | fputs(" | ", fp); |
| 1109 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1110 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1111 | fputs(" &", fp); |
| 1112 | if (ind >= 0) |
| 1113 | putc('\n', fp); |
| 1114 | break; |
| 1115 | default: |
| 1116 | fprintf(fp, "<node type %d>", n->type); |
| 1117 | if (ind >= 0) |
| 1118 | putc('\n', fp); |
| 1119 | break; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | static void |
| 1124 | showtree(union node *n) |
| 1125 | { |
| 1126 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1127 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1130 | #endif /* DEBUG */ |
| 1131 | |
| 1132 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1133 | /* ============ Parser data */ |
| 1134 | |
| 1135 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1136 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1137 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1138 | struct strlist { |
| 1139 | struct strlist *next; |
| 1140 | char *text; |
| 1141 | }; |
| 1142 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1143 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1144 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1145 | struct strpush { |
| 1146 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1147 | char *prev_string; |
| 1148 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1149 | #if ENABLE_ASH_ALIAS |
| 1150 | struct alias *ap; /* if push was associated with an alias */ |
| 1151 | #endif |
| 1152 | char *string; /* remember the string since it may change */ |
| 1153 | }; |
| 1154 | |
| 1155 | struct parsefile { |
| 1156 | struct parsefile *prev; /* preceding file on stack */ |
| 1157 | int linno; /* current line */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1158 | int pf_fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1159 | int left_in_line; /* number of chars left in this line */ |
| 1160 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1161 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1162 | char *buf; /* input buffer */ |
| 1163 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1164 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1165 | }; |
| 1166 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1167 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1168 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1169 | static int startlinno; /* line # where last token started */ |
| 1170 | static char *commandname; /* currently executing command */ |
| 1171 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1172 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1173 | |
| 1174 | |
| 1175 | /* ============ Message printing */ |
| 1176 | |
| 1177 | static void |
| 1178 | ash_vmsg(const char *msg, va_list ap) |
| 1179 | { |
| 1180 | fprintf(stderr, "%s: ", arg0); |
| 1181 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1182 | if (strcmp(arg0, commandname)) |
| 1183 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1184 | if (!iflag || g_parsefile->pf_fd > 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1185 | fprintf(stderr, "line %d: ", startlinno); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1186 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1187 | vfprintf(stderr, msg, ap); |
| 1188 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1189 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1190 | |
| 1191 | /* |
| 1192 | * Exverror is called to raise the error exception. If the second argument |
| 1193 | * is not NULL then error prints an error message using printf style |
| 1194 | * formatting. It then raises the error exception. |
| 1195 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1196 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1197 | static void |
| 1198 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1199 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1200 | #if DEBUG |
| 1201 | if (msg) { |
| 1202 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1203 | TRACEV((msg, ap)); |
| 1204 | TRACE(("\") pid=%d\n", getpid())); |
| 1205 | } else |
| 1206 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1207 | if (msg) |
| 1208 | #endif |
| 1209 | ash_vmsg(msg, ap); |
| 1210 | |
| 1211 | flush_stdout_stderr(); |
| 1212 | raise_exception(cond); |
| 1213 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1214 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1215 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1216 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1217 | static void |
| 1218 | ash_msg_and_raise_error(const char *msg, ...) |
| 1219 | { |
| 1220 | va_list ap; |
| 1221 | |
| 1222 | va_start(ap, msg); |
| 1223 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1224 | /* NOTREACHED */ |
| 1225 | va_end(ap); |
| 1226 | } |
| 1227 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1228 | static void raise_error_syntax(const char *) NORETURN; |
| 1229 | static void |
| 1230 | raise_error_syntax(const char *msg) |
| 1231 | { |
| 1232 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1233 | /* NOTREACHED */ |
| 1234 | } |
| 1235 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1236 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1237 | static void |
| 1238 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1239 | { |
| 1240 | va_list ap; |
| 1241 | |
| 1242 | va_start(ap, msg); |
| 1243 | ash_vmsg_and_raise(cond, msg, ap); |
| 1244 | /* NOTREACHED */ |
| 1245 | va_end(ap); |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * error/warning routines for external builtins |
| 1250 | */ |
| 1251 | static void |
| 1252 | ash_msg(const char *fmt, ...) |
| 1253 | { |
| 1254 | va_list ap; |
| 1255 | |
| 1256 | va_start(ap, fmt); |
| 1257 | ash_vmsg(fmt, ap); |
| 1258 | va_end(ap); |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Return a string describing an error. The returned string may be a |
| 1263 | * pointer to a static buffer that will be overwritten on the next call. |
| 1264 | * Action describes the operation that got the error. |
| 1265 | */ |
| 1266 | static const char * |
| 1267 | errmsg(int e, const char *em) |
| 1268 | { |
| 1269 | if (e == ENOENT || e == ENOTDIR) { |
| 1270 | return em; |
| 1271 | } |
| 1272 | return strerror(e); |
| 1273 | } |
| 1274 | |
| 1275 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1276 | /* ============ Memory allocation */ |
| 1277 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1278 | #if 0 |
| 1279 | /* I consider these wrappers nearly useless: |
| 1280 | * ok, they return you to nearest exception handler, but |
| 1281 | * how much memory do you leak in the process, making |
| 1282 | * memory starvation worse? |
| 1283 | */ |
| 1284 | static void * |
| 1285 | ckrealloc(void * p, size_t nbytes) |
| 1286 | { |
| 1287 | p = realloc(p, nbytes); |
| 1288 | if (!p) |
| 1289 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1290 | return p; |
| 1291 | } |
| 1292 | |
| 1293 | static void * |
| 1294 | ckmalloc(size_t nbytes) |
| 1295 | { |
| 1296 | return ckrealloc(NULL, nbytes); |
| 1297 | } |
| 1298 | |
| 1299 | static void * |
| 1300 | ckzalloc(size_t nbytes) |
| 1301 | { |
| 1302 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1303 | } |
| 1304 | |
| 1305 | static char * |
| 1306 | ckstrdup(const char *s) |
| 1307 | { |
| 1308 | char *p = strdup(s); |
| 1309 | if (!p) |
| 1310 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1311 | return p; |
| 1312 | } |
| 1313 | #else |
| 1314 | /* Using bbox equivalents. They exit if out of memory */ |
| 1315 | # define ckrealloc xrealloc |
| 1316 | # define ckmalloc xmalloc |
| 1317 | # define ckzalloc xzalloc |
| 1318 | # define ckstrdup xstrdup |
| 1319 | #endif |
| 1320 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1321 | /* |
| 1322 | * It appears that grabstackstr() will barf with such alignments |
| 1323 | * because stalloc() will return a string allocated in a new stackblock. |
| 1324 | */ |
| 1325 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1326 | enum { |
| 1327 | /* Most machines require the value returned from malloc to be aligned |
| 1328 | * in some way. The following macro will get this right |
| 1329 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1330 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1331 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1332 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1333 | }; |
| 1334 | |
| 1335 | struct stack_block { |
| 1336 | struct stack_block *prev; |
| 1337 | char space[MINSIZE]; |
| 1338 | }; |
| 1339 | |
| 1340 | struct stackmark { |
| 1341 | struct stack_block *stackp; |
| 1342 | char *stacknxt; |
| 1343 | size_t stacknleft; |
| 1344 | struct stackmark *marknext; |
| 1345 | }; |
| 1346 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1347 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1348 | struct globals_memstack { |
| 1349 | struct stack_block *g_stackp; // = &stackbase; |
| 1350 | struct stackmark *markp; |
| 1351 | char *g_stacknxt; // = stackbase.space; |
| 1352 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1353 | size_t g_stacknleft; // = MINSIZE; |
| 1354 | int herefd; // = -1; |
| 1355 | struct stack_block stackbase; |
| 1356 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1357 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1358 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1359 | #define g_stackp (G_memstack.g_stackp ) |
| 1360 | #define markp (G_memstack.markp ) |
| 1361 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1362 | #define sstrend (G_memstack.sstrend ) |
| 1363 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1364 | #define herefd (G_memstack.herefd ) |
| 1365 | #define stackbase (G_memstack.stackbase ) |
| 1366 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1367 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1368 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1369 | g_stackp = &stackbase; \ |
| 1370 | g_stacknxt = stackbase.space; \ |
| 1371 | g_stacknleft = MINSIZE; \ |
| 1372 | sstrend = stackbase.space + MINSIZE; \ |
| 1373 | herefd = -1; \ |
| 1374 | } while (0) |
| 1375 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1376 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1377 | #define stackblock() ((void *)g_stacknxt) |
| 1378 | #define stackblocksize() g_stacknleft |
| 1379 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1380 | /* |
| 1381 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1382 | * to make this more efficient, and also to avoid all sorts of exception |
| 1383 | * handling code to handle interrupts in the middle of a parse. |
| 1384 | * |
| 1385 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1386 | * well. |
| 1387 | */ |
| 1388 | static void * |
| 1389 | stalloc(size_t nbytes) |
| 1390 | { |
| 1391 | char *p; |
| 1392 | size_t aligned; |
| 1393 | |
| 1394 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1395 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1396 | size_t len; |
| 1397 | size_t blocksize; |
| 1398 | struct stack_block *sp; |
| 1399 | |
| 1400 | blocksize = aligned; |
| 1401 | if (blocksize < MINSIZE) |
| 1402 | blocksize = MINSIZE; |
| 1403 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1404 | if (len < blocksize) |
| 1405 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1406 | INT_OFF; |
| 1407 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1408 | sp->prev = g_stackp; |
| 1409 | g_stacknxt = sp->space; |
| 1410 | g_stacknleft = blocksize; |
| 1411 | sstrend = g_stacknxt + blocksize; |
| 1412 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1413 | INT_ON; |
| 1414 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1415 | p = g_stacknxt; |
| 1416 | g_stacknxt += aligned; |
| 1417 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1418 | return p; |
| 1419 | } |
| 1420 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1421 | static void * |
| 1422 | stzalloc(size_t nbytes) |
| 1423 | { |
| 1424 | return memset(stalloc(nbytes), 0, nbytes); |
| 1425 | } |
| 1426 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1427 | static void |
| 1428 | stunalloc(void *p) |
| 1429 | { |
| 1430 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1431 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1432 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1433 | abort(); |
| 1434 | } |
| 1435 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1436 | g_stacknleft += g_stacknxt - (char *)p; |
| 1437 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1440 | /* |
| 1441 | * Like strdup but works with the ash stack. |
| 1442 | */ |
| 1443 | static char * |
| 1444 | ststrdup(const char *p) |
| 1445 | { |
| 1446 | size_t len = strlen(p) + 1; |
| 1447 | return memcpy(stalloc(len), p, len); |
| 1448 | } |
| 1449 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1450 | static void |
| 1451 | setstackmark(struct stackmark *mark) |
| 1452 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1453 | mark->stackp = g_stackp; |
| 1454 | mark->stacknxt = g_stacknxt; |
| 1455 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1456 | mark->marknext = markp; |
| 1457 | markp = mark; |
| 1458 | } |
| 1459 | |
| 1460 | static void |
| 1461 | popstackmark(struct stackmark *mark) |
| 1462 | { |
| 1463 | struct stack_block *sp; |
| 1464 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1465 | if (!mark->stackp) |
| 1466 | return; |
| 1467 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1468 | INT_OFF; |
| 1469 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1470 | while (g_stackp != mark->stackp) { |
| 1471 | sp = g_stackp; |
| 1472 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1473 | free(sp); |
| 1474 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1475 | g_stacknxt = mark->stacknxt; |
| 1476 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1477 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1478 | INT_ON; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
| 1482 | * When the parser reads in a string, it wants to stick the string on the |
| 1483 | * stack and only adjust the stack pointer when it knows how big the |
| 1484 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1485 | * of space on top of the stack and stackblocklen returns the length of |
| 1486 | * this block. Growstackblock will grow this space by at least one byte, |
| 1487 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1488 | * part of the block that has been used. |
| 1489 | */ |
| 1490 | static void |
| 1491 | growstackblock(void) |
| 1492 | { |
| 1493 | size_t newlen; |
| 1494 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1495 | newlen = g_stacknleft * 2; |
| 1496 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1497 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1498 | if (newlen < 128) |
| 1499 | newlen += 128; |
| 1500 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1501 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1502 | struct stack_block *oldstackp; |
| 1503 | struct stackmark *xmark; |
| 1504 | struct stack_block *sp; |
| 1505 | struct stack_block *prevstackp; |
| 1506 | size_t grosslen; |
| 1507 | |
| 1508 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1509 | oldstackp = g_stackp; |
| 1510 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1511 | prevstackp = sp->prev; |
| 1512 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1513 | sp = ckrealloc(sp, grosslen); |
| 1514 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1515 | g_stackp = sp; |
| 1516 | g_stacknxt = sp->space; |
| 1517 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1518 | sstrend = sp->space + newlen; |
| 1519 | |
| 1520 | /* |
| 1521 | * Stack marks pointing to the start of the old block |
| 1522 | * must be relocated to point to the new block |
| 1523 | */ |
| 1524 | xmark = markp; |
| 1525 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1526 | xmark->stackp = g_stackp; |
| 1527 | xmark->stacknxt = g_stacknxt; |
| 1528 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1529 | xmark = xmark->marknext; |
| 1530 | } |
| 1531 | INT_ON; |
| 1532 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1533 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1534 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1535 | char *p = stalloc(newlen); |
| 1536 | |
| 1537 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1538 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1539 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | static void |
| 1544 | grabstackblock(size_t len) |
| 1545 | { |
| 1546 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1547 | g_stacknxt += len; |
| 1548 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | /* |
| 1552 | * The following routines are somewhat easier to use than the above. |
| 1553 | * The user declares a variable of type STACKSTR, which may be declared |
| 1554 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1555 | * the user uses the macro STPUTC to add characters to the string. In |
| 1556 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1557 | * grown as necessary. When the user is done, she can just leave the |
| 1558 | * string there and refer to it using stackblock(). Or she can allocate |
| 1559 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1560 | * someone else to use the stack temporarily and then continue to grow |
| 1561 | * the string, the user should use grabstack to allocate the space, and |
| 1562 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1563 | * |
| 1564 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1565 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1566 | * is space for at least one character. |
| 1567 | */ |
| 1568 | static void * |
| 1569 | growstackstr(void) |
| 1570 | { |
| 1571 | size_t len = stackblocksize(); |
| 1572 | if (herefd >= 0 && len >= 1024) { |
| 1573 | full_write(herefd, stackblock(), len); |
| 1574 | return stackblock(); |
| 1575 | } |
| 1576 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1577 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * Called from CHECKSTRSPACE. |
| 1582 | */ |
| 1583 | static char * |
| 1584 | makestrspace(size_t newlen, char *p) |
| 1585 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1586 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1587 | size_t size = stackblocksize(); |
| 1588 | |
| 1589 | for (;;) { |
| 1590 | size_t nleft; |
| 1591 | |
| 1592 | size = stackblocksize(); |
| 1593 | nleft = size - len; |
| 1594 | if (nleft >= newlen) |
| 1595 | break; |
| 1596 | growstackblock(); |
| 1597 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1598 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | static char * |
| 1602 | stack_nputstr(const char *s, size_t n, char *p) |
| 1603 | { |
| 1604 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1605 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1606 | return p; |
| 1607 | } |
| 1608 | |
| 1609 | static char * |
| 1610 | stack_putstr(const char *s, char *p) |
| 1611 | { |
| 1612 | return stack_nputstr(s, strlen(s), p); |
| 1613 | } |
| 1614 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1615 | static char * |
| 1616 | _STPUTC(int c, char *p) |
| 1617 | { |
| 1618 | if (p == sstrend) |
| 1619 | p = growstackstr(); |
| 1620 | *p++ = c; |
| 1621 | return p; |
| 1622 | } |
| 1623 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1624 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1625 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1626 | #define CHECKSTRSPACE(n, p) do { \ |
| 1627 | char *q = (p); \ |
| 1628 | size_t l = (n); \ |
| 1629 | size_t m = sstrend - q; \ |
| 1630 | if (l > m) \ |
| 1631 | (p) = makestrspace(l, q); \ |
| 1632 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1633 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1634 | #define STACKSTRNUL(p) do { \ |
| 1635 | if ((p) == sstrend) \ |
| 1636 | (p) = growstackstr(); \ |
| 1637 | *(p) = '\0'; \ |
| 1638 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1639 | #define STUNPUTC(p) (--(p)) |
| 1640 | #define STTOPC(p) ((p)[-1]) |
| 1641 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1642 | |
| 1643 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1644 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1645 | #define stackstrend() ((void *)sstrend) |
| 1646 | |
| 1647 | |
| 1648 | /* ============ String helpers */ |
| 1649 | |
| 1650 | /* |
| 1651 | * prefix -- see if pfx is a prefix of string. |
| 1652 | */ |
| 1653 | static char * |
| 1654 | prefix(const char *string, const char *pfx) |
| 1655 | { |
| 1656 | while (*pfx) { |
| 1657 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1658 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1659 | } |
| 1660 | return (char *) string; |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * Check for a valid number. This should be elsewhere. |
| 1665 | */ |
| 1666 | static int |
| 1667 | is_number(const char *p) |
| 1668 | { |
| 1669 | do { |
| 1670 | if (!isdigit(*p)) |
| 1671 | return 0; |
| 1672 | } while (*++p != '\0'); |
| 1673 | return 1; |
| 1674 | } |
| 1675 | |
| 1676 | /* |
| 1677 | * Convert a string of digits to an integer, printing an error message on |
| 1678 | * failure. |
| 1679 | */ |
| 1680 | static int |
| 1681 | number(const char *s) |
| 1682 | { |
| 1683 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1684 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1685 | return atoi(s); |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1690 | * The return string is allocated on the stack. |
| 1691 | */ |
| 1692 | static char * |
| 1693 | single_quote(const char *s) |
| 1694 | { |
| 1695 | char *p; |
| 1696 | |
| 1697 | STARTSTACKSTR(p); |
| 1698 | |
| 1699 | do { |
| 1700 | char *q; |
| 1701 | size_t len; |
| 1702 | |
| 1703 | len = strchrnul(s, '\'') - s; |
| 1704 | |
| 1705 | q = p = makestrspace(len + 3, p); |
| 1706 | |
| 1707 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1708 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1709 | *q++ = '\''; |
| 1710 | s += len; |
| 1711 | |
| 1712 | STADJUST(q - p, p); |
| 1713 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1714 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1715 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1716 | len = 0; |
| 1717 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1718 | |
| 1719 | q = p = makestrspace(len + 3, p); |
| 1720 | |
| 1721 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1722 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1723 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1724 | |
| 1725 | STADJUST(q - p, p); |
| 1726 | } while (*s); |
| 1727 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1728 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1729 | |
| 1730 | return stackblock(); |
| 1731 | } |
| 1732 | |
| 1733 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1734 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1735 | |
| 1736 | static char **argptr; /* argument list for builtin commands */ |
| 1737 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1738 | static char *optptr; /* used by nextopt */ |
| 1739 | |
| 1740 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1741 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1742 | * The library getopt must have the BSD extension static variable |
| 1743 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1744 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1745 | * Standard option processing (a la getopt) for builtin routines. |
| 1746 | * The only argument that is passed to nextopt is the option string; |
| 1747 | * the other arguments are unnecessary. It returns the character, |
| 1748 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1749 | */ |
| 1750 | static int |
| 1751 | nextopt(const char *optstring) |
| 1752 | { |
| 1753 | char *p; |
| 1754 | const char *q; |
| 1755 | char c; |
| 1756 | |
| 1757 | p = optptr; |
| 1758 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1759 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1760 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1761 | if (p == NULL) |
| 1762 | return '\0'; |
| 1763 | if (*p != '-') |
| 1764 | return '\0'; |
| 1765 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1766 | return '\0'; |
| 1767 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1768 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1769 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1770 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1771 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1772 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1773 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1774 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1775 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1776 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1777 | if (*++q == ':') |
| 1778 | q++; |
| 1779 | } |
| 1780 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1781 | if (*p == '\0') { |
| 1782 | p = *argptr++; |
| 1783 | if (p == NULL) |
| 1784 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1785 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1786 | optionarg = p; |
| 1787 | p = NULL; |
| 1788 | } |
| 1789 | optptr = p; |
| 1790 | return c; |
| 1791 | } |
| 1792 | |
| 1793 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1794 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1795 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1796 | /* |
| 1797 | * The parsefile structure pointed to by the global variable parsefile |
| 1798 | * contains information about the current file being read. |
| 1799 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1800 | struct shparam { |
| 1801 | int nparam; /* # of positional parameters (without $0) */ |
| 1802 | #if ENABLE_ASH_GETOPTS |
| 1803 | int optind; /* next parameter to be processed by getopts */ |
| 1804 | int optoff; /* used by getopts */ |
| 1805 | #endif |
| 1806 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1807 | char **p; /* parameter list */ |
| 1808 | }; |
| 1809 | |
| 1810 | /* |
| 1811 | * Free the list of positional parameters. |
| 1812 | */ |
| 1813 | static void |
| 1814 | freeparam(volatile struct shparam *param) |
| 1815 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1816 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1817 | char **ap, **ap1; |
| 1818 | ap = ap1 = param->p; |
| 1819 | while (*ap) |
| 1820 | free(*ap++); |
| 1821 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1826 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1827 | #endif |
| 1828 | |
| 1829 | struct var { |
| 1830 | struct var *next; /* next entry in hash list */ |
| 1831 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1832 | const char *var_text; /* name=value */ |
| 1833 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1834 | /* the variable gets set/unset */ |
| 1835 | }; |
| 1836 | |
| 1837 | struct localvar { |
| 1838 | struct localvar *next; /* next local variable in list */ |
| 1839 | struct var *vp; /* the variable that was made local */ |
| 1840 | int flags; /* saved flags */ |
| 1841 | const char *text; /* saved text */ |
| 1842 | }; |
| 1843 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1844 | /* flags */ |
| 1845 | #define VEXPORT 0x01 /* variable is exported */ |
| 1846 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1847 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1848 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1849 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1850 | #define VUNSET 0x20 /* the variable is not set */ |
| 1851 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1852 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1853 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1854 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1855 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1856 | #else |
| 1857 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1858 | #endif |
| 1859 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1860 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1861 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1862 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1863 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1864 | change_lc_all(const char *value) |
| 1865 | { |
| 1866 | if (value && *value != '\0') |
| 1867 | setlocale(LC_ALL, value); |
| 1868 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1869 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1870 | change_lc_ctype(const char *value) |
| 1871 | { |
| 1872 | if (value && *value != '\0') |
| 1873 | setlocale(LC_CTYPE, value); |
| 1874 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1875 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1876 | #if ENABLE_ASH_MAIL |
| 1877 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1878 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1879 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1880 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1881 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1882 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1883 | #endif |
| 1884 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1885 | static const struct { |
| 1886 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1887 | const char *var_text; |
| 1888 | void (*var_func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1889 | } varinit_data[] = { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1890 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1891 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1892 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 1893 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1894 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1895 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1896 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1897 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1898 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1899 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1900 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1901 | #endif |
| 1902 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1903 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1904 | #endif |
| 1905 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1906 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 1907 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1908 | #endif |
| 1909 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1910 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1911 | #endif |
| 1912 | }; |
| 1913 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1914 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1915 | |
| 1916 | struct globals_var { |
| 1917 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1918 | struct redirtab *redirlist; |
| 1919 | int g_nullredirs; |
| 1920 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1921 | struct var *vartab[VTABSIZE]; |
| 1922 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1923 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1924 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1925 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1926 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1927 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1928 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1929 | #define preverrout_fd (G_var.preverrout_fd) |
| 1930 | #define vartab (G_var.vartab ) |
| 1931 | #define varinit (G_var.varinit ) |
| 1932 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1933 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1934 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1935 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1936 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1937 | varinit[i].flags = varinit_data[i].flags; \ |
| 1938 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 1939 | varinit[i].var_func = varinit_data[i].var_func; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1940 | } \ |
| 1941 | } while (0) |
| 1942 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1943 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1944 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1945 | # define vmail (&vifs)[1] |
| 1946 | # define vmpath (&vmail)[1] |
| 1947 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1948 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1949 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1950 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1951 | #define vps1 (&vpath)[1] |
| 1952 | #define vps2 (&vps1)[1] |
| 1953 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1954 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1955 | # define voptind (&vps4)[1] |
| 1956 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1957 | # define vrandom (&voptind)[1] |
| 1958 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1959 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1960 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1961 | # define vrandom (&vps4)[1] |
| 1962 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1963 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1964 | |
| 1965 | /* |
| 1966 | * The following macros access the values of the above variables. |
| 1967 | * They have to skip over the name. They return the null string |
| 1968 | * for unset variables. |
| 1969 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1970 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1971 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1972 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1973 | # define mailval() (vmail.var_text + 5) |
| 1974 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1975 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1976 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1977 | #define pathval() (vpath.var_text + 5) |
| 1978 | #define ps1val() (vps1.var_text + 4) |
| 1979 | #define ps2val() (vps2.var_text + 4) |
| 1980 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1981 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1982 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1983 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1984 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1985 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1986 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1987 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
| 1988 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1989 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1990 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1991 | getoptsreset(const char *value) |
| 1992 | { |
| 1993 | shellparam.optind = number(value); |
| 1994 | shellparam.optoff = -1; |
| 1995 | } |
| 1996 | #endif |
| 1997 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1998 | /* |
| 1999 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 2000 | * more letters, underscores, and digits). |
| 2001 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2002 | static char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2003 | endofname(const char *name) |
| 2004 | { |
| 2005 | char *p; |
| 2006 | |
| 2007 | p = (char *) name; |
| 2008 | if (!is_name(*p)) |
| 2009 | return p; |
| 2010 | while (*++p) { |
| 2011 | if (!is_in_name(*p)) |
| 2012 | break; |
| 2013 | } |
| 2014 | return p; |
| 2015 | } |
| 2016 | |
| 2017 | /* |
| 2018 | * Compares two strings up to the first = or '\0'. The first |
| 2019 | * string must be terminated by '='; the second may be terminated by |
| 2020 | * either '=' or '\0'. |
| 2021 | */ |
| 2022 | static int |
| 2023 | varcmp(const char *p, const char *q) |
| 2024 | { |
| 2025 | int c, d; |
| 2026 | |
| 2027 | while ((c = *p) == (d = *q)) { |
| 2028 | if (!c || c == '=') |
| 2029 | goto out; |
| 2030 | p++; |
| 2031 | q++; |
| 2032 | } |
| 2033 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2034 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2035 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2036 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2037 | out: |
| 2038 | return c - d; |
| 2039 | } |
| 2040 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2041 | /* |
| 2042 | * Find the appropriate entry in the hash table from the name. |
| 2043 | */ |
| 2044 | static struct var ** |
| 2045 | hashvar(const char *p) |
| 2046 | { |
| 2047 | unsigned hashval; |
| 2048 | |
| 2049 | hashval = ((unsigned char) *p) << 4; |
| 2050 | while (*p && *p != '=') |
| 2051 | hashval += (unsigned char) *p++; |
| 2052 | return &vartab[hashval % VTABSIZE]; |
| 2053 | } |
| 2054 | |
| 2055 | static int |
| 2056 | vpcmp(const void *a, const void *b) |
| 2057 | { |
| 2058 | return varcmp(*(const char **)a, *(const char **)b); |
| 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * This routine initializes the builtin variables. |
| 2063 | */ |
| 2064 | static void |
| 2065 | initvar(void) |
| 2066 | { |
| 2067 | struct var *vp; |
| 2068 | struct var *end; |
| 2069 | struct var **vpp; |
| 2070 | |
| 2071 | /* |
| 2072 | * PS1 depends on uid |
| 2073 | */ |
| 2074 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2075 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2076 | #else |
| 2077 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2078 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2079 | #endif |
| 2080 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2081 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2082 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2083 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2084 | vp->next = *vpp; |
| 2085 | *vpp = vp; |
| 2086 | } while (++vp < end); |
| 2087 | } |
| 2088 | |
| 2089 | static struct var ** |
| 2090 | findvar(struct var **vpp, const char *name) |
| 2091 | { |
| 2092 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2093 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | return vpp; |
| 2098 | } |
| 2099 | |
| 2100 | /* |
| 2101 | * Find the value of a variable. Returns NULL if not set. |
| 2102 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2103 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2104 | lookupvar(const char *name) |
| 2105 | { |
| 2106 | struct var *v; |
| 2107 | |
| 2108 | v = *findvar(hashvar(name), name); |
| 2109 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2110 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2111 | /* |
| 2112 | * Dynamic variables are implemented roughly the same way they are |
| 2113 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2114 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2115 | * lookup will no longer happen at that point. -- PFM. |
| 2116 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2117 | if (v->flags & VDYNAMIC) |
| 2118 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2119 | #endif |
| 2120 | if (!(v->flags & VUNSET)) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2121 | return var_end(v->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2122 | } |
| 2123 | return NULL; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * Search the environment of a builtin command. |
| 2128 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2129 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2130 | bltinlookup(const char *name) |
| 2131 | { |
| 2132 | struct strlist *sp; |
| 2133 | |
| 2134 | for (sp = cmdenviron; sp; sp = sp->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2135 | if (varcmp(sp->text, name) == 0) |
| 2136 | return var_end(sp->text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2137 | } |
| 2138 | return lookupvar(name); |
| 2139 | } |
| 2140 | |
| 2141 | /* |
| 2142 | * Same as setvar except that the variable and value are passed in |
| 2143 | * the first argument as name=value. Since the first argument will |
| 2144 | * be actually stored in the table, it should not be a string that |
| 2145 | * will go away. |
| 2146 | * Called with interrupts off. |
| 2147 | */ |
| 2148 | static void |
| 2149 | setvareq(char *s, int flags) |
| 2150 | { |
| 2151 | struct var *vp, **vpp; |
| 2152 | |
| 2153 | vpp = hashvar(s); |
| 2154 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2155 | vp = *findvar(vpp, s); |
| 2156 | if (vp) { |
| 2157 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2158 | const char *n; |
| 2159 | |
| 2160 | if (flags & VNOSAVE) |
| 2161 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2162 | n = vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2163 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2164 | } |
| 2165 | |
| 2166 | if (flags & VNOSET) |
| 2167 | return; |
| 2168 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2169 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2170 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2171 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2172 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2173 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2174 | |
| 2175 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2176 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2177 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2178 | if (flags & VNOSET) |
| 2179 | return; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2180 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2181 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2182 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2183 | *vpp = vp; |
| 2184 | } |
| 2185 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2186 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2187 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2188 | vp->flags = flags; |
| 2189 | } |
| 2190 | |
| 2191 | /* |
| 2192 | * Set the value of a variable. The flags argument is ored with the |
| 2193 | * flags of the variable. If val is NULL, the variable is unset. |
| 2194 | */ |
| 2195 | static void |
| 2196 | setvar(const char *name, const char *val, int flags) |
| 2197 | { |
| 2198 | char *p, *q; |
| 2199 | size_t namelen; |
| 2200 | char *nameeq; |
| 2201 | size_t vallen; |
| 2202 | |
| 2203 | q = endofname(name); |
| 2204 | p = strchrnul(q, '='); |
| 2205 | namelen = p - name; |
| 2206 | if (!namelen || p != q) |
| 2207 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2208 | vallen = 0; |
| 2209 | if (val == NULL) { |
| 2210 | flags |= VUNSET; |
| 2211 | } else { |
| 2212 | vallen = strlen(val); |
| 2213 | } |
| 2214 | INT_OFF; |
| 2215 | nameeq = ckmalloc(namelen + vallen + 2); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2216 | p = (char *)memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2217 | if (val) { |
| 2218 | *p++ = '='; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2219 | p = (char *)memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2220 | } |
| 2221 | *p = '\0'; |
| 2222 | setvareq(nameeq, flags | VNOSAVE); |
| 2223 | INT_ON; |
| 2224 | } |
| 2225 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2226 | static void FAST_FUNC |
| 2227 | setvar2(const char *name, const char *val) |
| 2228 | { |
| 2229 | setvar(name, val, 0); |
| 2230 | } |
| 2231 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2232 | #if ENABLE_ASH_GETOPTS |
| 2233 | /* |
| 2234 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2235 | */ |
| 2236 | static int |
| 2237 | setvarsafe(const char *name, const char *val, int flags) |
| 2238 | { |
| 2239 | int err; |
| 2240 | volatile int saveint; |
| 2241 | struct jmploc *volatile savehandler = exception_handler; |
| 2242 | struct jmploc jmploc; |
| 2243 | |
| 2244 | SAVE_INT(saveint); |
| 2245 | if (setjmp(jmploc.loc)) |
| 2246 | err = 1; |
| 2247 | else { |
| 2248 | exception_handler = &jmploc; |
| 2249 | setvar(name, val, flags); |
| 2250 | err = 0; |
| 2251 | } |
| 2252 | exception_handler = savehandler; |
| 2253 | RESTORE_INT(saveint); |
| 2254 | return err; |
| 2255 | } |
| 2256 | #endif |
| 2257 | |
| 2258 | /* |
| 2259 | * Unset the specified variable. |
| 2260 | */ |
| 2261 | static int |
| 2262 | unsetvar(const char *s) |
| 2263 | { |
| 2264 | struct var **vpp; |
| 2265 | struct var *vp; |
| 2266 | int retval; |
| 2267 | |
| 2268 | vpp = findvar(hashvar(s), s); |
| 2269 | vp = *vpp; |
| 2270 | retval = 2; |
| 2271 | if (vp) { |
| 2272 | int flags = vp->flags; |
| 2273 | |
| 2274 | retval = 1; |
| 2275 | if (flags & VREADONLY) |
| 2276 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2277 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2278 | vp->flags &= ~VDYNAMIC; |
| 2279 | #endif |
| 2280 | if (flags & VUNSET) |
| 2281 | goto ok; |
| 2282 | if ((flags & VSTRFIXED) == 0) { |
| 2283 | INT_OFF; |
| 2284 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2285 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2286 | *vpp = vp->next; |
| 2287 | free(vp); |
| 2288 | INT_ON; |
| 2289 | } else { |
| 2290 | setvar(s, 0, 0); |
| 2291 | vp->flags &= ~VEXPORT; |
| 2292 | } |
| 2293 | ok: |
| 2294 | retval = 0; |
| 2295 | } |
| 2296 | out: |
| 2297 | return retval; |
| 2298 | } |
| 2299 | |
| 2300 | /* |
| 2301 | * Process a linked list of variable assignments. |
| 2302 | */ |
| 2303 | static void |
| 2304 | listsetvar(struct strlist *list_set_var, int flags) |
| 2305 | { |
| 2306 | struct strlist *lp = list_set_var; |
| 2307 | |
| 2308 | if (!lp) |
| 2309 | return; |
| 2310 | INT_OFF; |
| 2311 | do { |
| 2312 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2313 | lp = lp->next; |
| 2314 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2315 | INT_ON; |
| 2316 | } |
| 2317 | |
| 2318 | /* |
| 2319 | * Generate a list of variables satisfying the given conditions. |
| 2320 | */ |
| 2321 | static char ** |
| 2322 | listvars(int on, int off, char ***end) |
| 2323 | { |
| 2324 | struct var **vpp; |
| 2325 | struct var *vp; |
| 2326 | char **ep; |
| 2327 | int mask; |
| 2328 | |
| 2329 | STARTSTACKSTR(ep); |
| 2330 | vpp = vartab; |
| 2331 | mask = on | off; |
| 2332 | do { |
| 2333 | for (vp = *vpp; vp; vp = vp->next) { |
| 2334 | if ((vp->flags & mask) == on) { |
| 2335 | if (ep == stackstrend()) |
| 2336 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2337 | *ep++ = (char*)vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2338 | } |
| 2339 | } |
| 2340 | } while (++vpp < vartab + VTABSIZE); |
| 2341 | if (ep == stackstrend()) |
| 2342 | ep = growstackstr(); |
| 2343 | if (end) |
| 2344 | *end = ep; |
| 2345 | *ep++ = NULL; |
| 2346 | return grabstackstr(ep); |
| 2347 | } |
| 2348 | |
| 2349 | |
| 2350 | /* ============ Path search helper |
| 2351 | * |
| 2352 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2353 | * of the path before the first call; path_advance will update |
| 2354 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2355 | * the possible path expansions in sequence. If an option (indicated by |
| 2356 | * a percent sign) appears in the path entry then the global variable |
| 2357 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2358 | * NULL. |
| 2359 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2360 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2361 | |
| 2362 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2363 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2364 | { |
| 2365 | const char *p; |
| 2366 | char *q; |
| 2367 | const char *start; |
| 2368 | size_t len; |
| 2369 | |
| 2370 | if (*path == NULL) |
| 2371 | return NULL; |
| 2372 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2373 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2374 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2375 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2376 | while (stackblocksize() < len) |
| 2377 | growstackblock(); |
| 2378 | q = stackblock(); |
| 2379 | if (p != start) { |
| 2380 | memcpy(q, start, p - start); |
| 2381 | q += p - start; |
| 2382 | *q++ = '/'; |
| 2383 | } |
| 2384 | strcpy(q, name); |
| 2385 | pathopt = NULL; |
| 2386 | if (*p == '%') { |
| 2387 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2388 | while (*p && *p != ':') |
| 2389 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2390 | } |
| 2391 | if (*p == ':') |
| 2392 | *path = p + 1; |
| 2393 | else |
| 2394 | *path = NULL; |
| 2395 | return stalloc(len); |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | /* ============ Prompt */ |
| 2400 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2401 | static smallint doprompt; /* if set, prompt the user */ |
| 2402 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2403 | |
| 2404 | #if ENABLE_FEATURE_EDITING |
| 2405 | static line_input_t *line_input_state; |
| 2406 | static const char *cmdedit_prompt; |
| 2407 | static void |
| 2408 | putprompt(const char *s) |
| 2409 | { |
| 2410 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2411 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2412 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2413 | return; |
| 2414 | } |
| 2415 | cmdedit_prompt = s; |
| 2416 | } |
| 2417 | #else |
| 2418 | static void |
| 2419 | putprompt(const char *s) |
| 2420 | { |
| 2421 | out2str(s); |
| 2422 | } |
| 2423 | #endif |
| 2424 | |
| 2425 | #if ENABLE_ASH_EXPAND_PRMT |
| 2426 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2427 | static const char *expandstr(const char *ps); |
| 2428 | #else |
| 2429 | #define expandstr(s) s |
| 2430 | #endif |
| 2431 | |
| 2432 | static void |
| 2433 | setprompt(int whichprompt) |
| 2434 | { |
| 2435 | const char *prompt; |
| 2436 | #if ENABLE_ASH_EXPAND_PRMT |
| 2437 | struct stackmark smark; |
| 2438 | #endif |
| 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 | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3481 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3482 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3483 | break; |
| 3484 | case S_IGN: |
| 3485 | act.sa_handler = SIG_IGN; |
| 3486 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3487 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3488 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3489 | |
| 3490 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* mode flags for set_curjob */ |
| 3494 | #define CUR_DELETE 2 |
| 3495 | #define CUR_RUNNING 1 |
| 3496 | #define CUR_STOPPED 0 |
| 3497 | |
| 3498 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3499 | #define DOWAIT_NONBLOCK WNOHANG |
| 3500 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3501 | |
| 3502 | #if JOBS |
| 3503 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3504 | static int initialpgrp; //references:2 |
| 3505 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3506 | #endif |
| 3507 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3508 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3509 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3510 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3511 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3512 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3513 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3514 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3515 | |
| 3516 | static void |
| 3517 | set_curjob(struct job *jp, unsigned mode) |
| 3518 | { |
| 3519 | struct job *jp1; |
| 3520 | struct job **jpp, **curp; |
| 3521 | |
| 3522 | /* first remove from list */ |
| 3523 | jpp = curp = &curjob; |
| 3524 | do { |
| 3525 | jp1 = *jpp; |
| 3526 | if (jp1 == jp) |
| 3527 | break; |
| 3528 | jpp = &jp1->prev_job; |
| 3529 | } while (1); |
| 3530 | *jpp = jp1->prev_job; |
| 3531 | |
| 3532 | /* Then re-insert in correct position */ |
| 3533 | jpp = curp; |
| 3534 | switch (mode) { |
| 3535 | default: |
| 3536 | #if DEBUG |
| 3537 | abort(); |
| 3538 | #endif |
| 3539 | case CUR_DELETE: |
| 3540 | /* job being deleted */ |
| 3541 | break; |
| 3542 | case CUR_RUNNING: |
| 3543 | /* newly created job or backgrounded job, |
| 3544 | put after all stopped jobs. */ |
| 3545 | do { |
| 3546 | jp1 = *jpp; |
| 3547 | #if JOBS |
| 3548 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3549 | #endif |
| 3550 | break; |
| 3551 | jpp = &jp1->prev_job; |
| 3552 | } while (1); |
| 3553 | /* FALLTHROUGH */ |
| 3554 | #if JOBS |
| 3555 | case CUR_STOPPED: |
| 3556 | #endif |
| 3557 | /* newly stopped job - becomes curjob */ |
| 3558 | jp->prev_job = *jpp; |
| 3559 | *jpp = jp; |
| 3560 | break; |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | #if JOBS || DEBUG |
| 3565 | static int |
| 3566 | jobno(const struct job *jp) |
| 3567 | { |
| 3568 | return jp - jobtab + 1; |
| 3569 | } |
| 3570 | #endif |
| 3571 | |
| 3572 | /* |
| 3573 | * Convert a job name to a job structure. |
| 3574 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3575 | #if !JOBS |
| 3576 | #define getjob(name, getctl) getjob(name) |
| 3577 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3578 | static struct job * |
| 3579 | getjob(const char *name, int getctl) |
| 3580 | { |
| 3581 | struct job *jp; |
| 3582 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3583 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3584 | unsigned num; |
| 3585 | int c; |
| 3586 | const char *p; |
| 3587 | char *(*match)(const char *, const char *); |
| 3588 | |
| 3589 | jp = curjob; |
| 3590 | p = name; |
| 3591 | if (!p) |
| 3592 | goto currentjob; |
| 3593 | |
| 3594 | if (*p != '%') |
| 3595 | goto err; |
| 3596 | |
| 3597 | c = *++p; |
| 3598 | if (!c) |
| 3599 | goto currentjob; |
| 3600 | |
| 3601 | if (!p[1]) { |
| 3602 | if (c == '+' || c == '%') { |
| 3603 | currentjob: |
| 3604 | err_msg = "No current job"; |
| 3605 | goto check; |
| 3606 | } |
| 3607 | if (c == '-') { |
| 3608 | if (jp) |
| 3609 | jp = jp->prev_job; |
| 3610 | err_msg = "No previous job"; |
| 3611 | check: |
| 3612 | if (!jp) |
| 3613 | goto err; |
| 3614 | goto gotit; |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | if (is_number(p)) { |
| 3619 | num = atoi(p); |
| 3620 | if (num < njobs) { |
| 3621 | jp = jobtab + num - 1; |
| 3622 | if (jp->used) |
| 3623 | goto gotit; |
| 3624 | goto err; |
| 3625 | } |
| 3626 | } |
| 3627 | |
| 3628 | match = prefix; |
| 3629 | if (*p == '?') { |
| 3630 | match = strstr; |
| 3631 | p++; |
| 3632 | } |
| 3633 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3634 | found = NULL; |
| 3635 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3636 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3637 | if (found) |
| 3638 | goto err; |
| 3639 | found = jp; |
| 3640 | err_msg = "%s: ambiguous"; |
| 3641 | } |
| 3642 | jp = jp->prev_job; |
| 3643 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3644 | if (!found) |
| 3645 | goto err; |
| 3646 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3647 | |
| 3648 | gotit: |
| 3649 | #if JOBS |
| 3650 | err_msg = "job %s not created under job control"; |
| 3651 | if (getctl && jp->jobctl == 0) |
| 3652 | goto err; |
| 3653 | #endif |
| 3654 | return jp; |
| 3655 | err: |
| 3656 | ash_msg_and_raise_error(err_msg, name); |
| 3657 | } |
| 3658 | |
| 3659 | /* |
| 3660 | * Mark a job structure as unused. |
| 3661 | */ |
| 3662 | static void |
| 3663 | freejob(struct job *jp) |
| 3664 | { |
| 3665 | struct procstat *ps; |
| 3666 | int i; |
| 3667 | |
| 3668 | INT_OFF; |
| 3669 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3670 | if (ps->ps_cmd != nullstr) |
| 3671 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3672 | } |
| 3673 | if (jp->ps != &jp->ps0) |
| 3674 | free(jp->ps); |
| 3675 | jp->used = 0; |
| 3676 | set_curjob(jp, CUR_DELETE); |
| 3677 | INT_ON; |
| 3678 | } |
| 3679 | |
| 3680 | #if JOBS |
| 3681 | static void |
| 3682 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3683 | { |
| 3684 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3685 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | /* |
| 3689 | * Turn job control on and off. |
| 3690 | * |
| 3691 | * Note: This code assumes that the third arg to ioctl is a character |
| 3692 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3693 | * System V doesn't have job control yet, this isn't a problem now. |
| 3694 | * |
| 3695 | * Called with interrupts off. |
| 3696 | */ |
| 3697 | static void |
| 3698 | setjobctl(int on) |
| 3699 | { |
| 3700 | int fd; |
| 3701 | int pgrp; |
| 3702 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3703 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | if (on) { |
| 3706 | int ofd; |
| 3707 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3708 | if (fd < 0) { |
| 3709 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3710 | * That sometimes helps to acquire controlling tty. |
| 3711 | * Obviously, a workaround for bugs when someone |
| 3712 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3713 | fd = 2; |
| 3714 | while (!isatty(fd)) |
| 3715 | if (--fd < 0) |
| 3716 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3717 | } |
| 3718 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3719 | if (ofd >= 0) |
| 3720 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3721 | if (fd < 0) |
| 3722 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3723 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3724 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3725 | do { /* while we are in the background */ |
| 3726 | pgrp = tcgetpgrp(fd); |
| 3727 | if (pgrp < 0) { |
| 3728 | out: |
| 3729 | ash_msg("can't access tty; job control turned off"); |
| 3730 | mflag = on = 0; |
| 3731 | goto close; |
| 3732 | } |
| 3733 | if (pgrp == getpgrp()) |
| 3734 | break; |
| 3735 | killpg(0, SIGTTIN); |
| 3736 | } while (1); |
| 3737 | initialpgrp = pgrp; |
| 3738 | |
| 3739 | setsignal(SIGTSTP); |
| 3740 | setsignal(SIGTTOU); |
| 3741 | setsignal(SIGTTIN); |
| 3742 | pgrp = rootpid; |
| 3743 | setpgid(0, pgrp); |
| 3744 | xtcsetpgrp(fd, pgrp); |
| 3745 | } else { |
| 3746 | /* turning job control off */ |
| 3747 | fd = ttyfd; |
| 3748 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3749 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3750 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3751 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3752 | setpgid(0, pgrp); |
| 3753 | setsignal(SIGTSTP); |
| 3754 | setsignal(SIGTTOU); |
| 3755 | setsignal(SIGTTIN); |
| 3756 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3757 | if (fd >= 0) |
| 3758 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3759 | fd = -1; |
| 3760 | } |
| 3761 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3762 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3765 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3766 | killcmd(int argc, char **argv) |
| 3767 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3768 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3769 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3770 | do { |
| 3771 | if (argv[i][0] == '%') { |
| 3772 | struct job *jp = getjob(argv[i], 0); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3773 | unsigned pid = jp->ps[0].ps_pid; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3774 | /* Enough space for ' -NNN<nul>' */ |
| 3775 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3776 | /* kill_main has matching code to expect |
| 3777 | * leading space. Needed to not confuse |
| 3778 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3779 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3780 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3781 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3782 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3783 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3787 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3788 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3789 | struct procstat *ps; |
| 3790 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3791 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3792 | psend = jp->ps + jp->nprocs; |
| 3793 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3794 | printf(" | %s", ps->ps_cmd); |
| 3795 | outcslow('\n', stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3796 | flush_stdout_stderr(); |
| 3797 | } |
| 3798 | |
| 3799 | |
| 3800 | static int |
| 3801 | restartjob(struct job *jp, int mode) |
| 3802 | { |
| 3803 | struct procstat *ps; |
| 3804 | int i; |
| 3805 | int status; |
| 3806 | pid_t pgid; |
| 3807 | |
| 3808 | INT_OFF; |
| 3809 | if (jp->state == JOBDONE) |
| 3810 | goto out; |
| 3811 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3812 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3813 | if (mode == FORK_FG) |
| 3814 | xtcsetpgrp(ttyfd, pgid); |
| 3815 | killpg(pgid, SIGCONT); |
| 3816 | ps = jp->ps; |
| 3817 | i = jp->nprocs; |
| 3818 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3819 | if (WIFSTOPPED(ps->ps_status)) { |
| 3820 | ps->ps_status = -1; |
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 | ps++; |
| 3823 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3824 | out: |
| 3825 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3826 | INT_ON; |
| 3827 | return status; |
| 3828 | } |
| 3829 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3830 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3831 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3832 | { |
| 3833 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3834 | int mode; |
| 3835 | int retval; |
| 3836 | |
| 3837 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3838 | nextopt(nullstr); |
| 3839 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3840 | do { |
| 3841 | jp = getjob(*argv, 1); |
| 3842 | if (mode == FORK_BG) { |
| 3843 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3844 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3845 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3846 | out1str(jp->ps[0].ps_cmd); |
| 3847 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3848 | retval = restartjob(jp, mode); |
| 3849 | } while (*argv && *++argv); |
| 3850 | return retval; |
| 3851 | } |
| 3852 | #endif |
| 3853 | |
| 3854 | static int |
| 3855 | sprint_status(char *s, int status, int sigonly) |
| 3856 | { |
| 3857 | int col; |
| 3858 | int st; |
| 3859 | |
| 3860 | col = 0; |
| 3861 | if (!WIFEXITED(status)) { |
| 3862 | #if JOBS |
| 3863 | if (WIFSTOPPED(status)) |
| 3864 | st = WSTOPSIG(status); |
| 3865 | else |
| 3866 | #endif |
| 3867 | st = WTERMSIG(status); |
| 3868 | if (sigonly) { |
| 3869 | if (st == SIGINT || st == SIGPIPE) |
| 3870 | goto out; |
| 3871 | #if JOBS |
| 3872 | if (WIFSTOPPED(status)) |
| 3873 | goto out; |
| 3874 | #endif |
| 3875 | } |
| 3876 | st &= 0x7f; |
| 3877 | col = fmtstr(s, 32, strsignal(st)); |
| 3878 | if (WCOREDUMP(status)) { |
| 3879 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3880 | } |
| 3881 | } else if (!sigonly) { |
| 3882 | st = WEXITSTATUS(status); |
| 3883 | if (st) |
| 3884 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3885 | else |
| 3886 | col = fmtstr(s, 16, "Done"); |
| 3887 | } |
| 3888 | out: |
| 3889 | return col; |
| 3890 | } |
| 3891 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3892 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3893 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3894 | { |
| 3895 | int pid; |
| 3896 | int status; |
| 3897 | struct job *jp; |
| 3898 | struct job *thisjob; |
| 3899 | int state; |
| 3900 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3901 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3902 | |
| 3903 | /* Do a wait system call. If job control is compiled in, we accept |
| 3904 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3905 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3906 | if (doing_jobctl) |
| 3907 | wait_flags |= WUNTRACED; |
| 3908 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3909 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3910 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3911 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3912 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3913 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3914 | INT_OFF; |
| 3915 | thisjob = NULL; |
| 3916 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3917 | struct procstat *ps; |
| 3918 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3919 | if (jp->state == JOBDONE) |
| 3920 | continue; |
| 3921 | state = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3922 | ps = jp->ps; |
| 3923 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3924 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3925 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3926 | TRACE(("Job %d: changing status of proc %d " |
| 3927 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3928 | jobno(jp), pid, ps->ps_status, status)); |
| 3929 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3930 | thisjob = jp; |
| 3931 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3932 | if (ps->ps_status == -1) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3933 | state = JOBRUNNING; |
| 3934 | #if JOBS |
| 3935 | if (state == JOBRUNNING) |
| 3936 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3937 | if (WIFSTOPPED(ps->ps_status)) { |
| 3938 | jp->stopstatus = ps->ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3939 | state = JOBSTOPPED; |
| 3940 | } |
| 3941 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3942 | } while (++ps < psend); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3943 | if (thisjob) |
| 3944 | goto gotjob; |
| 3945 | } |
| 3946 | #if JOBS |
| 3947 | if (!WIFSTOPPED(status)) |
| 3948 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3949 | jobless--; |
| 3950 | goto out; |
| 3951 | |
| 3952 | gotjob: |
| 3953 | if (state != JOBRUNNING) { |
| 3954 | thisjob->changed = 1; |
| 3955 | |
| 3956 | if (thisjob->state != state) { |
| 3957 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3958 | jobno(thisjob), thisjob->state, state)); |
| 3959 | thisjob->state = state; |
| 3960 | #if JOBS |
| 3961 | if (state == JOBSTOPPED) { |
| 3962 | set_curjob(thisjob, CUR_STOPPED); |
| 3963 | } |
| 3964 | #endif |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | out: |
| 3969 | INT_ON; |
| 3970 | |
| 3971 | if (thisjob && thisjob == job) { |
| 3972 | char s[48 + 1]; |
| 3973 | int len; |
| 3974 | |
| 3975 | len = sprint_status(s, status, 1); |
| 3976 | if (len) { |
| 3977 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3978 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3979 | out2str(s); |
| 3980 | } |
| 3981 | } |
| 3982 | return pid; |
| 3983 | } |
| 3984 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3985 | static int |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3986 | blocking_wait_with_raise_on_sig(void) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3987 | { |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3988 | pid_t pid = dowait(DOWAIT_BLOCK, NULL); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3989 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3990 | raise_exception(EXSIG); |
| 3991 | return pid; |
| 3992 | } |
| 3993 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3994 | #if JOBS |
| 3995 | static void |
| 3996 | showjob(FILE *out, struct job *jp, int mode) |
| 3997 | { |
| 3998 | struct procstat *ps; |
| 3999 | struct procstat *psend; |
| 4000 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4001 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4002 | char s[80]; |
| 4003 | |
| 4004 | ps = jp->ps; |
| 4005 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4006 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4007 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4008 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4009 | return; |
| 4010 | } |
| 4011 | |
| 4012 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4013 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4014 | |
| 4015 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4016 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4017 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4018 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4019 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4020 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4021 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4022 | |
| 4023 | psend = ps + jp->nprocs; |
| 4024 | |
| 4025 | if (jp->state == JOBRUNNING) { |
| 4026 | strcpy(s + col, "Running"); |
| 4027 | col += sizeof("Running") - 1; |
| 4028 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4029 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4030 | if (jp->state == JOBSTOPPED) |
| 4031 | status = jp->stopstatus; |
| 4032 | col += sprint_status(s + col, status, 0); |
| 4033 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4034 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4035 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4036 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4037 | * or prints several "PID | <cmdN>" lines, |
| 4038 | * depending on SHOW_PIDS bit. |
| 4039 | * We do not print status of individual processes |
| 4040 | * between PID and <cmdN>. bash does it, but not very well: |
| 4041 | * first line shows overall job status, not process status, |
| 4042 | * making it impossible to know 1st process status. |
| 4043 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4044 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4045 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4046 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4047 | s[0] = '\0'; |
| 4048 | col = 33; |
| 4049 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4050 | 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] | 4051 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4052 | fprintf(out, "%s%*c%s%s", |
| 4053 | s, |
| 4054 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4055 | ps == jp->ps ? "" : "| ", |
| 4056 | ps->ps_cmd |
| 4057 | ); |
| 4058 | } while (++ps != psend); |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4059 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4060 | |
| 4061 | jp->changed = 0; |
| 4062 | |
| 4063 | if (jp->state == JOBDONE) { |
| 4064 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4065 | freejob(jp); |
| 4066 | } |
| 4067 | } |
| 4068 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4069 | /* |
| 4070 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4071 | * statuses have changed since the last call to showjobs. |
| 4072 | */ |
| 4073 | static void |
| 4074 | showjobs(FILE *out, int mode) |
| 4075 | { |
| 4076 | struct job *jp; |
| 4077 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4078 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4079 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4080 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4081 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4082 | continue; |
| 4083 | |
| 4084 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4085 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4086 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4087 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4088 | } |
| 4089 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4090 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4091 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4092 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4093 | { |
| 4094 | int mode, m; |
| 4095 | |
| 4096 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4097 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4098 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4099 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4100 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4101 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | argv = argptr; |
| 4105 | if (*argv) { |
| 4106 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4107 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4108 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4109 | } else { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4110 | showjobs(stdout, mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4111 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4112 | |
| 4113 | return 0; |
| 4114 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4115 | #endif /* JOBS */ |
| 4116 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4117 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4118 | static int |
| 4119 | getstatus(struct job *job) |
| 4120 | { |
| 4121 | int status; |
| 4122 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4123 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4124 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4125 | /* Fetch last member's status */ |
| 4126 | ps = job->ps + job->nprocs - 1; |
| 4127 | status = ps->ps_status; |
| 4128 | if (pipefail) { |
| 4129 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4130 | while (status == 0 && --ps >= job->ps) |
| 4131 | status = ps->ps_status; |
| 4132 | } |
| 4133 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4134 | retval = WEXITSTATUS(status); |
| 4135 | if (!WIFEXITED(status)) { |
| 4136 | #if JOBS |
| 4137 | retval = WSTOPSIG(status); |
| 4138 | if (!WIFSTOPPED(status)) |
| 4139 | #endif |
| 4140 | { |
| 4141 | /* XXX: limits number of signals */ |
| 4142 | retval = WTERMSIG(status); |
| 4143 | #if JOBS |
| 4144 | if (retval == SIGINT) |
| 4145 | job->sigint = 1; |
| 4146 | #endif |
| 4147 | } |
| 4148 | retval += 128; |
| 4149 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4150 | 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] | 4151 | jobno(job), job->nprocs, status, retval)); |
| 4152 | return retval; |
| 4153 | } |
| 4154 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4155 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4156 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4157 | { |
| 4158 | struct job *job; |
| 4159 | int retval; |
| 4160 | struct job *jp; |
| 4161 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4162 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4163 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4164 | |
| 4165 | nextopt(nullstr); |
| 4166 | retval = 0; |
| 4167 | |
| 4168 | argv = argptr; |
| 4169 | if (!*argv) { |
| 4170 | /* wait for all jobs */ |
| 4171 | for (;;) { |
| 4172 | jp = curjob; |
| 4173 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4174 | if (!jp) /* no running procs */ |
| 4175 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4176 | if (jp->state == JOBRUNNING) |
| 4177 | break; |
| 4178 | jp->waited = 1; |
| 4179 | jp = jp->prev_job; |
| 4180 | } |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4181 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4182 | /* man bash: |
| 4183 | * "When bash is waiting for an asynchronous command via |
| 4184 | * the wait builtin, the reception of a signal for which a trap |
| 4185 | * has been set will cause the wait builtin to return immediately |
| 4186 | * with an exit status greater than 128, immediately after which |
| 4187 | * the trap is executed." |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4188 | * |
| 4189 | * blocking_wait_with_raise_on_sig raises signal handlers |
| 4190 | * if it gets no pid (pid < 0). However, |
| 4191 | * if child sends us a signal *and immediately exits*, |
| 4192 | * blocking_wait_with_raise_on_sig gets pid > 0 |
| 4193 | * and does not handle pending_sig. Check this case: */ |
| 4194 | if (pending_sig) |
| 4195 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | retval = 127; |
| 4200 | do { |
| 4201 | if (**argv != '%') { |
| 4202 | pid_t pid = number(*argv); |
| 4203 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4204 | while (1) { |
| 4205 | if (!job) |
| 4206 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4207 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4208 | break; |
| 4209 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4210 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4211 | } else |
| 4212 | job = getjob(*argv, 0); |
| 4213 | /* loop until process terminated or stopped */ |
| 4214 | while (job->state == JOBRUNNING) |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4215 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4216 | job->waited = 1; |
| 4217 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4218 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4219 | } while (*++argv); |
| 4220 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4221 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4222 | return retval; |
| 4223 | } |
| 4224 | |
| 4225 | static struct job * |
| 4226 | growjobtab(void) |
| 4227 | { |
| 4228 | size_t len; |
| 4229 | ptrdiff_t offset; |
| 4230 | struct job *jp, *jq; |
| 4231 | |
| 4232 | len = njobs * sizeof(*jp); |
| 4233 | jq = jobtab; |
| 4234 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4235 | |
| 4236 | offset = (char *)jp - (char *)jq; |
| 4237 | if (offset) { |
| 4238 | /* Relocate pointers */ |
| 4239 | size_t l = len; |
| 4240 | |
| 4241 | jq = (struct job *)((char *)jq + l); |
| 4242 | while (l) { |
| 4243 | l -= sizeof(*jp); |
| 4244 | jq--; |
| 4245 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4246 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4247 | if (joff(jp)->ps == &jq->ps0) |
| 4248 | jmove(joff(jp)->ps); |
| 4249 | if (joff(jp)->prev_job) |
| 4250 | jmove(joff(jp)->prev_job); |
| 4251 | } |
| 4252 | if (curjob) |
| 4253 | jmove(curjob); |
| 4254 | #undef joff |
| 4255 | #undef jmove |
| 4256 | } |
| 4257 | |
| 4258 | njobs += 4; |
| 4259 | jobtab = jp; |
| 4260 | jp = (struct job *)((char *)jp + len); |
| 4261 | jq = jp + 3; |
| 4262 | do { |
| 4263 | jq->used = 0; |
| 4264 | } while (--jq >= jp); |
| 4265 | return jp; |
| 4266 | } |
| 4267 | |
| 4268 | /* |
| 4269 | * Return a new job structure. |
| 4270 | * Called with interrupts off. |
| 4271 | */ |
| 4272 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4273 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4274 | { |
| 4275 | int i; |
| 4276 | struct job *jp; |
| 4277 | |
| 4278 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4279 | if (--i < 0) { |
| 4280 | jp = growjobtab(); |
| 4281 | break; |
| 4282 | } |
| 4283 | if (jp->used == 0) |
| 4284 | break; |
| 4285 | if (jp->state != JOBDONE || !jp->waited) |
| 4286 | continue; |
| 4287 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4288 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4289 | continue; |
| 4290 | #endif |
| 4291 | freejob(jp); |
| 4292 | break; |
| 4293 | } |
| 4294 | memset(jp, 0, sizeof(*jp)); |
| 4295 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4296 | /* jp->jobctl is a bitfield. |
| 4297 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4298 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4299 | jp->jobctl = 1; |
| 4300 | #endif |
| 4301 | jp->prev_job = curjob; |
| 4302 | curjob = jp; |
| 4303 | jp->used = 1; |
| 4304 | jp->ps = &jp->ps0; |
| 4305 | if (nprocs > 1) { |
| 4306 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4307 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4308 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4309 | jobno(jp))); |
| 4310 | return jp; |
| 4311 | } |
| 4312 | |
| 4313 | #if JOBS |
| 4314 | /* |
| 4315 | * Return a string identifying a command (to be printed by the |
| 4316 | * jobs command). |
| 4317 | */ |
| 4318 | static char *cmdnextc; |
| 4319 | |
| 4320 | static void |
| 4321 | cmdputs(const char *s) |
| 4322 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4323 | static const char vstype[VSTYPE + 1][3] = { |
| 4324 | "", "}", "-", "+", "?", "=", |
| 4325 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4326 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4327 | }; |
| 4328 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4329 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4330 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4331 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4332 | unsigned char c; |
| 4333 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4334 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4335 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4336 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4337 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4338 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4339 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4340 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4341 | switch (c) { |
| 4342 | case CTLESC: |
| 4343 | c = *p++; |
| 4344 | break; |
| 4345 | case CTLVAR: |
| 4346 | subtype = *p++; |
| 4347 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4348 | str = "${#"; |
| 4349 | else |
| 4350 | str = "${"; |
| 4351 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4352 | goto dostr; |
| 4353 | quoted ^= 1; |
| 4354 | c = '"'; |
| 4355 | break; |
| 4356 | case CTLENDVAR: |
| 4357 | str = "\"}" + !(quoted & 1); |
| 4358 | quoted >>= 1; |
| 4359 | subtype = 0; |
| 4360 | goto dostr; |
| 4361 | case CTLBACKQ: |
| 4362 | str = "$(...)"; |
| 4363 | goto dostr; |
| 4364 | case CTLBACKQ+CTLQUOTE: |
| 4365 | str = "\"$(...)\""; |
| 4366 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4367 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4368 | case CTLARI: |
| 4369 | str = "$(("; |
| 4370 | goto dostr; |
| 4371 | case CTLENDARI: |
| 4372 | str = "))"; |
| 4373 | goto dostr; |
| 4374 | #endif |
| 4375 | case CTLQUOTEMARK: |
| 4376 | quoted ^= 1; |
| 4377 | c = '"'; |
| 4378 | break; |
| 4379 | case '=': |
| 4380 | if (subtype == 0) |
| 4381 | break; |
| 4382 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4383 | quoted <<= 1; |
| 4384 | str = vstype[subtype & VSTYPE]; |
| 4385 | if (subtype & VSNUL) |
| 4386 | c = ':'; |
| 4387 | else |
| 4388 | goto checkstr; |
| 4389 | break; |
| 4390 | case '\'': |
| 4391 | case '\\': |
| 4392 | case '"': |
| 4393 | case '$': |
| 4394 | /* These can only happen inside quotes */ |
| 4395 | cc[0] = c; |
| 4396 | str = cc; |
| 4397 | c = '\\'; |
| 4398 | break; |
| 4399 | default: |
| 4400 | break; |
| 4401 | } |
| 4402 | USTPUTC(c, nextc); |
| 4403 | checkstr: |
| 4404 | if (!str) |
| 4405 | continue; |
| 4406 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4407 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4408 | USTPUTC(c, nextc); |
| 4409 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4410 | } /* while *p++ not NUL */ |
| 4411 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4412 | if (quoted & 1) { |
| 4413 | USTPUTC('"', nextc); |
| 4414 | } |
| 4415 | *nextc = 0; |
| 4416 | cmdnextc = nextc; |
| 4417 | } |
| 4418 | |
| 4419 | /* cmdtxt() and cmdlist() call each other */ |
| 4420 | static void cmdtxt(union node *n); |
| 4421 | |
| 4422 | static void |
| 4423 | cmdlist(union node *np, int sep) |
| 4424 | { |
| 4425 | for (; np; np = np->narg.next) { |
| 4426 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4427 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4428 | cmdtxt(np); |
| 4429 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4430 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4431 | } |
| 4432 | } |
| 4433 | |
| 4434 | static void |
| 4435 | cmdtxt(union node *n) |
| 4436 | { |
| 4437 | union node *np; |
| 4438 | struct nodelist *lp; |
| 4439 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4440 | |
| 4441 | if (!n) |
| 4442 | return; |
| 4443 | switch (n->type) { |
| 4444 | default: |
| 4445 | #if DEBUG |
| 4446 | abort(); |
| 4447 | #endif |
| 4448 | case NPIPE: |
| 4449 | lp = n->npipe.cmdlist; |
| 4450 | for (;;) { |
| 4451 | cmdtxt(lp->n); |
| 4452 | lp = lp->next; |
| 4453 | if (!lp) |
| 4454 | break; |
| 4455 | cmdputs(" | "); |
| 4456 | } |
| 4457 | break; |
| 4458 | case NSEMI: |
| 4459 | p = "; "; |
| 4460 | goto binop; |
| 4461 | case NAND: |
| 4462 | p = " && "; |
| 4463 | goto binop; |
| 4464 | case NOR: |
| 4465 | p = " || "; |
| 4466 | binop: |
| 4467 | cmdtxt(n->nbinary.ch1); |
| 4468 | cmdputs(p); |
| 4469 | n = n->nbinary.ch2; |
| 4470 | goto donode; |
| 4471 | case NREDIR: |
| 4472 | case NBACKGND: |
| 4473 | n = n->nredir.n; |
| 4474 | goto donode; |
| 4475 | case NNOT: |
| 4476 | cmdputs("!"); |
| 4477 | n = n->nnot.com; |
| 4478 | donode: |
| 4479 | cmdtxt(n); |
| 4480 | break; |
| 4481 | case NIF: |
| 4482 | cmdputs("if "); |
| 4483 | cmdtxt(n->nif.test); |
| 4484 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4485 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4486 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4487 | cmdputs("; else "); |
| 4488 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4489 | } else { |
| 4490 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4491 | } |
| 4492 | p = "; fi"; |
| 4493 | goto dotail; |
| 4494 | case NSUBSHELL: |
| 4495 | cmdputs("("); |
| 4496 | n = n->nredir.n; |
| 4497 | p = ")"; |
| 4498 | goto dotail; |
| 4499 | case NWHILE: |
| 4500 | p = "while "; |
| 4501 | goto until; |
| 4502 | case NUNTIL: |
| 4503 | p = "until "; |
| 4504 | until: |
| 4505 | cmdputs(p); |
| 4506 | cmdtxt(n->nbinary.ch1); |
| 4507 | n = n->nbinary.ch2; |
| 4508 | p = "; done"; |
| 4509 | dodo: |
| 4510 | cmdputs("; do "); |
| 4511 | dotail: |
| 4512 | cmdtxt(n); |
| 4513 | goto dotail2; |
| 4514 | case NFOR: |
| 4515 | cmdputs("for "); |
| 4516 | cmdputs(n->nfor.var); |
| 4517 | cmdputs(" in "); |
| 4518 | cmdlist(n->nfor.args, 1); |
| 4519 | n = n->nfor.body; |
| 4520 | p = "; done"; |
| 4521 | goto dodo; |
| 4522 | case NDEFUN: |
| 4523 | cmdputs(n->narg.text); |
| 4524 | p = "() { ... }"; |
| 4525 | goto dotail2; |
| 4526 | case NCMD: |
| 4527 | cmdlist(n->ncmd.args, 1); |
| 4528 | cmdlist(n->ncmd.redirect, 0); |
| 4529 | break; |
| 4530 | case NARG: |
| 4531 | p = n->narg.text; |
| 4532 | dotail2: |
| 4533 | cmdputs(p); |
| 4534 | break; |
| 4535 | case NHERE: |
| 4536 | case NXHERE: |
| 4537 | p = "<<..."; |
| 4538 | goto dotail2; |
| 4539 | case NCASE: |
| 4540 | cmdputs("case "); |
| 4541 | cmdputs(n->ncase.expr->narg.text); |
| 4542 | cmdputs(" in "); |
| 4543 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4544 | cmdtxt(np->nclist.pattern); |
| 4545 | cmdputs(") "); |
| 4546 | cmdtxt(np->nclist.body); |
| 4547 | cmdputs(";; "); |
| 4548 | } |
| 4549 | p = "esac"; |
| 4550 | goto dotail2; |
| 4551 | case NTO: |
| 4552 | p = ">"; |
| 4553 | goto redir; |
| 4554 | case NCLOBBER: |
| 4555 | p = ">|"; |
| 4556 | goto redir; |
| 4557 | case NAPPEND: |
| 4558 | p = ">>"; |
| 4559 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4560 | #if ENABLE_ASH_BASH_COMPAT |
| 4561 | case NTO2: |
| 4562 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4563 | case NTOFD: |
| 4564 | p = ">&"; |
| 4565 | goto redir; |
| 4566 | case NFROM: |
| 4567 | p = "<"; |
| 4568 | goto redir; |
| 4569 | case NFROMFD: |
| 4570 | p = "<&"; |
| 4571 | goto redir; |
| 4572 | case NFROMTO: |
| 4573 | p = "<>"; |
| 4574 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4575 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4576 | cmdputs(p); |
| 4577 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4578 | cmdputs(utoa(n->ndup.dupfd)); |
| 4579 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4580 | } |
| 4581 | n = n->nfile.fname; |
| 4582 | goto donode; |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | static char * |
| 4587 | commandtext(union node *n) |
| 4588 | { |
| 4589 | char *name; |
| 4590 | |
| 4591 | STARTSTACKSTR(cmdnextc); |
| 4592 | cmdtxt(n); |
| 4593 | name = stackblock(); |
| 4594 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4595 | name, cmdnextc, cmdnextc)); |
| 4596 | return ckstrdup(name); |
| 4597 | } |
| 4598 | #endif /* JOBS */ |
| 4599 | |
| 4600 | /* |
| 4601 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4602 | * own process group. Jp is a job structure that the job is to be added to. |
| 4603 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4604 | * be NULL. The mode parameter can be one of the following: |
| 4605 | * FORK_FG - Fork off a foreground process. |
| 4606 | * FORK_BG - Fork off a background process. |
| 4607 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4608 | * process group even if job control is on. |
| 4609 | * |
| 4610 | * When job control is turned off, background processes have their standard |
| 4611 | * input redirected to /dev/null (except for the second and later processes |
| 4612 | * in a pipeline). |
| 4613 | * |
| 4614 | * Called with interrupts off. |
| 4615 | */ |
| 4616 | /* |
| 4617 | * Clear traps on a fork. |
| 4618 | */ |
| 4619 | static void |
| 4620 | clear_traps(void) |
| 4621 | { |
| 4622 | char **tp; |
| 4623 | |
| 4624 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4625 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4626 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4627 | if (trap_ptr == trap) |
| 4628 | free(*tp); |
| 4629 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4630 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4631 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4632 | setsignal(tp - trap); |
| 4633 | INT_ON; |
| 4634 | } |
| 4635 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 4636 | may_have_traps = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4637 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4638 | |
| 4639 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4640 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4641 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4642 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4643 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4644 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4645 | { |
| 4646 | int oldlvl; |
| 4647 | |
| 4648 | TRACE(("Child shell %d\n", getpid())); |
| 4649 | oldlvl = shlvl; |
| 4650 | shlvl++; |
| 4651 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4652 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4653 | * set to the values inherited by the shell from its parent". |
| 4654 | * Do we do it correctly? */ |
| 4655 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4656 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4657 | |
| 4658 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4659 | && n && n->type == NCMD /* is it single cmd? */ |
| 4660 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4661 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4662 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4663 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4664 | ) { |
| 4665 | TRACE(("Trap hack\n")); |
| 4666 | /* Awful hack for `trap` or $(trap). |
| 4667 | * |
| 4668 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4669 | * contains an example where "trap" is executed in a subshell: |
| 4670 | * |
| 4671 | * save_traps=$(trap) |
| 4672 | * ... |
| 4673 | * eval "$save_traps" |
| 4674 | * |
| 4675 | * Standard does not say that "trap" in subshell shall print |
| 4676 | * parent shell's traps. It only says that its output |
| 4677 | * must have suitable form, but then, in the above example |
| 4678 | * (which is not supposed to be normative), it implies that. |
| 4679 | * |
| 4680 | * bash (and probably other shell) does implement it |
| 4681 | * (traps are reset to defaults, but "trap" still shows them), |
| 4682 | * but as a result, "trap" logic is hopelessly messed up: |
| 4683 | * |
| 4684 | * # trap |
| 4685 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4686 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4687 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4688 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4689 | * trap -- 'echo Ho' SIGWINCH |
| 4690 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4691 | * trap -- 'echo Ho' SIGWINCH |
| 4692 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4693 | * trap -- 'echo Ho' SIGWINCH |
| 4694 | * |
| 4695 | * The rules when to forget and when to not forget traps |
| 4696 | * get really complex and nonsensical. |
| 4697 | * |
| 4698 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4699 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4700 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4701 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4702 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4703 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4704 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4705 | #if JOBS |
| 4706 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4707 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4708 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4709 | pid_t pgrp; |
| 4710 | |
| 4711 | if (jp->nprocs == 0) |
| 4712 | pgrp = getpid(); |
| 4713 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4714 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4715 | /* this can fail because we are doing it in the parent also */ |
| 4716 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4717 | if (mode == FORK_FG) |
| 4718 | xtcsetpgrp(ttyfd, pgrp); |
| 4719 | setsignal(SIGTSTP); |
| 4720 | setsignal(SIGTTOU); |
| 4721 | } else |
| 4722 | #endif |
| 4723 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4724 | /* man bash: "When job control is not in effect, |
| 4725 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4726 | ignoresig(SIGINT); |
| 4727 | ignoresig(SIGQUIT); |
| 4728 | if (jp->nprocs == 0) { |
| 4729 | close(0); |
| 4730 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4731 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4732 | } |
| 4733 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4734 | if (!oldlvl) { |
| 4735 | if (iflag) { /* why if iflag only? */ |
| 4736 | setsignal(SIGINT); |
| 4737 | setsignal(SIGTERM); |
| 4738 | } |
| 4739 | /* man bash: |
| 4740 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4741 | * commands run by bash have signal handlers |
| 4742 | * set to the values inherited by the shell |
| 4743 | * from its parent". |
| 4744 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4745 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4746 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4747 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4748 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4749 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4750 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4751 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4752 | /* "jobs": we do not want to clear job list for it, |
| 4753 | * instead we remove only _its_ own_ job from job list. |
| 4754 | * This makes "jobs .... | cat" more useful. |
| 4755 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4756 | freejob(curjob); |
| 4757 | return; |
| 4758 | } |
| 4759 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4760 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4761 | freejob(jp); |
| 4762 | jobless = 0; |
| 4763 | } |
| 4764 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4765 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4766 | #if !JOBS |
| 4767 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4768 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4769 | static void |
| 4770 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4771 | { |
| 4772 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4773 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4774 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4775 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4776 | jobless++; |
| 4777 | return; |
| 4778 | } |
| 4779 | #if JOBS |
| 4780 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4781 | int pgrp; |
| 4782 | |
| 4783 | if (jp->nprocs == 0) |
| 4784 | pgrp = pid; |
| 4785 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4786 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4787 | /* This can fail because we are doing it in the child also */ |
| 4788 | setpgid(pid, pgrp); |
| 4789 | } |
| 4790 | #endif |
| 4791 | if (mode == FORK_BG) { |
| 4792 | backgndpid = pid; /* set $! */ |
| 4793 | set_curjob(jp, CUR_RUNNING); |
| 4794 | } |
| 4795 | if (jp) { |
| 4796 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4797 | ps->ps_pid = pid; |
| 4798 | ps->ps_status = -1; |
| 4799 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4800 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4801 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4802 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4803 | #endif |
| 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | static int |
| 4808 | forkshell(struct job *jp, union node *n, int mode) |
| 4809 | { |
| 4810 | int pid; |
| 4811 | |
| 4812 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4813 | pid = fork(); |
| 4814 | if (pid < 0) { |
| 4815 | TRACE(("Fork failed, errno=%d", errno)); |
| 4816 | if (jp) |
| 4817 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4818 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4819 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4820 | if (pid == 0) { |
| 4821 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4822 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4823 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4824 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4825 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4826 | return pid; |
| 4827 | } |
| 4828 | |
| 4829 | /* |
| 4830 | * Wait for job to finish. |
| 4831 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4832 | * Under job control we have the problem that while a child process |
| 4833 | * is running interrupts generated by the user are sent to the child |
| 4834 | * but not to the shell. This means that an infinite loop started by |
| 4835 | * an interactive user may be hard to kill. With job control turned off, |
| 4836 | * an interactive user may place an interactive program inside a loop. |
| 4837 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4838 | * these interrupts to also abort the loop. The approach we take here |
| 4839 | * is to have the shell ignore interrupt signals while waiting for a |
| 4840 | * foreground process to terminate, and then send itself an interrupt |
| 4841 | * signal if the child process was terminated by an interrupt signal. |
| 4842 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4843 | * exit on interrupt; unless these processes terminate themselves by |
| 4844 | * sending a signal to themselves (instead of calling exit) they will |
| 4845 | * confuse this approach. |
| 4846 | * |
| 4847 | * Called with interrupts off. |
| 4848 | */ |
| 4849 | static int |
| 4850 | waitforjob(struct job *jp) |
| 4851 | { |
| 4852 | int st; |
| 4853 | |
| 4854 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4855 | |
| 4856 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4857 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4858 | /* In non-interactive shells, we _can_ get |
| 4859 | * a keyboard signal here and be EINTRed, |
| 4860 | * but we just loop back, waiting for command to complete. |
| 4861 | * |
| 4862 | * man bash: |
| 4863 | * "If bash is waiting for a command to complete and receives |
| 4864 | * a signal for which a trap has been set, the trap |
| 4865 | * will not be executed until the command completes." |
| 4866 | * |
| 4867 | * Reality is that even if trap is not set, bash |
| 4868 | * will not act on the signal until command completes. |
| 4869 | * Try this. sleep5intoff.c: |
| 4870 | * #include <signal.h> |
| 4871 | * #include <unistd.h> |
| 4872 | * int main() { |
| 4873 | * sigset_t set; |
| 4874 | * sigemptyset(&set); |
| 4875 | * sigaddset(&set, SIGINT); |
| 4876 | * sigaddset(&set, SIGQUIT); |
| 4877 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4878 | * sleep(5); |
| 4879 | * return 0; |
| 4880 | * } |
| 4881 | * $ bash -c './sleep5intoff; echo hi' |
| 4882 | * ^C^C^C^C <--- pressing ^C once a second |
| 4883 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4884 | * $ bash -c './sleep5intoff; echo hi' |
| 4885 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4886 | * $ _ |
| 4887 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4888 | dowait(DOWAIT_BLOCK, jp); |
| 4889 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4890 | INT_ON; |
| 4891 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4892 | st = getstatus(jp); |
| 4893 | #if JOBS |
| 4894 | if (jp->jobctl) { |
| 4895 | xtcsetpgrp(ttyfd, rootpid); |
| 4896 | /* |
| 4897 | * This is truly gross. |
| 4898 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4899 | * caused us (the shell) to no longer be in the controlling |
| 4900 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4901 | * intuit from the subprocess exit status whether a SIGINT |
| 4902 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4903 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4904 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4905 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4906 | } |
| 4907 | if (jp->state == JOBDONE) |
| 4908 | #endif |
| 4909 | freejob(jp); |
| 4910 | return st; |
| 4911 | } |
| 4912 | |
| 4913 | /* |
| 4914 | * return 1 if there are stopped jobs, otherwise 0 |
| 4915 | */ |
| 4916 | static int |
| 4917 | stoppedjobs(void) |
| 4918 | { |
| 4919 | struct job *jp; |
| 4920 | int retval; |
| 4921 | |
| 4922 | retval = 0; |
| 4923 | if (job_warning) |
| 4924 | goto out; |
| 4925 | jp = curjob; |
| 4926 | if (jp && jp->state == JOBSTOPPED) { |
| 4927 | out2str("You have stopped jobs.\n"); |
| 4928 | job_warning = 2; |
| 4929 | retval++; |
| 4930 | } |
| 4931 | out: |
| 4932 | return retval; |
| 4933 | } |
| 4934 | |
| 4935 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4936 | /* ============ redir.c |
| 4937 | * |
| 4938 | * Code for dealing with input/output redirection. |
| 4939 | */ |
| 4940 | |
| 4941 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4942 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4943 | |
| 4944 | /* |
| 4945 | * Open a file in noclobber mode. |
| 4946 | * The code was copied from bash. |
| 4947 | */ |
| 4948 | static int |
| 4949 | noclobberopen(const char *fname) |
| 4950 | { |
| 4951 | int r, fd; |
| 4952 | struct stat finfo, finfo2; |
| 4953 | |
| 4954 | /* |
| 4955 | * If the file exists and is a regular file, return an error |
| 4956 | * immediately. |
| 4957 | */ |
| 4958 | r = stat(fname, &finfo); |
| 4959 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4960 | errno = EEXIST; |
| 4961 | return -1; |
| 4962 | } |
| 4963 | |
| 4964 | /* |
| 4965 | * If the file was not present (r != 0), make sure we open it |
| 4966 | * exclusively so that if it is created before we open it, our open |
| 4967 | * will fail. Make sure that we do not truncate an existing file. |
| 4968 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4969 | * file was not a regular file, we leave O_EXCL off. |
| 4970 | */ |
| 4971 | if (r != 0) |
| 4972 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4973 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4974 | |
| 4975 | /* If the open failed, return the file descriptor right away. */ |
| 4976 | if (fd < 0) |
| 4977 | return fd; |
| 4978 | |
| 4979 | /* |
| 4980 | * OK, the open succeeded, but the file may have been changed from a |
| 4981 | * non-regular file to a regular file between the stat and the open. |
| 4982 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4983 | * did not exist and is symlinked to an existing file between the stat |
| 4984 | * and open. |
| 4985 | */ |
| 4986 | |
| 4987 | /* |
| 4988 | * If we can open it and fstat the file descriptor, and neither check |
| 4989 | * revealed that it was a regular file, and the file has not been |
| 4990 | * replaced, return the file descriptor. |
| 4991 | */ |
| 4992 | if (fstat(fd, &finfo2) == 0 && !S_ISREG(finfo2.st_mode) |
| 4993 | && finfo.st_dev == finfo2.st_dev && finfo.st_ino == finfo2.st_ino) |
| 4994 | return fd; |
| 4995 | |
| 4996 | /* The file has been replaced. badness. */ |
| 4997 | close(fd); |
| 4998 | errno = EEXIST; |
| 4999 | return -1; |
| 5000 | } |
| 5001 | |
| 5002 | /* |
| 5003 | * Handle here documents. Normally we fork off a process to write the |
| 5004 | * data to a pipe. If the document is short, we can stuff the data in |
| 5005 | * the pipe without forking. |
| 5006 | */ |
| 5007 | /* openhere needs this forward reference */ |
| 5008 | static void expandhere(union node *arg, int fd); |
| 5009 | static int |
| 5010 | openhere(union node *redir) |
| 5011 | { |
| 5012 | int pip[2]; |
| 5013 | size_t len = 0; |
| 5014 | |
| 5015 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5016 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5017 | if (redir->type == NHERE) { |
| 5018 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5019 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5020 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 5021 | goto out; |
| 5022 | } |
| 5023 | } |
| 5024 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5025 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5026 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5027 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5028 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5029 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5030 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5031 | signal(SIGPIPE, SIG_DFL); |
| 5032 | if (redir->type == NHERE) |
| 5033 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5034 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5035 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5036 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5037 | } |
| 5038 | out: |
| 5039 | close(pip[1]); |
| 5040 | return pip[0]; |
| 5041 | } |
| 5042 | |
| 5043 | static int |
| 5044 | openredirect(union node *redir) |
| 5045 | { |
| 5046 | char *fname; |
| 5047 | int f; |
| 5048 | |
| 5049 | switch (redir->nfile.type) { |
| 5050 | case NFROM: |
| 5051 | fname = redir->nfile.expfname; |
| 5052 | f = open(fname, O_RDONLY); |
| 5053 | if (f < 0) |
| 5054 | goto eopen; |
| 5055 | break; |
| 5056 | case NFROMTO: |
| 5057 | fname = redir->nfile.expfname; |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5058 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5059 | if (f < 0) |
| 5060 | goto ecreate; |
| 5061 | break; |
| 5062 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5063 | #if ENABLE_ASH_BASH_COMPAT |
| 5064 | case NTO2: |
| 5065 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5066 | /* Take care of noclobber mode. */ |
| 5067 | if (Cflag) { |
| 5068 | fname = redir->nfile.expfname; |
| 5069 | f = noclobberopen(fname); |
| 5070 | if (f < 0) |
| 5071 | goto ecreate; |
| 5072 | break; |
| 5073 | } |
| 5074 | /* FALLTHROUGH */ |
| 5075 | case NCLOBBER: |
| 5076 | fname = redir->nfile.expfname; |
| 5077 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5078 | if (f < 0) |
| 5079 | goto ecreate; |
| 5080 | break; |
| 5081 | case NAPPEND: |
| 5082 | fname = redir->nfile.expfname; |
| 5083 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5084 | if (f < 0) |
| 5085 | goto ecreate; |
| 5086 | break; |
| 5087 | default: |
| 5088 | #if DEBUG |
| 5089 | abort(); |
| 5090 | #endif |
| 5091 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5092 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5093 | // case NTOFD: |
| 5094 | // case NFROMFD: |
| 5095 | // f = -1; |
| 5096 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5097 | case NHERE: |
| 5098 | case NXHERE: |
| 5099 | f = openhere(redir); |
| 5100 | break; |
| 5101 | } |
| 5102 | |
| 5103 | return f; |
| 5104 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5105 | 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] | 5106 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5107 | 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] | 5108 | } |
| 5109 | |
| 5110 | /* |
| 5111 | * Copy a file descriptor to be >= to. Returns -1 |
| 5112 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5113 | * file descriptors left. |
| 5114 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5115 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5116 | * 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] | 5117 | enum { |
| 5118 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5119 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5120 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5121 | static int |
| 5122 | copyfd(int from, int to) |
| 5123 | { |
| 5124 | int newfd; |
| 5125 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5126 | if (to & COPYFD_EXACT) { |
| 5127 | to &= ~COPYFD_EXACT; |
| 5128 | /*if (from != to)*/ |
| 5129 | newfd = dup2(from, to); |
| 5130 | } else { |
| 5131 | newfd = fcntl(from, F_DUPFD, to); |
| 5132 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5133 | if (newfd < 0) { |
| 5134 | if (errno == EMFILE) |
| 5135 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5136 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5137 | ash_msg_and_raise_error("%d: %m", from); |
| 5138 | } |
| 5139 | return newfd; |
| 5140 | } |
| 5141 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5142 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5143 | struct two_fd_t { |
| 5144 | int orig, copy; |
| 5145 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5146 | struct redirtab { |
| 5147 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5148 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5149 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5150 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5151 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5152 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5153 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5154 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5155 | { |
| 5156 | int i; |
| 5157 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5158 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5159 | return 0; |
| 5160 | |
| 5161 | for (i = 0; i < rp->pair_count; i++) { |
| 5162 | if (rp->two_fd[i].orig == fd) { |
| 5163 | /* already remembered */ |
| 5164 | return 0; |
| 5165 | } |
| 5166 | } |
| 5167 | return 1; |
| 5168 | } |
| 5169 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5170 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5171 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5172 | { |
| 5173 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5174 | struct parsefile *pf; |
| 5175 | |
| 5176 | if (fd == -1) |
| 5177 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5178 | /* Check open scripts' fds */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5179 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5180 | while (pf) { |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5181 | /* We skip pf_fd == 0 case because of the following case: |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5182 | * $ ash # running ash interactively |
| 5183 | * $ . ./script.sh |
| 5184 | * and in script.sh: "exec 9>&0". |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5185 | * Even though top-level pf_fd _is_ 0, |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5186 | * it's still ok to use it: "read" builtin uses it, |
| 5187 | * why should we cripple "exec" builtin? |
| 5188 | */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5189 | if (pf->pf_fd > 0 && fd == pf->pf_fd) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5190 | return 1; |
| 5191 | } |
| 5192 | pf = pf->prev; |
| 5193 | } |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5194 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5195 | if (!rp) |
| 5196 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5197 | /* Check saved fds of redirects */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5198 | fd |= COPYFD_RESTORE; |
| 5199 | for (i = 0; i < rp->pair_count; i++) { |
| 5200 | if (rp->two_fd[i].copy == fd) { |
| 5201 | return 1; |
| 5202 | } |
| 5203 | } |
| 5204 | return 0; |
| 5205 | } |
| 5206 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5207 | /* |
| 5208 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5209 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5210 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5211 | */ |
| 5212 | /* flags passed to redirect */ |
| 5213 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5214 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5215 | static void |
| 5216 | redirect(union node *redir, int flags) |
| 5217 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5218 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5219 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5220 | int i; |
| 5221 | int fd; |
| 5222 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5223 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5224 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5225 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5226 | if (!redir) { |
| 5227 | return; |
| 5228 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5229 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5230 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5231 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5232 | INT_OFF; |
| 5233 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5234 | union node *tmp = redir; |
| 5235 | do { |
| 5236 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5237 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5238 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5239 | sv_pos++; |
| 5240 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5241 | tmp = tmp->nfile.next; |
| 5242 | } while (tmp); |
| 5243 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5244 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5245 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5246 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5247 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5248 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5249 | while (sv_pos > 0) { |
| 5250 | sv_pos--; |
| 5251 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5252 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5253 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5254 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5255 | do { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5256 | int right_fd = -1; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5257 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5258 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5259 | right_fd = redir->ndup.dupfd; |
| 5260 | //bb_error_msg("doing %d > %d", fd, right_fd); |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5261 | /* redirect from/to same file descriptor? */ |
| 5262 | if (right_fd == fd) |
| 5263 | continue; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5264 | /* "echo >&10" and 10 is a fd opened to a sh script? */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5265 | if (is_hidden_fd(sv, right_fd)) { |
| 5266 | errno = EBADF; /* as if it is closed */ |
| 5267 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5268 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5269 | newfd = -1; |
| 5270 | } else { |
| 5271 | newfd = openredirect(redir); /* always >= 0 */ |
| 5272 | if (fd == newfd) { |
| 5273 | /* Descriptor wasn't open before redirect. |
| 5274 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5275 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5276 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5277 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5278 | continue; |
| 5279 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5280 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5281 | #if ENABLE_ASH_BASH_COMPAT |
| 5282 | redirect_more: |
| 5283 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5284 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5285 | /* Copy old descriptor */ |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5286 | /* Careful to not accidentally "save" |
| 5287 | * to the same fd as right side fd in N>&M */ |
| 5288 | int minfd = right_fd < 10 ? 10 : right_fd + 1; |
| 5289 | i = fcntl(fd, F_DUPFD, minfd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5290 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5291 | * are closed in popredir() in the child, preventing them from leaking |
| 5292 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5293 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5294 | if (i == -1) { |
| 5295 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5296 | if (i != EBADF) { |
| 5297 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5298 | if (newfd >= 0) |
| 5299 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5300 | errno = i; |
| 5301 | ash_msg_and_raise_error("%d: %m", fd); |
| 5302 | /* NOTREACHED */ |
| 5303 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5304 | /* EBADF: it is not open - good, remember to close it */ |
| 5305 | remember_to_close: |
| 5306 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5307 | } else { /* fd is open, save its copy */ |
| 5308 | /* "exec fd>&-" should not close fds |
| 5309 | * which point to script file(s). |
| 5310 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5311 | if (is_hidden_fd(sv, fd)) |
| 5312 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5313 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5314 | if (fd == 2) |
| 5315 | copied_fd2 = i; |
| 5316 | sv->two_fd[sv_pos].orig = fd; |
| 5317 | sv->two_fd[sv_pos].copy = i; |
| 5318 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5319 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5320 | if (newfd < 0) { |
| 5321 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5322 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5323 | /* Don't want to trigger debugging */ |
| 5324 | if (fd != -1) |
| 5325 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5326 | } else { |
| 5327 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5328 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5329 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5330 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5331 | #if ENABLE_ASH_BASH_COMPAT |
| 5332 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5333 | #endif |
| 5334 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5335 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5336 | #if ENABLE_ASH_BASH_COMPAT |
| 5337 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5338 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5339 | newfd = 1; |
| 5340 | fd = 2; |
| 5341 | goto redirect_more; |
| 5342 | } |
| 5343 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5344 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5345 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5346 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5347 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5348 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5349 | } |
| 5350 | |
| 5351 | /* |
| 5352 | * Undo the effects of the last redirection. |
| 5353 | */ |
| 5354 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5355 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5356 | { |
| 5357 | struct redirtab *rp; |
| 5358 | int i; |
| 5359 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5360 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5361 | return; |
| 5362 | INT_OFF; |
| 5363 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5364 | for (i = 0; i < rp->pair_count; i++) { |
| 5365 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5366 | int copy = rp->two_fd[i].copy; |
| 5367 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5368 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5369 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5370 | continue; |
| 5371 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5372 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5373 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5374 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5375 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5376 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5377 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5378 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5379 | } |
| 5380 | } |
| 5381 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5382 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5383 | free(rp); |
| 5384 | INT_ON; |
| 5385 | } |
| 5386 | |
| 5387 | /* |
| 5388 | * Undo all redirections. Called on error or interrupt. |
| 5389 | */ |
| 5390 | |
| 5391 | /* |
| 5392 | * Discard all saved file descriptors. |
| 5393 | */ |
| 5394 | static void |
| 5395 | clearredir(int drop) |
| 5396 | { |
| 5397 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5398 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5399 | if (!redirlist) |
| 5400 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5401 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5402 | } |
| 5403 | } |
| 5404 | |
| 5405 | static int |
| 5406 | redirectsafe(union node *redir, int flags) |
| 5407 | { |
| 5408 | int err; |
| 5409 | volatile int saveint; |
| 5410 | struct jmploc *volatile savehandler = exception_handler; |
| 5411 | struct jmploc jmploc; |
| 5412 | |
| 5413 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5414 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5415 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5416 | if (!err) { |
| 5417 | exception_handler = &jmploc; |
| 5418 | redirect(redir, flags); |
| 5419 | } |
| 5420 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5421 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5422 | longjmp(exception_handler->loc, 1); |
| 5423 | RESTORE_INT(saveint); |
| 5424 | return err; |
| 5425 | } |
| 5426 | |
| 5427 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5428 | /* ============ Routines to expand arguments to commands |
| 5429 | * |
| 5430 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5431 | */ |
| 5432 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5433 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5434 | static arith_t |
| 5435 | ash_arith(const char *s) |
| 5436 | { |
| 5437 | arith_eval_hooks_t math_hooks; |
| 5438 | arith_t result; |
| 5439 | int errcode = 0; |
| 5440 | |
| 5441 | math_hooks.lookupvar = lookupvar; |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 5442 | math_hooks.setvar = setvar2; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5443 | math_hooks.endofname = endofname; |
| 5444 | |
| 5445 | INT_OFF; |
| 5446 | result = arith(s, &errcode, &math_hooks); |
| 5447 | if (errcode < 0) { |
| 5448 | if (errcode == -3) |
| 5449 | ash_msg_and_raise_error("exponent less than 0"); |
| 5450 | if (errcode == -2) |
| 5451 | ash_msg_and_raise_error("divide by zero"); |
| 5452 | if (errcode == -5) |
| 5453 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5454 | raise_error_syntax(s); |
| 5455 | } |
| 5456 | INT_ON; |
| 5457 | |
| 5458 | return result; |
| 5459 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5460 | #endif |
| 5461 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5462 | /* |
| 5463 | * expandarg flags |
| 5464 | */ |
| 5465 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5466 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5467 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5468 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5469 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5470 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5471 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5472 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5473 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5474 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5475 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5476 | */ |
| 5477 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5478 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5479 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5480 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5481 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5482 | |
| 5483 | /* |
| 5484 | * Structure specifying which parts of the string should be searched |
| 5485 | * for IFS characters. |
| 5486 | */ |
| 5487 | struct ifsregion { |
| 5488 | struct ifsregion *next; /* next region in list */ |
| 5489 | int begoff; /* offset of start of region */ |
| 5490 | int endoff; /* offset of end of region */ |
| 5491 | int nulonly; /* search for nul bytes only */ |
| 5492 | }; |
| 5493 | |
| 5494 | struct arglist { |
| 5495 | struct strlist *list; |
| 5496 | struct strlist **lastp; |
| 5497 | }; |
| 5498 | |
| 5499 | /* output of current string */ |
| 5500 | static char *expdest; |
| 5501 | /* list of back quote expressions */ |
| 5502 | static struct nodelist *argbackq; |
| 5503 | /* first struct in list of ifs regions */ |
| 5504 | static struct ifsregion ifsfirst; |
| 5505 | /* last struct in list */ |
| 5506 | static struct ifsregion *ifslastp; |
| 5507 | /* holds expanded arg list */ |
| 5508 | static struct arglist exparg; |
| 5509 | |
| 5510 | /* |
| 5511 | * Our own itoa(). |
| 5512 | */ |
| 5513 | static int |
| 5514 | cvtnum(arith_t num) |
| 5515 | { |
| 5516 | int len; |
| 5517 | |
| 5518 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5519 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5520 | STADJUST(len, expdest); |
| 5521 | return len; |
| 5522 | } |
| 5523 | |
| 5524 | static size_t |
| 5525 | esclen(const char *start, const char *p) |
| 5526 | { |
| 5527 | size_t esc = 0; |
| 5528 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5529 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5530 | esc++; |
| 5531 | } |
| 5532 | return esc; |
| 5533 | } |
| 5534 | |
| 5535 | /* |
| 5536 | * Remove any CTLESC characters from a string. |
| 5537 | */ |
| 5538 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5539 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5540 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5541 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5542 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5543 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5544 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5545 | unsigned protect_against_glob; |
| 5546 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5547 | |
| 5548 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5549 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5550 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5551 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5552 | q = p; |
| 5553 | r = str; |
| 5554 | if (flag & RMESCAPE_ALLOC) { |
| 5555 | size_t len = p - str; |
| 5556 | size_t fulllen = len + strlen(p) + 1; |
| 5557 | |
| 5558 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5559 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5560 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5561 | /* p and str may be invalidated by makestrspace */ |
| 5562 | str = (char *)stackblock() + strloc; |
| 5563 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5564 | } else if (flag & RMESCAPE_HEAP) { |
| 5565 | r = ckmalloc(fulllen); |
| 5566 | } else { |
| 5567 | r = stalloc(fulllen); |
| 5568 | } |
| 5569 | q = r; |
| 5570 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5571 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5572 | } |
| 5573 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5574 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5575 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5576 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5577 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5578 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5579 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5580 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5581 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5582 | // Note: both inquotes and protect_against_glob only affect whether |
| 5583 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5584 | inquotes = ~inquotes; |
| 5585 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5586 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5587 | continue; |
| 5588 | } |
| 5589 | if (*p == '\\') { |
| 5590 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5591 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5592 | goto copy; |
| 5593 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5594 | if ((unsigned char)*p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5595 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5596 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5597 | *q++ = '\\'; |
| 5598 | } |
| 5599 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5600 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5601 | copy: |
| 5602 | *q++ = *p++; |
| 5603 | } |
| 5604 | *q = '\0'; |
| 5605 | if (flag & RMESCAPE_GROW) { |
| 5606 | expdest = r; |
| 5607 | STADJUST(q - r + 1, expdest); |
| 5608 | } |
| 5609 | return r; |
| 5610 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5611 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5612 | |
| 5613 | /* |
| 5614 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5615 | * |
| 5616 | * Returns an stalloced string. |
| 5617 | */ |
| 5618 | static char * |
| 5619 | preglob(const char *pattern, int quoted, int flag) |
| 5620 | { |
| 5621 | flag |= RMESCAPE_GLOB; |
| 5622 | if (quoted) { |
| 5623 | flag |= RMESCAPE_QUOTED; |
| 5624 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5625 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5626 | } |
| 5627 | |
| 5628 | /* |
| 5629 | * Put a string on the stack. |
| 5630 | */ |
| 5631 | static void |
| 5632 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5633 | { |
| 5634 | char *q = expdest; |
| 5635 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5636 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5637 | |
| 5638 | while (len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5639 | unsigned char c = *p++; |
| 5640 | if (c == '\0') |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5641 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5642 | if (quotes) { |
| 5643 | int n = SIT(c, syntax); |
| 5644 | if (n == CCTL || n == CBACK) |
| 5645 | USTPUTC(CTLESC, q); |
| 5646 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5647 | USTPUTC(c, q); |
| 5648 | } |
| 5649 | |
| 5650 | expdest = q; |
| 5651 | } |
| 5652 | |
| 5653 | static void |
| 5654 | strtodest(const char *p, int syntax, int quotes) |
| 5655 | { |
| 5656 | memtodest(p, strlen(p), syntax, quotes); |
| 5657 | } |
| 5658 | |
| 5659 | /* |
| 5660 | * Record the fact that we have to scan this region of the |
| 5661 | * string for IFS characters. |
| 5662 | */ |
| 5663 | static void |
| 5664 | recordregion(int start, int end, int nulonly) |
| 5665 | { |
| 5666 | struct ifsregion *ifsp; |
| 5667 | |
| 5668 | if (ifslastp == NULL) { |
| 5669 | ifsp = &ifsfirst; |
| 5670 | } else { |
| 5671 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5672 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5673 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5674 | ifslastp->next = ifsp; |
| 5675 | INT_ON; |
| 5676 | } |
| 5677 | ifslastp = ifsp; |
| 5678 | ifslastp->begoff = start; |
| 5679 | ifslastp->endoff = end; |
| 5680 | ifslastp->nulonly = nulonly; |
| 5681 | } |
| 5682 | |
| 5683 | static void |
| 5684 | removerecordregions(int endoff) |
| 5685 | { |
| 5686 | if (ifslastp == NULL) |
| 5687 | return; |
| 5688 | |
| 5689 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5690 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5691 | struct ifsregion *ifsp; |
| 5692 | INT_OFF; |
| 5693 | ifsp = ifsfirst.next->next; |
| 5694 | free(ifsfirst.next); |
| 5695 | ifsfirst.next = ifsp; |
| 5696 | INT_ON; |
| 5697 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5698 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5699 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5700 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5701 | ifslastp = &ifsfirst; |
| 5702 | ifsfirst.endoff = endoff; |
| 5703 | } |
| 5704 | return; |
| 5705 | } |
| 5706 | |
| 5707 | ifslastp = &ifsfirst; |
| 5708 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5709 | ifslastp = ifslastp->next; |
| 5710 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5711 | struct ifsregion *ifsp; |
| 5712 | INT_OFF; |
| 5713 | ifsp = ifslastp->next->next; |
| 5714 | free(ifslastp->next); |
| 5715 | ifslastp->next = ifsp; |
| 5716 | INT_ON; |
| 5717 | } |
| 5718 | if (ifslastp->endoff > endoff) |
| 5719 | ifslastp->endoff = endoff; |
| 5720 | } |
| 5721 | |
| 5722 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5723 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5724 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5725 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5726 | char *name; |
| 5727 | struct passwd *pw; |
| 5728 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5729 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5730 | int startloc; |
| 5731 | |
| 5732 | name = p + 1; |
| 5733 | |
| 5734 | while ((c = *++p) != '\0') { |
| 5735 | switch (c) { |
| 5736 | case CTLESC: |
| 5737 | return startp; |
| 5738 | case CTLQUOTEMARK: |
| 5739 | return startp; |
| 5740 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5741 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5742 | goto done; |
| 5743 | break; |
| 5744 | case '/': |
| 5745 | case CTLENDVAR: |
| 5746 | goto done; |
| 5747 | } |
| 5748 | } |
| 5749 | done: |
| 5750 | *p = '\0'; |
| 5751 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 5752 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5753 | } else { |
| 5754 | pw = getpwnam(name); |
| 5755 | if (pw == NULL) |
| 5756 | goto lose; |
| 5757 | home = pw->pw_dir; |
| 5758 | } |
| 5759 | if (!home || !*home) |
| 5760 | goto lose; |
| 5761 | *p = c; |
| 5762 | startloc = expdest - (char *)stackblock(); |
| 5763 | strtodest(home, SQSYNTAX, quotes); |
| 5764 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5765 | return p; |
| 5766 | lose: |
| 5767 | *p = c; |
| 5768 | return startp; |
| 5769 | } |
| 5770 | |
| 5771 | /* |
| 5772 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5773 | * want to save its output in a block obtained from malloc. Otherwise |
| 5774 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5775 | * Should be called with interrupts off. |
| 5776 | */ |
| 5777 | struct backcmd { /* result of evalbackcmd */ |
| 5778 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5779 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5780 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5781 | struct job *jp; /* job structure for command */ |
| 5782 | }; |
| 5783 | |
| 5784 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5785 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5786 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5787 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5788 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5789 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5790 | evalbackcmd(union node *n, struct backcmd *result) |
| 5791 | { |
| 5792 | int saveherefd; |
| 5793 | |
| 5794 | result->fd = -1; |
| 5795 | result->buf = NULL; |
| 5796 | result->nleft = 0; |
| 5797 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5798 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5799 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5800 | |
| 5801 | saveherefd = herefd; |
| 5802 | herefd = -1; |
| 5803 | |
| 5804 | { |
| 5805 | int pip[2]; |
| 5806 | struct job *jp; |
| 5807 | |
| 5808 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5809 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5810 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5811 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5812 | FORCE_INT_ON; |
| 5813 | close(pip[0]); |
| 5814 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5815 | /*close(1);*/ |
| 5816 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5817 | close(pip[1]); |
| 5818 | } |
| 5819 | eflag = 0; |
| 5820 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5821 | /* NOTREACHED */ |
| 5822 | } |
| 5823 | close(pip[1]); |
| 5824 | result->fd = pip[0]; |
| 5825 | result->jp = jp; |
| 5826 | } |
| 5827 | herefd = saveherefd; |
| 5828 | out: |
| 5829 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5830 | result->fd, result->buf, result->nleft, result->jp)); |
| 5831 | } |
| 5832 | |
| 5833 | /* |
| 5834 | * Expand stuff in backwards quotes. |
| 5835 | */ |
| 5836 | static void |
| 5837 | expbackq(union node *cmd, int quoted, int quotes) |
| 5838 | { |
| 5839 | struct backcmd in; |
| 5840 | int i; |
| 5841 | char buf[128]; |
| 5842 | char *p; |
| 5843 | char *dest; |
| 5844 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5845 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5846 | struct stackmark smark; |
| 5847 | |
| 5848 | INT_OFF; |
| 5849 | setstackmark(&smark); |
| 5850 | dest = expdest; |
| 5851 | startloc = dest - (char *)stackblock(); |
| 5852 | grabstackstr(dest); |
| 5853 | evalbackcmd(cmd, &in); |
| 5854 | popstackmark(&smark); |
| 5855 | |
| 5856 | p = in.buf; |
| 5857 | i = in.nleft; |
| 5858 | if (i == 0) |
| 5859 | goto read; |
| 5860 | for (;;) { |
| 5861 | memtodest(p, i, syntax, quotes); |
| 5862 | read: |
| 5863 | if (in.fd < 0) |
| 5864 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5865 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5866 | TRACE(("expbackq: read returns %d\n", i)); |
| 5867 | if (i <= 0) |
| 5868 | break; |
| 5869 | p = buf; |
| 5870 | } |
| 5871 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5872 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5873 | if (in.fd >= 0) { |
| 5874 | close(in.fd); |
| 5875 | back_exitstatus = waitforjob(in.jp); |
| 5876 | } |
| 5877 | INT_ON; |
| 5878 | |
| 5879 | /* Eat all trailing newlines */ |
| 5880 | dest = expdest; |
| 5881 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5882 | STUNPUTC(dest); |
| 5883 | expdest = dest; |
| 5884 | |
| 5885 | if (quoted == 0) |
| 5886 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5887 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 5888 | (int)((dest - (char *)stackblock()) - startloc), |
| 5889 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5890 | stackblock() + startloc)); |
| 5891 | } |
| 5892 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5893 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5894 | /* |
| 5895 | * Expand arithmetic expression. Backup to start of expression, |
| 5896 | * evaluate, place result in (backed up) result, adjust string position. |
| 5897 | */ |
| 5898 | static void |
| 5899 | expari(int quotes) |
| 5900 | { |
| 5901 | char *p, *start; |
| 5902 | int begoff; |
| 5903 | int flag; |
| 5904 | int len; |
| 5905 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5906 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5907 | |
| 5908 | /* |
| 5909 | * This routine is slightly over-complicated for |
| 5910 | * efficiency. Next we scan backwards looking for the |
| 5911 | * start of arithmetic. |
| 5912 | */ |
| 5913 | start = stackblock(); |
| 5914 | p = expdest - 1; |
| 5915 | *p = '\0'; |
| 5916 | p--; |
| 5917 | do { |
| 5918 | int esc; |
| 5919 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5920 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5921 | p--; |
| 5922 | #if DEBUG |
| 5923 | if (p < start) { |
| 5924 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5925 | } |
| 5926 | #endif |
| 5927 | } |
| 5928 | |
| 5929 | esc = esclen(start, p); |
| 5930 | if (!(esc % 2)) { |
| 5931 | break; |
| 5932 | } |
| 5933 | |
| 5934 | p -= esc + 1; |
| 5935 | } while (1); |
| 5936 | |
| 5937 | begoff = p - start; |
| 5938 | |
| 5939 | removerecordregions(begoff); |
| 5940 | |
| 5941 | flag = p[1]; |
| 5942 | |
| 5943 | expdest = p; |
| 5944 | |
| 5945 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5946 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5947 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5948 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5949 | |
| 5950 | if (flag != '"') |
| 5951 | recordregion(begoff, begoff + len, 0); |
| 5952 | } |
| 5953 | #endif |
| 5954 | |
| 5955 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5956 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5957 | |
| 5958 | /* |
| 5959 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5960 | * characters to allow for further processing. Otherwise treat |
| 5961 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5962 | * |
| 5963 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5964 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5965 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5966 | */ |
| 5967 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5968 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5969 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5970 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5971 | '=', |
| 5972 | ':', |
| 5973 | CTLQUOTEMARK, |
| 5974 | CTLENDVAR, |
| 5975 | CTLESC, |
| 5976 | CTLVAR, |
| 5977 | CTLBACKQ, |
| 5978 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5979 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5980 | CTLENDARI, |
| 5981 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5982 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5983 | }; |
| 5984 | const char *reject = spclchars; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5985 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 5986 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5987 | int inquotes; |
| 5988 | size_t length; |
| 5989 | int startloc; |
| 5990 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5991 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5992 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5993 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5994 | reject++; |
| 5995 | } |
| 5996 | inquotes = 0; |
| 5997 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5998 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5999 | char *q; |
| 6000 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6001 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6002 | tilde: |
| 6003 | q = p; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6004 | if (*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6005 | q++; |
| 6006 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6007 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6008 | } |
| 6009 | start: |
| 6010 | startloc = expdest - (char *)stackblock(); |
| 6011 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6012 | unsigned char c; |
| 6013 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6014 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6015 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6016 | if (c) { |
| 6017 | if (!(c & 0x80) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6018 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6019 | || c == CTLENDARI |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6020 | #endif |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6021 | ) { |
| 6022 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 6023 | length++; |
| 6024 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6025 | } |
| 6026 | if (length > 0) { |
| 6027 | int newloc; |
| 6028 | expdest = stack_nputstr(p, length, expdest); |
| 6029 | newloc = expdest - (char *)stackblock(); |
| 6030 | if (breakall && !inquotes && newloc > startloc) { |
| 6031 | recordregion(startloc, newloc, 0); |
| 6032 | } |
| 6033 | startloc = newloc; |
| 6034 | } |
| 6035 | p += length + 1; |
| 6036 | length = 0; |
| 6037 | |
| 6038 | switch (c) { |
| 6039 | case '\0': |
| 6040 | goto breakloop; |
| 6041 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6042 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6043 | p--; |
| 6044 | continue; |
| 6045 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6046 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6047 | reject++; |
| 6048 | /* fall through */ |
| 6049 | case ':': |
| 6050 | /* |
| 6051 | * sort of a hack - expand tildes in variable |
| 6052 | * assignments (after the first '=' and after ':'s). |
| 6053 | */ |
| 6054 | if (*--p == '~') { |
| 6055 | goto tilde; |
| 6056 | } |
| 6057 | continue; |
| 6058 | } |
| 6059 | |
| 6060 | switch (c) { |
| 6061 | case CTLENDVAR: /* ??? */ |
| 6062 | goto breakloop; |
| 6063 | case CTLQUOTEMARK: |
| 6064 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6065 | if (!inquotes |
| 6066 | && memcmp(p, dolatstr, 4) == 0 |
| 6067 | && ( p[4] == CTLQUOTEMARK |
| 6068 | || (p[4] == CTLENDVAR && p[5] == CTLQUOTEMARK) |
| 6069 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6070 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6071 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6072 | goto start; |
| 6073 | } |
| 6074 | inquotes = !inquotes; |
| 6075 | addquote: |
| 6076 | if (quotes) { |
| 6077 | p--; |
| 6078 | length++; |
| 6079 | startloc++; |
| 6080 | } |
| 6081 | break; |
| 6082 | case CTLESC: |
| 6083 | startloc++; |
| 6084 | length++; |
| 6085 | goto addquote; |
| 6086 | case CTLVAR: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6087 | p = evalvar(p, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6088 | goto start; |
| 6089 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6090 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6091 | case CTLBACKQ|CTLQUOTE: |
| 6092 | expbackq(argbackq->n, c, quotes); |
| 6093 | argbackq = argbackq->next; |
| 6094 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6095 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6096 | case CTLENDARI: |
| 6097 | p--; |
| 6098 | expari(quotes); |
| 6099 | goto start; |
| 6100 | #endif |
| 6101 | } |
| 6102 | } |
| 6103 | breakloop: |
| 6104 | ; |
| 6105 | } |
| 6106 | |
| 6107 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6108 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6109 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6110 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6111 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6112 | char c; |
| 6113 | |
| 6114 | loc = startp; |
| 6115 | loc2 = rmesc; |
| 6116 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6117 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6118 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6119 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6120 | c = *loc2; |
| 6121 | if (zero) { |
| 6122 | *loc2 = '\0'; |
| 6123 | s = rmesc; |
| 6124 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6125 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6126 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6127 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6128 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6129 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6130 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6131 | loc++; |
| 6132 | loc++; |
| 6133 | loc2++; |
| 6134 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6135 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6139 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6140 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6141 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6142 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6143 | int try2optimize = match_at_start; |
| 6144 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6145 | int esc = 0; |
| 6146 | char *loc; |
| 6147 | char *loc2; |
| 6148 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6149 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6150 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6151 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6152 | * Logic: |
| 6153 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6154 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6155 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6156 | */ |
| 6157 | /* TODO: document in what other circumstances we are called. */ |
| 6158 | |
| 6159 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6160 | int match; |
| 6161 | char c = *loc2; |
| 6162 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6163 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6164 | *loc2 = '\0'; |
| 6165 | s = rmesc; |
| 6166 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6167 | match = pmatch(pattern, s); |
| 6168 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6169 | *loc2 = c; |
| 6170 | if (match) |
| 6171 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6172 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6173 | if (try2optimize) { |
| 6174 | /* Maybe we can optimize this: |
| 6175 | * if pattern ends with unescaped *, we can avoid checking |
| 6176 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6177 | * it wont match truncated "raw_value_of_" strings too. |
| 6178 | */ |
| 6179 | unsigned plen = strlen(pattern); |
| 6180 | /* Does it end with "*"? */ |
| 6181 | if (plen != 0 && pattern[--plen] == '*') { |
| 6182 | /* "xxxx*" is not escaped */ |
| 6183 | /* "xxx\*" is escaped */ |
| 6184 | /* "xx\\*" is not escaped */ |
| 6185 | /* "x\\\*" is escaped */ |
| 6186 | int slashes = 0; |
| 6187 | while (plen != 0 && pattern[--plen] == '\\') |
| 6188 | slashes++; |
| 6189 | if (!(slashes & 1)) |
| 6190 | break; /* ends with unescaped "*" */ |
| 6191 | } |
| 6192 | try2optimize = 0; |
| 6193 | } |
| 6194 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6195 | loc--; |
| 6196 | if (quotes) { |
| 6197 | if (--esc < 0) { |
| 6198 | esc = esclen(startp, loc); |
| 6199 | } |
| 6200 | if (esc % 2) { |
| 6201 | esc--; |
| 6202 | loc--; |
| 6203 | } |
| 6204 | } |
| 6205 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6206 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6207 | } |
| 6208 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6209 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6210 | static void |
| 6211 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6212 | { |
| 6213 | const char *msg; |
| 6214 | const char *tail; |
| 6215 | |
| 6216 | tail = nullstr; |
| 6217 | msg = "parameter not set"; |
| 6218 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6219 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6220 | if (varflags & VSNUL) |
| 6221 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6222 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6223 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6224 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6225 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6226 | 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] | 6227 | } |
| 6228 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6229 | #if ENABLE_ASH_BASH_COMPAT |
| 6230 | static char * |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6231 | parse_sub_pattern(char *arg, int varflags) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6232 | { |
| 6233 | char *idx, *repl = NULL; |
| 6234 | unsigned char c; |
| 6235 | |
Denys Vlasenko | 1614900 | 2010-08-06 22:06:21 +0200 | [diff] [blame] | 6236 | //char *org_arg = arg; |
| 6237 | //bb_error_msg("arg:'%s'", arg); |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6238 | idx = arg; |
| 6239 | while (1) { |
| 6240 | c = *arg; |
| 6241 | if (!c) |
| 6242 | break; |
| 6243 | if (c == '/') { |
| 6244 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6245 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6246 | repl = idx + 1; |
| 6247 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6248 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6249 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6250 | *idx++ = c; |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6251 | if (!(varflags & VSQUOTE) && c == '\\' && arg[1] == '\\') |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6252 | arg++; /* skip both \\, not just first one */ |
| 6253 | arg++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6254 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6255 | *idx = c; /* NUL */ |
Denys Vlasenko | 1614900 | 2010-08-06 22:06:21 +0200 | [diff] [blame] | 6256 | //bb_error_msg("pattern:'%s' repl:'%s'", org_arg, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6257 | |
| 6258 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6259 | } |
| 6260 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6261 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6262 | static const char * |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6263 | subevalvar(char *p, char *varname, int strloc, int subtype, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6264 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6265 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6266 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6267 | char *startp; |
| 6268 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6269 | char *rmesc, *rmescend; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6270 | char *str; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6271 | IF_ASH_BASH_COMPAT(const char *repl = NULL;) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6272 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6273 | int saveherefd = herefd; |
| 6274 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6275 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6276 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6277 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6278 | //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d", |
| 6279 | // p, varname, strloc, subtype, startloc, varflags, quotes); |
| 6280 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6281 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6282 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6283 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6284 | STPUTC('\0', expdest); |
| 6285 | herefd = saveherefd; |
| 6286 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6287 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6288 | |
| 6289 | switch (subtype) { |
| 6290 | case VSASSIGN: |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6291 | setvar(varname, startp, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6292 | amount = startp - expdest; |
| 6293 | STADJUST(amount, expdest); |
| 6294 | return startp; |
| 6295 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6296 | case VSQUESTION: |
| 6297 | varunset(p, varname, startp, varflags); |
| 6298 | /* NOTREACHED */ |
| 6299 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6300 | #if ENABLE_ASH_BASH_COMPAT |
| 6301 | case VSSUBSTR: |
| 6302 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6303 | /* Read POS in ${var:POS:LEN} */ |
| 6304 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6305 | len = str - startp - 1; |
| 6306 | |
| 6307 | /* *loc != '\0', guaranteed by parser */ |
| 6308 | if (quotes) { |
| 6309 | char *ptr; |
| 6310 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6311 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6312 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6313 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6314 | len--; |
| 6315 | ptr++; |
| 6316 | } |
| 6317 | } |
| 6318 | } |
| 6319 | orig_len = len; |
| 6320 | |
| 6321 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6322 | /* ${var::LEN} */ |
| 6323 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6324 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6325 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6326 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6327 | while (*loc && *loc != ':') { |
| 6328 | /* TODO? |
| 6329 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6330 | if (!isdigit(*loc)) |
| 6331 | ash_msg_and_raise_error(msg_illnum, str); |
| 6332 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6333 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6334 | } |
| 6335 | if (*loc++ == ':') { |
| 6336 | len = number(loc); |
| 6337 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6338 | } |
| 6339 | if (pos >= orig_len) { |
| 6340 | pos = 0; |
| 6341 | len = 0; |
| 6342 | } |
| 6343 | if (len > (orig_len - pos)) |
| 6344 | len = orig_len - pos; |
| 6345 | |
| 6346 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6347 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6348 | str++; |
| 6349 | } |
| 6350 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6351 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6352 | *loc++ = *str++; |
| 6353 | *loc++ = *str++; |
| 6354 | } |
| 6355 | *loc = '\0'; |
| 6356 | amount = loc - expdest; |
| 6357 | STADJUST(amount, expdest); |
| 6358 | return loc; |
| 6359 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6360 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6361 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6362 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6363 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6364 | /* We'll comeback here if we grow the stack while handling |
| 6365 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6366 | * stack will need rebasing, and we'll need to remove our work |
| 6367 | * areas each time |
| 6368 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6369 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6370 | |
| 6371 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6372 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6373 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6374 | |
| 6375 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6376 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6377 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6378 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6379 | if (rmesc != startp) { |
| 6380 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6381 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6382 | } |
| 6383 | } |
| 6384 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6385 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6386 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6387 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6388 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6389 | #if ENABLE_ASH_BASH_COMPAT |
| 6390 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6391 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6392 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6393 | if (!repl) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6394 | repl = parse_sub_pattern(str, varflags); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6395 | //bb_error_msg("repl:'%s'", repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6396 | if (!repl) |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6397 | repl = nullstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
| 6400 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6401 | if (str[0] == '\0') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6402 | return NULL; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6403 | |
| 6404 | len = 0; |
| 6405 | idx = startp; |
| 6406 | end = str - 1; |
| 6407 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6408 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6409 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6410 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6411 | if (!loc) { |
| 6412 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6413 | char *restart_detect = stackblock(); |
| 6414 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6415 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6416 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6417 | idx++; |
| 6418 | len++; |
| 6419 | STPUTC(*idx, expdest); |
| 6420 | } |
| 6421 | if (stackblock() != restart_detect) |
| 6422 | goto restart; |
| 6423 | idx++; |
| 6424 | len++; |
| 6425 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6426 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6427 | if (idx >= end) |
| 6428 | break; |
| 6429 | if (str[0] == '*') { |
| 6430 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6431 | * it would never match "ong_string" etc, no point in trying. |
| 6432 | */ |
| 6433 | goto skip_matching; |
| 6434 | } |
| 6435 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6436 | } |
| 6437 | |
| 6438 | if (subtype == VSREPLACEALL) { |
| 6439 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6440 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6441 | idx++; |
| 6442 | idx++; |
| 6443 | rmesc++; |
| 6444 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6445 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6446 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6447 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6448 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6449 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6450 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6451 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6452 | if (quotes && *loc == '\\') { |
| 6453 | STPUTC(CTLESC, expdest); |
| 6454 | len++; |
| 6455 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6456 | STPUTC(*loc, expdest); |
| 6457 | if (stackblock() != restart_detect) |
| 6458 | goto restart; |
| 6459 | len++; |
| 6460 | } |
| 6461 | |
| 6462 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6463 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6464 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6465 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6466 | STPUTC(*idx, expdest); |
| 6467 | if (stackblock() != restart_detect) |
| 6468 | goto restart; |
| 6469 | len++; |
| 6470 | idx++; |
| 6471 | } |
| 6472 | break; |
| 6473 | } |
| 6474 | } |
| 6475 | |
| 6476 | /* We've put the replaced text into a buffer at workloc, now |
| 6477 | * move it to the right place and adjust the stack. |
| 6478 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6479 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6480 | startp = (char *)stackblock() + startloc; |
| 6481 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6482 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6483 | amount = expdest - (startp + len); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6484 | STADJUST(-amount, expdest); |
| 6485 | return startp; |
| 6486 | } |
| 6487 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6488 | |
| 6489 | subtype -= VSTRIMRIGHT; |
| 6490 | #if DEBUG |
| 6491 | if (subtype < 0 || subtype > 7) |
| 6492 | abort(); |
| 6493 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6494 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6495 | zero = subtype >> 1; |
| 6496 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6497 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6498 | |
| 6499 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6500 | if (loc) { |
| 6501 | if (zero) { |
| 6502 | memmove(startp, loc, str - loc); |
| 6503 | loc = startp + (str - loc) - 1; |
| 6504 | } |
| 6505 | *loc = '\0'; |
| 6506 | amount = loc - expdest; |
| 6507 | STADJUST(amount, expdest); |
| 6508 | } |
| 6509 | return loc; |
| 6510 | } |
| 6511 | |
| 6512 | /* |
| 6513 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6514 | * name parameter (examples): |
| 6515 | * ash -c 'echo $1' name:'1=' |
| 6516 | * ash -c 'echo $qwe' name:'qwe=' |
| 6517 | * ash -c 'echo $$' name:'$=' |
| 6518 | * ash -c 'echo ${$}' name:'$=' |
| 6519 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6520 | * ash -c 'echo ${#$}' name:'$=' |
| 6521 | * note: examples with bad shell syntax: |
| 6522 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6523 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6524 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6525 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6526 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6527 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6528 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6529 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6530 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6531 | int sepq = 0; |
| 6532 | ssize_t len = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6533 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6534 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6535 | int quoted = varflags & VSQUOTE; |
| 6536 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6537 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6538 | switch (*name) { |
| 6539 | case '$': |
| 6540 | num = rootpid; |
| 6541 | goto numvar; |
| 6542 | case '?': |
| 6543 | num = exitstatus; |
| 6544 | goto numvar; |
| 6545 | case '#': |
| 6546 | num = shellparam.nparam; |
| 6547 | goto numvar; |
| 6548 | case '!': |
| 6549 | num = backgndpid; |
| 6550 | if (num == 0) |
| 6551 | return -1; |
| 6552 | numvar: |
| 6553 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6554 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6555 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6556 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6557 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6558 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6559 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6560 | len++; |
| 6561 | } |
| 6562 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6563 | check_1char_name: |
| 6564 | #if 0 |
| 6565 | /* handles cases similar to ${#$1} */ |
| 6566 | if (name[2] != '\0') |
| 6567 | raise_error_syntax("bad substitution"); |
| 6568 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6569 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6570 | case '@': { |
| 6571 | char **ap; |
| 6572 | int sep; |
| 6573 | |
| 6574 | if (quoted && (flags & EXP_FULL)) { |
| 6575 | /* note: this is not meant as PEOF value */ |
| 6576 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6577 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6578 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6579 | /* fall through */ |
| 6580 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6581 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6582 | i = SIT(sep, syntax); |
| 6583 | if (quotes && (i == CCTL || i == CBACK)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6584 | sepq = 1; |
| 6585 | param: |
| 6586 | ap = shellparam.p; |
| 6587 | if (!ap) |
| 6588 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6589 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6590 | size_t partlen; |
| 6591 | |
| 6592 | partlen = strlen(p); |
| 6593 | len += partlen; |
| 6594 | |
| 6595 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6596 | memtodest(p, partlen, syntax, quotes); |
| 6597 | |
| 6598 | if (*ap && sep) { |
| 6599 | char *q; |
| 6600 | |
| 6601 | len++; |
| 6602 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6603 | continue; |
| 6604 | } |
| 6605 | q = expdest; |
| 6606 | if (sepq) |
| 6607 | STPUTC(CTLESC, q); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6608 | /* note: may put NUL despite sep != 0 |
| 6609 | * (see sep = 1 << CHAR_BIT above) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6610 | STPUTC(sep, q); |
| 6611 | expdest = q; |
| 6612 | } |
| 6613 | } |
| 6614 | return len; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6615 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6616 | case '0': |
| 6617 | case '1': |
| 6618 | case '2': |
| 6619 | case '3': |
| 6620 | case '4': |
| 6621 | case '5': |
| 6622 | case '6': |
| 6623 | case '7': |
| 6624 | case '8': |
| 6625 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6626 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6627 | if (num < 0 || num > shellparam.nparam) |
| 6628 | return -1; |
| 6629 | p = num ? shellparam.p[num - 1] : arg0; |
| 6630 | goto value; |
| 6631 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6632 | /* NB: name has form "VAR=..." */ |
| 6633 | |
| 6634 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6635 | * which should be considered before we check variables. */ |
| 6636 | if (var_str_list) { |
| 6637 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6638 | p = NULL; |
| 6639 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6640 | char *str, *eq; |
| 6641 | str = var_str_list->text; |
| 6642 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6643 | if (!eq) /* stop at first non-assignment */ |
| 6644 | break; |
| 6645 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6646 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6647 | && strncmp(str, name, name_len) == 0 |
| 6648 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6649 | p = eq; |
| 6650 | /* goto value; - WRONG! */ |
| 6651 | /* think "A=1 A=2 B=$A" */ |
| 6652 | } |
| 6653 | var_str_list = var_str_list->next; |
| 6654 | } while (var_str_list); |
| 6655 | if (p) |
| 6656 | goto value; |
| 6657 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6658 | p = lookupvar(name); |
| 6659 | value: |
| 6660 | if (!p) |
| 6661 | return -1; |
| 6662 | |
| 6663 | len = strlen(p); |
| 6664 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6665 | memtodest(p, len, syntax, quotes); |
| 6666 | return len; |
| 6667 | } |
| 6668 | |
| 6669 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6670 | STADJUST(-len, expdest); |
| 6671 | return len; |
| 6672 | } |
| 6673 | |
| 6674 | /* |
| 6675 | * Expand a variable, and return a pointer to the next character in the |
| 6676 | * input string. |
| 6677 | */ |
| 6678 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6679 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6680 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6681 | char varflags; |
| 6682 | char subtype; |
| 6683 | char quoted; |
| 6684 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6685 | char *var; |
| 6686 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6687 | int startloc; |
| 6688 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6689 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6690 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6691 | subtype = varflags & VSTYPE; |
| 6692 | quoted = varflags & VSQUOTE; |
| 6693 | var = p; |
| 6694 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6695 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 6696 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6697 | |
| 6698 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6699 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6700 | if (varflags & VSNUL) |
| 6701 | varlen--; |
| 6702 | |
| 6703 | if (subtype == VSPLUS) { |
| 6704 | varlen = -1 - varlen; |
| 6705 | goto vsplus; |
| 6706 | } |
| 6707 | |
| 6708 | if (subtype == VSMINUS) { |
| 6709 | vsplus: |
| 6710 | if (varlen < 0) { |
| 6711 | argstr( |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6712 | p, flags | EXP_TILDE | |
| 6713 | (quoted ? EXP_QWORD : EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6714 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6715 | ); |
| 6716 | goto end; |
| 6717 | } |
| 6718 | if (easy) |
| 6719 | goto record; |
| 6720 | goto end; |
| 6721 | } |
| 6722 | |
| 6723 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6724 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6725 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6726 | subtype, startloc, varflags, |
| 6727 | /* quotes: */ 0, |
| 6728 | var_str_list) |
| 6729 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6730 | varflags &= ~VSNUL; |
| 6731 | /* |
| 6732 | * Remove any recorded regions beyond |
| 6733 | * start of variable |
| 6734 | */ |
| 6735 | removerecordregions(startloc); |
| 6736 | goto again; |
| 6737 | } |
| 6738 | goto end; |
| 6739 | } |
| 6740 | if (easy) |
| 6741 | goto record; |
| 6742 | goto end; |
| 6743 | } |
| 6744 | |
| 6745 | if (varlen < 0 && uflag) |
| 6746 | varunset(p, var, 0, 0); |
| 6747 | |
| 6748 | if (subtype == VSLENGTH) { |
| 6749 | cvtnum(varlen > 0 ? varlen : 0); |
| 6750 | goto record; |
| 6751 | } |
| 6752 | |
| 6753 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6754 | if (easy) |
| 6755 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6756 | goto end; |
| 6757 | } |
| 6758 | |
| 6759 | #if DEBUG |
| 6760 | switch (subtype) { |
| 6761 | case VSTRIMLEFT: |
| 6762 | case VSTRIMLEFTMAX: |
| 6763 | case VSTRIMRIGHT: |
| 6764 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6765 | #if ENABLE_ASH_BASH_COMPAT |
| 6766 | case VSSUBSTR: |
| 6767 | case VSREPLACE: |
| 6768 | case VSREPLACEALL: |
| 6769 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6770 | break; |
| 6771 | default: |
| 6772 | abort(); |
| 6773 | } |
| 6774 | #endif |
| 6775 | |
| 6776 | if (varlen >= 0) { |
| 6777 | /* |
| 6778 | * Terminate the string and start recording the pattern |
| 6779 | * right after it |
| 6780 | */ |
| 6781 | STPUTC('\0', expdest); |
| 6782 | patloc = expdest - (char *)stackblock(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6783 | if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6784 | startloc, varflags, |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6785 | //TODO: | EXP_REDIR too? All other such places do it too |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6786 | /* quotes: */ flags & (EXP_FULL | EXP_CASE), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6787 | var_str_list) |
| 6788 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6789 | int amount = expdest - ( |
| 6790 | (char *)stackblock() + patloc - 1 |
| 6791 | ); |
| 6792 | STADJUST(-amount, expdest); |
| 6793 | } |
| 6794 | /* Remove any recorded regions beyond start of variable */ |
| 6795 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6796 | record: |
| 6797 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | end: |
| 6801 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6802 | int nesting = 1; |
| 6803 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6804 | unsigned char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6805 | if (c == CTLESC) |
| 6806 | p++; |
| 6807 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6808 | if (varlen >= 0) |
| 6809 | argbackq = argbackq->next; |
| 6810 | } else if (c == CTLVAR) { |
| 6811 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6812 | nesting++; |
| 6813 | } else if (c == CTLENDVAR) { |
| 6814 | if (--nesting == 0) |
| 6815 | break; |
| 6816 | } |
| 6817 | } |
| 6818 | } |
| 6819 | return p; |
| 6820 | } |
| 6821 | |
| 6822 | /* |
| 6823 | * Break the argument string into pieces based upon IFS and add the |
| 6824 | * strings to the argument list. The regions of the string to be |
| 6825 | * searched for IFS characters have been stored by recordregion. |
| 6826 | */ |
| 6827 | static void |
| 6828 | ifsbreakup(char *string, struct arglist *arglist) |
| 6829 | { |
| 6830 | struct ifsregion *ifsp; |
| 6831 | struct strlist *sp; |
| 6832 | char *start; |
| 6833 | char *p; |
| 6834 | char *q; |
| 6835 | const char *ifs, *realifs; |
| 6836 | int ifsspc; |
| 6837 | int nulonly; |
| 6838 | |
| 6839 | start = string; |
| 6840 | if (ifslastp != NULL) { |
| 6841 | ifsspc = 0; |
| 6842 | nulonly = 0; |
| 6843 | realifs = ifsset() ? ifsval() : defifs; |
| 6844 | ifsp = &ifsfirst; |
| 6845 | do { |
| 6846 | p = string + ifsp->begoff; |
| 6847 | nulonly = ifsp->nulonly; |
| 6848 | ifs = nulonly ? nullstr : realifs; |
| 6849 | ifsspc = 0; |
| 6850 | while (p < string + ifsp->endoff) { |
| 6851 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6852 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6853 | p++; |
| 6854 | if (!strchr(ifs, *p)) { |
| 6855 | p++; |
| 6856 | continue; |
| 6857 | } |
| 6858 | if (!nulonly) |
| 6859 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6860 | /* Ignore IFS whitespace at start */ |
| 6861 | if (q == start && ifsspc) { |
| 6862 | p++; |
| 6863 | start = p; |
| 6864 | continue; |
| 6865 | } |
| 6866 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6867 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6868 | sp->text = start; |
| 6869 | *arglist->lastp = sp; |
| 6870 | arglist->lastp = &sp->next; |
| 6871 | p++; |
| 6872 | if (!nulonly) { |
| 6873 | for (;;) { |
| 6874 | if (p >= string + ifsp->endoff) { |
| 6875 | break; |
| 6876 | } |
| 6877 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6878 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6879 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6880 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6881 | p = q; |
| 6882 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6883 | } |
| 6884 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6885 | if (ifsspc) { |
| 6886 | p++; |
| 6887 | ifsspc = 0; |
| 6888 | } else { |
| 6889 | p = q; |
| 6890 | break; |
| 6891 | } |
| 6892 | } else |
| 6893 | p++; |
| 6894 | } |
| 6895 | } |
| 6896 | start = p; |
| 6897 | } /* while */ |
| 6898 | ifsp = ifsp->next; |
| 6899 | } while (ifsp != NULL); |
| 6900 | if (nulonly) |
| 6901 | goto add; |
| 6902 | } |
| 6903 | |
| 6904 | if (!*start) |
| 6905 | return; |
| 6906 | |
| 6907 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6908 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6909 | sp->text = start; |
| 6910 | *arglist->lastp = sp; |
| 6911 | arglist->lastp = &sp->next; |
| 6912 | } |
| 6913 | |
| 6914 | static void |
| 6915 | ifsfree(void) |
| 6916 | { |
| 6917 | struct ifsregion *p; |
| 6918 | |
| 6919 | INT_OFF; |
| 6920 | p = ifsfirst.next; |
| 6921 | do { |
| 6922 | struct ifsregion *ifsp; |
| 6923 | ifsp = p->next; |
| 6924 | free(p); |
| 6925 | p = ifsp; |
| 6926 | } while (p); |
| 6927 | ifslastp = NULL; |
| 6928 | ifsfirst.next = NULL; |
| 6929 | INT_ON; |
| 6930 | } |
| 6931 | |
| 6932 | /* |
| 6933 | * Add a file name to the list. |
| 6934 | */ |
| 6935 | static void |
| 6936 | addfname(const char *name) |
| 6937 | { |
| 6938 | struct strlist *sp; |
| 6939 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6940 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6941 | sp->text = ststrdup(name); |
| 6942 | *exparg.lastp = sp; |
| 6943 | exparg.lastp = &sp->next; |
| 6944 | } |
| 6945 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6946 | /* |
| 6947 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6948 | */ |
| 6949 | static void |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6950 | expmeta(char *expdir, char *enddir, char *name) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6951 | { |
| 6952 | char *p; |
| 6953 | const char *cp; |
| 6954 | char *start; |
| 6955 | char *endname; |
| 6956 | int metaflag; |
| 6957 | struct stat statb; |
| 6958 | DIR *dirp; |
| 6959 | struct dirent *dp; |
| 6960 | int atend; |
| 6961 | int matchdot; |
| 6962 | |
| 6963 | metaflag = 0; |
| 6964 | start = name; |
| 6965 | for (p = name; *p; p++) { |
| 6966 | if (*p == '*' || *p == '?') |
| 6967 | metaflag = 1; |
| 6968 | else if (*p == '[') { |
| 6969 | char *q = p + 1; |
| 6970 | if (*q == '!') |
| 6971 | q++; |
| 6972 | for (;;) { |
| 6973 | if (*q == '\\') |
| 6974 | q++; |
| 6975 | if (*q == '/' || *q == '\0') |
| 6976 | break; |
| 6977 | if (*++q == ']') { |
| 6978 | metaflag = 1; |
| 6979 | break; |
| 6980 | } |
| 6981 | } |
| 6982 | } else if (*p == '\\') |
| 6983 | p++; |
| 6984 | else if (*p == '/') { |
| 6985 | if (metaflag) |
| 6986 | goto out; |
| 6987 | start = p + 1; |
| 6988 | } |
| 6989 | } |
| 6990 | out: |
| 6991 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 6992 | if (enddir != expdir) |
| 6993 | metaflag++; |
| 6994 | p = name; |
| 6995 | do { |
| 6996 | if (*p == '\\') |
| 6997 | p++; |
| 6998 | *enddir++ = *p; |
| 6999 | } while (*p++); |
| 7000 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 7001 | addfname(expdir); |
| 7002 | return; |
| 7003 | } |
| 7004 | endname = p; |
| 7005 | if (name < start) { |
| 7006 | p = name; |
| 7007 | do { |
| 7008 | if (*p == '\\') |
| 7009 | p++; |
| 7010 | *enddir++ = *p++; |
| 7011 | } while (p < start); |
| 7012 | } |
| 7013 | if (enddir == expdir) { |
| 7014 | cp = "."; |
| 7015 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 7016 | cp = "/"; |
| 7017 | } else { |
| 7018 | cp = expdir; |
| 7019 | enddir[-1] = '\0'; |
| 7020 | } |
| 7021 | dirp = opendir(cp); |
| 7022 | if (dirp == NULL) |
| 7023 | return; |
| 7024 | if (enddir != expdir) |
| 7025 | enddir[-1] = '/'; |
| 7026 | if (*endname == 0) { |
| 7027 | atend = 1; |
| 7028 | } else { |
| 7029 | atend = 0; |
| 7030 | *endname++ = '\0'; |
| 7031 | } |
| 7032 | matchdot = 0; |
| 7033 | p = start; |
| 7034 | if (*p == '\\') |
| 7035 | p++; |
| 7036 | if (*p == '.') |
| 7037 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7038 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7039 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7040 | continue; |
| 7041 | if (pmatch(start, dp->d_name)) { |
| 7042 | if (atend) { |
| 7043 | strcpy(enddir, dp->d_name); |
| 7044 | addfname(expdir); |
| 7045 | } else { |
| 7046 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 7047 | continue; |
| 7048 | p[-1] = '/'; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7049 | expmeta(expdir, p, endname); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7050 | } |
| 7051 | } |
| 7052 | } |
| 7053 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7054 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7055 | endname[-1] = '/'; |
| 7056 | } |
| 7057 | |
| 7058 | static struct strlist * |
| 7059 | msort(struct strlist *list, int len) |
| 7060 | { |
| 7061 | struct strlist *p, *q = NULL; |
| 7062 | struct strlist **lpp; |
| 7063 | int half; |
| 7064 | int n; |
| 7065 | |
| 7066 | if (len <= 1) |
| 7067 | return list; |
| 7068 | half = len >> 1; |
| 7069 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7070 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7071 | q = p; |
| 7072 | p = p->next; |
| 7073 | } |
| 7074 | q->next = NULL; /* terminate first half of list */ |
| 7075 | q = msort(list, half); /* sort first half of list */ |
| 7076 | p = msort(p, len - half); /* sort second half */ |
| 7077 | lpp = &list; |
| 7078 | for (;;) { |
| 7079 | #if ENABLE_LOCALE_SUPPORT |
| 7080 | if (strcoll(p->text, q->text) < 0) |
| 7081 | #else |
| 7082 | if (strcmp(p->text, q->text) < 0) |
| 7083 | #endif |
| 7084 | { |
| 7085 | *lpp = p; |
| 7086 | lpp = &p->next; |
| 7087 | p = *lpp; |
| 7088 | if (p == NULL) { |
| 7089 | *lpp = q; |
| 7090 | break; |
| 7091 | } |
| 7092 | } else { |
| 7093 | *lpp = q; |
| 7094 | lpp = &q->next; |
| 7095 | q = *lpp; |
| 7096 | if (q == NULL) { |
| 7097 | *lpp = p; |
| 7098 | break; |
| 7099 | } |
| 7100 | } |
| 7101 | } |
| 7102 | return list; |
| 7103 | } |
| 7104 | |
| 7105 | /* |
| 7106 | * Sort the results of file name expansion. It calculates the number of |
| 7107 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7108 | * work. |
| 7109 | */ |
| 7110 | static struct strlist * |
| 7111 | expsort(struct strlist *str) |
| 7112 | { |
| 7113 | int len; |
| 7114 | struct strlist *sp; |
| 7115 | |
| 7116 | len = 0; |
| 7117 | for (sp = str; sp; sp = sp->next) |
| 7118 | len++; |
| 7119 | return msort(str, len); |
| 7120 | } |
| 7121 | |
| 7122 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7123 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7124 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7125 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7126 | '*', '?', '[', 0 |
| 7127 | }; |
| 7128 | /* TODO - EXP_REDIR */ |
| 7129 | |
| 7130 | while (str) { |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7131 | char *expdir; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7132 | struct strlist **savelastp; |
| 7133 | struct strlist *sp; |
| 7134 | char *p; |
| 7135 | |
| 7136 | if (fflag) |
| 7137 | goto nometa; |
| 7138 | if (!strpbrk(str->text, metachars)) |
| 7139 | goto nometa; |
| 7140 | savelastp = exparg.lastp; |
| 7141 | |
| 7142 | INT_OFF; |
| 7143 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7144 | { |
| 7145 | int i = strlen(str->text); |
| 7146 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 7147 | } |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7148 | expmeta(expdir, expdir, p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7149 | free(expdir); |
| 7150 | if (p != str->text) |
| 7151 | free(p); |
| 7152 | INT_ON; |
| 7153 | if (exparg.lastp == savelastp) { |
| 7154 | /* |
| 7155 | * no matches |
| 7156 | */ |
| 7157 | nometa: |
| 7158 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7159 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7160 | exparg.lastp = &str->next; |
| 7161 | } else { |
| 7162 | *exparg.lastp = NULL; |
| 7163 | *savelastp = sp = expsort(*savelastp); |
| 7164 | while (sp->next != NULL) |
| 7165 | sp = sp->next; |
| 7166 | exparg.lastp = &sp->next; |
| 7167 | } |
| 7168 | str = str->next; |
| 7169 | } |
| 7170 | } |
| 7171 | |
| 7172 | /* |
| 7173 | * Perform variable substitution and command substitution on an argument, |
| 7174 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7175 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7176 | * here document expansion. |
| 7177 | */ |
| 7178 | static void |
| 7179 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7180 | { |
| 7181 | struct strlist *sp; |
| 7182 | char *p; |
| 7183 | |
| 7184 | argbackq = arg->narg.backquote; |
| 7185 | STARTSTACKSTR(expdest); |
| 7186 | ifsfirst.next = NULL; |
| 7187 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7188 | argstr(arg->narg.text, flag, |
| 7189 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7190 | p = _STPUTC('\0', expdest); |
| 7191 | expdest = p - 1; |
| 7192 | if (arglist == NULL) { |
| 7193 | return; /* here document expanded */ |
| 7194 | } |
| 7195 | p = grabstackstr(p); |
| 7196 | exparg.lastp = &exparg.list; |
| 7197 | /* |
| 7198 | * TODO - EXP_REDIR |
| 7199 | */ |
| 7200 | if (flag & EXP_FULL) { |
| 7201 | ifsbreakup(p, &exparg); |
| 7202 | *exparg.lastp = NULL; |
| 7203 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7204 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7205 | } else { |
| 7206 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7207 | rmescapes(p, 0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7208 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7209 | sp->text = p; |
| 7210 | *exparg.lastp = sp; |
| 7211 | exparg.lastp = &sp->next; |
| 7212 | } |
| 7213 | if (ifsfirst.next) |
| 7214 | ifsfree(); |
| 7215 | *exparg.lastp = NULL; |
| 7216 | if (exparg.list) { |
| 7217 | *arglist->lastp = exparg.list; |
| 7218 | arglist->lastp = exparg.lastp; |
| 7219 | } |
| 7220 | } |
| 7221 | |
| 7222 | /* |
| 7223 | * Expand shell variables and backquotes inside a here document. |
| 7224 | */ |
| 7225 | static void |
| 7226 | expandhere(union node *arg, int fd) |
| 7227 | { |
| 7228 | herefd = fd; |
| 7229 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7230 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7231 | } |
| 7232 | |
| 7233 | /* |
| 7234 | * Returns true if the pattern matches the string. |
| 7235 | */ |
| 7236 | static int |
| 7237 | patmatch(char *pattern, const char *string) |
| 7238 | { |
| 7239 | return pmatch(preglob(pattern, 0, 0), string); |
| 7240 | } |
| 7241 | |
| 7242 | /* |
| 7243 | * See if a pattern matches in a case statement. |
| 7244 | */ |
| 7245 | static int |
| 7246 | casematch(union node *pattern, char *val) |
| 7247 | { |
| 7248 | struct stackmark smark; |
| 7249 | int result; |
| 7250 | |
| 7251 | setstackmark(&smark); |
| 7252 | argbackq = pattern->narg.backquote; |
| 7253 | STARTSTACKSTR(expdest); |
| 7254 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7255 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7256 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7257 | STACKSTRNUL(expdest); |
| 7258 | result = patmatch(stackblock(), val); |
| 7259 | popstackmark(&smark); |
| 7260 | return result; |
| 7261 | } |
| 7262 | |
| 7263 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7264 | /* ============ find_command */ |
| 7265 | |
| 7266 | struct builtincmd { |
| 7267 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7268 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7269 | /* unsigned flags; */ |
| 7270 | }; |
| 7271 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7272 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7273 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7274 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7275 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7276 | |
| 7277 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7278 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7279 | union param { |
| 7280 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7281 | /* index >= 0 for commands without path (slashes) */ |
| 7282 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7283 | /* index == -1 for commands with slashes */ |
| 7284 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7285 | const struct builtincmd *cmd; |
| 7286 | struct funcnode *func; |
| 7287 | } u; |
| 7288 | }; |
| 7289 | /* values of cmdtype */ |
| 7290 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7291 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7292 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7293 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7294 | |
| 7295 | /* action to find_command() */ |
| 7296 | #define DO_ERR 0x01 /* prints errors */ |
| 7297 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7298 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7299 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7300 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7301 | |
| 7302 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7303 | |
| 7304 | |
| 7305 | /* ============ Hashing commands */ |
| 7306 | |
| 7307 | /* |
| 7308 | * When commands are first encountered, they are entered in a hash table. |
| 7309 | * This ensures that a full path search will not have to be done for them |
| 7310 | * on each invocation. |
| 7311 | * |
| 7312 | * We should investigate converting to a linear search, even though that |
| 7313 | * would make the command name "hash" a misnomer. |
| 7314 | */ |
| 7315 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7316 | struct tblentry { |
| 7317 | struct tblentry *next; /* next entry in hash chain */ |
| 7318 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7319 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7320 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7321 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7322 | }; |
| 7323 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7324 | static struct tblentry **cmdtable; |
| 7325 | #define INIT_G_cmdtable() do { \ |
| 7326 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7327 | } while (0) |
| 7328 | |
| 7329 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7330 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7331 | |
| 7332 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7333 | 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] | 7334 | { |
| 7335 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7336 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7337 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7338 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7339 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 7340 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7341 | while (*envp) |
| 7342 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7343 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7344 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7345 | /* re-exec ourselves with the new arguments */ |
| 7346 | execve(bb_busybox_exec_path, argv, envp); |
| 7347 | /* If they called chroot or otherwise made the binary no longer |
| 7348 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7349 | } |
| 7350 | #endif |
| 7351 | |
| 7352 | repeat: |
| 7353 | #ifdef SYSV |
| 7354 | do { |
| 7355 | execve(cmd, argv, envp); |
| 7356 | } while (errno == EINTR); |
| 7357 | #else |
| 7358 | execve(cmd, argv, envp); |
| 7359 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7360 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7361 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7362 | return; |
| 7363 | } |
| 7364 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7365 | char **ap; |
| 7366 | char **new; |
| 7367 | |
| 7368 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7369 | continue; |
| 7370 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7371 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7372 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7373 | ap += 2; |
| 7374 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7375 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7376 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7377 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7378 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7379 | goto repeat; |
| 7380 | } |
| 7381 | } |
| 7382 | |
| 7383 | /* |
| 7384 | * Exec a program. Never returns. If you change this routine, you may |
| 7385 | * have to change the find_command routine as well. |
| 7386 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7387 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7388 | static void |
| 7389 | shellexec(char **argv, const char *path, int idx) |
| 7390 | { |
| 7391 | char *cmdname; |
| 7392 | int e; |
| 7393 | char **envp; |
| 7394 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7395 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7396 | int applet_no = -1; |
| 7397 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7398 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7399 | clearredir(/*drop:*/ 1); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 7400 | envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7401 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7402 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7403 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7404 | #endif |
| 7405 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7406 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7407 | e = errno; |
| 7408 | } else { |
| 7409 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7410 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7411 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7412 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7413 | if (errno != ENOENT && errno != ENOTDIR) |
| 7414 | e = errno; |
| 7415 | } |
| 7416 | stunalloc(cmdname); |
| 7417 | } |
| 7418 | } |
| 7419 | |
| 7420 | /* Map to POSIX errors */ |
| 7421 | switch (e) { |
| 7422 | case EACCES: |
| 7423 | exerrno = 126; |
| 7424 | break; |
| 7425 | case ENOENT: |
| 7426 | exerrno = 127; |
| 7427 | break; |
| 7428 | default: |
| 7429 | exerrno = 2; |
| 7430 | break; |
| 7431 | } |
| 7432 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7433 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7434 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7435 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7436 | /* NOTREACHED */ |
| 7437 | } |
| 7438 | |
| 7439 | static void |
| 7440 | printentry(struct tblentry *cmdp) |
| 7441 | { |
| 7442 | int idx; |
| 7443 | const char *path; |
| 7444 | char *name; |
| 7445 | |
| 7446 | idx = cmdp->param.index; |
| 7447 | path = pathval(); |
| 7448 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7449 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7450 | stunalloc(name); |
| 7451 | } while (--idx >= 0); |
| 7452 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7453 | } |
| 7454 | |
| 7455 | /* |
| 7456 | * Clear out command entries. The argument specifies the first entry in |
| 7457 | * PATH which has changed. |
| 7458 | */ |
| 7459 | static void |
| 7460 | clearcmdentry(int firstchange) |
| 7461 | { |
| 7462 | struct tblentry **tblp; |
| 7463 | struct tblentry **pp; |
| 7464 | struct tblentry *cmdp; |
| 7465 | |
| 7466 | INT_OFF; |
| 7467 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7468 | pp = tblp; |
| 7469 | while ((cmdp = *pp) != NULL) { |
| 7470 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7471 | cmdp->param.index >= firstchange) |
| 7472 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7473 | builtinloc >= firstchange) |
| 7474 | ) { |
| 7475 | *pp = cmdp->next; |
| 7476 | free(cmdp); |
| 7477 | } else { |
| 7478 | pp = &cmdp->next; |
| 7479 | } |
| 7480 | } |
| 7481 | } |
| 7482 | INT_ON; |
| 7483 | } |
| 7484 | |
| 7485 | /* |
| 7486 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7487 | * add the command to the table if it is not already present. The |
| 7488 | * variable "lastcmdentry" is set to point to the address of the link |
| 7489 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7490 | * entry. |
| 7491 | * |
| 7492 | * Interrupts must be off if called with add != 0. |
| 7493 | */ |
| 7494 | static struct tblentry **lastcmdentry; |
| 7495 | |
| 7496 | static struct tblentry * |
| 7497 | cmdlookup(const char *name, int add) |
| 7498 | { |
| 7499 | unsigned int hashval; |
| 7500 | const char *p; |
| 7501 | struct tblentry *cmdp; |
| 7502 | struct tblentry **pp; |
| 7503 | |
| 7504 | p = name; |
| 7505 | hashval = (unsigned char)*p << 4; |
| 7506 | while (*p) |
| 7507 | hashval += (unsigned char)*p++; |
| 7508 | hashval &= 0x7FFF; |
| 7509 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7510 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7511 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7512 | break; |
| 7513 | pp = &cmdp->next; |
| 7514 | } |
| 7515 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7516 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7517 | + strlen(name) |
| 7518 | /* + 1 - already done because |
| 7519 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7520 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7521 | cmdp->cmdtype = CMDUNKNOWN; |
| 7522 | strcpy(cmdp->cmdname, name); |
| 7523 | } |
| 7524 | lastcmdentry = pp; |
| 7525 | return cmdp; |
| 7526 | } |
| 7527 | |
| 7528 | /* |
| 7529 | * Delete the command entry returned on the last lookup. |
| 7530 | */ |
| 7531 | static void |
| 7532 | delete_cmd_entry(void) |
| 7533 | { |
| 7534 | struct tblentry *cmdp; |
| 7535 | |
| 7536 | INT_OFF; |
| 7537 | cmdp = *lastcmdentry; |
| 7538 | *lastcmdentry = cmdp->next; |
| 7539 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7540 | freefunc(cmdp->param.func); |
| 7541 | free(cmdp); |
| 7542 | INT_ON; |
| 7543 | } |
| 7544 | |
| 7545 | /* |
| 7546 | * Add a new command entry, replacing any existing command entry for |
| 7547 | * the same name - except special builtins. |
| 7548 | */ |
| 7549 | static void |
| 7550 | addcmdentry(char *name, struct cmdentry *entry) |
| 7551 | { |
| 7552 | struct tblentry *cmdp; |
| 7553 | |
| 7554 | cmdp = cmdlookup(name, 1); |
| 7555 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7556 | freefunc(cmdp->param.func); |
| 7557 | } |
| 7558 | cmdp->cmdtype = entry->cmdtype; |
| 7559 | cmdp->param = entry->u; |
| 7560 | cmdp->rehash = 0; |
| 7561 | } |
| 7562 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7563 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7564 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7565 | { |
| 7566 | struct tblentry **pp; |
| 7567 | struct tblentry *cmdp; |
| 7568 | int c; |
| 7569 | struct cmdentry entry; |
| 7570 | char *name; |
| 7571 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7572 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7573 | clearcmdentry(0); |
| 7574 | return 0; |
| 7575 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7576 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7577 | if (*argptr == NULL) { |
| 7578 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7579 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7580 | if (cmdp->cmdtype == CMDNORMAL) |
| 7581 | printentry(cmdp); |
| 7582 | } |
| 7583 | } |
| 7584 | return 0; |
| 7585 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7586 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7587 | c = 0; |
| 7588 | while ((name = *argptr) != NULL) { |
| 7589 | cmdp = cmdlookup(name, 0); |
| 7590 | if (cmdp != NULL |
| 7591 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7592 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7593 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7594 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7595 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7596 | find_command(name, &entry, DO_ERR, pathval()); |
| 7597 | if (entry.cmdtype == CMDUNKNOWN) |
| 7598 | c = 1; |
| 7599 | argptr++; |
| 7600 | } |
| 7601 | return c; |
| 7602 | } |
| 7603 | |
| 7604 | /* |
| 7605 | * Called when a cd is done. Marks all commands so the next time they |
| 7606 | * are executed they will be rehashed. |
| 7607 | */ |
| 7608 | static void |
| 7609 | hashcd(void) |
| 7610 | { |
| 7611 | struct tblentry **pp; |
| 7612 | struct tblentry *cmdp; |
| 7613 | |
| 7614 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7615 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7616 | if (cmdp->cmdtype == CMDNORMAL |
| 7617 | || (cmdp->cmdtype == CMDBUILTIN |
| 7618 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7619 | && builtinloc > 0) |
| 7620 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7621 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7622 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7623 | } |
| 7624 | } |
| 7625 | } |
| 7626 | |
| 7627 | /* |
| 7628 | * Fix command hash table when PATH changed. |
| 7629 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7630 | * pathval() still returns the old value at this point. |
| 7631 | * Called with interrupts off. |
| 7632 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7633 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7634 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7635 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7636 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7637 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7638 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7639 | int idx_bltin; |
| 7640 | |
| 7641 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7642 | firstchange = 9999; /* assume no change */ |
| 7643 | idx = 0; |
| 7644 | idx_bltin = -1; |
| 7645 | for (;;) { |
| 7646 | if (*old != *new) { |
| 7647 | firstchange = idx; |
| 7648 | if ((*old == '\0' && *new == ':') |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7649 | || (*old == ':' && *new == '\0') |
| 7650 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7651 | firstchange++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7652 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7653 | old = new; /* ignore subsequent differences */ |
| 7654 | } |
| 7655 | if (*new == '\0') |
| 7656 | break; |
| 7657 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7658 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7659 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7660 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7661 | new++; |
| 7662 | old++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7663 | } |
| 7664 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7665 | builtinloc = idx_bltin; /* zap builtins */ |
| 7666 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7667 | firstchange = 0; |
| 7668 | clearcmdentry(firstchange); |
| 7669 | builtinloc = idx_bltin; |
| 7670 | } |
| 7671 | |
| 7672 | #define TEOF 0 |
| 7673 | #define TNL 1 |
| 7674 | #define TREDIR 2 |
| 7675 | #define TWORD 3 |
| 7676 | #define TSEMI 4 |
| 7677 | #define TBACKGND 5 |
| 7678 | #define TAND 6 |
| 7679 | #define TOR 7 |
| 7680 | #define TPIPE 8 |
| 7681 | #define TLP 9 |
| 7682 | #define TRP 10 |
| 7683 | #define TENDCASE 11 |
| 7684 | #define TENDBQUOTE 12 |
| 7685 | #define TNOT 13 |
| 7686 | #define TCASE 14 |
| 7687 | #define TDO 15 |
| 7688 | #define TDONE 16 |
| 7689 | #define TELIF 17 |
| 7690 | #define TELSE 18 |
| 7691 | #define TESAC 19 |
| 7692 | #define TFI 20 |
| 7693 | #define TFOR 21 |
| 7694 | #define TIF 22 |
| 7695 | #define TIN 23 |
| 7696 | #define TTHEN 24 |
| 7697 | #define TUNTIL 25 |
| 7698 | #define TWHILE 26 |
| 7699 | #define TBEGIN 27 |
| 7700 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7701 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7702 | |
| 7703 | /* first char is indicating which tokens mark the end of a list */ |
| 7704 | static const char *const tokname_array[] = { |
| 7705 | "\1end of file", |
| 7706 | "\0newline", |
| 7707 | "\0redirection", |
| 7708 | "\0word", |
| 7709 | "\0;", |
| 7710 | "\0&", |
| 7711 | "\0&&", |
| 7712 | "\0||", |
| 7713 | "\0|", |
| 7714 | "\0(", |
| 7715 | "\1)", |
| 7716 | "\1;;", |
| 7717 | "\1`", |
| 7718 | #define KWDOFFSET 13 |
| 7719 | /* the following are keywords */ |
| 7720 | "\0!", |
| 7721 | "\0case", |
| 7722 | "\1do", |
| 7723 | "\1done", |
| 7724 | "\1elif", |
| 7725 | "\1else", |
| 7726 | "\1esac", |
| 7727 | "\1fi", |
| 7728 | "\0for", |
| 7729 | "\0if", |
| 7730 | "\0in", |
| 7731 | "\1then", |
| 7732 | "\0until", |
| 7733 | "\0while", |
| 7734 | "\0{", |
| 7735 | "\1}", |
| 7736 | }; |
| 7737 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7738 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7739 | static int |
| 7740 | pstrcmp(const void *a, const void *b) |
| 7741 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7742 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7743 | } |
| 7744 | |
| 7745 | static const char *const * |
| 7746 | findkwd(const char *s) |
| 7747 | { |
| 7748 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7749 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7750 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7751 | } |
| 7752 | |
| 7753 | /* |
| 7754 | * Locate and print what a word is... |
| 7755 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7756 | static int |
| 7757 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7758 | { |
| 7759 | struct cmdentry entry; |
| 7760 | struct tblentry *cmdp; |
| 7761 | #if ENABLE_ASH_ALIAS |
| 7762 | const struct alias *ap; |
| 7763 | #endif |
| 7764 | const char *path = pathval(); |
| 7765 | |
| 7766 | if (describe_command_verbose) { |
| 7767 | out1str(command); |
| 7768 | } |
| 7769 | |
| 7770 | /* First look at the keywords */ |
| 7771 | if (findkwd(command)) { |
| 7772 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7773 | goto out; |
| 7774 | } |
| 7775 | |
| 7776 | #if ENABLE_ASH_ALIAS |
| 7777 | /* Then look at the aliases */ |
| 7778 | ap = lookupalias(command, 0); |
| 7779 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7780 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7781 | out1str("alias "); |
| 7782 | printalias(ap); |
| 7783 | return 0; |
| 7784 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7785 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7786 | goto out; |
| 7787 | } |
| 7788 | #endif |
| 7789 | /* Then check if it is a tracked alias */ |
| 7790 | cmdp = cmdlookup(command, 0); |
| 7791 | if (cmdp != NULL) { |
| 7792 | entry.cmdtype = cmdp->cmdtype; |
| 7793 | entry.u = cmdp->param; |
| 7794 | } else { |
| 7795 | /* Finally use brute force */ |
| 7796 | find_command(command, &entry, DO_ABS, path); |
| 7797 | } |
| 7798 | |
| 7799 | switch (entry.cmdtype) { |
| 7800 | case CMDNORMAL: { |
| 7801 | int j = entry.u.index; |
| 7802 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7803 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7804 | p = command; |
| 7805 | } else { |
| 7806 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7807 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7808 | stunalloc(p); |
| 7809 | } while (--j >= 0); |
| 7810 | } |
| 7811 | if (describe_command_verbose) { |
| 7812 | out1fmt(" is%s %s", |
| 7813 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7814 | ); |
| 7815 | } else { |
| 7816 | out1str(p); |
| 7817 | } |
| 7818 | break; |
| 7819 | } |
| 7820 | |
| 7821 | case CMDFUNCTION: |
| 7822 | if (describe_command_verbose) { |
| 7823 | out1str(" is a shell function"); |
| 7824 | } else { |
| 7825 | out1str(command); |
| 7826 | } |
| 7827 | break; |
| 7828 | |
| 7829 | case CMDBUILTIN: |
| 7830 | if (describe_command_verbose) { |
| 7831 | out1fmt(" is a %sshell builtin", |
| 7832 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7833 | "special " : nullstr |
| 7834 | ); |
| 7835 | } else { |
| 7836 | out1str(command); |
| 7837 | } |
| 7838 | break; |
| 7839 | |
| 7840 | default: |
| 7841 | if (describe_command_verbose) { |
| 7842 | out1str(": not found\n"); |
| 7843 | } |
| 7844 | return 127; |
| 7845 | } |
| 7846 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7847 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7848 | return 0; |
| 7849 | } |
| 7850 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7851 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7852 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7853 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7854 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7855 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7856 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7857 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7858 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7859 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7860 | i++; |
| 7861 | verbose = 0; |
| 7862 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7863 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7864 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7865 | } |
| 7866 | return err; |
| 7867 | } |
| 7868 | |
| 7869 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7870 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7871 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7872 | { |
| 7873 | int c; |
| 7874 | enum { |
| 7875 | VERIFY_BRIEF = 1, |
| 7876 | VERIFY_VERBOSE = 2, |
| 7877 | } verify = 0; |
| 7878 | |
| 7879 | while ((c = nextopt("pvV")) != '\0') |
| 7880 | if (c == 'V') |
| 7881 | verify |= VERIFY_VERBOSE; |
| 7882 | else if (c == 'v') |
| 7883 | verify |= VERIFY_BRIEF; |
| 7884 | #if DEBUG |
| 7885 | else if (c != 'p') |
| 7886 | abort(); |
| 7887 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7888 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7889 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7890 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7891 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7892 | |
| 7893 | return 0; |
| 7894 | } |
| 7895 | #endif |
| 7896 | |
| 7897 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7898 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7899 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7900 | static int funcblocksize; /* size of structures in function */ |
| 7901 | static int funcstringsize; /* size of strings in node */ |
| 7902 | static void *funcblock; /* block to allocate function from */ |
| 7903 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7904 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7905 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7906 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7907 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7908 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7909 | |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 7910 | static const uint8_t nodesize[N_NUMBER] = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7911 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7912 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7913 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7914 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7915 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7916 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7917 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7918 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7919 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7920 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7921 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7922 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7923 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7924 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7925 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7926 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7927 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7928 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7929 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7930 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7931 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7932 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7933 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7934 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7935 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7936 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7937 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7938 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7939 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7940 | }; |
| 7941 | |
| 7942 | static void calcsize(union node *n); |
| 7943 | |
| 7944 | static void |
| 7945 | sizenodelist(struct nodelist *lp) |
| 7946 | { |
| 7947 | while (lp) { |
| 7948 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7949 | calcsize(lp->n); |
| 7950 | lp = lp->next; |
| 7951 | } |
| 7952 | } |
| 7953 | |
| 7954 | static void |
| 7955 | calcsize(union node *n) |
| 7956 | { |
| 7957 | if (n == NULL) |
| 7958 | return; |
| 7959 | funcblocksize += nodesize[n->type]; |
| 7960 | switch (n->type) { |
| 7961 | case NCMD: |
| 7962 | calcsize(n->ncmd.redirect); |
| 7963 | calcsize(n->ncmd.args); |
| 7964 | calcsize(n->ncmd.assign); |
| 7965 | break; |
| 7966 | case NPIPE: |
| 7967 | sizenodelist(n->npipe.cmdlist); |
| 7968 | break; |
| 7969 | case NREDIR: |
| 7970 | case NBACKGND: |
| 7971 | case NSUBSHELL: |
| 7972 | calcsize(n->nredir.redirect); |
| 7973 | calcsize(n->nredir.n); |
| 7974 | break; |
| 7975 | case NAND: |
| 7976 | case NOR: |
| 7977 | case NSEMI: |
| 7978 | case NWHILE: |
| 7979 | case NUNTIL: |
| 7980 | calcsize(n->nbinary.ch2); |
| 7981 | calcsize(n->nbinary.ch1); |
| 7982 | break; |
| 7983 | case NIF: |
| 7984 | calcsize(n->nif.elsepart); |
| 7985 | calcsize(n->nif.ifpart); |
| 7986 | calcsize(n->nif.test); |
| 7987 | break; |
| 7988 | case NFOR: |
| 7989 | funcstringsize += strlen(n->nfor.var) + 1; |
| 7990 | calcsize(n->nfor.body); |
| 7991 | calcsize(n->nfor.args); |
| 7992 | break; |
| 7993 | case NCASE: |
| 7994 | calcsize(n->ncase.cases); |
| 7995 | calcsize(n->ncase.expr); |
| 7996 | break; |
| 7997 | case NCLIST: |
| 7998 | calcsize(n->nclist.body); |
| 7999 | calcsize(n->nclist.pattern); |
| 8000 | calcsize(n->nclist.next); |
| 8001 | break; |
| 8002 | case NDEFUN: |
| 8003 | case NARG: |
| 8004 | sizenodelist(n->narg.backquote); |
| 8005 | funcstringsize += strlen(n->narg.text) + 1; |
| 8006 | calcsize(n->narg.next); |
| 8007 | break; |
| 8008 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8009 | #if ENABLE_ASH_BASH_COMPAT |
| 8010 | case NTO2: |
| 8011 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8012 | case NCLOBBER: |
| 8013 | case NFROM: |
| 8014 | case NFROMTO: |
| 8015 | case NAPPEND: |
| 8016 | calcsize(n->nfile.fname); |
| 8017 | calcsize(n->nfile.next); |
| 8018 | break; |
| 8019 | case NTOFD: |
| 8020 | case NFROMFD: |
| 8021 | calcsize(n->ndup.vname); |
| 8022 | calcsize(n->ndup.next); |
| 8023 | break; |
| 8024 | case NHERE: |
| 8025 | case NXHERE: |
| 8026 | calcsize(n->nhere.doc); |
| 8027 | calcsize(n->nhere.next); |
| 8028 | break; |
| 8029 | case NNOT: |
| 8030 | calcsize(n->nnot.com); |
| 8031 | break; |
| 8032 | }; |
| 8033 | } |
| 8034 | |
| 8035 | static char * |
| 8036 | nodeckstrdup(char *s) |
| 8037 | { |
| 8038 | char *rtn = funcstring; |
| 8039 | |
| 8040 | strcpy(funcstring, s); |
| 8041 | funcstring += strlen(s) + 1; |
| 8042 | return rtn; |
| 8043 | } |
| 8044 | |
| 8045 | static union node *copynode(union node *); |
| 8046 | |
| 8047 | static struct nodelist * |
| 8048 | copynodelist(struct nodelist *lp) |
| 8049 | { |
| 8050 | struct nodelist *start; |
| 8051 | struct nodelist **lpp; |
| 8052 | |
| 8053 | lpp = &start; |
| 8054 | while (lp) { |
| 8055 | *lpp = funcblock; |
| 8056 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 8057 | (*lpp)->n = copynode(lp->n); |
| 8058 | lp = lp->next; |
| 8059 | lpp = &(*lpp)->next; |
| 8060 | } |
| 8061 | *lpp = NULL; |
| 8062 | return start; |
| 8063 | } |
| 8064 | |
| 8065 | static union node * |
| 8066 | copynode(union node *n) |
| 8067 | { |
| 8068 | union node *new; |
| 8069 | |
| 8070 | if (n == NULL) |
| 8071 | return NULL; |
| 8072 | new = funcblock; |
| 8073 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8074 | |
| 8075 | switch (n->type) { |
| 8076 | case NCMD: |
| 8077 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8078 | new->ncmd.args = copynode(n->ncmd.args); |
| 8079 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8080 | break; |
| 8081 | case NPIPE: |
| 8082 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8083 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8084 | break; |
| 8085 | case NREDIR: |
| 8086 | case NBACKGND: |
| 8087 | case NSUBSHELL: |
| 8088 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8089 | new->nredir.n = copynode(n->nredir.n); |
| 8090 | break; |
| 8091 | case NAND: |
| 8092 | case NOR: |
| 8093 | case NSEMI: |
| 8094 | case NWHILE: |
| 8095 | case NUNTIL: |
| 8096 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8097 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8098 | break; |
| 8099 | case NIF: |
| 8100 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8101 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8102 | new->nif.test = copynode(n->nif.test); |
| 8103 | break; |
| 8104 | case NFOR: |
| 8105 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8106 | new->nfor.body = copynode(n->nfor.body); |
| 8107 | new->nfor.args = copynode(n->nfor.args); |
| 8108 | break; |
| 8109 | case NCASE: |
| 8110 | new->ncase.cases = copynode(n->ncase.cases); |
| 8111 | new->ncase.expr = copynode(n->ncase.expr); |
| 8112 | break; |
| 8113 | case NCLIST: |
| 8114 | new->nclist.body = copynode(n->nclist.body); |
| 8115 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8116 | new->nclist.next = copynode(n->nclist.next); |
| 8117 | break; |
| 8118 | case NDEFUN: |
| 8119 | case NARG: |
| 8120 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8121 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8122 | new->narg.next = copynode(n->narg.next); |
| 8123 | break; |
| 8124 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8125 | #if ENABLE_ASH_BASH_COMPAT |
| 8126 | case NTO2: |
| 8127 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8128 | case NCLOBBER: |
| 8129 | case NFROM: |
| 8130 | case NFROMTO: |
| 8131 | case NAPPEND: |
| 8132 | new->nfile.fname = copynode(n->nfile.fname); |
| 8133 | new->nfile.fd = n->nfile.fd; |
| 8134 | new->nfile.next = copynode(n->nfile.next); |
| 8135 | break; |
| 8136 | case NTOFD: |
| 8137 | case NFROMFD: |
| 8138 | new->ndup.vname = copynode(n->ndup.vname); |
| 8139 | new->ndup.dupfd = n->ndup.dupfd; |
| 8140 | new->ndup.fd = n->ndup.fd; |
| 8141 | new->ndup.next = copynode(n->ndup.next); |
| 8142 | break; |
| 8143 | case NHERE: |
| 8144 | case NXHERE: |
| 8145 | new->nhere.doc = copynode(n->nhere.doc); |
| 8146 | new->nhere.fd = n->nhere.fd; |
| 8147 | new->nhere.next = copynode(n->nhere.next); |
| 8148 | break; |
| 8149 | case NNOT: |
| 8150 | new->nnot.com = copynode(n->nnot.com); |
| 8151 | break; |
| 8152 | }; |
| 8153 | new->type = n->type; |
| 8154 | return new; |
| 8155 | } |
| 8156 | |
| 8157 | /* |
| 8158 | * Make a copy of a parse tree. |
| 8159 | */ |
| 8160 | static struct funcnode * |
| 8161 | copyfunc(union node *n) |
| 8162 | { |
| 8163 | struct funcnode *f; |
| 8164 | size_t blocksize; |
| 8165 | |
| 8166 | funcblocksize = offsetof(struct funcnode, n); |
| 8167 | funcstringsize = 0; |
| 8168 | calcsize(n); |
| 8169 | blocksize = funcblocksize; |
| 8170 | f = ckmalloc(blocksize + funcstringsize); |
| 8171 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8172 | funcstring = (char *) f + blocksize; |
| 8173 | copynode(n); |
| 8174 | f->count = 0; |
| 8175 | return f; |
| 8176 | } |
| 8177 | |
| 8178 | /* |
| 8179 | * Define a shell function. |
| 8180 | */ |
| 8181 | static void |
| 8182 | defun(char *name, union node *func) |
| 8183 | { |
| 8184 | struct cmdentry entry; |
| 8185 | |
| 8186 | INT_OFF; |
| 8187 | entry.cmdtype = CMDFUNCTION; |
| 8188 | entry.u.func = copyfunc(func); |
| 8189 | addcmdentry(name, &entry); |
| 8190 | INT_ON; |
| 8191 | } |
| 8192 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8193 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8194 | #define SKIPBREAK (1 << 0) |
| 8195 | #define SKIPCONT (1 << 1) |
| 8196 | #define SKIPFUNC (1 << 2) |
| 8197 | #define SKIPFILE (1 << 3) |
| 8198 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8199 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8200 | static int skipcount; /* number of levels to skip */ |
| 8201 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8202 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8203 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8204 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8205 | static int evalstring(char *s, int mask); |
| 8206 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8207 | /* Called to execute a trap. |
| 8208 | * Single callsite - at the end of evaltree(). |
| 8209 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 8210 | * |
| 8211 | * Perhaps we should avoid entering new trap handlers |
| 8212 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8213 | */ |
| 8214 | static int |
| 8215 | dotrap(void) |
| 8216 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8217 | uint8_t *g; |
| 8218 | int sig; |
| 8219 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8220 | |
| 8221 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8222 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8223 | xbarrier(); |
| 8224 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8225 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8226 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8227 | int want_exexit; |
| 8228 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8229 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8230 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8231 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8232 | t = trap[sig]; |
| 8233 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8234 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8235 | if (sig == SIGINT && !t) |
| 8236 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8237 | |
| 8238 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8239 | *g = 0; |
| 8240 | if (!t) |
| 8241 | continue; |
| 8242 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8243 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8244 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8245 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8246 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8247 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8248 | } |
| 8249 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8250 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8251 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8252 | } |
| 8253 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8254 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8255 | static void evalloop(union node *, int); |
| 8256 | static void evalfor(union node *, int); |
| 8257 | static void evalcase(union node *, int); |
| 8258 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8259 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8260 | static void evalpipe(union node *, int); |
| 8261 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8262 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8263 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8264 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8265 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8266 | * Evaluate a parse tree. The value is left in the global variable |
| 8267 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8268 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8269 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8270 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8271 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8272 | struct jmploc *volatile savehandler = exception_handler; |
| 8273 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8274 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8275 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8276 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8277 | int int_level; |
| 8278 | |
| 8279 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8280 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8281 | if (n == NULL) { |
| 8282 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8283 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8284 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8285 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8286 | |
| 8287 | exception_handler = &jmploc; |
| 8288 | { |
| 8289 | int err = setjmp(jmploc.loc); |
| 8290 | if (err) { |
| 8291 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8292 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8293 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8294 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8295 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8296 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8297 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8298 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8299 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8300 | exception_handler = savehandler; |
| 8301 | longjmp(exception_handler->loc, err); |
| 8302 | } |
| 8303 | } |
| 8304 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8305 | switch (n->type) { |
| 8306 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8307 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8308 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8309 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8310 | break; |
| 8311 | #endif |
| 8312 | case NNOT: |
| 8313 | evaltree(n->nnot.com, EV_TESTED); |
| 8314 | status = !exitstatus; |
| 8315 | goto setstatus; |
| 8316 | case NREDIR: |
| 8317 | expredir(n->nredir.redirect); |
| 8318 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8319 | if (!status) { |
| 8320 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8321 | status = exitstatus; |
| 8322 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8323 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8324 | goto setstatus; |
| 8325 | case NCMD: |
| 8326 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8327 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8328 | if (eflag && !(flags & EV_TESTED)) |
| 8329 | checkexit = ~0; |
| 8330 | goto calleval; |
| 8331 | case NFOR: |
| 8332 | evalfn = evalfor; |
| 8333 | goto calleval; |
| 8334 | case NWHILE: |
| 8335 | case NUNTIL: |
| 8336 | evalfn = evalloop; |
| 8337 | goto calleval; |
| 8338 | case NSUBSHELL: |
| 8339 | case NBACKGND: |
| 8340 | evalfn = evalsubshell; |
| 8341 | goto calleval; |
| 8342 | case NPIPE: |
| 8343 | evalfn = evalpipe; |
| 8344 | goto checkexit; |
| 8345 | case NCASE: |
| 8346 | evalfn = evalcase; |
| 8347 | goto calleval; |
| 8348 | case NAND: |
| 8349 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8350 | case NSEMI: { |
| 8351 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8352 | #if NAND + 1 != NOR |
| 8353 | #error NAND + 1 != NOR |
| 8354 | #endif |
| 8355 | #if NOR + 1 != NSEMI |
| 8356 | #error NOR + 1 != NSEMI |
| 8357 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8358 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8359 | evaltree( |
| 8360 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8361 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8362 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8363 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8364 | break; |
| 8365 | if (!evalskip) { |
| 8366 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8367 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8368 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8369 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8370 | evalfn(n, flags); |
| 8371 | break; |
| 8372 | } |
| 8373 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8374 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8375 | case NIF: |
| 8376 | evaltree(n->nif.test, EV_TESTED); |
| 8377 | if (evalskip) |
| 8378 | break; |
| 8379 | if (exitstatus == 0) { |
| 8380 | n = n->nif.ifpart; |
| 8381 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8382 | } |
| 8383 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8384 | n = n->nif.elsepart; |
| 8385 | goto evaln; |
| 8386 | } |
| 8387 | goto success; |
| 8388 | case NDEFUN: |
| 8389 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8390 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8391 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8392 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8393 | exitstatus = status; |
| 8394 | break; |
| 8395 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8396 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8397 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8398 | exception_handler = savehandler; |
| 8399 | out1: |
| 8400 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8401 | evalskip |= SKIPEVAL; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8402 | else if (pending_sig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8403 | goto exexit; |
| 8404 | |
| 8405 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8406 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8407 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8408 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8409 | |
| 8410 | RESTORE_INT(int_level); |
| 8411 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8412 | } |
| 8413 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8414 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8415 | static |
| 8416 | #endif |
| 8417 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8418 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8419 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8420 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8421 | { |
| 8422 | int status; |
| 8423 | |
| 8424 | loopnest++; |
| 8425 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8426 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8427 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8428 | int i; |
| 8429 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8430 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8431 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8432 | skipping: |
| 8433 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8434 | evalskip = 0; |
| 8435 | continue; |
| 8436 | } |
| 8437 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8438 | evalskip = 0; |
| 8439 | break; |
| 8440 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8441 | i = exitstatus; |
| 8442 | if (n->type != NWHILE) |
| 8443 | i = !i; |
| 8444 | if (i != 0) |
| 8445 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8446 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8447 | status = exitstatus; |
| 8448 | if (evalskip) |
| 8449 | goto skipping; |
| 8450 | } |
| 8451 | loopnest--; |
| 8452 | exitstatus = status; |
| 8453 | } |
| 8454 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8455 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8456 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8457 | { |
| 8458 | struct arglist arglist; |
| 8459 | union node *argp; |
| 8460 | struct strlist *sp; |
| 8461 | struct stackmark smark; |
| 8462 | |
| 8463 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8464 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8465 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8466 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8467 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8468 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8469 | if (evalskip) |
| 8470 | goto out; |
| 8471 | } |
| 8472 | *arglist.lastp = NULL; |
| 8473 | |
| 8474 | exitstatus = 0; |
| 8475 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8476 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8477 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8478 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8479 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8480 | if (evalskip) { |
| 8481 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8482 | evalskip = 0; |
| 8483 | continue; |
| 8484 | } |
| 8485 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8486 | evalskip = 0; |
| 8487 | break; |
| 8488 | } |
| 8489 | } |
| 8490 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8491 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8492 | popstackmark(&smark); |
| 8493 | } |
| 8494 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8495 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8496 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8497 | { |
| 8498 | union node *cp; |
| 8499 | union node *patp; |
| 8500 | struct arglist arglist; |
| 8501 | struct stackmark smark; |
| 8502 | |
| 8503 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8504 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8505 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8506 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8507 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8508 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8509 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8510 | if (casematch(patp, arglist.list->text)) { |
| 8511 | if (evalskip == 0) { |
| 8512 | evaltree(cp->nclist.body, flags); |
| 8513 | } |
| 8514 | goto out; |
| 8515 | } |
| 8516 | } |
| 8517 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8518 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8519 | popstackmark(&smark); |
| 8520 | } |
| 8521 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8522 | /* |
| 8523 | * Kick off a subshell to evaluate a tree. |
| 8524 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8525 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8526 | evalsubshell(union node *n, int flags) |
| 8527 | { |
| 8528 | struct job *jp; |
| 8529 | int backgnd = (n->type == NBACKGND); |
| 8530 | int status; |
| 8531 | |
| 8532 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8533 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8534 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8535 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8536 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8537 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8538 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8539 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8540 | flags |= EV_EXIT; |
| 8541 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8542 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8543 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8544 | redirect(n->nredir.redirect, 0); |
| 8545 | evaltreenr(n->nredir.n, flags); |
| 8546 | /* never returns */ |
| 8547 | } |
| 8548 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8549 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8550 | status = waitforjob(jp); |
| 8551 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8552 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8553 | } |
| 8554 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8555 | /* |
| 8556 | * Compute the names of the files in a redirection list. |
| 8557 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8558 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8559 | static void |
| 8560 | expredir(union node *n) |
| 8561 | { |
| 8562 | union node *redir; |
| 8563 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8564 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8565 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8566 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8567 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8568 | fn.lastp = &fn.list; |
| 8569 | switch (redir->type) { |
| 8570 | case NFROMTO: |
| 8571 | case NFROM: |
| 8572 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8573 | #if ENABLE_ASH_BASH_COMPAT |
| 8574 | case NTO2: |
| 8575 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8576 | case NCLOBBER: |
| 8577 | case NAPPEND: |
| 8578 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8579 | #if ENABLE_ASH_BASH_COMPAT |
| 8580 | store_expfname: |
| 8581 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8582 | redir->nfile.expfname = fn.list->text; |
| 8583 | break; |
| 8584 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8585 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8586 | if (redir->ndup.vname) { |
| 8587 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8588 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8589 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8590 | #if ENABLE_ASH_BASH_COMPAT |
| 8591 | //FIXME: we used expandarg with different args! |
| 8592 | if (!isdigit_str9(fn.list->text)) { |
| 8593 | /* >&file, not >&fd */ |
| 8594 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8595 | ash_msg_and_raise_error("redir error"); |
| 8596 | redir->type = NTO2; |
| 8597 | goto store_expfname; |
| 8598 | } |
| 8599 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8600 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8601 | } |
| 8602 | break; |
| 8603 | } |
| 8604 | } |
| 8605 | } |
| 8606 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8607 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8608 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8609 | * of the process creating the pipeline. (This differs from some versions |
| 8610 | * of the shell, which make the last process in a pipeline the parent |
| 8611 | * of all the rest.) |
| 8612 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8613 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8614 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8615 | { |
| 8616 | struct job *jp; |
| 8617 | struct nodelist *lp; |
| 8618 | int pipelen; |
| 8619 | int prevfd; |
| 8620 | int pip[2]; |
| 8621 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8622 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8623 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8624 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8625 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8626 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8627 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8628 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8629 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8630 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8631 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8632 | pip[1] = -1; |
| 8633 | if (lp->next) { |
| 8634 | if (pipe(pip) < 0) { |
| 8635 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8636 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8637 | } |
| 8638 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8639 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8640 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8641 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8642 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8643 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8644 | if (prevfd > 0) { |
| 8645 | dup2(prevfd, 0); |
| 8646 | close(prevfd); |
| 8647 | } |
| 8648 | if (pip[1] > 1) { |
| 8649 | dup2(pip[1], 1); |
| 8650 | close(pip[1]); |
| 8651 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8652 | evaltreenr(lp->n, flags); |
| 8653 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8654 | } |
| 8655 | if (prevfd >= 0) |
| 8656 | close(prevfd); |
| 8657 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8658 | /* Don't want to trigger debugging */ |
| 8659 | if (pip[1] != -1) |
| 8660 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8661 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8662 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8663 | exitstatus = waitforjob(jp); |
| 8664 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8665 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8666 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8667 | } |
| 8668 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8669 | /* |
| 8670 | * Controls whether the shell is interactive or not. |
| 8671 | */ |
| 8672 | static void |
| 8673 | setinteractive(int on) |
| 8674 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8675 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8676 | |
| 8677 | if (++on == is_interactive) |
| 8678 | return; |
| 8679 | is_interactive = on; |
| 8680 | setsignal(SIGINT); |
| 8681 | setsignal(SIGQUIT); |
| 8682 | setsignal(SIGTERM); |
| 8683 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8684 | if (is_interactive > 1) { |
| 8685 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8686 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8687 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8688 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8689 | /* note: ash and hush share this string */ |
| 8690 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8691 | "Enter 'help' for a list of built-in commands." |
| 8692 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8693 | bb_banner, |
| 8694 | "built-in shell (ash)" |
| 8695 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8696 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8697 | } |
| 8698 | } |
| 8699 | #endif |
| 8700 | } |
| 8701 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8702 | static void |
| 8703 | optschanged(void) |
| 8704 | { |
| 8705 | #if DEBUG |
| 8706 | opentrace(); |
| 8707 | #endif |
| 8708 | setinteractive(iflag); |
| 8709 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8710 | #if ENABLE_FEATURE_EDITING_VI |
| 8711 | if (viflag) |
| 8712 | line_input_state->flags |= VI_MODE; |
| 8713 | else |
| 8714 | line_input_state->flags &= ~VI_MODE; |
| 8715 | #else |
| 8716 | viflag = 0; /* forcibly keep the option off */ |
| 8717 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8718 | } |
| 8719 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8720 | static struct localvar *localvars; |
| 8721 | |
| 8722 | /* |
| 8723 | * Called after a function returns. |
| 8724 | * Interrupts must be off. |
| 8725 | */ |
| 8726 | static void |
| 8727 | poplocalvars(void) |
| 8728 | { |
| 8729 | struct localvar *lvp; |
| 8730 | struct var *vp; |
| 8731 | |
| 8732 | while ((lvp = localvars) != NULL) { |
| 8733 | localvars = lvp->next; |
| 8734 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 8735 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8736 | if (vp == NULL) { /* $- saved */ |
| 8737 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8738 | free((char*)lvp->text); |
| 8739 | optschanged(); |
| 8740 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8741 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8742 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8743 | if (vp->var_func) |
| 8744 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8745 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8746 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8747 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8748 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8749 | } |
| 8750 | free(lvp); |
| 8751 | } |
| 8752 | } |
| 8753 | |
| 8754 | static int |
| 8755 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8756 | { |
| 8757 | volatile struct shparam saveparam; |
| 8758 | struct localvar *volatile savelocalvars; |
| 8759 | struct jmploc *volatile savehandler; |
| 8760 | struct jmploc jmploc; |
| 8761 | int e; |
| 8762 | |
| 8763 | saveparam = shellparam; |
| 8764 | savelocalvars = localvars; |
| 8765 | e = setjmp(jmploc.loc); |
| 8766 | if (e) { |
| 8767 | goto funcdone; |
| 8768 | } |
| 8769 | INT_OFF; |
| 8770 | savehandler = exception_handler; |
| 8771 | exception_handler = &jmploc; |
| 8772 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8773 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8774 | func->count++; |
| 8775 | funcnest++; |
| 8776 | INT_ON; |
| 8777 | shellparam.nparam = argc - 1; |
| 8778 | shellparam.p = argv + 1; |
| 8779 | #if ENABLE_ASH_GETOPTS |
| 8780 | shellparam.optind = 1; |
| 8781 | shellparam.optoff = -1; |
| 8782 | #endif |
| 8783 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8784 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8785 | INT_OFF; |
| 8786 | funcnest--; |
| 8787 | freefunc(func); |
| 8788 | poplocalvars(); |
| 8789 | localvars = savelocalvars; |
| 8790 | freeparam(&shellparam); |
| 8791 | shellparam = saveparam; |
| 8792 | exception_handler = savehandler; |
| 8793 | INT_ON; |
| 8794 | evalskip &= ~SKIPFUNC; |
| 8795 | return e; |
| 8796 | } |
| 8797 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8798 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8799 | static char ** |
| 8800 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8801 | { |
| 8802 | char *cp, c; |
| 8803 | |
| 8804 | for (;;) { |
| 8805 | cp = *++argv; |
| 8806 | if (!cp) |
| 8807 | return 0; |
| 8808 | if (*cp++ != '-') |
| 8809 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8810 | c = *cp++; |
| 8811 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8812 | break; |
| 8813 | if (c == '-' && !*cp) { |
| 8814 | argv++; |
| 8815 | break; |
| 8816 | } |
| 8817 | do { |
| 8818 | switch (c) { |
| 8819 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8820 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8821 | break; |
| 8822 | default: |
| 8823 | /* run 'typecmd' for other options */ |
| 8824 | return 0; |
| 8825 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8826 | c = *cp++; |
| 8827 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8828 | } |
| 8829 | return argv; |
| 8830 | } |
| 8831 | #endif |
| 8832 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8833 | /* |
| 8834 | * Make a variable a local variable. When a variable is made local, it's |
| 8835 | * value and flags are saved in a localvar structure. The saved values |
| 8836 | * will be restored when the shell function returns. We handle the name |
| 8837 | * "-" as a special case. |
| 8838 | */ |
| 8839 | static void |
| 8840 | mklocal(char *name) |
| 8841 | { |
| 8842 | struct localvar *lvp; |
| 8843 | struct var **vpp; |
| 8844 | struct var *vp; |
| 8845 | |
| 8846 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8847 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8848 | if (LONE_DASH(name)) { |
| 8849 | char *p; |
| 8850 | p = ckmalloc(sizeof(optlist)); |
| 8851 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8852 | vp = NULL; |
| 8853 | } else { |
| 8854 | char *eq; |
| 8855 | |
| 8856 | vpp = hashvar(name); |
| 8857 | vp = *findvar(vpp, name); |
| 8858 | eq = strchr(name, '='); |
| 8859 | if (vp == NULL) { |
| 8860 | if (eq) |
| 8861 | setvareq(name, VSTRFIXED); |
| 8862 | else |
| 8863 | setvar(name, NULL, VSTRFIXED); |
| 8864 | vp = *vpp; /* the new variable */ |
| 8865 | lvp->flags = VUNSET; |
| 8866 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8867 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8868 | lvp->flags = vp->flags; |
| 8869 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8870 | if (eq) |
| 8871 | setvareq(name, 0); |
| 8872 | } |
| 8873 | } |
| 8874 | lvp->vp = vp; |
| 8875 | lvp->next = localvars; |
| 8876 | localvars = lvp; |
| 8877 | INT_ON; |
| 8878 | } |
| 8879 | |
| 8880 | /* |
| 8881 | * The "local" command. |
| 8882 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8883 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8884 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8885 | { |
| 8886 | char *name; |
| 8887 | |
| 8888 | argv = argptr; |
| 8889 | while ((name = *argv++) != NULL) { |
| 8890 | mklocal(name); |
| 8891 | } |
| 8892 | return 0; |
| 8893 | } |
| 8894 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8895 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8896 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8897 | { |
| 8898 | return 1; |
| 8899 | } |
| 8900 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8901 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8902 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8903 | { |
| 8904 | return 0; |
| 8905 | } |
| 8906 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8907 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8908 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8909 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8910 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8911 | iflag = 0; /* exit on error */ |
| 8912 | mflag = 0; |
| 8913 | optschanged(); |
| 8914 | shellexec(argv + 1, pathval(), 0); |
| 8915 | } |
| 8916 | return 0; |
| 8917 | } |
| 8918 | |
| 8919 | /* |
| 8920 | * The return command. |
| 8921 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8922 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8923 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8924 | { |
| 8925 | /* |
| 8926 | * If called outside a function, do what ksh does; |
| 8927 | * skip the rest of the file. |
| 8928 | */ |
| 8929 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8930 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8931 | } |
| 8932 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8933 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8934 | static int breakcmd(int, char **) FAST_FUNC; |
| 8935 | static int dotcmd(int, char **) FAST_FUNC; |
| 8936 | static int evalcmd(int, char **) FAST_FUNC; |
| 8937 | static int exitcmd(int, char **) FAST_FUNC; |
| 8938 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8939 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8940 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8941 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8942 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8943 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8944 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8945 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8946 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8947 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8948 | static int readcmd(int, char **) FAST_FUNC; |
| 8949 | static int setcmd(int, char **) FAST_FUNC; |
| 8950 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8951 | static int timescmd(int, char **) FAST_FUNC; |
| 8952 | static int trapcmd(int, char **) FAST_FUNC; |
| 8953 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8954 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8955 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8956 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8957 | #define BUILTIN_NOSPEC "0" |
| 8958 | #define BUILTIN_SPECIAL "1" |
| 8959 | #define BUILTIN_REGULAR "2" |
| 8960 | #define BUILTIN_SPEC_REG "3" |
| 8961 | #define BUILTIN_ASSIGN "4" |
| 8962 | #define BUILTIN_SPEC_ASSG "5" |
| 8963 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8964 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8965 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8966 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8967 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8968 | 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] | 8969 | #endif |
| 8970 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8971 | 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] | 8972 | #endif |
| 8973 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8974 | 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] | 8975 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8976 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8977 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8978 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8979 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 8980 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8981 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8982 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8983 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8984 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8985 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8986 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8987 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8988 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8989 | #endif |
| 8990 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8991 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8992 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8993 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 8994 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 8995 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8996 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8997 | { BUILTIN_REGULAR "command" , commandcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8998 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8999 | { BUILTIN_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9000 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9001 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9002 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9003 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 9004 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 9005 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 9006 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 9007 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9008 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9009 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9010 | #endif |
| 9011 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9012 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9013 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9014 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9015 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9016 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9017 | #endif |
| 9018 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9019 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 9020 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9021 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9022 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9023 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9024 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9025 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9026 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9027 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9028 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9029 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 9030 | { BUILTIN_REGULAR "read" , readcmd }, |
| 9031 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 9032 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 9033 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 9034 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9035 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9036 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9037 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9038 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9039 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9040 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9041 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 9042 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 9043 | { BUILTIN_REGULAR "true" , truecmd }, |
| 9044 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 9045 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 9046 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9047 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9048 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9049 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9050 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 9051 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9052 | }; |
| 9053 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9054 | /* Should match the above table! */ |
| 9055 | #define COMMANDCMD (builtintab + \ |
| 9056 | 2 + \ |
| 9057 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9058 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9059 | 1 * ENABLE_ASH_ALIAS + \ |
| 9060 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9061 | 3) |
| 9062 | #define EXECCMD (builtintab + \ |
| 9063 | 2 + \ |
| 9064 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9065 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9066 | 1 * ENABLE_ASH_ALIAS + \ |
| 9067 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9068 | 3 + \ |
| 9069 | 1 * ENABLE_ASH_CMDCMD + \ |
| 9070 | 1 + \ |
| 9071 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 9072 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9073 | |
| 9074 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9075 | * Search the table of builtin commands. |
| 9076 | */ |
| 9077 | static struct builtincmd * |
| 9078 | find_builtin(const char *name) |
| 9079 | { |
| 9080 | struct builtincmd *bp; |
| 9081 | |
| 9082 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 9083 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9084 | pstrcmp |
| 9085 | ); |
| 9086 | return bp; |
| 9087 | } |
| 9088 | |
| 9089 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9090 | * Execute a simple command. |
| 9091 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9092 | static int |
| 9093 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9094 | { |
| 9095 | const char *q = endofname(p); |
| 9096 | if (p == q) |
| 9097 | return 0; |
| 9098 | return *q == '='; |
| 9099 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9100 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9101 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9102 | { |
| 9103 | /* Preserve exitstatus of a previous possible redirection |
| 9104 | * as POSIX mandates */ |
| 9105 | return back_exitstatus; |
| 9106 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 9107 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9108 | evalcommand(union node *cmd, int flags) |
| 9109 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9110 | static const struct builtincmd null_bltin = { |
| 9111 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9112 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9113 | struct stackmark smark; |
| 9114 | union node *argp; |
| 9115 | struct arglist arglist; |
| 9116 | struct arglist varlist; |
| 9117 | char **argv; |
| 9118 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9119 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9120 | struct cmdentry cmdentry; |
| 9121 | struct job *jp; |
| 9122 | char *lastarg; |
| 9123 | const char *path; |
| 9124 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9125 | int status; |
| 9126 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9127 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9128 | smallint cmd_is_exec; |
| 9129 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9130 | |
| 9131 | /* First expand the arguments. */ |
| 9132 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9133 | setstackmark(&smark); |
| 9134 | back_exitstatus = 0; |
| 9135 | |
| 9136 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9137 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9138 | varlist.lastp = &varlist.list; |
| 9139 | *varlist.lastp = NULL; |
| 9140 | arglist.lastp = &arglist.list; |
| 9141 | *arglist.lastp = NULL; |
| 9142 | |
| 9143 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9144 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9145 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9146 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9147 | } |
| 9148 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9149 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9150 | struct strlist **spp; |
| 9151 | |
| 9152 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9153 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9154 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9155 | else |
| 9156 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9157 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9158 | for (sp = *spp; sp; sp = sp->next) |
| 9159 | argc++; |
| 9160 | } |
| 9161 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9162 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9163 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9164 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9165 | *nargv++ = sp->text; |
| 9166 | } |
| 9167 | *nargv = NULL; |
| 9168 | |
| 9169 | lastarg = NULL; |
| 9170 | if (iflag && funcnest == 0 && argc > 0) |
| 9171 | lastarg = nargv[-1]; |
| 9172 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9173 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9174 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9175 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9176 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9177 | path = vpath.var_text; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9178 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9179 | struct strlist **spp; |
| 9180 | char *p; |
| 9181 | |
| 9182 | spp = varlist.lastp; |
| 9183 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9184 | |
| 9185 | /* |
| 9186 | * Modify the command lookup path, if a PATH= assignment |
| 9187 | * is present |
| 9188 | */ |
| 9189 | p = (*spp)->text; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9190 | if (varcmp(p, path) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9191 | path = p; |
| 9192 | } |
| 9193 | |
| 9194 | /* Print the command if xflag is set. */ |
| 9195 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9196 | int n; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9197 | const char *p = " %s" + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9198 | |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9199 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9200 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9201 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9202 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9203 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9204 | sp = sp->next; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9205 | p = " %s"; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9206 | } |
| 9207 | sp = arglist.list; |
| 9208 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9209 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9210 | } |
| 9211 | |
| 9212 | cmd_is_exec = 0; |
| 9213 | spclbltin = -1; |
| 9214 | |
| 9215 | /* Now locate the command. */ |
| 9216 | if (argc) { |
| 9217 | const char *oldpath; |
| 9218 | int cmd_flag = DO_ERR; |
| 9219 | |
| 9220 | path += 5; |
| 9221 | oldpath = path; |
| 9222 | for (;;) { |
| 9223 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9224 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9225 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9226 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9227 | goto bail; |
| 9228 | } |
| 9229 | |
| 9230 | /* implement bltin and command here */ |
| 9231 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9232 | break; |
| 9233 | if (spclbltin < 0) |
| 9234 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9235 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9236 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9237 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9238 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9239 | path = oldpath; |
| 9240 | nargv = parse_command_args(argv, &path); |
| 9241 | if (!nargv) |
| 9242 | break; |
| 9243 | argc -= nargv - argv; |
| 9244 | argv = nargv; |
| 9245 | cmd_flag |= DO_NOFUNC; |
| 9246 | } else |
| 9247 | #endif |
| 9248 | break; |
| 9249 | } |
| 9250 | } |
| 9251 | |
| 9252 | if (status) { |
| 9253 | /* We have a redirection error. */ |
| 9254 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9255 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9256 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9257 | exitstatus = status; |
| 9258 | goto out; |
| 9259 | } |
| 9260 | |
| 9261 | /* Execute the command. */ |
| 9262 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9263 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9264 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9265 | #if ENABLE_FEATURE_SH_NOFORK |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9266 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 9267 | * around run_nofork_applet() call. |
| 9268 | * (2) Should this check also be done in forkshell()? |
| 9269 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 9270 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9271 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9272 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9273 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9274 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9275 | /* run <applet>_main() */ |
| 9276 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9277 | break; |
| 9278 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9279 | #endif |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9280 | /* Can we avoid forking off? For example, very last command |
| 9281 | * in a script or a subshell does not need forking, |
| 9282 | * we can just exec it. |
| 9283 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9284 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9285 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9286 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9287 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9288 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9289 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9290 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9291 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9292 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9293 | break; |
| 9294 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9295 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9296 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 9297 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9298 | } |
| 9299 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9300 | shellexec(argv, path, cmdentry.u.index); |
| 9301 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9302 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9303 | case CMDBUILTIN: |
| 9304 | cmdenviron = varlist.list; |
| 9305 | if (cmdenviron) { |
| 9306 | struct strlist *list = cmdenviron; |
| 9307 | int i = VNOSET; |
| 9308 | if (spclbltin > 0 || argc == 0) { |
| 9309 | i = 0; |
| 9310 | if (cmd_is_exec && argc > 1) |
| 9311 | i = VEXPORT; |
| 9312 | } |
| 9313 | listsetvar(list, i); |
| 9314 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9315 | /* Tight loop with builtins only: |
| 9316 | * "while kill -0 $child; do true; done" |
| 9317 | * will never exit even if $child died, unless we do this |
| 9318 | * to reap the zombie and make kill detect that it's gone: */ |
| 9319 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9320 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9321 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9322 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9323 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9324 | if (i == EXEXIT) |
| 9325 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9326 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9327 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9328 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9329 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9330 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9331 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9332 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9333 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9334 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9335 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9336 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9337 | } |
| 9338 | break; |
| 9339 | |
| 9340 | case CMDFUNCTION: |
| 9341 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9342 | /* See above for the rationale */ |
| 9343 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9344 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9345 | goto raise; |
| 9346 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9347 | |
| 9348 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9349 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9350 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9351 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9352 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9353 | /* dsl: I think this is intended to be used to support |
| 9354 | * '_' in 'vi' command mode during line editing... |
| 9355 | * However I implemented that within libedit itself. |
| 9356 | */ |
| 9357 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9358 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9359 | popstackmark(&smark); |
| 9360 | } |
| 9361 | |
| 9362 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9363 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9364 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9365 | char *volatile savecmdname; |
| 9366 | struct jmploc *volatile savehandler; |
| 9367 | struct jmploc jmploc; |
| 9368 | int i; |
| 9369 | |
| 9370 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9371 | i = setjmp(jmploc.loc); |
| 9372 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9373 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9374 | savehandler = exception_handler; |
| 9375 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9376 | commandname = argv[0]; |
| 9377 | argptr = argv + 1; |
| 9378 | optptr = NULL; /* initialize nextopt */ |
| 9379 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9380 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9381 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9382 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9383 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9384 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9385 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9386 | |
| 9387 | return i; |
| 9388 | } |
| 9389 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9390 | static int |
| 9391 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9392 | { |
| 9393 | return !*endofname(p); |
| 9394 | } |
| 9395 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9396 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9397 | /* |
| 9398 | * Search for a command. This is called before we fork so that the |
| 9399 | * 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] | 9400 | * the child. The check for "goodname" is an overly conservative |
| 9401 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9402 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9403 | static void |
| 9404 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9405 | { |
| 9406 | struct cmdentry entry; |
| 9407 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9408 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9409 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9410 | } |
| 9411 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9412 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9413 | /* ============ Builtin commands |
| 9414 | * |
| 9415 | * Builtin commands whose functions are closely tied to evaluation |
| 9416 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9417 | */ |
| 9418 | |
| 9419 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9420 | * Handle break and continue commands. Break, continue, and return are |
| 9421 | * all handled by setting the evalskip flag. The evaluation routines |
| 9422 | * above all check this flag, and if it is set they start skipping |
| 9423 | * commands rather than executing them. The variable skipcount is |
| 9424 | * the number of loops to break/continue, or the number of function |
| 9425 | * levels to return. (The latter is always 1.) It should probably |
| 9426 | * be an error to break out of more loops than exist, but it isn't |
| 9427 | * in the standard shell so we don't make it one here. |
| 9428 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9429 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9430 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9431 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9432 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9433 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9434 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9435 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9436 | if (n > loopnest) |
| 9437 | n = loopnest; |
| 9438 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9439 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9440 | skipcount = n; |
| 9441 | } |
| 9442 | return 0; |
| 9443 | } |
| 9444 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9445 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9446 | /* ============ input.c |
| 9447 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9448 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9449 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9450 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9451 | enum { |
| 9452 | INPUT_PUSH_FILE = 1, |
| 9453 | INPUT_NOFILE_OK = 2, |
| 9454 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9455 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9456 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9457 | /* values of checkkwd variable */ |
| 9458 | #define CHKALIAS 0x1 |
| 9459 | #define CHKKWD 0x2 |
| 9460 | #define CHKNL 0x4 |
| 9461 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9462 | /* |
| 9463 | * Push a string back onto the input at this current parsefile level. |
| 9464 | * We handle aliases this way. |
| 9465 | */ |
| 9466 | #if !ENABLE_ASH_ALIAS |
| 9467 | #define pushstring(s, ap) pushstring(s) |
| 9468 | #endif |
| 9469 | static void |
| 9470 | pushstring(char *s, struct alias *ap) |
| 9471 | { |
| 9472 | struct strpush *sp; |
| 9473 | int len; |
| 9474 | |
| 9475 | len = strlen(s); |
| 9476 | INT_OFF; |
| 9477 | if (g_parsefile->strpush) { |
| 9478 | sp = ckzalloc(sizeof(*sp)); |
| 9479 | sp->prev = g_parsefile->strpush; |
| 9480 | } else { |
| 9481 | sp = &(g_parsefile->basestrpush); |
| 9482 | } |
| 9483 | g_parsefile->strpush = sp; |
| 9484 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9485 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9486 | #if ENABLE_ASH_ALIAS |
| 9487 | sp->ap = ap; |
| 9488 | if (ap) { |
| 9489 | ap->flag |= ALIASINUSE; |
| 9490 | sp->string = s; |
| 9491 | } |
| 9492 | #endif |
| 9493 | g_parsefile->next_to_pgetc = s; |
| 9494 | g_parsefile->left_in_line = len; |
| 9495 | INT_ON; |
| 9496 | } |
| 9497 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9498 | static void |
| 9499 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9500 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9501 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9502 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9503 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9504 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9505 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9506 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9507 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9508 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9509 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9510 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9511 | if (sp->string != sp->ap->val) { |
| 9512 | free(sp->string); |
| 9513 | } |
| 9514 | sp->ap->flag &= ~ALIASINUSE; |
| 9515 | if (sp->ap->flag & ALIASDEAD) { |
| 9516 | unalias(sp->ap->name); |
| 9517 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9518 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9519 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9520 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9521 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9522 | g_parsefile->strpush = sp->prev; |
| 9523 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9524 | free(sp); |
| 9525 | INT_ON; |
| 9526 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9527 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9528 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9529 | //it peeks whether it is &>, and then pushes back both chars. |
| 9530 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9531 | //to make two pungetc() reliable. Currently, |
| 9532 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9533 | static int |
| 9534 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9535 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9536 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9537 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9538 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9539 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9540 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9541 | retry: |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9542 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
| 9543 | nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9544 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9545 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9546 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9547 | #endif |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9548 | nr = read_line_input(cmdedit_prompt, buf, IBUFSIZ, line_input_state); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9549 | if (nr == 0) { |
| 9550 | /* Ctrl+C pressed */ |
| 9551 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9552 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9553 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9554 | raise(SIGINT); |
| 9555 | return 1; |
| 9556 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9557 | goto retry; |
| 9558 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9559 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9560 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9561 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9562 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9563 | } |
| 9564 | #else |
Denys Vlasenko | 161bb8f | 2010-06-06 05:07:11 +0200 | [diff] [blame] | 9565 | nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9566 | #endif |
| 9567 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9568 | #if 0 |
| 9569 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9570 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9571 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9572 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9573 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9574 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9575 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9576 | out2str("sh: turning off NDELAY mode\n"); |
| 9577 | goto retry; |
| 9578 | } |
| 9579 | } |
| 9580 | } |
| 9581 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9582 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9583 | return nr; |
| 9584 | } |
| 9585 | |
| 9586 | /* |
| 9587 | * Refill the input buffer and return the next input character: |
| 9588 | * |
| 9589 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9590 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9591 | * or we are reading from a string so we can't refill the buffer, |
| 9592 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9593 | * 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] | 9594 | * 4) Process input up to the next newline, deleting nul characters. |
| 9595 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9596 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9597 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9598 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9599 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9600 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9601 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9602 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9603 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9604 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9605 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9606 | if (g_parsefile->left_in_line == -1 |
| 9607 | && g_parsefile->strpush->ap |
| 9608 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9609 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9610 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9611 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9612 | return PEOA; |
| 9613 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9614 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9615 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9616 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9617 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9618 | g_parsefile->left_in_line, |
| 9619 | g_parsefile->next_to_pgetc, |
| 9620 | g_parsefile->next_to_pgetc); |
| 9621 | if (--g_parsefile->left_in_line >= 0) |
| 9622 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9623 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9624 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9625 | * "pgetc" needs refilling. |
| 9626 | */ |
| 9627 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9628 | /* -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] | 9629 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9630 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9631 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9632 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9633 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9634 | /* even in failure keep left_in_line and next_to_pgetc |
| 9635 | * in lock step, for correct multi-layer pungetc. |
| 9636 | * left_in_line was decremented before preadbuffer(), |
| 9637 | * must inc next_to_pgetc: */ |
| 9638 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9639 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9640 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9641 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9642 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9643 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9644 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9645 | again: |
| 9646 | more = preadfd(); |
| 9647 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9648 | /* don't try reading again */ |
| 9649 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9650 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9651 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9652 | return PEOF; |
| 9653 | } |
| 9654 | } |
| 9655 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9656 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9657 | * Set g_parsefile->left_in_line |
| 9658 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9659 | * NUL chars are deleted. |
| 9660 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9661 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9662 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9663 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9664 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9665 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9666 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9667 | c = *q; |
| 9668 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9669 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9670 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9671 | q++; |
| 9672 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9673 | 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] | 9674 | break; |
| 9675 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9676 | } |
| 9677 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9678 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9679 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9680 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9681 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9682 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9683 | } |
| 9684 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9685 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9686 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9687 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9688 | char save = *q; |
| 9689 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9690 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9691 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9692 | } |
| 9693 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9694 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9695 | g_parsefile->left_in_line, |
| 9696 | g_parsefile->next_to_pgetc, |
| 9697 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9698 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9699 | } |
| 9700 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9701 | #define pgetc_as_macro() \ |
| 9702 | (--g_parsefile->left_in_line >= 0 \ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9703 | ? (unsigned char)*g_parsefile->next_to_pgetc++ \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9704 | : preadbuffer() \ |
| 9705 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9706 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9707 | static int |
| 9708 | pgetc(void) |
| 9709 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9710 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9711 | g_parsefile->left_in_line, |
| 9712 | g_parsefile->next_to_pgetc, |
| 9713 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9714 | return pgetc_as_macro(); |
| 9715 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9716 | |
| 9717 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9718 | # define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9719 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9720 | # define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9721 | #endif |
| 9722 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9723 | #if ENABLE_ASH_ALIAS |
| 9724 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9725 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9726 | { |
| 9727 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9728 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9729 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9730 | g_parsefile->left_in_line, |
| 9731 | g_parsefile->next_to_pgetc, |
| 9732 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9733 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9734 | } while (c == PEOA); |
| 9735 | return c; |
| 9736 | } |
| 9737 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9738 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9739 | #endif |
| 9740 | |
| 9741 | /* |
| 9742 | * Read a line from the script. |
| 9743 | */ |
| 9744 | static char * |
| 9745 | pfgets(char *line, int len) |
| 9746 | { |
| 9747 | char *p = line; |
| 9748 | int nleft = len; |
| 9749 | int c; |
| 9750 | |
| 9751 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9752 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9753 | if (c == PEOF) { |
| 9754 | if (p == line) |
| 9755 | return NULL; |
| 9756 | break; |
| 9757 | } |
| 9758 | *p++ = c; |
| 9759 | if (c == '\n') |
| 9760 | break; |
| 9761 | } |
| 9762 | *p = '\0'; |
| 9763 | return line; |
| 9764 | } |
| 9765 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9766 | /* |
| 9767 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9768 | * PEOF may be pushed back. |
| 9769 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9770 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9771 | pungetc(void) |
| 9772 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9773 | g_parsefile->left_in_line++; |
| 9774 | g_parsefile->next_to_pgetc--; |
| 9775 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9776 | g_parsefile->left_in_line, |
| 9777 | g_parsefile->next_to_pgetc, |
| 9778 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9779 | } |
| 9780 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9781 | /* |
| 9782 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9783 | * adds a new entry to the stack and popfile restores the previous level. |
| 9784 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9785 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9786 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9787 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9788 | struct parsefile *pf; |
| 9789 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9790 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9791 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9792 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9793 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9794 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9795 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9796 | } |
| 9797 | |
| 9798 | static void |
| 9799 | popfile(void) |
| 9800 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9801 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9802 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9803 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9804 | if (pf->pf_fd >= 0) |
| 9805 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9806 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9807 | while (pf->strpush) |
| 9808 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9809 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9810 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9811 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9814 | /* |
| 9815 | * Return to top level. |
| 9816 | */ |
| 9817 | static void |
| 9818 | popallfiles(void) |
| 9819 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9820 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9821 | popfile(); |
| 9822 | } |
| 9823 | |
| 9824 | /* |
| 9825 | * Close the file(s) that the shell is reading commands from. Called |
| 9826 | * after a fork is done. |
| 9827 | */ |
| 9828 | static void |
| 9829 | closescript(void) |
| 9830 | { |
| 9831 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9832 | if (g_parsefile->pf_fd > 0) { |
| 9833 | close(g_parsefile->pf_fd); |
| 9834 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9835 | } |
| 9836 | } |
| 9837 | |
| 9838 | /* |
| 9839 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9840 | * interrupts off. |
| 9841 | */ |
| 9842 | static void |
| 9843 | setinputfd(int fd, int push) |
| 9844 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9845 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9846 | if (push) { |
| 9847 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9848 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9849 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9850 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9851 | if (g_parsefile->buf == NULL) |
| 9852 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9853 | g_parsefile->left_in_buffer = 0; |
| 9854 | g_parsefile->left_in_line = 0; |
| 9855 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9856 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9857 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9858 | /* |
| 9859 | * Set the input to take input from a file. If push is set, push the |
| 9860 | * old input onto the stack first. |
| 9861 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9862 | static int |
| 9863 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9864 | { |
| 9865 | int fd; |
| 9866 | int fd2; |
| 9867 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9868 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9869 | fd = open(fname, O_RDONLY); |
| 9870 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9871 | if (flags & INPUT_NOFILE_OK) |
| 9872 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9873 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9874 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9875 | if (fd < 10) { |
| 9876 | fd2 = copyfd(fd, 10); |
| 9877 | close(fd); |
| 9878 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9879 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9880 | fd = fd2; |
| 9881 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9882 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9883 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9884 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9885 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9888 | /* |
| 9889 | * Like setinputfile, but takes input from a string. |
| 9890 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9891 | static void |
| 9892 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9893 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9894 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9895 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9896 | g_parsefile->next_to_pgetc = string; |
| 9897 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9898 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9899 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9900 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9901 | } |
| 9902 | |
| 9903 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9904 | /* ============ mail.c |
| 9905 | * |
| 9906 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9907 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9908 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9909 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9910 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9911 | #define MAXMBOXES 10 |
| 9912 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9913 | /* times of mailboxes */ |
| 9914 | static time_t mailtime[MAXMBOXES]; |
| 9915 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9916 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9917 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9918 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9919 | * Print appropriate message(s) if mail has arrived. |
| 9920 | * If mail_var_path_changed is set, |
| 9921 | * then the value of MAIL has mail_var_path_changed, |
| 9922 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9923 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9924 | static void |
| 9925 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9926 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9927 | const char *mpath; |
| 9928 | char *p; |
| 9929 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9930 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9931 | struct stackmark smark; |
| 9932 | struct stat statb; |
| 9933 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9934 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9935 | mpath = mpathset() ? mpathval() : mailval(); |
| 9936 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9937 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9938 | if (p == NULL) |
| 9939 | break; |
| 9940 | if (*p == '\0') |
| 9941 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9942 | for (q = p; *q; q++) |
| 9943 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9944 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9945 | if (q[-1] != '/') |
| 9946 | abort(); |
| 9947 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9948 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9949 | if (stat(p, &statb) < 0) { |
| 9950 | *mtp = 0; |
| 9951 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9952 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9953 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9954 | fprintf( |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 9955 | stderr, "%s\n", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9956 | pathopt ? pathopt : "you have mail" |
| 9957 | ); |
| 9958 | } |
| 9959 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9960 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9961 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9962 | popstackmark(&smark); |
| 9963 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9964 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9965 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9966 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9967 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9968 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9969 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9970 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9971 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9972 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9973 | |
| 9974 | /* ============ ??? */ |
| 9975 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9976 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9977 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9978 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9979 | static void |
| 9980 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9981 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9982 | char **newparam; |
| 9983 | char **ap; |
| 9984 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9985 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9986 | for (nparam = 0; argv[nparam]; nparam++) |
| 9987 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9988 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 9989 | while (*argv) { |
| 9990 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9991 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9992 | *ap = NULL; |
| 9993 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 9994 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9995 | shellparam.nparam = nparam; |
| 9996 | shellparam.p = newparam; |
| 9997 | #if ENABLE_ASH_GETOPTS |
| 9998 | shellparam.optind = 1; |
| 9999 | shellparam.optoff = -1; |
| 10000 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10001 | } |
| 10002 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10003 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10004 | * Process shell options. The global variable argptr contains a pointer |
| 10005 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10006 | * |
| 10007 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 10008 | * For a non-interactive shell, an error condition encountered |
| 10009 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 10010 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 10011 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10012 | * ... |
| 10013 | * Utility syntax error (option or operand error) Shall exit |
| 10014 | * ... |
| 10015 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 10016 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 10017 | * and shell continues), and people rely on this behavior! |
| 10018 | * Testcase: |
| 10019 | * set -o barfoo 2>/dev/null |
| 10020 | * echo $? |
| 10021 | * |
| 10022 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10023 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10024 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10025 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10026 | { |
| 10027 | int i; |
| 10028 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10029 | if (name) { |
| 10030 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10031 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10032 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10033 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10034 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10035 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10036 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10037 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10038 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10039 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10040 | if (val) { |
| 10041 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 10042 | } else { |
| 10043 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 10044 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10045 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10046 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10047 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10048 | static void |
| 10049 | setoption(int flag, int val) |
| 10050 | { |
| 10051 | int i; |
| 10052 | |
| 10053 | for (i = 0; i < NOPTS; i++) { |
| 10054 | if (optletters(i) == flag) { |
| 10055 | optlist[i] = val; |
| 10056 | return; |
| 10057 | } |
| 10058 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10059 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10060 | /* NOTREACHED */ |
| 10061 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10062 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10063 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10064 | { |
| 10065 | char *p; |
| 10066 | int val; |
| 10067 | int c; |
| 10068 | |
| 10069 | if (cmdline) |
| 10070 | minusc = NULL; |
| 10071 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10072 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10073 | if (c != '-' && c != '+') |
| 10074 | break; |
| 10075 | argptr++; |
| 10076 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10077 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10078 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10079 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10080 | if (!cmdline) { |
| 10081 | /* "-" means turn off -x and -v */ |
| 10082 | if (p[0] == '\0') |
| 10083 | xflag = vflag = 0; |
| 10084 | /* "--" means reset params */ |
| 10085 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10086 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10087 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10088 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10089 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10090 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10091 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10092 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10093 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10094 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10095 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10096 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10097 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10098 | /* it already printed err message */ |
| 10099 | return 1; /* error */ |
| 10100 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10101 | if (*argptr) |
| 10102 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10103 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10104 | isloginsh = 1; |
| 10105 | /* bash does not accept +-login, we also won't */ |
| 10106 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10107 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10108 | isloginsh = 1; |
| 10109 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10110 | } else { |
| 10111 | setoption(c, val); |
| 10112 | } |
| 10113 | } |
| 10114 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10115 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10116 | } |
| 10117 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10118 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10119 | * The shift builtin command. |
| 10120 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10121 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10122 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10123 | { |
| 10124 | int n; |
| 10125 | char **ap1, **ap2; |
| 10126 | |
| 10127 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10128 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10129 | n = number(argv[1]); |
| 10130 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10131 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10132 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10133 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10134 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10135 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10136 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10137 | } |
| 10138 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10139 | while ((*ap2++ = *ap1++) != NULL) |
| 10140 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10141 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10142 | shellparam.optind = 1; |
| 10143 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10144 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10145 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10146 | return 0; |
| 10147 | } |
| 10148 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10149 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10150 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10151 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10152 | * Maybe we could keep them in an ordered balanced binary tree |
| 10153 | * instead of hashed lists. |
| 10154 | * For now just roll 'em through qsort for printing... |
| 10155 | */ |
| 10156 | static int |
| 10157 | showvars(const char *sep_prefix, int on, int off) |
| 10158 | { |
| 10159 | const char *sep; |
| 10160 | char **ep, **epend; |
| 10161 | |
| 10162 | ep = listvars(on, off, &epend); |
| 10163 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10164 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10165 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10166 | |
| 10167 | for (; ep < epend; ep++) { |
| 10168 | const char *p; |
| 10169 | const char *q; |
| 10170 | |
| 10171 | p = strchrnul(*ep, '='); |
| 10172 | q = nullstr; |
| 10173 | if (*p) |
| 10174 | q = single_quote(++p); |
| 10175 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10176 | } |
| 10177 | return 0; |
| 10178 | } |
| 10179 | |
| 10180 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10181 | * The set command builtin. |
| 10182 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10183 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10184 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10185 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10186 | int retval; |
| 10187 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10188 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10189 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10190 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10191 | retval = 1; |
| 10192 | if (!options(0)) { /* if no parse error... */ |
| 10193 | retval = 0; |
| 10194 | optschanged(); |
| 10195 | if (*argptr != NULL) { |
| 10196 | setparam(argptr); |
| 10197 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10198 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10199 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10200 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10201 | } |
| 10202 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10203 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10204 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10205 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10206 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10207 | uint32_t t; |
| 10208 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10209 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10210 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10211 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10212 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 10213 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10214 | vrandom.flags &= ~VNOFUNC; |
| 10215 | } else { |
| 10216 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10217 | t = strtoul(value, NULL, 10); |
| 10218 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10219 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10220 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10221 | #endif |
| 10222 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10223 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10224 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10225 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10226 | { |
| 10227 | char *p, *q; |
| 10228 | char c = '?'; |
| 10229 | int done = 0; |
| 10230 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10231 | char s[12]; |
| 10232 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10233 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10234 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10235 | return 1; |
| 10236 | optnext = optfirst + *param_optind - 1; |
| 10237 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10238 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10239 | p = NULL; |
| 10240 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10241 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10242 | if (p == NULL || *p == '\0') { |
| 10243 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10244 | p = *optnext; |
| 10245 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10246 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10247 | p = NULL; |
| 10248 | done = 1; |
| 10249 | goto out; |
| 10250 | } |
| 10251 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10252 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10253 | goto atend; |
| 10254 | } |
| 10255 | |
| 10256 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10257 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10258 | if (*q == '\0') { |
| 10259 | if (optstr[0] == ':') { |
| 10260 | s[0] = c; |
| 10261 | s[1] = '\0'; |
| 10262 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10263 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10264 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10265 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10266 | } |
| 10267 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10268 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10269 | } |
| 10270 | if (*++q == ':') |
| 10271 | q++; |
| 10272 | } |
| 10273 | |
| 10274 | if (*++q == ':') { |
| 10275 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10276 | if (optstr[0] == ':') { |
| 10277 | s[0] = c; |
| 10278 | s[1] = '\0'; |
| 10279 | err |= setvarsafe("OPTARG", s, 0); |
| 10280 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10281 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10282 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10283 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10284 | c = '?'; |
| 10285 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10286 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10287 | } |
| 10288 | |
| 10289 | if (p == *optnext) |
| 10290 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10291 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10292 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10293 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10294 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10295 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10296 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10297 | *param_optind = optnext - optfirst + 1; |
| 10298 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10299 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10300 | s[0] = c; |
| 10301 | s[1] = '\0'; |
| 10302 | err |= setvarsafe(optvar, s, 0); |
| 10303 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10304 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10305 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10306 | flush_stdout_stderr(); |
| 10307 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10308 | } |
| 10309 | return done; |
| 10310 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10311 | |
| 10312 | /* |
| 10313 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10314 | * to be processed. Shellparam.optptr points to the next character to |
| 10315 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10316 | * then it's the first time getopts has been called. |
| 10317 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10318 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10319 | getoptscmd(int argc, char **argv) |
| 10320 | { |
| 10321 | char **optbase; |
| 10322 | |
| 10323 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10324 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10325 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10326 | optbase = shellparam.p; |
| 10327 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10328 | shellparam.optind = 1; |
| 10329 | shellparam.optoff = -1; |
| 10330 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10331 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10332 | optbase = &argv[3]; |
| 10333 | if (shellparam.optind > argc - 2) { |
| 10334 | shellparam.optind = 1; |
| 10335 | shellparam.optoff = -1; |
| 10336 | } |
| 10337 | } |
| 10338 | |
| 10339 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10340 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10341 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10342 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10343 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10344 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10345 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10346 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10347 | struct heredoc { |
| 10348 | struct heredoc *next; /* next here document in list */ |
| 10349 | union node *here; /* redirection node */ |
| 10350 | char *eofmark; /* string indicating end of input */ |
| 10351 | smallint striptabs; /* if set, strip leading tabs */ |
| 10352 | }; |
| 10353 | |
| 10354 | static smallint tokpushback; /* last token pushed back */ |
| 10355 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10356 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10357 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10358 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10359 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10360 | static struct nodelist *backquotelist; |
| 10361 | static union node *redirnode; |
| 10362 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10363 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10364 | static const char * |
| 10365 | tokname(char *buf, int tok) |
| 10366 | { |
| 10367 | if (tok < TSEMI) |
| 10368 | return tokname_array[tok] + 1; |
| 10369 | sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 10370 | return buf; |
| 10371 | } |
| 10372 | |
| 10373 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10374 | * Called when an unexpected token is read during the parse. The argument |
| 10375 | * is the token that is expected, or -1 if more than one type of token can |
| 10376 | * occur at this point. |
| 10377 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10378 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10379 | static void |
| 10380 | raise_error_unexpected_syntax(int token) |
| 10381 | { |
| 10382 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10383 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10384 | int l; |
| 10385 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10386 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10387 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10388 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10389 | raise_error_syntax(msg); |
| 10390 | /* NOTREACHED */ |
| 10391 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10392 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10393 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10394 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10395 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10396 | static union node *andor(void); |
| 10397 | static union node *pipeline(void); |
| 10398 | static union node *parse_command(void); |
| 10399 | static void parseheredoc(void); |
| 10400 | static char peektoken(void); |
| 10401 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10402 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10403 | static union node * |
| 10404 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10405 | { |
| 10406 | union node *n1, *n2, *n3; |
| 10407 | int tok; |
| 10408 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10409 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10410 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10411 | return NULL; |
| 10412 | n1 = NULL; |
| 10413 | for (;;) { |
| 10414 | n2 = andor(); |
| 10415 | tok = readtoken(); |
| 10416 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10417 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10418 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10419 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10420 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10421 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10422 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10423 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10424 | n2 = n3; |
| 10425 | } |
| 10426 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10427 | } |
| 10428 | } |
| 10429 | if (n1 == NULL) { |
| 10430 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10431 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10432 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10433 | n3->type = NSEMI; |
| 10434 | n3->nbinary.ch1 = n1; |
| 10435 | n3->nbinary.ch2 = n2; |
| 10436 | n1 = n3; |
| 10437 | } |
| 10438 | switch (tok) { |
| 10439 | case TBACKGND: |
| 10440 | case TSEMI: |
| 10441 | tok = readtoken(); |
| 10442 | /* fall through */ |
| 10443 | case TNL: |
| 10444 | if (tok == TNL) { |
| 10445 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10446 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10447 | return n1; |
| 10448 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10449 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10450 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10451 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10452 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10453 | return n1; |
| 10454 | break; |
| 10455 | case TEOF: |
| 10456 | if (heredoclist) |
| 10457 | parseheredoc(); |
| 10458 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10459 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10460 | return n1; |
| 10461 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10462 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10463 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10464 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10465 | return n1; |
| 10466 | } |
| 10467 | } |
| 10468 | } |
| 10469 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10470 | static union node * |
| 10471 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10472 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10473 | union node *n1, *n2, *n3; |
| 10474 | int t; |
| 10475 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10476 | n1 = pipeline(); |
| 10477 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10478 | t = readtoken(); |
| 10479 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10480 | t = NAND; |
| 10481 | } else if (t == TOR) { |
| 10482 | t = NOR; |
| 10483 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10484 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10485 | return n1; |
| 10486 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10487 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10488 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10489 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10490 | n3->type = t; |
| 10491 | n3->nbinary.ch1 = n1; |
| 10492 | n3->nbinary.ch2 = n2; |
| 10493 | n1 = n3; |
| 10494 | } |
| 10495 | } |
| 10496 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10497 | static union node * |
| 10498 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10499 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10500 | union node *n1, *n2, *pipenode; |
| 10501 | struct nodelist *lp, *prev; |
| 10502 | int negate; |
| 10503 | |
| 10504 | negate = 0; |
| 10505 | TRACE(("pipeline: entered\n")); |
| 10506 | if (readtoken() == TNOT) { |
| 10507 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10508 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10509 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10510 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10511 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10512 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10513 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10514 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10515 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10516 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10517 | pipenode->npipe.cmdlist = lp; |
| 10518 | lp->n = n1; |
| 10519 | do { |
| 10520 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10521 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10522 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10523 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10524 | prev->next = lp; |
| 10525 | } while (readtoken() == TPIPE); |
| 10526 | lp->next = NULL; |
| 10527 | n1 = pipenode; |
| 10528 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10529 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10530 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10531 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10532 | n2->type = NNOT; |
| 10533 | n2->nnot.com = n1; |
| 10534 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10535 | } |
| 10536 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10537 | } |
| 10538 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10539 | static union node * |
| 10540 | makename(void) |
| 10541 | { |
| 10542 | union node *n; |
| 10543 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10544 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10545 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10546 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10547 | n->narg.text = wordtext; |
| 10548 | n->narg.backquote = backquotelist; |
| 10549 | return n; |
| 10550 | } |
| 10551 | |
| 10552 | static void |
| 10553 | fixredir(union node *n, const char *text, int err) |
| 10554 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10555 | int fd; |
| 10556 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10557 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10558 | if (!err) |
| 10559 | n->ndup.vname = NULL; |
| 10560 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10561 | fd = bb_strtou(text, NULL, 10); |
| 10562 | if (!errno && fd >= 0) |
| 10563 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10564 | else if (LONE_DASH(text)) |
| 10565 | n->ndup.dupfd = -1; |
| 10566 | else { |
| 10567 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10568 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10569 | n->ndup.vname = makename(); |
| 10570 | } |
| 10571 | } |
| 10572 | |
| 10573 | /* |
| 10574 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10575 | * or backquotes). |
| 10576 | */ |
| 10577 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10578 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10579 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10580 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10581 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10582 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10583 | if (c == CTLQUOTEMARK) |
| 10584 | continue; |
| 10585 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10586 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10587 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10588 | return 0; |
| 10589 | } |
| 10590 | return 1; |
| 10591 | } |
| 10592 | |
| 10593 | static void |
| 10594 | parsefname(void) |
| 10595 | { |
| 10596 | union node *n = redirnode; |
| 10597 | |
| 10598 | if (readtoken() != TWORD) |
| 10599 | raise_error_unexpected_syntax(-1); |
| 10600 | if (n->type == NHERE) { |
| 10601 | struct heredoc *here = heredoc; |
| 10602 | struct heredoc *p; |
| 10603 | int i; |
| 10604 | |
| 10605 | if (quoteflag == 0) |
| 10606 | n->type = NXHERE; |
| 10607 | TRACE(("Here document %d\n", n->type)); |
| 10608 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10609 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10610 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10611 | here->eofmark = wordtext; |
| 10612 | here->next = NULL; |
| 10613 | if (heredoclist == NULL) |
| 10614 | heredoclist = here; |
| 10615 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10616 | for (p = heredoclist; p->next; p = p->next) |
| 10617 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10618 | p->next = here; |
| 10619 | } |
| 10620 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10621 | fixredir(n, wordtext, 0); |
| 10622 | } else { |
| 10623 | n->nfile.fname = makename(); |
| 10624 | } |
| 10625 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10626 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10627 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10628 | simplecmd(void) |
| 10629 | { |
| 10630 | union node *args, **app; |
| 10631 | union node *n = NULL; |
| 10632 | union node *vars, **vpp; |
| 10633 | union node **rpp, *redir; |
| 10634 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10635 | #if ENABLE_ASH_BASH_COMPAT |
| 10636 | smallint double_brackets_flag = 0; |
| 10637 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10638 | |
| 10639 | args = NULL; |
| 10640 | app = &args; |
| 10641 | vars = NULL; |
| 10642 | vpp = &vars; |
| 10643 | redir = NULL; |
| 10644 | rpp = &redir; |
| 10645 | |
| 10646 | savecheckkwd = CHKALIAS; |
| 10647 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10648 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10649 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10650 | t = readtoken(); |
| 10651 | switch (t) { |
| 10652 | #if ENABLE_ASH_BASH_COMPAT |
| 10653 | case TAND: /* "&&" */ |
| 10654 | case TOR: /* "||" */ |
| 10655 | if (!double_brackets_flag) { |
| 10656 | tokpushback = 1; |
| 10657 | goto out; |
| 10658 | } |
| 10659 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10660 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10661 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10662 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10663 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10664 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10665 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10666 | #if ENABLE_ASH_BASH_COMPAT |
| 10667 | if (strcmp("[[", wordtext) == 0) |
| 10668 | double_brackets_flag = 1; |
| 10669 | else if (strcmp("]]", wordtext) == 0) |
| 10670 | double_brackets_flag = 0; |
| 10671 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10672 | n->narg.backquote = backquotelist; |
| 10673 | if (savecheckkwd && isassignment(wordtext)) { |
| 10674 | *vpp = n; |
| 10675 | vpp = &n->narg.next; |
| 10676 | } else { |
| 10677 | *app = n; |
| 10678 | app = &n->narg.next; |
| 10679 | savecheckkwd = 0; |
| 10680 | } |
| 10681 | break; |
| 10682 | case TREDIR: |
| 10683 | *rpp = n = redirnode; |
| 10684 | rpp = &n->nfile.next; |
| 10685 | parsefname(); /* read name of redirection file */ |
| 10686 | break; |
| 10687 | case TLP: |
| 10688 | if (args && app == &args->narg.next |
| 10689 | && !vars && !redir |
| 10690 | ) { |
| 10691 | struct builtincmd *bcmd; |
| 10692 | const char *name; |
| 10693 | |
| 10694 | /* We have a function */ |
| 10695 | if (readtoken() != TRP) |
| 10696 | raise_error_unexpected_syntax(TRP); |
| 10697 | name = n->narg.text; |
| 10698 | if (!goodname(name) |
| 10699 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10700 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10701 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10702 | } |
| 10703 | n->type = NDEFUN; |
| 10704 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10705 | n->narg.next = parse_command(); |
| 10706 | return n; |
| 10707 | } |
| 10708 | /* fall through */ |
| 10709 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10710 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10711 | goto out; |
| 10712 | } |
| 10713 | } |
| 10714 | out: |
| 10715 | *app = NULL; |
| 10716 | *vpp = NULL; |
| 10717 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10718 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10719 | n->type = NCMD; |
| 10720 | n->ncmd.args = args; |
| 10721 | n->ncmd.assign = vars; |
| 10722 | n->ncmd.redirect = redir; |
| 10723 | return n; |
| 10724 | } |
| 10725 | |
| 10726 | static union node * |
| 10727 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10728 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10729 | union node *n1, *n2; |
| 10730 | union node *ap, **app; |
| 10731 | union node *cp, **cpp; |
| 10732 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10733 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10734 | int t; |
| 10735 | |
| 10736 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10737 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10738 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10739 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10740 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10741 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10742 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10743 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10744 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10745 | n1->type = NIF; |
| 10746 | n1->nif.test = list(0); |
| 10747 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10748 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10749 | n1->nif.ifpart = list(0); |
| 10750 | n2 = n1; |
| 10751 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10752 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10753 | n2 = n2->nif.elsepart; |
| 10754 | n2->type = NIF; |
| 10755 | n2->nif.test = list(0); |
| 10756 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10757 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10758 | n2->nif.ifpart = list(0); |
| 10759 | } |
| 10760 | if (lasttoken == TELSE) |
| 10761 | n2->nif.elsepart = list(0); |
| 10762 | else { |
| 10763 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10764 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10765 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10766 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10767 | break; |
| 10768 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10769 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10770 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10771 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10772 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10773 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10774 | got = readtoken(); |
| 10775 | if (got != TDO) { |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10776 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got] + 1, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10777 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10778 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10779 | } |
| 10780 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10781 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10782 | break; |
| 10783 | } |
| 10784 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10785 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10786 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10787 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10788 | n1->type = NFOR; |
| 10789 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10790 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10791 | if (readtoken() == TIN) { |
| 10792 | app = ≈ |
| 10793 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10794 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10795 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10796 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10797 | n2->narg.text = wordtext; |
| 10798 | n2->narg.backquote = backquotelist; |
| 10799 | *app = n2; |
| 10800 | app = &n2->narg.next; |
| 10801 | } |
| 10802 | *app = NULL; |
| 10803 | n1->nfor.args = ap; |
| 10804 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10805 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10806 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10807 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10808 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10809 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10810 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10811 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10812 | n1->nfor.args = n2; |
| 10813 | /* |
| 10814 | * Newline or semicolon here is optional (but note |
| 10815 | * that the original Bourne shell only allowed NL). |
| 10816 | */ |
| 10817 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10818 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10819 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10820 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10821 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10822 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10823 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10824 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10825 | break; |
| 10826 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10827 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10828 | n1->type = NCASE; |
| 10829 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10830 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10831 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10832 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10833 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10834 | n2->narg.text = wordtext; |
| 10835 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10836 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10837 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10838 | } while (readtoken() == TNL); |
| 10839 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10840 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10841 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10842 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10843 | checkkwd = CHKNL | CHKKWD; |
| 10844 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10845 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10846 | if (lasttoken == TLP) |
| 10847 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10848 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10849 | cp->type = NCLIST; |
| 10850 | app = &cp->nclist.pattern; |
| 10851 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10852 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10853 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10854 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10855 | ap->narg.text = wordtext; |
| 10856 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10857 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10858 | break; |
| 10859 | app = &ap->narg.next; |
| 10860 | readtoken(); |
| 10861 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10862 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10863 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10864 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10865 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10866 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10867 | cpp = &cp->nclist.next; |
| 10868 | |
| 10869 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10870 | t = readtoken(); |
| 10871 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10872 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10873 | raise_error_unexpected_syntax(TENDCASE); |
| 10874 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10875 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10876 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10877 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10878 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10879 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10880 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | n1->type = NSUBSHELL; |
| 10882 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10883 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10884 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10885 | break; |
| 10886 | case TBEGIN: |
| 10887 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10888 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10889 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10890 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10891 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10892 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10893 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10894 | } |
| 10895 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10896 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10897 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10898 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10899 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10900 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10901 | checkkwd = CHKKWD | CHKALIAS; |
| 10902 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10903 | while (readtoken() == TREDIR) { |
| 10904 | *rpp = n2 = redirnode; |
| 10905 | rpp = &n2->nfile.next; |
| 10906 | parsefname(); |
| 10907 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10908 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10909 | *rpp = NULL; |
| 10910 | if (redir) { |
| 10911 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10912 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10913 | n2->type = NREDIR; |
| 10914 | n2->nredir.n = n1; |
| 10915 | n1 = n2; |
| 10916 | } |
| 10917 | n1->nredir.redirect = redir; |
| 10918 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10919 | return n1; |
| 10920 | } |
| 10921 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10922 | #if ENABLE_ASH_BASH_COMPAT |
| 10923 | static int decode_dollar_squote(void) |
| 10924 | { |
| 10925 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10926 | int c, cnt; |
| 10927 | char *p; |
| 10928 | char buf[4]; |
| 10929 | |
| 10930 | c = pgetc(); |
| 10931 | p = strchr(C_escapes, c); |
| 10932 | if (p) { |
| 10933 | buf[0] = c; |
| 10934 | p = buf; |
| 10935 | cnt = 3; |
| 10936 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10937 | do { |
| 10938 | c = pgetc(); |
| 10939 | *++p = c; |
| 10940 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10941 | pungetc(); |
| 10942 | } else if (c == 'x') { /* \xHH */ |
| 10943 | do { |
| 10944 | c = pgetc(); |
| 10945 | *++p = c; |
| 10946 | } while (isxdigit(c) && --cnt); |
| 10947 | pungetc(); |
| 10948 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10949 | c = 'x'; |
| 10950 | goto unrecognized; |
| 10951 | } |
| 10952 | } else { /* simple seq like \\ or \t */ |
| 10953 | p++; |
| 10954 | } |
| 10955 | *p = '\0'; |
| 10956 | p = buf; |
| 10957 | c = bb_process_escape_sequence((void*)&p); |
| 10958 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10959 | if (c != '\'' && c != '"') { |
| 10960 | unrecognized: |
| 10961 | c |= 0x100; /* "please encode \, then me" */ |
| 10962 | } |
| 10963 | } |
| 10964 | return c; |
| 10965 | } |
| 10966 | #endif |
| 10967 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10968 | /* |
| 10969 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10970 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10971 | * 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] | 10972 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10973 | * is the first character of the input token or document. |
| 10974 | * |
| 10975 | * Because C does not have internal subroutines, I have simulated them |
| 10976 | * using goto's to implement the subroutine linkage. The following macros |
| 10977 | * will run code that appears at the end of readtoken1. |
| 10978 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10979 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10980 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10981 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10982 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10983 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10984 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10985 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10986 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10987 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10988 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10989 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10990 | char *out; |
| 10991 | int len; |
| 10992 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10993 | struct nodelist *bqlist; |
| 10994 | smallint quotef; |
| 10995 | smallint dblquote; |
| 10996 | smallint oldstyle; |
| 10997 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 10998 | #if ENABLE_ASH_EXPAND_PRMT |
| 10999 | smallint pssyntax; /* we are expanding a prompt string */ |
| 11000 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11001 | int varnest; /* levels of variables expansion */ |
| 11002 | int arinest; /* levels of arithmetic expansion */ |
| 11003 | int parenlevel; /* levels of parens in arithmetic */ |
| 11004 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 11005 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11006 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11007 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11008 | #if __GNUC__ |
| 11009 | /* Avoid longjmp clobbering */ |
| 11010 | (void) &out; |
| 11011 | (void) "ef; |
| 11012 | (void) &dblquote; |
| 11013 | (void) &varnest; |
| 11014 | (void) &arinest; |
| 11015 | (void) &parenlevel; |
| 11016 | (void) &dqvarnest; |
| 11017 | (void) &oldstyle; |
| 11018 | (void) &prevsyntax; |
| 11019 | (void) &syntax; |
| 11020 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11021 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11022 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11023 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11024 | oldstyle = 0; |
| 11025 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11026 | #if ENABLE_ASH_EXPAND_PRMT |
| 11027 | pssyntax = (syntax == PSSYNTAX); |
| 11028 | if (pssyntax) |
| 11029 | syntax = DQSYNTAX; |
| 11030 | #endif |
| 11031 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11032 | varnest = 0; |
| 11033 | arinest = 0; |
| 11034 | parenlevel = 0; |
| 11035 | dqvarnest = 0; |
| 11036 | |
| 11037 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11038 | loop: |
| 11039 | /* For each line, until end of word */ |
| 11040 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11041 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 11042 | for (;;) { /* until end of line or end of word */ |
| 11043 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 11044 | switch (SIT(c, syntax)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11045 | case CNL: /* '\n' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11046 | if (syntax == BASESYNTAX) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11047 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11048 | USTPUTC(c, out); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11049 | g_parsefile->linno++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11050 | if (doprompt) |
| 11051 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11052 | c = pgetc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11053 | goto loop; /* continue outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11054 | case CWORD: |
| 11055 | USTPUTC(c, out); |
| 11056 | break; |
| 11057 | case CCTL: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11058 | if (eofmark == NULL || dblquote) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11059 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11060 | #if ENABLE_ASH_BASH_COMPAT |
| 11061 | if (c == '\\' && bash_dollar_squote) { |
| 11062 | c = decode_dollar_squote(); |
| 11063 | if (c & 0x100) { |
| 11064 | USTPUTC('\\', out); |
| 11065 | c = (unsigned char)c; |
| 11066 | } |
| 11067 | } |
| 11068 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11069 | USTPUTC(c, out); |
| 11070 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11071 | case CBACK: /* backslash */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11072 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11073 | if (c == PEOF) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11074 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11075 | USTPUTC('\\', out); |
| 11076 | pungetc(); |
| 11077 | } else if (c == '\n') { |
| 11078 | if (doprompt) |
| 11079 | setprompt(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11080 | } else { |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11081 | #if ENABLE_ASH_EXPAND_PRMT |
| 11082 | if (c == '$' && pssyntax) { |
| 11083 | USTPUTC(CTLESC, out); |
| 11084 | USTPUTC('\\', out); |
| 11085 | } |
| 11086 | #endif |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11087 | /* Backslash is retained if we are in "str" and next char isn't special */ |
| 11088 | if (dblquote |
| 11089 | && c != '\\' |
| 11090 | && c != '`' |
| 11091 | && c != '$' |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11092 | && (c != '"' || eofmark != NULL) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11093 | ) { |
| 11094 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11095 | USTPUTC('\\', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11096 | } |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11097 | if (SIT(c, SQSYNTAX) == CCTL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11098 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11099 | USTPUTC(c, out); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11100 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11101 | } |
| 11102 | break; |
| 11103 | case CSQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11104 | syntax = SQSYNTAX; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11105 | quotemark: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11106 | if (eofmark == NULL) { |
| 11107 | USTPUTC(CTLQUOTEMARK, out); |
| 11108 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11109 | break; |
| 11110 | case CDQUOTE: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11111 | syntax = DQSYNTAX; |
| 11112 | dblquote = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11113 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11114 | case CENDQUOTE: |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11115 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11116 | if (eofmark != NULL && arinest == 0 |
| 11117 | && varnest == 0 |
| 11118 | ) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11119 | USTPUTC(c, out); |
| 11120 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11121 | if (dqvarnest == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11122 | syntax = BASESYNTAX; |
| 11123 | dblquote = 0; |
| 11124 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11125 | quotef = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11126 | goto quotemark; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11127 | } |
| 11128 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11129 | case CVAR: /* '$' */ |
| 11130 | PARSESUB(); /* parse substitution */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11131 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11132 | case CENDVAR: /* '}' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11133 | if (varnest > 0) { |
| 11134 | varnest--; |
| 11135 | if (dqvarnest > 0) { |
| 11136 | dqvarnest--; |
| 11137 | } |
| 11138 | USTPUTC(CTLENDVAR, out); |
| 11139 | } else { |
| 11140 | USTPUTC(c, out); |
| 11141 | } |
| 11142 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11143 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11144 | case CLP: /* '(' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11145 | parenlevel++; |
| 11146 | USTPUTC(c, out); |
| 11147 | break; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11148 | case CRP: /* ')' in arithmetic */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11149 | if (parenlevel > 0) { |
| 11150 | USTPUTC(c, out); |
| 11151 | --parenlevel; |
| 11152 | } else { |
| 11153 | if (pgetc() == ')') { |
| 11154 | if (--arinest == 0) { |
| 11155 | USTPUTC(CTLENDARI, out); |
| 11156 | syntax = prevsyntax; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11157 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11158 | } else |
| 11159 | USTPUTC(')', out); |
| 11160 | } else { |
| 11161 | /* |
| 11162 | * unbalanced parens |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11163 | * (don't 2nd guess - no error) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11164 | */ |
| 11165 | pungetc(); |
| 11166 | USTPUTC(')', out); |
| 11167 | } |
| 11168 | } |
| 11169 | break; |
| 11170 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11171 | case CBQUOTE: /* '`' */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11172 | PARSEBACKQOLD(); |
| 11173 | break; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11174 | case CENDFILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11175 | goto endword; /* exit outer loop */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11176 | case CIGN: |
| 11177 | break; |
| 11178 | default: |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11179 | if (varnest == 0) { |
| 11180 | #if ENABLE_ASH_BASH_COMPAT |
| 11181 | if (c == '&') { |
| 11182 | if (pgetc() == '>') |
| 11183 | c = 0x100 + '>'; /* flag &> */ |
| 11184 | pungetc(); |
| 11185 | } |
| 11186 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11187 | goto endword; /* exit outer loop */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11188 | } |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11189 | IF_ASH_ALIAS(if (c != PEOA)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11190 | USTPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11191 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11192 | c = pgetc_fast(); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11193 | } /* for (;;) */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11194 | } |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11195 | endword: |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11196 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11197 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11198 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11199 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11200 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11201 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11202 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11203 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11204 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11205 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11206 | } |
| 11207 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11208 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11209 | out = stackblock(); |
| 11210 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11211 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11212 | && quotef == 0 |
| 11213 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11214 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11215 | PARSEREDIR(); /* passed as params: out, c */ |
| 11216 | lasttoken = TREDIR; |
| 11217 | return lasttoken; |
| 11218 | } |
| 11219 | /* else: non-number X seen, interpret it |
| 11220 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11221 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11222 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11223 | } |
| 11224 | quoteflag = quotef; |
| 11225 | backquotelist = bqlist; |
| 11226 | grabstackblock(len); |
| 11227 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11228 | lasttoken = TWORD; |
| 11229 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11230 | /* end of readtoken routine */ |
| 11231 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11232 | /* |
| 11233 | * Check to see whether we are at the end of the here document. When this |
| 11234 | * is called, c is set to the first character of the next input line. If |
| 11235 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11236 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11237 | checkend: { |
| 11238 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11239 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11240 | if (c == PEOA) |
| 11241 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11242 | #endif |
| 11243 | if (striptabs) { |
| 11244 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11245 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11246 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11247 | } |
| 11248 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11249 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11250 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11251 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11252 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11253 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11254 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11255 | if (*p == '\n' && *q == '\0') { |
| 11256 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11257 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11258 | needprompt = doprompt; |
| 11259 | } else { |
| 11260 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11261 | } |
| 11262 | } |
| 11263 | } |
| 11264 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11265 | goto checkend_return; |
| 11266 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11267 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11268 | /* |
| 11269 | * Parse a redirection operator. The variable "out" points to a string |
| 11270 | * specifying the fd to be redirected. The variable "c" contains the |
| 11271 | * first character of the redirection operator. |
| 11272 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11273 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11274 | /* out is already checked to be a valid number or "" */ |
| 11275 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11276 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11277 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11278 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11279 | if (c == '>') { |
| 11280 | np->nfile.fd = 1; |
| 11281 | c = pgetc(); |
| 11282 | if (c == '>') |
| 11283 | np->type = NAPPEND; |
| 11284 | else if (c == '|') |
| 11285 | np->type = NCLOBBER; |
| 11286 | else if (c == '&') |
| 11287 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11288 | /* 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] | 11289 | else { |
| 11290 | np->type = NTO; |
| 11291 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11292 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11293 | } |
| 11294 | #if ENABLE_ASH_BASH_COMPAT |
| 11295 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11296 | np->nfile.fd = 1; |
| 11297 | pgetc(); /* this is '>', no need to check */ |
| 11298 | np->type = NTO2; |
| 11299 | } |
| 11300 | #endif |
| 11301 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11302 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11303 | c = pgetc(); |
| 11304 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11305 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11306 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11307 | np = stzalloc(sizeof(struct nhere)); |
| 11308 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11309 | } |
| 11310 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11311 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11312 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11313 | c = pgetc(); |
| 11314 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11315 | heredoc->striptabs = 1; |
| 11316 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11317 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11318 | pungetc(); |
| 11319 | } |
| 11320 | break; |
| 11321 | |
| 11322 | case '&': |
| 11323 | np->type = NFROMFD; |
| 11324 | break; |
| 11325 | |
| 11326 | case '>': |
| 11327 | np->type = NFROMTO; |
| 11328 | break; |
| 11329 | |
| 11330 | default: |
| 11331 | np->type = NFROM; |
| 11332 | pungetc(); |
| 11333 | break; |
| 11334 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11335 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11336 | if (fd >= 0) |
| 11337 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11338 | redirnode = np; |
| 11339 | goto parseredir_return; |
| 11340 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11341 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11342 | /* |
| 11343 | * Parse a substitution. At this point, we have read the dollar sign |
| 11344 | * and nothing else. |
| 11345 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11346 | |
| 11347 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11348 | * (assuming ascii char codes, as the original implementation did) */ |
| 11349 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11350 | (((unsigned)(c) - 33 < 32) \ |
| 11351 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11352 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11353 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11354 | int typeloc; |
| 11355 | int flags; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11356 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11357 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11358 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11359 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11360 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11361 | #if ENABLE_ASH_BASH_COMPAT |
| 11362 | if (c == '\'') |
| 11363 | bash_dollar_squote = 1; |
| 11364 | else |
| 11365 | #endif |
| 11366 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11367 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11368 | } else if (c == '(') { |
| 11369 | /* $(command) or $((arith)) */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11370 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11371 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11372 | PARSEARITH(); |
| 11373 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11374 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11375 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11376 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11377 | pungetc(); |
| 11378 | PARSEBACKQNEW(); |
| 11379 | } |
| 11380 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11381 | /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11382 | USTPUTC(CTLVAR, out); |
| 11383 | typeloc = out - (char *)stackblock(); |
| 11384 | USTPUTC(VSNORMAL, out); |
| 11385 | subtype = VSNORMAL; |
| 11386 | if (c == '{') { |
| 11387 | c = pgetc(); |
| 11388 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11389 | c = pgetc(); |
| 11390 | if (c == '}') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11391 | c = '#'; /* ${#} - same as $# */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11392 | else |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11393 | subtype = VSLENGTH; /* ${#VAR} */ |
| 11394 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11395 | subtype = 0; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11396 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11397 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11398 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11399 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11400 | do { |
| 11401 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11402 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11403 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11404 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11405 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11406 | do { |
| 11407 | STPUTC(c, out); |
| 11408 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11409 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11410 | } else if (is_special(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11411 | /* $[{[#]]<specialchar>[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11412 | USTPUTC(c, out); |
| 11413 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11414 | } else { |
| 11415 | badsub: |
| 11416 | raise_error_syntax("bad substitution"); |
| 11417 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11418 | if (c != '}' && subtype == VSLENGTH) { |
| 11419 | /* ${#VAR didn't end with } */ |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11420 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11421 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11422 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11423 | STPUTC('=', out); |
| 11424 | flags = 0; |
| 11425 | if (subtype == 0) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11426 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 11427 | /* c == first char after VAR */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11428 | switch (c) { |
| 11429 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11430 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11431 | #if ENABLE_ASH_BASH_COMPAT |
| 11432 | if (c == ':' || c == '$' || isdigit(c)) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11433 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11434 | pungetc(); |
| 11435 | break; /* "goto do_pungetc" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11436 | } |
| 11437 | #endif |
| 11438 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11439 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11440 | default: { |
| 11441 | static const char types[] ALIGN1 = "}-+?="; |
| 11442 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11443 | if (p == NULL) |
| 11444 | goto badsub; |
| 11445 | subtype = p - types + VSNORMAL; |
| 11446 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11447 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11448 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11449 | case '#': { |
| 11450 | int cc = c; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11451 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11452 | c = pgetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11453 | if (c != cc) |
| 11454 | goto do_pungetc; |
| 11455 | subtype++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11456 | break; |
| 11457 | } |
| 11458 | #if ENABLE_ASH_BASH_COMPAT |
| 11459 | case '/': |
| 11460 | subtype = VSREPLACE; |
| 11461 | c = pgetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11462 | if (c != '/') |
| 11463 | goto do_pungetc; |
| 11464 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11465 | break; |
| 11466 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11467 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11468 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11469 | do_pungetc: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11470 | pungetc(); |
| 11471 | } |
| 11472 | if (dblquote || arinest) |
| 11473 | flags |= VSQUOTE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11474 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11475 | if (subtype != VSNORMAL) { |
| 11476 | varnest++; |
| 11477 | if (dblquote || arinest) { |
| 11478 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11479 | } |
| 11480 | } |
| 11481 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11482 | goto parsesub_return; |
| 11483 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11484 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11485 | /* |
| 11486 | * Called to parse command substitutions. Newstyle is set if the command |
| 11487 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11488 | * list of commands (passed by reference), and savelen is the number of |
| 11489 | * characters on the top of the stack which must be preserved. |
| 11490 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11491 | parsebackq: { |
| 11492 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11493 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11494 | union node *n; |
| 11495 | char *volatile str; |
| 11496 | struct jmploc jmploc; |
| 11497 | struct jmploc *volatile savehandler; |
| 11498 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11499 | smallint saveprompt = 0; |
| 11500 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11501 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11502 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11503 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11504 | savepbq = parsebackquote; |
| 11505 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11506 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11507 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11508 | exception_handler = savehandler; |
| 11509 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11510 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11511 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11512 | str = NULL; |
| 11513 | savelen = out - (char *)stackblock(); |
| 11514 | if (savelen > 0) { |
| 11515 | str = ckmalloc(savelen); |
| 11516 | memcpy(str, stackblock(), savelen); |
| 11517 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11518 | savehandler = exception_handler; |
| 11519 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11520 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11521 | if (oldstyle) { |
| 11522 | /* We must read until the closing backquote, giving special |
| 11523 | treatment to some slashes, and then push the string and |
| 11524 | reread it as input, interpreting it normally. */ |
| 11525 | char *pout; |
| 11526 | int pc; |
| 11527 | size_t psavelen; |
| 11528 | char *pstr; |
| 11529 | |
| 11530 | |
| 11531 | STARTSTACKSTR(pout); |
| 11532 | for (;;) { |
| 11533 | if (needprompt) { |
| 11534 | setprompt(2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11535 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11536 | pc = pgetc(); |
| 11537 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11538 | case '`': |
| 11539 | goto done; |
| 11540 | |
| 11541 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11542 | pc = pgetc(); |
| 11543 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11544 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11545 | if (doprompt) |
| 11546 | setprompt(2); |
| 11547 | /* |
| 11548 | * If eating a newline, avoid putting |
| 11549 | * the newline into the new character |
| 11550 | * stream (via the STPUTC after the |
| 11551 | * switch). |
| 11552 | */ |
| 11553 | continue; |
| 11554 | } |
| 11555 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11556 | && (!dblquote || pc != '"') |
| 11557 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11558 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11559 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11560 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11561 | break; |
| 11562 | } |
| 11563 | /* fall through */ |
| 11564 | |
| 11565 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11566 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11567 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11568 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11569 | |
| 11570 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11571 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11572 | needprompt = doprompt; |
| 11573 | break; |
| 11574 | |
| 11575 | default: |
| 11576 | break; |
| 11577 | } |
| 11578 | STPUTC(pc, pout); |
| 11579 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11580 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11581 | STPUTC('\0', pout); |
| 11582 | psavelen = pout - (char *)stackblock(); |
| 11583 | if (psavelen > 0) { |
| 11584 | pstr = grabstackstr(pout); |
| 11585 | setinputstring(pstr); |
| 11586 | } |
| 11587 | } |
| 11588 | nlpp = &bqlist; |
| 11589 | while (*nlpp) |
| 11590 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11591 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11592 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11593 | parsebackquote = oldstyle; |
| 11594 | |
| 11595 | if (oldstyle) { |
| 11596 | saveprompt = doprompt; |
| 11597 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11598 | } |
| 11599 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11600 | n = list(2); |
| 11601 | |
| 11602 | if (oldstyle) |
| 11603 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11604 | else if (readtoken() != TRP) |
| 11605 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11606 | |
| 11607 | (*nlpp)->n = n; |
| 11608 | if (oldstyle) { |
| 11609 | /* |
| 11610 | * Start reading from old file again, ignoring any pushed back |
| 11611 | * tokens left from the backquote parsing |
| 11612 | */ |
| 11613 | popfile(); |
| 11614 | tokpushback = 0; |
| 11615 | } |
| 11616 | while (stackblocksize() <= savelen) |
| 11617 | growstackblock(); |
| 11618 | STARTSTACKSTR(out); |
| 11619 | if (str) { |
| 11620 | memcpy(out, str, savelen); |
| 11621 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11622 | INT_OFF; |
| 11623 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11624 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11625 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11626 | } |
| 11627 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11628 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11629 | if (arinest || dblquote) |
| 11630 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11631 | else |
| 11632 | USTPUTC(CTLBACKQ, out); |
| 11633 | if (oldstyle) |
| 11634 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11635 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11636 | } |
| 11637 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11638 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11639 | /* |
| 11640 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11641 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11642 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11643 | if (++arinest == 1) { |
| 11644 | prevsyntax = syntax; |
| 11645 | syntax = ARISYNTAX; |
| 11646 | USTPUTC(CTLARI, out); |
| 11647 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11648 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11649 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11650 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11651 | } else { |
| 11652 | /* |
| 11653 | * we collapse embedded arithmetic expansion to |
| 11654 | * parenthesis, which should be equivalent |
| 11655 | */ |
| 11656 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11657 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11658 | goto parsearith_return; |
| 11659 | } |
| 11660 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11661 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11662 | } /* end of readtoken */ |
| 11663 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11664 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11665 | * Read the next input token. |
| 11666 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11667 | * backquotes. We set quoteflag to true if any part of the word was |
| 11668 | * quoted. |
| 11669 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11670 | * the redirection. |
| 11671 | * In all cases, the variable startlinno is set to the number of the line |
| 11672 | * on which the token starts. |
| 11673 | * |
| 11674 | * [Change comment: here documents and internal procedures] |
| 11675 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11676 | * word parsing code into a separate routine. In this case, readtoken |
| 11677 | * doesn't need to have any internal procedures, but parseword does. |
| 11678 | * We could also make parseoperator in essence the main routine, and |
| 11679 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11680 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11681 | #define NEW_xxreadtoken |
| 11682 | #ifdef NEW_xxreadtoken |
| 11683 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11684 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11685 | '\n', '(', ')', /* singles */ |
| 11686 | '&', '|', ';', /* doubles */ |
| 11687 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11688 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11689 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11690 | #define xxreadtoken_singles 3 |
| 11691 | #define xxreadtoken_doubles 3 |
| 11692 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11693 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11694 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11695 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11696 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11697 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11698 | }; |
| 11699 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11700 | static int |
| 11701 | xxreadtoken(void) |
| 11702 | { |
| 11703 | int c; |
| 11704 | |
| 11705 | if (tokpushback) { |
| 11706 | tokpushback = 0; |
| 11707 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11708 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11709 | if (needprompt) { |
| 11710 | setprompt(2); |
| 11711 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11712 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11713 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11714 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11715 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11716 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11717 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11718 | if (c == '#') { |
| 11719 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11720 | continue; |
| 11721 | pungetc(); |
| 11722 | } else if (c == '\\') { |
| 11723 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11724 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11725 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11726 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11727 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11728 | if (doprompt) |
| 11729 | setprompt(2); |
| 11730 | } else { |
| 11731 | const char *p; |
| 11732 | |
| 11733 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11734 | if (c != PEOF) { |
| 11735 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11736 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11737 | needprompt = doprompt; |
| 11738 | } |
| 11739 | |
| 11740 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11741 | if (p == NULL) |
| 11742 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11743 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11744 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11745 | int cc = pgetc(); |
| 11746 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11747 | p += xxreadtoken_doubles + 1; |
| 11748 | } else { |
| 11749 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11750 | #if ENABLE_ASH_BASH_COMPAT |
| 11751 | if (c == '&' && cc == '>') /* &> */ |
| 11752 | break; /* return readtoken1(...) */ |
| 11753 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11754 | } |
| 11755 | } |
| 11756 | } |
| 11757 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11758 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11759 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11760 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11761 | |
| 11762 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11763 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11764 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11765 | #define RETURN(token) return lasttoken = token |
| 11766 | static int |
| 11767 | xxreadtoken(void) |
| 11768 | { |
| 11769 | int c; |
| 11770 | |
| 11771 | if (tokpushback) { |
| 11772 | tokpushback = 0; |
| 11773 | return lasttoken; |
| 11774 | } |
| 11775 | if (needprompt) { |
| 11776 | setprompt(2); |
| 11777 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11778 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11779 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11780 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11781 | switch (c) { |
| 11782 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11783 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11784 | continue; |
| 11785 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11786 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11787 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11788 | pungetc(); |
| 11789 | continue; |
| 11790 | case '\\': |
| 11791 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11792 | startlinno = ++g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11793 | if (doprompt) |
| 11794 | setprompt(2); |
| 11795 | continue; |
| 11796 | } |
| 11797 | pungetc(); |
| 11798 | goto breakloop; |
| 11799 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11800 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11801 | needprompt = doprompt; |
| 11802 | RETURN(TNL); |
| 11803 | case PEOF: |
| 11804 | RETURN(TEOF); |
| 11805 | case '&': |
| 11806 | if (pgetc() == '&') |
| 11807 | RETURN(TAND); |
| 11808 | pungetc(); |
| 11809 | RETURN(TBACKGND); |
| 11810 | case '|': |
| 11811 | if (pgetc() == '|') |
| 11812 | RETURN(TOR); |
| 11813 | pungetc(); |
| 11814 | RETURN(TPIPE); |
| 11815 | case ';': |
| 11816 | if (pgetc() == ';') |
| 11817 | RETURN(TENDCASE); |
| 11818 | pungetc(); |
| 11819 | RETURN(TSEMI); |
| 11820 | case '(': |
| 11821 | RETURN(TLP); |
| 11822 | case ')': |
| 11823 | RETURN(TRP); |
| 11824 | default: |
| 11825 | goto breakloop; |
| 11826 | } |
| 11827 | } |
| 11828 | breakloop: |
| 11829 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11830 | #undef RETURN |
| 11831 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11832 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11833 | |
| 11834 | static int |
| 11835 | readtoken(void) |
| 11836 | { |
| 11837 | int t; |
| 11838 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11839 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11840 | #endif |
| 11841 | |
| 11842 | #if ENABLE_ASH_ALIAS |
| 11843 | top: |
| 11844 | #endif |
| 11845 | |
| 11846 | t = xxreadtoken(); |
| 11847 | |
| 11848 | /* |
| 11849 | * eat newlines |
| 11850 | */ |
| 11851 | if (checkkwd & CHKNL) { |
| 11852 | while (t == TNL) { |
| 11853 | parseheredoc(); |
| 11854 | t = xxreadtoken(); |
| 11855 | } |
| 11856 | } |
| 11857 | |
| 11858 | if (t != TWORD || quoteflag) { |
| 11859 | goto out; |
| 11860 | } |
| 11861 | |
| 11862 | /* |
| 11863 | * check for keywords |
| 11864 | */ |
| 11865 | if (checkkwd & CHKKWD) { |
| 11866 | const char *const *pp; |
| 11867 | |
| 11868 | pp = findkwd(wordtext); |
| 11869 | if (pp) { |
| 11870 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11871 | TRACE(("keyword '%s' recognized\n", tokname_array[t] + 1)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11872 | goto out; |
| 11873 | } |
| 11874 | } |
| 11875 | |
| 11876 | if (checkkwd & CHKALIAS) { |
| 11877 | #if ENABLE_ASH_ALIAS |
| 11878 | struct alias *ap; |
| 11879 | ap = lookupalias(wordtext, 1); |
| 11880 | if (ap != NULL) { |
| 11881 | if (*ap->val) { |
| 11882 | pushstring(ap->val, ap); |
| 11883 | } |
| 11884 | goto top; |
| 11885 | } |
| 11886 | #endif |
| 11887 | } |
| 11888 | out: |
| 11889 | checkkwd = 0; |
| 11890 | #if DEBUG |
| 11891 | if (!alreadyseen) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11892 | TRACE(("token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11893 | else |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11894 | 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] | 11895 | #endif |
| 11896 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11897 | } |
| 11898 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11899 | static char |
| 11900 | peektoken(void) |
| 11901 | { |
| 11902 | int t; |
| 11903 | |
| 11904 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11905 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11906 | return tokname_array[t][0]; |
| 11907 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11908 | |
| 11909 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11910 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 11911 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11912 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11913 | static union node * |
| 11914 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11915 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11916 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11917 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11918 | tokpushback = 0; |
| 11919 | doprompt = interact; |
| 11920 | if (doprompt) |
| 11921 | setprompt(doprompt); |
| 11922 | needprompt = 0; |
| 11923 | t = readtoken(); |
| 11924 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11925 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11926 | if (t == TNL) |
| 11927 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11928 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11929 | return list(1); |
| 11930 | } |
| 11931 | |
| 11932 | /* |
| 11933 | * Input any here documents. |
| 11934 | */ |
| 11935 | static void |
| 11936 | parseheredoc(void) |
| 11937 | { |
| 11938 | struct heredoc *here; |
| 11939 | union node *n; |
| 11940 | |
| 11941 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11942 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11943 | |
| 11944 | while (here) { |
| 11945 | if (needprompt) { |
| 11946 | setprompt(2); |
| 11947 | } |
| 11948 | readtoken1(pgetc(), here->here->type == NHERE? SQSYNTAX : DQSYNTAX, |
| 11949 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11950 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11951 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11952 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11953 | n->narg.text = wordtext; |
| 11954 | n->narg.backquote = backquotelist; |
| 11955 | here->here->nhere.doc = n; |
| 11956 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11957 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11958 | } |
| 11959 | |
| 11960 | |
| 11961 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11962 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11963 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11964 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11965 | static const char * |
| 11966 | expandstr(const char *ps) |
| 11967 | { |
| 11968 | union node n; |
| 11969 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11970 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11971 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11972 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11973 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11974 | popfile(); |
| 11975 | |
| 11976 | n.narg.type = NARG; |
| 11977 | n.narg.next = NULL; |
| 11978 | n.narg.text = wordtext; |
| 11979 | n.narg.backquote = backquotelist; |
| 11980 | |
| 11981 | expandarg(&n, NULL, 0); |
| 11982 | return stackblock(); |
| 11983 | } |
| 11984 | #endif |
| 11985 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11986 | /* |
| 11987 | * Execute a command or commands contained in a string. |
| 11988 | */ |
| 11989 | static int |
| 11990 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11991 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11992 | union node *n; |
| 11993 | struct stackmark smark; |
| 11994 | int skip; |
| 11995 | |
| 11996 | setinputstring(s); |
| 11997 | setstackmark(&smark); |
| 11998 | |
| 11999 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12000 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12001 | evaltree(n, 0); |
| 12002 | popstackmark(&smark); |
| 12003 | skip = evalskip; |
| 12004 | if (skip) |
| 12005 | break; |
| 12006 | } |
| 12007 | popfile(); |
| 12008 | |
| 12009 | skip &= mask; |
| 12010 | evalskip = skip; |
| 12011 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12012 | } |
| 12013 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12014 | /* |
| 12015 | * The eval command. |
| 12016 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12017 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12018 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12019 | { |
| 12020 | char *p; |
| 12021 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12022 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12023 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12024 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12025 | argv += 2; |
| 12026 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12027 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12028 | for (;;) { |
| 12029 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12030 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12031 | if (p == NULL) |
| 12032 | break; |
| 12033 | STPUTC(' ', concat); |
| 12034 | } |
| 12035 | STPUTC('\0', concat); |
| 12036 | p = grabstackstr(concat); |
| 12037 | } |
| 12038 | evalstring(p, ~SKIPEVAL); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12039 | } |
| 12040 | return exitstatus; |
| 12041 | } |
| 12042 | |
| 12043 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 12044 | * Read and execute commands. |
| 12045 | * "Top" is nonzero for the top level command loop; |
| 12046 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12047 | */ |
| 12048 | static int |
| 12049 | cmdloop(int top) |
| 12050 | { |
| 12051 | union node *n; |
| 12052 | struct stackmark smark; |
| 12053 | int inter; |
| 12054 | int numeof = 0; |
| 12055 | |
| 12056 | TRACE(("cmdloop(%d) called\n", top)); |
| 12057 | for (;;) { |
| 12058 | int skip; |
| 12059 | |
| 12060 | setstackmark(&smark); |
| 12061 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 12062 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12063 | showjobs(stderr, SHOW_CHANGED); |
| 12064 | #endif |
| 12065 | inter = 0; |
| 12066 | if (iflag && top) { |
| 12067 | inter++; |
| 12068 | #if ENABLE_ASH_MAIL |
| 12069 | chkmail(); |
| 12070 | #endif |
| 12071 | } |
| 12072 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 12073 | #if DEBUG |
| 12074 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 12075 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 12076 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12077 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12078 | if (!top || numeof >= 50) |
| 12079 | break; |
| 12080 | if (!stoppedjobs()) { |
| 12081 | if (!Iflag) |
| 12082 | break; |
| 12083 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 12084 | } |
| 12085 | numeof++; |
| 12086 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 12087 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 12088 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12089 | numeof = 0; |
| 12090 | evaltree(n, 0); |
| 12091 | } |
| 12092 | popstackmark(&smark); |
| 12093 | skip = evalskip; |
| 12094 | |
| 12095 | if (skip) { |
| 12096 | evalskip = 0; |
| 12097 | return skip & SKIPEVAL; |
| 12098 | } |
| 12099 | } |
| 12100 | return 0; |
| 12101 | } |
| 12102 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12103 | /* |
| 12104 | * Take commands from a file. To be compatible we should do a path |
| 12105 | * search for the file, which is necessary to find sub-commands. |
| 12106 | */ |
| 12107 | static char * |
| 12108 | find_dot_file(char *name) |
| 12109 | { |
| 12110 | char *fullname; |
| 12111 | const char *path = pathval(); |
| 12112 | struct stat statb; |
| 12113 | |
| 12114 | /* don't try this for absolute or relative paths */ |
| 12115 | if (strchr(name, '/')) |
| 12116 | return name; |
| 12117 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12118 | /* IIRC standards do not say whether . is to be searched. |
| 12119 | * And it is even smaller this way, making it unconditional for now: |
| 12120 | */ |
| 12121 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12122 | fullname = name; |
| 12123 | goto try_cur_dir; |
| 12124 | } |
| 12125 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12126 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12127 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12128 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12129 | /* |
| 12130 | * Don't bother freeing here, since it will |
| 12131 | * be freed by the caller. |
| 12132 | */ |
| 12133 | return fullname; |
| 12134 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12135 | if (fullname != name) |
| 12136 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12137 | } |
| 12138 | |
| 12139 | /* not found in the PATH */ |
| 12140 | ash_msg_and_raise_error("%s: not found", name); |
| 12141 | /* NOTREACHED */ |
| 12142 | } |
| 12143 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12144 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12145 | dotcmd(int argc, char **argv) |
| 12146 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12147 | char *fullname; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12148 | struct strlist *sp; |
| 12149 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12150 | |
| 12151 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12152 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12153 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12154 | if (!argv[1]) { |
| 12155 | /* bash says: "bash: .: filename argument required" */ |
| 12156 | return 2; /* bash compat */ |
| 12157 | } |
| 12158 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12159 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 12160 | exitstatus = 0; |
| 12161 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12162 | fullname = find_dot_file(argv[1]); |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12163 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12164 | argv += 2; |
| 12165 | argc -= 2; |
| 12166 | if (argc) { /* argc > 0, argv[0] != NULL */ |
| 12167 | saveparam = shellparam; |
| 12168 | shellparam.malloced = 0; |
| 12169 | shellparam.nparam = argc; |
| 12170 | shellparam.p = argv; |
| 12171 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12172 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12173 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12174 | commandname = fullname; |
| 12175 | cmdloop(0); |
| 12176 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12177 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12178 | if (argc) { |
| 12179 | freeparam(&shellparam); |
| 12180 | shellparam = saveparam; |
| 12181 | }; |
| 12182 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12183 | return exitstatus; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12184 | } |
| 12185 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12186 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12187 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12188 | { |
| 12189 | if (stoppedjobs()) |
| 12190 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12191 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12192 | exitstatus = number(argv[1]); |
| 12193 | raise_exception(EXEXIT); |
| 12194 | /* NOTREACHED */ |
| 12195 | } |
| 12196 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12197 | /* |
| 12198 | * Read a file containing shell functions. |
| 12199 | */ |
| 12200 | static void |
| 12201 | readcmdfile(char *name) |
| 12202 | { |
| 12203 | setinputfile(name, INPUT_PUSH_FILE); |
| 12204 | cmdloop(0); |
| 12205 | popfile(); |
| 12206 | } |
| 12207 | |
| 12208 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12209 | /* ============ find_command inplementation */ |
| 12210 | |
| 12211 | /* |
| 12212 | * Resolve a command name. If you change this routine, you may have to |
| 12213 | * change the shellexec routine as well. |
| 12214 | */ |
| 12215 | static void |
| 12216 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12217 | { |
| 12218 | struct tblentry *cmdp; |
| 12219 | int idx; |
| 12220 | int prev; |
| 12221 | char *fullname; |
| 12222 | struct stat statb; |
| 12223 | int e; |
| 12224 | int updatetbl; |
| 12225 | struct builtincmd *bcmd; |
| 12226 | |
| 12227 | /* If name contains a slash, don't use PATH or hash table */ |
| 12228 | if (strchr(name, '/') != NULL) { |
| 12229 | entry->u.index = -1; |
| 12230 | if (act & DO_ABS) { |
| 12231 | while (stat(name, &statb) < 0) { |
| 12232 | #ifdef SYSV |
| 12233 | if (errno == EINTR) |
| 12234 | continue; |
| 12235 | #endif |
| 12236 | entry->cmdtype = CMDUNKNOWN; |
| 12237 | return; |
| 12238 | } |
| 12239 | } |
| 12240 | entry->cmdtype = CMDNORMAL; |
| 12241 | return; |
| 12242 | } |
| 12243 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12244 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12245 | |
| 12246 | updatetbl = (path == pathval()); |
| 12247 | if (!updatetbl) { |
| 12248 | act |= DO_ALTPATH; |
| 12249 | if (strstr(path, "%builtin") != NULL) |
| 12250 | act |= DO_ALTBLTIN; |
| 12251 | } |
| 12252 | |
| 12253 | /* If name is in the table, check answer will be ok */ |
| 12254 | cmdp = cmdlookup(name, 0); |
| 12255 | if (cmdp != NULL) { |
| 12256 | int bit; |
| 12257 | |
| 12258 | switch (cmdp->cmdtype) { |
| 12259 | default: |
| 12260 | #if DEBUG |
| 12261 | abort(); |
| 12262 | #endif |
| 12263 | case CMDNORMAL: |
| 12264 | bit = DO_ALTPATH; |
| 12265 | break; |
| 12266 | case CMDFUNCTION: |
| 12267 | bit = DO_NOFUNC; |
| 12268 | break; |
| 12269 | case CMDBUILTIN: |
| 12270 | bit = DO_ALTBLTIN; |
| 12271 | break; |
| 12272 | } |
| 12273 | if (act & bit) { |
| 12274 | updatetbl = 0; |
| 12275 | cmdp = NULL; |
| 12276 | } else if (cmdp->rehash == 0) |
| 12277 | /* if not invalidated by cd, we're done */ |
| 12278 | goto success; |
| 12279 | } |
| 12280 | |
| 12281 | /* If %builtin not in path, check for builtin next */ |
| 12282 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12283 | if (bcmd) { |
| 12284 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12285 | goto builtin_success; |
| 12286 | if (act & DO_ALTPATH) { |
| 12287 | if (!(act & DO_ALTBLTIN)) |
| 12288 | goto builtin_success; |
| 12289 | } else if (builtinloc <= 0) { |
| 12290 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12291 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12292 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12293 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12294 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12295 | { |
| 12296 | int applet_no = find_applet_by_name(name); |
| 12297 | if (applet_no >= 0) { |
| 12298 | entry->cmdtype = CMDNORMAL; |
| 12299 | entry->u.index = -2 - applet_no; |
| 12300 | return; |
| 12301 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12302 | } |
| 12303 | #endif |
| 12304 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12305 | /* We have to search path. */ |
| 12306 | prev = -1; /* where to start */ |
| 12307 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12308 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12309 | prev = builtinloc; |
| 12310 | else |
| 12311 | prev = cmdp->param.index; |
| 12312 | } |
| 12313 | |
| 12314 | e = ENOENT; |
| 12315 | idx = -1; |
| 12316 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12317 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12318 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12319 | /* NB: code below will still use fullname |
| 12320 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12321 | idx++; |
| 12322 | if (pathopt) { |
| 12323 | if (prefix(pathopt, "builtin")) { |
| 12324 | if (bcmd) |
| 12325 | goto builtin_success; |
| 12326 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12327 | } |
| 12328 | if ((act & DO_NOFUNC) |
| 12329 | || !prefix(pathopt, "func") |
| 12330 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12331 | continue; |
| 12332 | } |
| 12333 | } |
| 12334 | /* if rehash, don't redo absolute path names */ |
| 12335 | if (fullname[0] == '/' && idx <= prev) { |
| 12336 | if (idx < prev) |
| 12337 | continue; |
| 12338 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12339 | goto success; |
| 12340 | } |
| 12341 | while (stat(fullname, &statb) < 0) { |
| 12342 | #ifdef SYSV |
| 12343 | if (errno == EINTR) |
| 12344 | continue; |
| 12345 | #endif |
| 12346 | if (errno != ENOENT && errno != ENOTDIR) |
| 12347 | e = errno; |
| 12348 | goto loop; |
| 12349 | } |
| 12350 | e = EACCES; /* if we fail, this will be the error */ |
| 12351 | if (!S_ISREG(statb.st_mode)) |
| 12352 | continue; |
| 12353 | if (pathopt) { /* this is a %func directory */ |
| 12354 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12355 | /* NB: stalloc will return space pointed by fullname |
| 12356 | * (because we don't have any intervening allocations |
| 12357 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12358 | readcmdfile(fullname); |
| 12359 | cmdp = cmdlookup(name, 0); |
| 12360 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12361 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12362 | stunalloc(fullname); |
| 12363 | goto success; |
| 12364 | } |
| 12365 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12366 | if (!updatetbl) { |
| 12367 | entry->cmdtype = CMDNORMAL; |
| 12368 | entry->u.index = idx; |
| 12369 | return; |
| 12370 | } |
| 12371 | INT_OFF; |
| 12372 | cmdp = cmdlookup(name, 1); |
| 12373 | cmdp->cmdtype = CMDNORMAL; |
| 12374 | cmdp->param.index = idx; |
| 12375 | INT_ON; |
| 12376 | goto success; |
| 12377 | } |
| 12378 | |
| 12379 | /* We failed. If there was an entry for this command, delete it */ |
| 12380 | if (cmdp && updatetbl) |
| 12381 | delete_cmd_entry(); |
| 12382 | if (act & DO_ERR) |
| 12383 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12384 | entry->cmdtype = CMDUNKNOWN; |
| 12385 | return; |
| 12386 | |
| 12387 | builtin_success: |
| 12388 | if (!updatetbl) { |
| 12389 | entry->cmdtype = CMDBUILTIN; |
| 12390 | entry->u.cmd = bcmd; |
| 12391 | return; |
| 12392 | } |
| 12393 | INT_OFF; |
| 12394 | cmdp = cmdlookup(name, 1); |
| 12395 | cmdp->cmdtype = CMDBUILTIN; |
| 12396 | cmdp->param.cmd = bcmd; |
| 12397 | INT_ON; |
| 12398 | success: |
| 12399 | cmdp->rehash = 0; |
| 12400 | entry->cmdtype = cmdp->cmdtype; |
| 12401 | entry->u = cmdp->param; |
| 12402 | } |
| 12403 | |
| 12404 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12405 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12406 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12407 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12408 | * The trap builtin. |
| 12409 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12410 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12411 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12412 | { |
| 12413 | char *action; |
| 12414 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12415 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12416 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12417 | nextopt(nullstr); |
| 12418 | ap = argptr; |
| 12419 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12420 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12421 | char *tr = trap_ptr[signo]; |
| 12422 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12423 | /* note: bash adds "SIG", but only if invoked |
| 12424 | * as "bash". If called as "sh", or if set -o posix, |
| 12425 | * then it prints short signal names. |
| 12426 | * We are printing short names: */ |
| 12427 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12428 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12429 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12430 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12431 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12432 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12433 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12434 | } |
| 12435 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12436 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12437 | if (trap_ptr != trap) { |
| 12438 | free(trap_ptr); |
| 12439 | trap_ptr = trap; |
| 12440 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12441 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12442 | return 0; |
| 12443 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12444 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12445 | action = NULL; |
| 12446 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12447 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12448 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12449 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12450 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12451 | if (signo < 0) { |
| 12452 | /* Mimic bash message exactly */ |
| 12453 | ash_msg("%s: invalid signal specification", *ap); |
| 12454 | exitcode = 1; |
| 12455 | goto next; |
| 12456 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12457 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12458 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12459 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12460 | action = NULL; |
| 12461 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12462 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12463 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12464 | free(trap[signo]); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 12465 | if (action) |
| 12466 | may_have_traps = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12467 | trap[signo] = action; |
| 12468 | if (signo != 0) |
| 12469 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12470 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12471 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12472 | ap++; |
| 12473 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12474 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12475 | } |
| 12476 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12477 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12478 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12479 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12480 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12481 | /* |
| 12482 | * Lists available builtins |
| 12483 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12484 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12485 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12486 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12487 | unsigned col; |
| 12488 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12489 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12490 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12491 | "Built-in commands:\n" |
| 12492 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12493 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12494 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12495 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12496 | if (col > 60) { |
| 12497 | out1fmt("\n"); |
| 12498 | col = 0; |
| 12499 | } |
| 12500 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12501 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12502 | { |
| 12503 | const char *a = applet_names; |
| 12504 | while (*a) { |
| 12505 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12506 | if (col > 60) { |
| 12507 | out1fmt("\n"); |
| 12508 | col = 0; |
| 12509 | } |
| 12510 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12511 | } |
| 12512 | } |
| 12513 | #endif |
| 12514 | out1fmt("\n\n"); |
| 12515 | return EXIT_SUCCESS; |
| 12516 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12517 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12518 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12519 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12520 | * The export and readonly commands. |
| 12521 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12522 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12523 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12524 | { |
| 12525 | struct var *vp; |
| 12526 | char *name; |
| 12527 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12528 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12529 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12530 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12531 | if (nextopt("p") != 'p') { |
| 12532 | aptr = argptr; |
| 12533 | name = *aptr; |
| 12534 | if (name) { |
| 12535 | do { |
| 12536 | p = strchr(name, '='); |
| 12537 | if (p != NULL) { |
| 12538 | p++; |
| 12539 | } else { |
| 12540 | vp = *findvar(hashvar(name), name); |
| 12541 | if (vp) { |
| 12542 | vp->flags |= flag; |
| 12543 | continue; |
| 12544 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12545 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12546 | setvar(name, p, flag); |
| 12547 | } while ((name = *++aptr) != NULL); |
| 12548 | return 0; |
| 12549 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12550 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12551 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12552 | return 0; |
| 12553 | } |
| 12554 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12555 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12556 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12557 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12558 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12559 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12560 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12561 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12562 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12563 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12564 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12565 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12566 | } |
| 12567 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12568 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12569 | * The unset builtin command. We unset the function before we unset the |
| 12570 | * variable to allow a function to be unset when there is a readonly variable |
| 12571 | * with the same name. |
| 12572 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12573 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12574 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12575 | { |
| 12576 | char **ap; |
| 12577 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12578 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12579 | int ret = 0; |
| 12580 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12581 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12582 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12583 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12584 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12585 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12586 | if (flag != 'f') { |
| 12587 | i = unsetvar(*ap); |
| 12588 | ret |= i; |
| 12589 | if (!(i & 2)) |
| 12590 | continue; |
| 12591 | } |
| 12592 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12593 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12594 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12595 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12596 | } |
| 12597 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12598 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12599 | ' ', offsetof(struct tms, tms_utime), |
| 12600 | '\n', offsetof(struct tms, tms_stime), |
| 12601 | ' ', offsetof(struct tms, tms_cutime), |
| 12602 | '\n', offsetof(struct tms, tms_cstime), |
| 12603 | 0 |
| 12604 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12605 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12606 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12607 | { |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12608 | unsigned long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12609 | const unsigned char *p; |
| 12610 | struct tms buf; |
| 12611 | |
| 12612 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12613 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12614 | |
| 12615 | p = timescmd_str; |
| 12616 | do { |
| 12617 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12618 | s = t / clk_tck; |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12619 | t = t % clk_tck; |
| 12620 | out1fmt("%lum%lu.%03lus%c", |
| 12621 | s / 60, s % 60, |
| 12622 | (t * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12623 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12624 | p += 2; |
| 12625 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12626 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12627 | return 0; |
| 12628 | } |
| 12629 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12630 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12631 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12632 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12633 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12634 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12635 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12636 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12637 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12638 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12639 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12640 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12641 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12642 | argv++; |
| 12643 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12644 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12645 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12646 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12647 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12648 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12649 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12650 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12651 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12652 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12653 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12654 | * The read builtin. Options: |
| 12655 | * -r Do not interpret '\' specially |
| 12656 | * -s Turn off echo (tty only) |
| 12657 | * -n NCHARS Read NCHARS max |
| 12658 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12659 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12660 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12661 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12662 | * TODO: bash also has: |
| 12663 | * -a ARRAY Read into array[0],[1],etc |
| 12664 | * -d DELIM End on DELIM char, not newline |
| 12665 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12666 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12667 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12668 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12669 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12670 | char *opt_n = NULL; |
| 12671 | char *opt_p = NULL; |
| 12672 | char *opt_t = NULL; |
| 12673 | char *opt_u = NULL; |
| 12674 | int read_flags = 0; |
| 12675 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12676 | int i; |
| 12677 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12678 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12679 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12680 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12681 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12682 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12683 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12684 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12685 | break; |
| 12686 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12687 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12688 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12689 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12690 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12691 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12692 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12693 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12694 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12695 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12696 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12697 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12698 | default: |
| 12699 | break; |
| 12700 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12701 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12702 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 12703 | r = shell_builtin_read(setvar2, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12704 | argptr, |
| 12705 | bltinlookup("IFS"), /* can be NULL */ |
| 12706 | read_flags, |
| 12707 | opt_n, |
| 12708 | opt_p, |
| 12709 | opt_t, |
| 12710 | opt_u |
| 12711 | ); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12712 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12713 | if ((uintptr_t)r > 1) |
| 12714 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12715 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12716 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12717 | } |
| 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 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12721 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12722 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12723 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12724 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12725 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12726 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12727 | S_IROTH, S_IWOTH, S_IXOTH |
| 12728 | }; |
| 12729 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12730 | /* TODO: use bb_parse_mode() instead */ |
| 12731 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12732 | char *ap; |
| 12733 | mode_t mask; |
| 12734 | int i; |
| 12735 | int symbolic_mode = 0; |
| 12736 | |
| 12737 | while (nextopt("S") != '\0') { |
| 12738 | symbolic_mode = 1; |
| 12739 | } |
| 12740 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12741 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12742 | mask = umask(0); |
| 12743 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12744 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12745 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12746 | ap = *argptr; |
| 12747 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12748 | if (symbolic_mode) { |
| 12749 | char buf[18]; |
| 12750 | char *p = buf; |
| 12751 | |
| 12752 | for (i = 0; i < 3; i++) { |
| 12753 | int j; |
| 12754 | |
| 12755 | *p++ = permuser[i]; |
| 12756 | *p++ = '='; |
| 12757 | for (j = 0; j < 3; j++) { |
| 12758 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12759 | *p++ = permmode[j]; |
| 12760 | } |
| 12761 | } |
| 12762 | *p++ = ','; |
| 12763 | } |
| 12764 | *--p = 0; |
| 12765 | puts(buf); |
| 12766 | } else { |
| 12767 | out1fmt("%.4o\n", mask); |
| 12768 | } |
| 12769 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12770 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12771 | mask = 0; |
| 12772 | do { |
| 12773 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12774 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12775 | mask = (mask << 3) + (*ap - '0'); |
| 12776 | } while (*++ap != '\0'); |
| 12777 | umask(mask); |
| 12778 | } else { |
| 12779 | mask = ~mask & 0777; |
| 12780 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12781 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12782 | } |
| 12783 | umask(~mask & 0777); |
| 12784 | } |
| 12785 | } |
| 12786 | return 0; |
| 12787 | } |
| 12788 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12789 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12790 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12791 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12792 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12793 | } |
| 12794 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12795 | /* ============ main() and helpers */ |
| 12796 | |
| 12797 | /* |
| 12798 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12799 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12800 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12801 | static void |
| 12802 | exitshell(void) |
| 12803 | { |
| 12804 | struct jmploc loc; |
| 12805 | char *p; |
| 12806 | int status; |
| 12807 | |
| 12808 | status = exitstatus; |
| 12809 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12810 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12811 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12812 | /* dash bug: it just does _exit(exitstatus) here |
| 12813 | * but we have to do setjobctl(0) first! |
| 12814 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12815 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12816 | status = exitstatus; |
| 12817 | goto out; |
| 12818 | } |
| 12819 | exception_handler = &loc; |
| 12820 | p = trap[0]; |
| 12821 | if (p) { |
| 12822 | trap[0] = NULL; |
| 12823 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 12824 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12825 | } |
| 12826 | flush_stdout_stderr(); |
| 12827 | out: |
| 12828 | setjobctl(0); |
| 12829 | _exit(status); |
| 12830 | /* NOTREACHED */ |
| 12831 | } |
| 12832 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12833 | static void |
| 12834 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12835 | { |
| 12836 | /* from input.c: */ |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 12837 | /* we will never free this */ |
| 12838 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12839 | |
| 12840 | /* from trap.c: */ |
| 12841 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 12842 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 12843 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 12844 | */ |
| 12845 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12846 | |
| 12847 | /* from var.c: */ |
| 12848 | { |
| 12849 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12850 | const char *p; |
| 12851 | struct stat st1, st2; |
| 12852 | |
| 12853 | initvar(); |
| 12854 | for (envp = environ; envp && *envp; envp++) { |
| 12855 | if (strchr(*envp, '=')) { |
| 12856 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 12857 | } |
| 12858 | } |
| 12859 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 12860 | setvar("PPID", utoa(getppid()), 0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12861 | |
| 12862 | p = lookupvar("PWD"); |
| 12863 | if (p) |
| 12864 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 12865 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 12866 | p = '\0'; |
| 12867 | setpwd(p, 0); |
| 12868 | } |
| 12869 | } |
| 12870 | |
| 12871 | /* |
| 12872 | * Process the shell command line arguments. |
| 12873 | */ |
| 12874 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12875 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12876 | { |
| 12877 | int i; |
| 12878 | const char *xminusc; |
| 12879 | char **xargv; |
| 12880 | |
| 12881 | xargv = argv; |
| 12882 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12883 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12884 | xargv++; |
| 12885 | for (i = 0; i < NOPTS; i++) |
| 12886 | optlist[i] = 2; |
| 12887 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 12888 | if (options(1)) { |
| 12889 | /* it already printed err message */ |
| 12890 | raise_exception(EXERROR); |
| 12891 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12892 | xargv = argptr; |
| 12893 | xminusc = minusc; |
| 12894 | if (*xargv == NULL) { |
| 12895 | if (xminusc) |
| 12896 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 12897 | sflag = 1; |
| 12898 | } |
| 12899 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 12900 | iflag = 1; |
| 12901 | if (mflag == 2) |
| 12902 | mflag = iflag; |
| 12903 | for (i = 0; i < NOPTS; i++) |
| 12904 | if (optlist[i] == 2) |
| 12905 | optlist[i] = 0; |
| 12906 | #if DEBUG == 2 |
| 12907 | debug = 1; |
| 12908 | #endif |
| 12909 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 12910 | if (xminusc) { |
| 12911 | minusc = *xargv++; |
| 12912 | if (*xargv) |
| 12913 | goto setarg0; |
| 12914 | } else if (!sflag) { |
| 12915 | setinputfile(*xargv, 0); |
| 12916 | setarg0: |
| 12917 | arg0 = *xargv++; |
| 12918 | commandname = arg0; |
| 12919 | } |
| 12920 | |
| 12921 | shellparam.p = xargv; |
| 12922 | #if ENABLE_ASH_GETOPTS |
| 12923 | shellparam.optind = 1; |
| 12924 | shellparam.optoff = -1; |
| 12925 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12926 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12927 | while (*xargv) { |
| 12928 | shellparam.nparam++; |
| 12929 | xargv++; |
| 12930 | } |
| 12931 | optschanged(); |
| 12932 | } |
| 12933 | |
| 12934 | /* |
| 12935 | * Read /etc/profile or .profile. |
| 12936 | */ |
| 12937 | static void |
| 12938 | read_profile(const char *name) |
| 12939 | { |
| 12940 | int skip; |
| 12941 | |
| 12942 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 12943 | return; |
| 12944 | skip = cmdloop(0); |
| 12945 | popfile(); |
| 12946 | if (skip) |
| 12947 | exitshell(); |
| 12948 | } |
| 12949 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12950 | /* |
| 12951 | * This routine is called when an error or an interrupt occurs in an |
| 12952 | * interactive shell and control is returned to the main command loop. |
| 12953 | */ |
| 12954 | static void |
| 12955 | reset(void) |
| 12956 | { |
| 12957 | /* from eval.c: */ |
| 12958 | evalskip = 0; |
| 12959 | loopnest = 0; |
| 12960 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12961 | g_parsefile->left_in_buffer = 0; |
| 12962 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12963 | popallfiles(); |
| 12964 | /* from parser.c: */ |
| 12965 | tokpushback = 0; |
| 12966 | checkkwd = 0; |
| 12967 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 12968 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12969 | } |
| 12970 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12971 | #if PROFILE |
| 12972 | static short profile_buf[16384]; |
| 12973 | extern int etext(); |
| 12974 | #endif |
| 12975 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12976 | /* |
| 12977 | * Main routine. We initialize things, parse the arguments, execute |
| 12978 | * profiles if we're a login shell, and then call cmdloop to execute |
| 12979 | * commands. The setjmp call sets up the location to jump to when an |
| 12980 | * exception occurs. When an exception occurs the variable "state" |
| 12981 | * is used to figure out how far we had gotten. |
| 12982 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 12983 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12984 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12985 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12986 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12987 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12988 | struct jmploc jmploc; |
| 12989 | struct stackmark smark; |
| 12990 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12991 | /* Initialize global data */ |
| 12992 | INIT_G_misc(); |
| 12993 | INIT_G_memstack(); |
| 12994 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12995 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12996 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12997 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12998 | INIT_G_cmdtable(); |
| 12999 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13000 | #if PROFILE |
| 13001 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13002 | #endif |
| 13003 | |
| 13004 | #if ENABLE_FEATURE_EDITING |
| 13005 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13006 | #endif |
| 13007 | state = 0; |
| 13008 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13009 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13010 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13011 | |
| 13012 | reset(); |
| 13013 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13014 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13015 | if (e == EXERROR) |
| 13016 | exitstatus = 2; |
| 13017 | s = state; |
| 13018 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 13019 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13020 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13021 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13022 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13023 | popstackmark(&smark); |
| 13024 | FORCE_INT_ON; /* enable interrupts */ |
| 13025 | if (s == 1) |
| 13026 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13027 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13028 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13029 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13030 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13031 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13032 | } |
| 13033 | exception_handler = &jmploc; |
| 13034 | #if DEBUG |
| 13035 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 13036 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13037 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13038 | #endif |
| 13039 | rootpid = getpid(); |
| 13040 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13041 | init(); |
| 13042 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13043 | procargs(argv); |
| 13044 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13045 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13046 | if (iflag) { |
| 13047 | const char *hp = lookupvar("HISTFILE"); |
| 13048 | |
| 13049 | if (hp == NULL) { |
| 13050 | hp = lookupvar("HOME"); |
| 13051 | if (hp != NULL) { |
| 13052 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 13053 | setvar("HISTFILE", defhp, 0); |
| 13054 | free(defhp); |
| 13055 | } |
| 13056 | } |
| 13057 | } |
| 13058 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13059 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13060 | isloginsh = 1; |
| 13061 | if (isloginsh) { |
| 13062 | state = 1; |
| 13063 | read_profile("/etc/profile"); |
| 13064 | state1: |
| 13065 | state = 2; |
| 13066 | read_profile(".profile"); |
| 13067 | } |
| 13068 | state2: |
| 13069 | state = 3; |
| 13070 | if ( |
| 13071 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13072 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13073 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13074 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13075 | ) { |
| 13076 | shinit = lookupvar("ENV"); |
| 13077 | if (shinit != NULL && *shinit != '\0') { |
| 13078 | read_profile(shinit); |
| 13079 | } |
| 13080 | } |
| 13081 | state3: |
| 13082 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13083 | if (minusc) { |
| 13084 | /* evalstring pushes parsefile stack. |
| 13085 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13086 | * is one of stacked source fds. |
| 13087 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 13088 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 13089 | // ^^ not necessary since now we special-case fd 0 |
| 13090 | // in is_hidden_fd() to not be considered "hidden fd" |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13091 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13092 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13093 | |
| 13094 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 0337e03 | 2009-11-29 00:12:30 +0100 | [diff] [blame] | 13095 | #if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13096 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13097 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13098 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13099 | line_input_state->hist_file = hp; |
| 13100 | } |
| 13101 | #endif |
| 13102 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13103 | cmdloop(1); |
| 13104 | } |
| 13105 | #if PROFILE |
| 13106 | monitor(0); |
| 13107 | #endif |
| 13108 | #ifdef GPROF |
| 13109 | { |
| 13110 | extern void _mcleanup(void); |
| 13111 | _mcleanup(); |
| 13112 | } |
| 13113 | #endif |
| 13114 | exitshell(); |
| 13115 | /* NOTREACHED */ |
| 13116 | } |
| 13117 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13118 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13119 | /*- |
| 13120 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13121 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13122 | * |
| 13123 | * This code is derived from software contributed to Berkeley by |
| 13124 | * Kenneth Almquist. |
| 13125 | * |
| 13126 | * Redistribution and use in source and binary forms, with or without |
| 13127 | * modification, are permitted provided that the following conditions |
| 13128 | * are met: |
| 13129 | * 1. Redistributions of source code must retain the above copyright |
| 13130 | * notice, this list of conditions and the following disclaimer. |
| 13131 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13132 | * notice, this list of conditions and the following disclaimer in the |
| 13133 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13134 | * 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] | 13135 | * may be used to endorse or promote products derived from this software |
| 13136 | * without specific prior written permission. |
| 13137 | * |
| 13138 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13139 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13140 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13141 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13142 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13143 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13144 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13145 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13146 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13147 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13148 | * SUCH DAMAGE. |
| 13149 | */ |