Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * ash shell port for busybox |
| 4 | * |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 5 | * This code is derived from software contributed to Berkeley by |
| 6 | * Kenneth Almquist. |
| 7 | * |
| 8 | * Original BSD copyright notice is retained at the end of this file. |
| 9 | * |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 10 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 11 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12 | * |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13 | * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au> |
Eric Andersen | 81fe123 | 2003-07-29 06:38:40 +0000 | [diff] [blame] | 14 | * was re-ported from NetBSD and debianized. |
| 15 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 16 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 19 | /* |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 20 | * The following should be set to reflect the type of system you have: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 21 | * JOBS -> 1 if you have Berkeley job control, 0 otherwise. |
| 22 | * define SYSV if you are running under System V. |
| 23 | * define DEBUG=1 to compile in debugging ('set -o debug' to turn on) |
| 24 | * define DEBUG=2 to compile in and turn on debugging. |
| 25 | * |
| 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 | |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 77 | //kbuild:lib-$(CONFIG_ASH) += ash.o ash_ptr_hack.o shell_common.o |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 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: |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 956 | p++; |
| 957 | putc(*p, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 958 | break; |
| 959 | case CTLVAR: |
| 960 | putc('$', fp); |
| 961 | putc('{', fp); |
| 962 | subtype = *++p; |
| 963 | if (subtype == VSLENGTH) |
| 964 | putc('#', fp); |
| 965 | |
Dan Fandrich | 77d4872 | 2010-09-07 23:38:28 -0700 | [diff] [blame] | 966 | while (*p != '=') { |
| 967 | putc(*p, fp); |
| 968 | p++; |
| 969 | } |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 970 | |
| 971 | if (subtype & VSNUL) |
| 972 | putc(':', fp); |
| 973 | |
| 974 | switch (subtype & VSTYPE) { |
| 975 | case VSNORMAL: |
| 976 | putc('}', fp); |
| 977 | break; |
| 978 | case VSMINUS: |
| 979 | putc('-', fp); |
| 980 | break; |
| 981 | case VSPLUS: |
| 982 | putc('+', fp); |
| 983 | break; |
| 984 | case VSQUESTION: |
| 985 | putc('?', fp); |
| 986 | break; |
| 987 | case VSASSIGN: |
| 988 | putc('=', fp); |
| 989 | break; |
| 990 | case VSTRIMLEFT: |
| 991 | putc('#', fp); |
| 992 | break; |
| 993 | case VSTRIMLEFTMAX: |
| 994 | putc('#', fp); |
| 995 | putc('#', fp); |
| 996 | break; |
| 997 | case VSTRIMRIGHT: |
| 998 | putc('%', fp); |
| 999 | break; |
| 1000 | case VSTRIMRIGHTMAX: |
| 1001 | putc('%', fp); |
| 1002 | putc('%', fp); |
| 1003 | break; |
| 1004 | case VSLENGTH: |
| 1005 | break; |
| 1006 | default: |
| 1007 | out1fmt("<subtype %d>", subtype); |
| 1008 | } |
| 1009 | break; |
| 1010 | case CTLENDVAR: |
| 1011 | putc('}', fp); |
| 1012 | break; |
| 1013 | case CTLBACKQ: |
| 1014 | case CTLBACKQ|CTLQUOTE: |
| 1015 | putc('$', fp); |
| 1016 | putc('(', fp); |
| 1017 | shtree(bqlist->n, -1, NULL, fp); |
| 1018 | putc(')', fp); |
| 1019 | break; |
| 1020 | default: |
| 1021 | putc(*p, fp); |
| 1022 | break; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 1027 | static void |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1028 | shcmd(union node *cmd, FILE *fp) |
| 1029 | { |
| 1030 | union node *np; |
| 1031 | int first; |
| 1032 | const char *s; |
| 1033 | int dftfd; |
| 1034 | |
| 1035 | first = 1; |
| 1036 | for (np = cmd->ncmd.args; np; np = np->narg.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1037 | if (!first) |
| 1038 | putc(' ', fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1039 | sharg(np, fp); |
| 1040 | first = 0; |
| 1041 | } |
| 1042 | for (np = cmd->ncmd.redirect; np; np = np->nfile.next) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1043 | if (!first) |
| 1044 | putc(' ', fp); |
| 1045 | dftfd = 0; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1046 | switch (np->nfile.type) { |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1047 | case NTO: s = ">>"+1; dftfd = 1; break; |
| 1048 | case NCLOBBER: s = ">|"; dftfd = 1; break; |
| 1049 | case NAPPEND: s = ">>"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1050 | #if ENABLE_ASH_BASH_COMPAT |
| 1051 | case NTO2: |
| 1052 | #endif |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1053 | case NTOFD: s = ">&"; dftfd = 1; break; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 1054 | case NFROM: s = "<"; break; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 1055 | case NFROMFD: s = "<&"; break; |
| 1056 | case NFROMTO: s = "<>"; break; |
| 1057 | default: s = "*error*"; break; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1058 | } |
| 1059 | if (np->nfile.fd != dftfd) |
| 1060 | fprintf(fp, "%d", np->nfile.fd); |
| 1061 | fputs(s, fp); |
| 1062 | if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) { |
| 1063 | fprintf(fp, "%d", np->ndup.dupfd); |
| 1064 | } else { |
| 1065 | sharg(np->nfile.fname, fp); |
| 1066 | } |
| 1067 | first = 0; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | static void |
| 1072 | shtree(union node *n, int ind, char *pfx, FILE *fp) |
| 1073 | { |
| 1074 | struct nodelist *lp; |
| 1075 | const char *s; |
| 1076 | |
| 1077 | if (n == NULL) |
| 1078 | return; |
| 1079 | |
| 1080 | indent(ind, pfx, fp); |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 1081 | |
| 1082 | if (n == NODE_EOF) { |
| 1083 | fputs("<EOF>", fp); |
| 1084 | return; |
| 1085 | } |
| 1086 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1087 | switch (n->type) { |
| 1088 | case NSEMI: |
| 1089 | s = "; "; |
| 1090 | goto binop; |
| 1091 | case NAND: |
| 1092 | s = " && "; |
| 1093 | goto binop; |
| 1094 | case NOR: |
| 1095 | s = " || "; |
| 1096 | binop: |
| 1097 | shtree(n->nbinary.ch1, ind, NULL, fp); |
| 1098 | /* if (ind < 0) */ |
| 1099 | fputs(s, fp); |
| 1100 | shtree(n->nbinary.ch2, ind, NULL, fp); |
| 1101 | break; |
| 1102 | case NCMD: |
| 1103 | shcmd(n, fp); |
| 1104 | if (ind >= 0) |
| 1105 | putc('\n', fp); |
| 1106 | break; |
| 1107 | case NPIPE: |
| 1108 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 1109 | shtree(lp->n, 0, NULL, fp); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1110 | if (lp->next) |
| 1111 | fputs(" | ", fp); |
| 1112 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 1113 | if (n->npipe.pipe_backgnd) |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1114 | fputs(" &", fp); |
| 1115 | if (ind >= 0) |
| 1116 | putc('\n', fp); |
| 1117 | break; |
| 1118 | default: |
| 1119 | fprintf(fp, "<node type %d>", n->type); |
| 1120 | if (ind >= 0) |
| 1121 | putc('\n', fp); |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | static void |
| 1127 | showtree(union node *n) |
| 1128 | { |
| 1129 | trace_puts("showtree called\n"); |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 1130 | shtree(n, 1, NULL, stderr); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1133 | #endif /* DEBUG */ |
| 1134 | |
| 1135 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1136 | /* ============ Parser data */ |
| 1137 | |
| 1138 | /* |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1139 | * ash_vmsg() needs parsefile->fd, hence parsefile definition is moved up. |
| 1140 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1141 | struct strlist { |
| 1142 | struct strlist *next; |
| 1143 | char *text; |
| 1144 | }; |
| 1145 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1146 | struct alias; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 1147 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1148 | struct strpush { |
| 1149 | struct strpush *prev; /* preceding string on stack */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1150 | char *prev_string; |
| 1151 | int prev_left_in_line; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1152 | #if ENABLE_ASH_ALIAS |
| 1153 | struct alias *ap; /* if push was associated with an alias */ |
| 1154 | #endif |
| 1155 | char *string; /* remember the string since it may change */ |
| 1156 | }; |
| 1157 | |
| 1158 | struct parsefile { |
| 1159 | struct parsefile *prev; /* preceding file on stack */ |
| 1160 | int linno; /* current line */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1161 | int pf_fd; /* file descriptor (or -1 if string) */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 1162 | int left_in_line; /* number of chars left in this line */ |
| 1163 | int left_in_buffer; /* number of chars left in this buffer past the line */ |
| 1164 | char *next_to_pgetc; /* next char in buffer */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1165 | char *buf; /* input buffer */ |
| 1166 | struct strpush *strpush; /* for pushing strings at this level */ |
| 1167 | struct strpush basestrpush; /* so pushing one is fast */ |
| 1168 | }; |
| 1169 | |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1170 | static struct parsefile basepf; /* top level input file */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 1171 | static struct parsefile *g_parsefile = &basepf; /* current input file */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1172 | static int startlinno; /* line # where last token started */ |
| 1173 | static char *commandname; /* currently executing command */ |
| 1174 | static struct strlist *cmdenviron; /* environment for builtin command */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1175 | static uint8_t exitstatus; /* exit status of last command */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1176 | |
| 1177 | |
| 1178 | /* ============ Message printing */ |
| 1179 | |
| 1180 | static void |
| 1181 | ash_vmsg(const char *msg, va_list ap) |
| 1182 | { |
| 1183 | fprintf(stderr, "%s: ", arg0); |
| 1184 | if (commandname) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1185 | if (strcmp(arg0, commandname)) |
| 1186 | fprintf(stderr, "%s: ", commandname); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 1187 | if (!iflag || g_parsefile->pf_fd > 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1188 | fprintf(stderr, "line %d: ", startlinno); |
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 | vfprintf(stderr, msg, ap); |
| 1191 | outcslow('\n', stderr); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1192 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1193 | |
| 1194 | /* |
| 1195 | * Exverror is called to raise the error exception. If the second argument |
| 1196 | * is not NULL then error prints an error message using printf style |
| 1197 | * formatting. It then raises the error exception. |
| 1198 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1199 | static void ash_vmsg_and_raise(int, const char *, va_list) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1200 | static void |
| 1201 | ash_vmsg_and_raise(int cond, const char *msg, va_list ap) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1202 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1203 | #if DEBUG |
| 1204 | if (msg) { |
| 1205 | TRACE(("ash_vmsg_and_raise(%d, \"", cond)); |
| 1206 | TRACEV((msg, ap)); |
| 1207 | TRACE(("\") pid=%d\n", getpid())); |
| 1208 | } else |
| 1209 | TRACE(("ash_vmsg_and_raise(%d, NULL) pid=%d\n", cond, getpid())); |
| 1210 | if (msg) |
| 1211 | #endif |
| 1212 | ash_vmsg(msg, ap); |
| 1213 | |
| 1214 | flush_stdout_stderr(); |
| 1215 | raise_exception(cond); |
| 1216 | /* NOTREACHED */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 1217 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1218 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1219 | static void ash_msg_and_raise_error(const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1220 | static void |
| 1221 | ash_msg_and_raise_error(const char *msg, ...) |
| 1222 | { |
| 1223 | va_list ap; |
| 1224 | |
| 1225 | va_start(ap, msg); |
| 1226 | ash_vmsg_and_raise(EXERROR, msg, ap); |
| 1227 | /* NOTREACHED */ |
| 1228 | va_end(ap); |
| 1229 | } |
| 1230 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1231 | static void raise_error_syntax(const char *) NORETURN; |
| 1232 | static void |
| 1233 | raise_error_syntax(const char *msg) |
| 1234 | { |
| 1235 | ash_msg_and_raise_error("syntax error: %s", msg); |
| 1236 | /* NOTREACHED */ |
| 1237 | } |
| 1238 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1239 | static void ash_msg_and_raise(int, const char *, ...) NORETURN; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 1240 | static void |
| 1241 | ash_msg_and_raise(int cond, const char *msg, ...) |
| 1242 | { |
| 1243 | va_list ap; |
| 1244 | |
| 1245 | va_start(ap, msg); |
| 1246 | ash_vmsg_and_raise(cond, msg, ap); |
| 1247 | /* NOTREACHED */ |
| 1248 | va_end(ap); |
| 1249 | } |
| 1250 | |
| 1251 | /* |
| 1252 | * error/warning routines for external builtins |
| 1253 | */ |
| 1254 | static void |
| 1255 | ash_msg(const char *fmt, ...) |
| 1256 | { |
| 1257 | va_list ap; |
| 1258 | |
| 1259 | va_start(ap, fmt); |
| 1260 | ash_vmsg(fmt, ap); |
| 1261 | va_end(ap); |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | * Return a string describing an error. The returned string may be a |
| 1266 | * pointer to a static buffer that will be overwritten on the next call. |
| 1267 | * Action describes the operation that got the error. |
| 1268 | */ |
| 1269 | static const char * |
| 1270 | errmsg(int e, const char *em) |
| 1271 | { |
| 1272 | if (e == ENOENT || e == ENOTDIR) { |
| 1273 | return em; |
| 1274 | } |
| 1275 | return strerror(e); |
| 1276 | } |
| 1277 | |
| 1278 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1279 | /* ============ Memory allocation */ |
| 1280 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1281 | #if 0 |
| 1282 | /* I consider these wrappers nearly useless: |
| 1283 | * ok, they return you to nearest exception handler, but |
| 1284 | * how much memory do you leak in the process, making |
| 1285 | * memory starvation worse? |
| 1286 | */ |
| 1287 | static void * |
| 1288 | ckrealloc(void * p, size_t nbytes) |
| 1289 | { |
| 1290 | p = realloc(p, nbytes); |
| 1291 | if (!p) |
| 1292 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1293 | return p; |
| 1294 | } |
| 1295 | |
| 1296 | static void * |
| 1297 | ckmalloc(size_t nbytes) |
| 1298 | { |
| 1299 | return ckrealloc(NULL, nbytes); |
| 1300 | } |
| 1301 | |
| 1302 | static void * |
| 1303 | ckzalloc(size_t nbytes) |
| 1304 | { |
| 1305 | return memset(ckmalloc(nbytes), 0, nbytes); |
| 1306 | } |
| 1307 | |
| 1308 | static char * |
| 1309 | ckstrdup(const char *s) |
| 1310 | { |
| 1311 | char *p = strdup(s); |
| 1312 | if (!p) |
| 1313 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1314 | return p; |
| 1315 | } |
| 1316 | #else |
| 1317 | /* Using bbox equivalents. They exit if out of memory */ |
| 1318 | # define ckrealloc xrealloc |
| 1319 | # define ckmalloc xmalloc |
| 1320 | # define ckzalloc xzalloc |
| 1321 | # define ckstrdup xstrdup |
| 1322 | #endif |
| 1323 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1324 | /* |
| 1325 | * It appears that grabstackstr() will barf with such alignments |
| 1326 | * because stalloc() will return a string allocated in a new stackblock. |
| 1327 | */ |
| 1328 | #define SHELL_ALIGN(nbytes) (((nbytes) + SHELL_SIZE) & ~SHELL_SIZE) |
| 1329 | enum { |
| 1330 | /* Most machines require the value returned from malloc to be aligned |
| 1331 | * in some way. The following macro will get this right |
| 1332 | * on many machines. */ |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 1333 | SHELL_SIZE = sizeof(union { int i; char *cp; double d; }) - 1, |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1334 | /* Minimum size of a block */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1335 | MINSIZE = SHELL_ALIGN(504), |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1336 | }; |
| 1337 | |
| 1338 | struct stack_block { |
| 1339 | struct stack_block *prev; |
| 1340 | char space[MINSIZE]; |
| 1341 | }; |
| 1342 | |
| 1343 | struct stackmark { |
| 1344 | struct stack_block *stackp; |
| 1345 | char *stacknxt; |
| 1346 | size_t stacknleft; |
| 1347 | struct stackmark *marknext; |
| 1348 | }; |
| 1349 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1350 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1351 | struct globals_memstack { |
| 1352 | struct stack_block *g_stackp; // = &stackbase; |
| 1353 | struct stackmark *markp; |
| 1354 | char *g_stacknxt; // = stackbase.space; |
| 1355 | char *sstrend; // = stackbase.space + MINSIZE; |
| 1356 | size_t g_stacknleft; // = MINSIZE; |
| 1357 | int herefd; // = -1; |
| 1358 | struct stack_block stackbase; |
| 1359 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1360 | extern struct globals_memstack *const ash_ptr_to_globals_memstack; |
| 1361 | #define G_memstack (*ash_ptr_to_globals_memstack) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1362 | #define g_stackp (G_memstack.g_stackp ) |
| 1363 | #define markp (G_memstack.markp ) |
| 1364 | #define g_stacknxt (G_memstack.g_stacknxt ) |
| 1365 | #define sstrend (G_memstack.sstrend ) |
| 1366 | #define g_stacknleft (G_memstack.g_stacknleft) |
| 1367 | #define herefd (G_memstack.herefd ) |
| 1368 | #define stackbase (G_memstack.stackbase ) |
| 1369 | #define INIT_G_memstack() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1370 | (*(struct globals_memstack**)&ash_ptr_to_globals_memstack) = xzalloc(sizeof(G_memstack)); \ |
| 1371 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1372 | g_stackp = &stackbase; \ |
| 1373 | g_stacknxt = stackbase.space; \ |
| 1374 | g_stacknleft = MINSIZE; \ |
| 1375 | sstrend = stackbase.space + MINSIZE; \ |
| 1376 | herefd = -1; \ |
| 1377 | } while (0) |
| 1378 | |
Denys Vlasenko | e7670ff | 2009-10-11 00:45:25 +0200 | [diff] [blame] | 1379 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1380 | #define stackblock() ((void *)g_stacknxt) |
| 1381 | #define stackblocksize() g_stacknleft |
| 1382 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1383 | /* |
| 1384 | * Parse trees for commands are allocated in lifo order, so we use a stack |
| 1385 | * to make this more efficient, and also to avoid all sorts of exception |
| 1386 | * handling code to handle interrupts in the middle of a parse. |
| 1387 | * |
| 1388 | * The size 504 was chosen because the Ultrix malloc handles that size |
| 1389 | * well. |
| 1390 | */ |
| 1391 | static void * |
| 1392 | stalloc(size_t nbytes) |
| 1393 | { |
| 1394 | char *p; |
| 1395 | size_t aligned; |
| 1396 | |
| 1397 | aligned = SHELL_ALIGN(nbytes); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1398 | if (aligned > g_stacknleft) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1399 | size_t len; |
| 1400 | size_t blocksize; |
| 1401 | struct stack_block *sp; |
| 1402 | |
| 1403 | blocksize = aligned; |
| 1404 | if (blocksize < MINSIZE) |
| 1405 | blocksize = MINSIZE; |
| 1406 | len = sizeof(struct stack_block) - MINSIZE + blocksize; |
| 1407 | if (len < blocksize) |
| 1408 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1409 | INT_OFF; |
| 1410 | sp = ckmalloc(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1411 | sp->prev = g_stackp; |
| 1412 | g_stacknxt = sp->space; |
| 1413 | g_stacknleft = blocksize; |
| 1414 | sstrend = g_stacknxt + blocksize; |
| 1415 | g_stackp = sp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1416 | INT_ON; |
| 1417 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1418 | p = g_stacknxt; |
| 1419 | g_stacknxt += aligned; |
| 1420 | g_stacknleft -= aligned; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1421 | return p; |
| 1422 | } |
| 1423 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 1424 | static void * |
| 1425 | stzalloc(size_t nbytes) |
| 1426 | { |
| 1427 | return memset(stalloc(nbytes), 0, nbytes); |
| 1428 | } |
| 1429 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1430 | static void |
| 1431 | stunalloc(void *p) |
| 1432 | { |
| 1433 | #if DEBUG |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1434 | if (!p || (g_stacknxt < (char *)p) || ((char *)p < g_stackp->space)) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 1435 | write(STDERR_FILENO, "stunalloc\n", 10); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1436 | abort(); |
| 1437 | } |
| 1438 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1439 | g_stacknleft += g_stacknxt - (char *)p; |
| 1440 | g_stacknxt = p; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1443 | /* |
| 1444 | * Like strdup but works with the ash stack. |
| 1445 | */ |
| 1446 | static char * |
| 1447 | ststrdup(const char *p) |
| 1448 | { |
| 1449 | size_t len = strlen(p) + 1; |
| 1450 | return memcpy(stalloc(len), p, len); |
| 1451 | } |
| 1452 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1453 | static void |
| 1454 | setstackmark(struct stackmark *mark) |
| 1455 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1456 | mark->stackp = g_stackp; |
| 1457 | mark->stacknxt = g_stacknxt; |
| 1458 | mark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1459 | mark->marknext = markp; |
| 1460 | markp = mark; |
| 1461 | } |
| 1462 | |
| 1463 | static void |
| 1464 | popstackmark(struct stackmark *mark) |
| 1465 | { |
| 1466 | struct stack_block *sp; |
| 1467 | |
Denis Vlasenko | 93ebd4f | 2007-03-13 20:55:36 +0000 | [diff] [blame] | 1468 | if (!mark->stackp) |
| 1469 | return; |
| 1470 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1471 | INT_OFF; |
| 1472 | markp = mark->marknext; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1473 | while (g_stackp != mark->stackp) { |
| 1474 | sp = g_stackp; |
| 1475 | g_stackp = sp->prev; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1476 | free(sp); |
| 1477 | } |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1478 | g_stacknxt = mark->stacknxt; |
| 1479 | g_stacknleft = mark->stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1480 | sstrend = mark->stacknxt + mark->stacknleft; |
| 1481 | INT_ON; |
| 1482 | } |
| 1483 | |
| 1484 | /* |
| 1485 | * When the parser reads in a string, it wants to stick the string on the |
| 1486 | * stack and only adjust the stack pointer when it knows how big the |
| 1487 | * string is. Stackblock (defined in stack.h) returns a pointer to a block |
| 1488 | * of space on top of the stack and stackblocklen returns the length of |
| 1489 | * this block. Growstackblock will grow this space by at least one byte, |
| 1490 | * possibly moving it (like realloc). Grabstackblock actually allocates the |
| 1491 | * part of the block that has been used. |
| 1492 | */ |
| 1493 | static void |
| 1494 | growstackblock(void) |
| 1495 | { |
| 1496 | size_t newlen; |
| 1497 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1498 | newlen = g_stacknleft * 2; |
| 1499 | if (newlen < g_stacknleft) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1500 | ash_msg_and_raise_error(bb_msg_memory_exhausted); |
| 1501 | if (newlen < 128) |
| 1502 | newlen += 128; |
| 1503 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1504 | if (g_stacknxt == g_stackp->space && g_stackp != &stackbase) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1505 | struct stack_block *oldstackp; |
| 1506 | struct stackmark *xmark; |
| 1507 | struct stack_block *sp; |
| 1508 | struct stack_block *prevstackp; |
| 1509 | size_t grosslen; |
| 1510 | |
| 1511 | INT_OFF; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1512 | oldstackp = g_stackp; |
| 1513 | sp = g_stackp; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1514 | prevstackp = sp->prev; |
| 1515 | grosslen = newlen + sizeof(struct stack_block) - MINSIZE; |
| 1516 | sp = ckrealloc(sp, grosslen); |
| 1517 | sp->prev = prevstackp; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1518 | g_stackp = sp; |
| 1519 | g_stacknxt = sp->space; |
| 1520 | g_stacknleft = newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1521 | sstrend = sp->space + newlen; |
| 1522 | |
| 1523 | /* |
| 1524 | * Stack marks pointing to the start of the old block |
| 1525 | * must be relocated to point to the new block |
| 1526 | */ |
| 1527 | xmark = markp; |
| 1528 | while (xmark != NULL && xmark->stackp == oldstackp) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1529 | xmark->stackp = g_stackp; |
| 1530 | xmark->stacknxt = g_stacknxt; |
| 1531 | xmark->stacknleft = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1532 | xmark = xmark->marknext; |
| 1533 | } |
| 1534 | INT_ON; |
| 1535 | } else { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1536 | char *oldspace = g_stacknxt; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1537 | size_t oldlen = g_stacknleft; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1538 | char *p = stalloc(newlen); |
| 1539 | |
| 1540 | /* free the space we just allocated */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1541 | g_stacknxt = memcpy(p, oldspace, oldlen); |
| 1542 | g_stacknleft += newlen; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | static void |
| 1547 | grabstackblock(size_t len) |
| 1548 | { |
| 1549 | len = SHELL_ALIGN(len); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1550 | g_stacknxt += len; |
| 1551 | g_stacknleft -= len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * The following routines are somewhat easier to use than the above. |
| 1556 | * The user declares a variable of type STACKSTR, which may be declared |
| 1557 | * to be a register. The macro STARTSTACKSTR initializes things. Then |
| 1558 | * the user uses the macro STPUTC to add characters to the string. In |
| 1559 | * effect, STPUTC(c, p) is the same as *p++ = c except that the stack is |
| 1560 | * grown as necessary. When the user is done, she can just leave the |
| 1561 | * string there and refer to it using stackblock(). Or she can allocate |
| 1562 | * the space for it using grabstackstr(). If it is necessary to allow |
| 1563 | * someone else to use the stack temporarily and then continue to grow |
| 1564 | * the string, the user should use grabstack to allocate the space, and |
| 1565 | * then call ungrabstr(p) to return to the previous mode of operation. |
| 1566 | * |
| 1567 | * USTPUTC is like STPUTC except that it doesn't check for overflow. |
| 1568 | * CHECKSTACKSPACE can be called before USTPUTC to ensure that there |
| 1569 | * is space for at least one character. |
| 1570 | */ |
| 1571 | static void * |
| 1572 | growstackstr(void) |
| 1573 | { |
| 1574 | size_t len = stackblocksize(); |
| 1575 | if (herefd >= 0 && len >= 1024) { |
| 1576 | full_write(herefd, stackblock(), len); |
| 1577 | return stackblock(); |
| 1578 | } |
| 1579 | growstackblock(); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1580 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* |
| 1584 | * Called from CHECKSTRSPACE. |
| 1585 | */ |
| 1586 | static char * |
| 1587 | makestrspace(size_t newlen, char *p) |
| 1588 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1589 | size_t len = p - g_stacknxt; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1590 | size_t size = stackblocksize(); |
| 1591 | |
| 1592 | for (;;) { |
| 1593 | size_t nleft; |
| 1594 | |
| 1595 | size = stackblocksize(); |
| 1596 | nleft = size - len; |
| 1597 | if (nleft >= newlen) |
| 1598 | break; |
| 1599 | growstackblock(); |
| 1600 | } |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1601 | return (char *)stackblock() + len; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | static char * |
| 1605 | stack_nputstr(const char *s, size_t n, char *p) |
| 1606 | { |
| 1607 | p = makestrspace(n, p); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1608 | p = (char *)memcpy(p, s, n) + n; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 1609 | return p; |
| 1610 | } |
| 1611 | |
| 1612 | static char * |
| 1613 | stack_putstr(const char *s, char *p) |
| 1614 | { |
| 1615 | return stack_nputstr(s, strlen(s), p); |
| 1616 | } |
| 1617 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1618 | static char * |
| 1619 | _STPUTC(int c, char *p) |
| 1620 | { |
| 1621 | if (p == sstrend) |
| 1622 | p = growstackstr(); |
| 1623 | *p++ = c; |
| 1624 | return p; |
| 1625 | } |
| 1626 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1627 | #define STARTSTACKSTR(p) ((p) = stackblock()) |
| 1628 | #define STPUTC(c, p) ((p) = _STPUTC((c), (p))) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1629 | #define CHECKSTRSPACE(n, p) do { \ |
| 1630 | char *q = (p); \ |
| 1631 | size_t l = (n); \ |
| 1632 | size_t m = sstrend - q; \ |
| 1633 | if (l > m) \ |
| 1634 | (p) = makestrspace(l, q); \ |
| 1635 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1636 | #define USTPUTC(c, p) (*(p)++ = (c)) |
Denis Vlasenko | 843cbd5 | 2008-06-27 00:23:18 +0000 | [diff] [blame] | 1637 | #define STACKSTRNUL(p) do { \ |
| 1638 | if ((p) == sstrend) \ |
| 1639 | (p) = growstackstr(); \ |
| 1640 | *(p) = '\0'; \ |
| 1641 | } while (0) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1642 | #define STUNPUTC(p) (--(p)) |
| 1643 | #define STTOPC(p) ((p)[-1]) |
| 1644 | #define STADJUST(amount, p) ((p) += (amount)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1645 | |
| 1646 | #define grabstackstr(p) stalloc((char *)(p) - (char *)stackblock()) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 1647 | #define ungrabstackstr(s, p) stunalloc(s) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1648 | #define stackstrend() ((void *)sstrend) |
| 1649 | |
| 1650 | |
| 1651 | /* ============ String helpers */ |
| 1652 | |
| 1653 | /* |
| 1654 | * prefix -- see if pfx is a prefix of string. |
| 1655 | */ |
| 1656 | static char * |
| 1657 | prefix(const char *string, const char *pfx) |
| 1658 | { |
| 1659 | while (*pfx) { |
| 1660 | if (*pfx++ != *string++) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 1661 | return NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1662 | } |
| 1663 | return (char *) string; |
| 1664 | } |
| 1665 | |
| 1666 | /* |
| 1667 | * Check for a valid number. This should be elsewhere. |
| 1668 | */ |
| 1669 | static int |
| 1670 | is_number(const char *p) |
| 1671 | { |
| 1672 | do { |
| 1673 | if (!isdigit(*p)) |
| 1674 | return 0; |
| 1675 | } while (*++p != '\0'); |
| 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Convert a string of digits to an integer, printing an error message on |
| 1681 | * failure. |
| 1682 | */ |
| 1683 | static int |
| 1684 | number(const char *s) |
| 1685 | { |
| 1686 | if (!is_number(s)) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 1687 | ash_msg_and_raise_error(msg_illnum, s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1688 | return atoi(s); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Produce a possibly single quoted string suitable as input to the shell. |
| 1693 | * The return string is allocated on the stack. |
| 1694 | */ |
| 1695 | static char * |
| 1696 | single_quote(const char *s) |
| 1697 | { |
| 1698 | char *p; |
| 1699 | |
| 1700 | STARTSTACKSTR(p); |
| 1701 | |
| 1702 | do { |
| 1703 | char *q; |
| 1704 | size_t len; |
| 1705 | |
| 1706 | len = strchrnul(s, '\'') - s; |
| 1707 | |
| 1708 | q = p = makestrspace(len + 3, p); |
| 1709 | |
| 1710 | *q++ = '\''; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 1711 | q = (char *)memcpy(q, s, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1712 | *q++ = '\''; |
| 1713 | s += len; |
| 1714 | |
| 1715 | STADJUST(q - p, p); |
| 1716 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1717 | if (*s != '\'') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1718 | break; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1719 | len = 0; |
| 1720 | do len++; while (*++s == '\''); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1721 | |
| 1722 | q = p = makestrspace(len + 3, p); |
| 1723 | |
| 1724 | *q++ = '"'; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1725 | q = (char *)memcpy(q, s - len, len) + len; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1726 | *q++ = '"'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1727 | |
| 1728 | STADJUST(q - p, p); |
| 1729 | } while (*s); |
| 1730 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 1731 | USTPUTC('\0', p); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1732 | |
| 1733 | return stackblock(); |
| 1734 | } |
| 1735 | |
| 1736 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 1737 | /* ============ nextopt */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1738 | |
| 1739 | static char **argptr; /* argument list for builtin commands */ |
| 1740 | static char *optionarg; /* set by nextopt (like getopt) */ |
| 1741 | static char *optptr; /* used by nextopt */ |
| 1742 | |
| 1743 | /* |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1744 | * XXX - should get rid of. Have all builtins use getopt(3). |
| 1745 | * The library getopt must have the BSD extension static variable |
| 1746 | * "optreset", otherwise it can't be used within the shell safely. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1747 | * |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1748 | * Standard option processing (a la getopt) for builtin routines. |
| 1749 | * The only argument that is passed to nextopt is the option string; |
| 1750 | * the other arguments are unnecessary. It returns the character, |
| 1751 | * or '\0' on end of input. |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1752 | */ |
| 1753 | static int |
| 1754 | nextopt(const char *optstring) |
| 1755 | { |
| 1756 | char *p; |
| 1757 | const char *q; |
| 1758 | char c; |
| 1759 | |
| 1760 | p = optptr; |
| 1761 | if (p == NULL || *p == '\0') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1762 | /* We ate entire "-param", take next one */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1763 | p = *argptr; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1764 | if (p == NULL) |
| 1765 | return '\0'; |
| 1766 | if (*p != '-') |
| 1767 | return '\0'; |
| 1768 | if (*++p == '\0') /* just "-" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1769 | return '\0'; |
| 1770 | argptr++; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1771 | if (LONE_DASH(p)) /* "--" ? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1772 | return '\0'; |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1773 | /* p => next "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1774 | } |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1775 | /* p => some option char in the middle of a "-param" */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1776 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 1777 | for (q = optstring; *q != c;) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1778 | if (*q == '\0') |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 1779 | ash_msg_and_raise_error("illegal option -%c", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1780 | if (*++q == ':') |
| 1781 | q++; |
| 1782 | } |
| 1783 | if (*++q == ':') { |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 1784 | if (*p == '\0') { |
| 1785 | p = *argptr++; |
| 1786 | if (p == NULL) |
| 1787 | ash_msg_and_raise_error("no arg for -%c option", c); |
| 1788 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1789 | optionarg = p; |
| 1790 | p = NULL; |
| 1791 | } |
| 1792 | optptr = p; |
| 1793 | return c; |
| 1794 | } |
| 1795 | |
| 1796 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1797 | /* ============ Shell variables */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1798 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1799 | /* |
| 1800 | * The parsefile structure pointed to by the global variable parsefile |
| 1801 | * contains information about the current file being read. |
| 1802 | */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1803 | struct shparam { |
| 1804 | int nparam; /* # of positional parameters (without $0) */ |
| 1805 | #if ENABLE_ASH_GETOPTS |
| 1806 | int optind; /* next parameter to be processed by getopts */ |
| 1807 | int optoff; /* used by getopts */ |
| 1808 | #endif |
| 1809 | unsigned char malloced; /* if parameter list dynamically allocated */ |
| 1810 | char **p; /* parameter list */ |
| 1811 | }; |
| 1812 | |
| 1813 | /* |
| 1814 | * Free the list of positional parameters. |
| 1815 | */ |
| 1816 | static void |
| 1817 | freeparam(volatile struct shparam *param) |
| 1818 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1819 | if (param->malloced) { |
Denis Vlasenko | 3177ba0 | 2008-07-13 20:39:23 +0000 | [diff] [blame] | 1820 | char **ap, **ap1; |
| 1821 | ap = ap1 = param->p; |
| 1822 | while (*ap) |
| 1823 | free(*ap++); |
| 1824 | free(ap1); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1829 | static void FAST_FUNC getoptsreset(const char *value); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1830 | #endif |
| 1831 | |
| 1832 | struct var { |
| 1833 | struct var *next; /* next entry in hash list */ |
| 1834 | int flags; /* flags are defined above */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1835 | const char *var_text; /* name=value */ |
| 1836 | void (*var_func)(const char *) FAST_FUNC; /* function to be called when */ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1837 | /* the variable gets set/unset */ |
| 1838 | }; |
| 1839 | |
| 1840 | struct localvar { |
| 1841 | struct localvar *next; /* next local variable in list */ |
| 1842 | struct var *vp; /* the variable that was made local */ |
| 1843 | int flags; /* saved flags */ |
| 1844 | const char *text; /* saved text */ |
| 1845 | }; |
| 1846 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1847 | /* flags */ |
| 1848 | #define VEXPORT 0x01 /* variable is exported */ |
| 1849 | #define VREADONLY 0x02 /* variable cannot be modified */ |
| 1850 | #define VSTRFIXED 0x04 /* variable struct is statically allocated */ |
| 1851 | #define VTEXTFIXED 0x08 /* text is statically allocated */ |
| 1852 | #define VSTACK 0x10 /* text is allocated on the stack */ |
| 1853 | #define VUNSET 0x20 /* the variable is not set */ |
| 1854 | #define VNOFUNC 0x40 /* don't call the callback function */ |
| 1855 | #define VNOSET 0x80 /* do not set variable - just readonly test */ |
| 1856 | #define VNOSAVE 0x100 /* when text is on the heap before setvareq */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 1857 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 1858 | # define VDYNAMIC 0x200 /* dynamic variable */ |
| 1859 | #else |
| 1860 | # define VDYNAMIC 0 |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1861 | #endif |
| 1862 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1863 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1864 | /* Need to be before varinit_data[] */ |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1865 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1866 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1867 | change_lc_all(const char *value) |
| 1868 | { |
| 1869 | if (value && *value != '\0') |
| 1870 | setlocale(LC_ALL, value); |
| 1871 | } |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 1872 | static void FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 1873 | change_lc_ctype(const char *value) |
| 1874 | { |
| 1875 | if (value && *value != '\0') |
| 1876 | setlocale(LC_CTYPE, value); |
| 1877 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 1878 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1879 | #if ENABLE_ASH_MAIL |
| 1880 | static void chkmail(void); |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1881 | static void changemail(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1882 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1883 | static void changepath(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1884 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1885 | static void change_random(const char *) FAST_FUNC; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1886 | #endif |
| 1887 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1888 | static const struct { |
| 1889 | int flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1890 | const char *var_text; |
| 1891 | void (*var_func)(const char *) FAST_FUNC; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1892 | } varinit_data[] = { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1893 | { VSTRFIXED|VTEXTFIXED , defifsvar , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1894 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1895 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAIL" , changemail }, |
| 1896 | { VSTRFIXED|VTEXTFIXED|VUNSET, "MAILPATH" , changemail }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1897 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1898 | { VSTRFIXED|VTEXTFIXED , bb_PATH_root_path, changepath }, |
| 1899 | { VSTRFIXED|VTEXTFIXED , "PS1=$ " , NULL }, |
| 1900 | { VSTRFIXED|VTEXTFIXED , "PS2=> " , NULL }, |
| 1901 | { VSTRFIXED|VTEXTFIXED , "PS4=+ " , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1902 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1903 | { VSTRFIXED|VTEXTFIXED , "OPTIND=1" , getoptsreset }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1904 | #endif |
| 1905 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1906 | { VSTRFIXED|VTEXTFIXED|VUNSET|VDYNAMIC, "RANDOM", change_random }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1907 | #endif |
| 1908 | #if ENABLE_LOCALE_SUPPORT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1909 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_ALL" , change_lc_all }, |
| 1910 | { VSTRFIXED|VTEXTFIXED|VUNSET, "LC_CTYPE" , change_lc_ctype }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1911 | #endif |
| 1912 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1913 | { VSTRFIXED|VTEXTFIXED|VUNSET, "HISTFILE" , NULL }, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1914 | #endif |
| 1915 | }; |
| 1916 | |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1917 | struct redirtab; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1918 | |
| 1919 | struct globals_var { |
| 1920 | struct shparam shellparam; /* $@ current positional parameters */ |
| 1921 | struct redirtab *redirlist; |
| 1922 | int g_nullredirs; |
| 1923 | int preverrout_fd; /* save fd2 before print debug if xflag is set. */ |
| 1924 | struct var *vartab[VTABSIZE]; |
| 1925 | struct var varinit[ARRAY_SIZE(varinit_data)]; |
| 1926 | }; |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1927 | extern struct globals_var *const ash_ptr_to_globals_var; |
| 1928 | #define G_var (*ash_ptr_to_globals_var) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1929 | #define shellparam (G_var.shellparam ) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 1930 | //#define redirlist (G_var.redirlist ) |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1931 | #define g_nullredirs (G_var.g_nullredirs ) |
| 1932 | #define preverrout_fd (G_var.preverrout_fd) |
| 1933 | #define vartab (G_var.vartab ) |
| 1934 | #define varinit (G_var.varinit ) |
| 1935 | #define INIT_G_var() do { \ |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1936 | unsigned i; \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 1937 | (*(struct globals_var**)&ash_ptr_to_globals_var) = xzalloc(sizeof(G_var)); \ |
| 1938 | barrier(); \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1939 | for (i = 0; i < ARRAY_SIZE(varinit_data); i++) { \ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1940 | varinit[i].flags = varinit_data[i].flags; \ |
| 1941 | varinit[i].var_text = varinit_data[i].var_text; \ |
| 1942 | varinit[i].var_func = varinit_data[i].var_func; \ |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1943 | } \ |
| 1944 | } while (0) |
| 1945 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1946 | #define vifs varinit[0] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1947 | #if ENABLE_ASH_MAIL |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1948 | # define vmail (&vifs)[1] |
| 1949 | # define vmpath (&vmail)[1] |
| 1950 | # define vpath (&vmpath)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1951 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1952 | # define vpath (&vifs)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1953 | #endif |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1954 | #define vps1 (&vpath)[1] |
| 1955 | #define vps2 (&vps1)[1] |
| 1956 | #define vps4 (&vps2)[1] |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1957 | #if ENABLE_ASH_GETOPTS |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1958 | # define voptind (&vps4)[1] |
| 1959 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1960 | # define vrandom (&voptind)[1] |
| 1961 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1962 | #else |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1963 | # if ENABLE_ASH_RANDOM_SUPPORT |
| 1964 | # define vrandom (&vps4)[1] |
| 1965 | # endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1966 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1967 | |
| 1968 | /* |
| 1969 | * The following macros access the values of the above variables. |
| 1970 | * They have to skip over the name. They return the null string |
| 1971 | * for unset variables. |
| 1972 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1973 | #define ifsval() (vifs.var_text + 4) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1974 | #define ifsset() ((vifs.flags & VUNSET) == 0) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1975 | #if ENABLE_ASH_MAIL |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1976 | # define mailval() (vmail.var_text + 5) |
| 1977 | # define mpathval() (vmpath.var_text + 9) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1978 | # define mpathset() ((vmpath.flags & VUNSET) == 0) |
| 1979 | #endif |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1980 | #define pathval() (vpath.var_text + 5) |
| 1981 | #define ps1val() (vps1.var_text + 4) |
| 1982 | #define ps2val() (vps2.var_text + 4) |
| 1983 | #define ps4val() (vps4.var_text + 4) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1984 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 1985 | # define optindval() (voptind.var_text + 7) |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 1986 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 1987 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1988 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1989 | static void FAST_FUNC |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 1990 | getoptsreset(const char *value) |
| 1991 | { |
| 1992 | shellparam.optind = number(value); |
| 1993 | shellparam.optoff = -1; |
| 1994 | } |
| 1995 | #endif |
| 1996 | |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 1997 | /* math.h has these, otherwise define our private copies */ |
| 1998 | #if !ENABLE_SH_MATH_SUPPORT |
| 1999 | #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 2000 | #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2001 | /* |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2002 | * Return the pointer to the first char which is not part of a legal variable name |
| 2003 | * (a letter or underscore followed by letters, underscores, and digits). |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2004 | */ |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2005 | static const char* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2006 | endofname(const char *name) |
| 2007 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2008 | if (!is_name(*name)) |
| 2009 | return name; |
| 2010 | while (*++name) { |
| 2011 | if (!is_in_name(*name)) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2012 | break; |
| 2013 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2014 | return name; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2015 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2016 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2017 | |
| 2018 | /* |
| 2019 | * Compares two strings up to the first = or '\0'. The first |
| 2020 | * string must be terminated by '='; the second may be terminated by |
| 2021 | * either '=' or '\0'. |
| 2022 | */ |
| 2023 | static int |
| 2024 | varcmp(const char *p, const char *q) |
| 2025 | { |
| 2026 | int c, d; |
| 2027 | |
| 2028 | while ((c = *p) == (d = *q)) { |
| 2029 | if (!c || c == '=') |
| 2030 | goto out; |
| 2031 | p++; |
| 2032 | q++; |
| 2033 | } |
| 2034 | if (c == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2035 | c = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2036 | if (d == '=') |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2037 | d = '\0'; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2038 | out: |
| 2039 | return c - d; |
| 2040 | } |
| 2041 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2042 | /* |
| 2043 | * Find the appropriate entry in the hash table from the name. |
| 2044 | */ |
| 2045 | static struct var ** |
| 2046 | hashvar(const char *p) |
| 2047 | { |
| 2048 | unsigned hashval; |
| 2049 | |
| 2050 | hashval = ((unsigned char) *p) << 4; |
| 2051 | while (*p && *p != '=') |
| 2052 | hashval += (unsigned char) *p++; |
| 2053 | return &vartab[hashval % VTABSIZE]; |
| 2054 | } |
| 2055 | |
| 2056 | static int |
| 2057 | vpcmp(const void *a, const void *b) |
| 2058 | { |
| 2059 | return varcmp(*(const char **)a, *(const char **)b); |
| 2060 | } |
| 2061 | |
| 2062 | /* |
| 2063 | * This routine initializes the builtin variables. |
| 2064 | */ |
| 2065 | static void |
| 2066 | initvar(void) |
| 2067 | { |
| 2068 | struct var *vp; |
| 2069 | struct var *end; |
| 2070 | struct var **vpp; |
| 2071 | |
| 2072 | /* |
| 2073 | * PS1 depends on uid |
| 2074 | */ |
| 2075 | #if ENABLE_FEATURE_EDITING && ENABLE_FEATURE_EDITING_FANCY_PROMPT |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2076 | vps1.var_text = "PS1=\\w \\$ "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2077 | #else |
| 2078 | if (!geteuid()) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2079 | vps1.var_text = "PS1=# "; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2080 | #endif |
| 2081 | vp = varinit; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 2082 | end = vp + ARRAY_SIZE(varinit); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2083 | do { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2084 | vpp = hashvar(vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2085 | vp->next = *vpp; |
| 2086 | *vpp = vp; |
| 2087 | } while (++vp < end); |
| 2088 | } |
| 2089 | |
| 2090 | static struct var ** |
| 2091 | findvar(struct var **vpp, const char *name) |
| 2092 | { |
| 2093 | for (; *vpp; vpp = &(*vpp)->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2094 | if (varcmp((*vpp)->var_text, name) == 0) { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2095 | break; |
| 2096 | } |
| 2097 | } |
| 2098 | return vpp; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * Find the value of a variable. Returns NULL if not set. |
| 2103 | */ |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2104 | static const char* FAST_FUNC |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2105 | lookupvar(const char *name) |
| 2106 | { |
| 2107 | struct var *v; |
| 2108 | |
| 2109 | v = *findvar(hashvar(name), name); |
| 2110 | if (v) { |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2111 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2112 | /* |
| 2113 | * Dynamic variables are implemented roughly the same way they are |
| 2114 | * in bash. Namely, they're "special" so long as they aren't unset. |
| 2115 | * As soon as they're unset, they're no longer dynamic, and dynamic |
| 2116 | * lookup will no longer happen at that point. -- PFM. |
| 2117 | */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2118 | if (v->flags & VDYNAMIC) |
| 2119 | v->var_func(NULL); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2120 | #endif |
| 2121 | if (!(v->flags & VUNSET)) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2122 | return var_end(v->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2123 | } |
| 2124 | return NULL; |
| 2125 | } |
| 2126 | |
| 2127 | /* |
| 2128 | * Search the environment of a builtin command. |
| 2129 | */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2130 | static const char * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2131 | bltinlookup(const char *name) |
| 2132 | { |
| 2133 | struct strlist *sp; |
| 2134 | |
| 2135 | for (sp = cmdenviron; sp; sp = sp->next) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2136 | if (varcmp(sp->text, name) == 0) |
| 2137 | return var_end(sp->text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2138 | } |
| 2139 | return lookupvar(name); |
| 2140 | } |
| 2141 | |
| 2142 | /* |
| 2143 | * Same as setvar except that the variable and value are passed in |
| 2144 | * the first argument as name=value. Since the first argument will |
| 2145 | * be actually stored in the table, it should not be a string that |
| 2146 | * will go away. |
| 2147 | * Called with interrupts off. |
| 2148 | */ |
| 2149 | static void |
| 2150 | setvareq(char *s, int flags) |
| 2151 | { |
| 2152 | struct var *vp, **vpp; |
| 2153 | |
| 2154 | vpp = hashvar(s); |
| 2155 | flags |= (VEXPORT & (((unsigned) (1 - aflag)) - 1)); |
| 2156 | vp = *findvar(vpp, s); |
| 2157 | if (vp) { |
| 2158 | if ((vp->flags & (VREADONLY|VDYNAMIC)) == VREADONLY) { |
| 2159 | const char *n; |
| 2160 | |
| 2161 | if (flags & VNOSAVE) |
| 2162 | free(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2163 | n = vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2164 | ash_msg_and_raise_error("%.*s: is read only", strchrnul(n, '=') - n, n); |
| 2165 | } |
| 2166 | |
| 2167 | if (flags & VNOSET) |
| 2168 | return; |
| 2169 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2170 | if (vp->var_func && !(flags & VNOFUNC)) |
| 2171 | vp->var_func(var_end(s)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2172 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2173 | if (!(vp->flags & (VTEXTFIXED|VSTACK))) |
| 2174 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2175 | |
| 2176 | flags |= vp->flags & ~(VTEXTFIXED|VSTACK|VNOSAVE|VUNSET); |
| 2177 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2178 | /* variable s is not found */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2179 | if (flags & VNOSET) |
| 2180 | return; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2181 | vp = ckzalloc(sizeof(*vp)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2182 | vp->next = *vpp; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 2183 | /*vp->func = NULL; - ckzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2184 | *vpp = vp; |
| 2185 | } |
| 2186 | if (!(flags & (VTEXTFIXED|VSTACK|VNOSAVE))) |
| 2187 | s = ckstrdup(s); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2188 | vp->var_text = s; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2189 | vp->flags = flags; |
| 2190 | } |
| 2191 | |
| 2192 | /* |
| 2193 | * Set the value of a variable. The flags argument is ored with the |
| 2194 | * flags of the variable. If val is NULL, the variable is unset. |
| 2195 | */ |
| 2196 | static void |
| 2197 | setvar(const char *name, const char *val, int flags) |
| 2198 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2199 | const char *q; |
| 2200 | char *p; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2201 | char *nameeq; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2202 | size_t namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2203 | size_t vallen; |
| 2204 | |
| 2205 | q = endofname(name); |
| 2206 | p = strchrnul(q, '='); |
| 2207 | namelen = p - name; |
| 2208 | if (!namelen || p != q) |
| 2209 | ash_msg_and_raise_error("%.*s: bad variable name", namelen, name); |
| 2210 | vallen = 0; |
| 2211 | if (val == NULL) { |
| 2212 | flags |= VUNSET; |
| 2213 | } else { |
| 2214 | vallen = strlen(val); |
| 2215 | } |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2216 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2217 | INT_OFF; |
| 2218 | nameeq = ckmalloc(namelen + vallen + 2); |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2219 | p = memcpy(nameeq, name, namelen) + namelen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2220 | if (val) { |
| 2221 | *p++ = '='; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 2222 | p = memcpy(p, val, vallen) + vallen; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2223 | } |
| 2224 | *p = '\0'; |
| 2225 | setvareq(nameeq, flags | VNOSAVE); |
| 2226 | INT_ON; |
| 2227 | } |
| 2228 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 2229 | static void FAST_FUNC |
| 2230 | setvar2(const char *name, const char *val) |
| 2231 | { |
| 2232 | setvar(name, val, 0); |
| 2233 | } |
| 2234 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2235 | #if ENABLE_ASH_GETOPTS |
| 2236 | /* |
| 2237 | * Safe version of setvar, returns 1 on success 0 on failure. |
| 2238 | */ |
| 2239 | static int |
| 2240 | setvarsafe(const char *name, const char *val, int flags) |
| 2241 | { |
| 2242 | int err; |
| 2243 | volatile int saveint; |
| 2244 | struct jmploc *volatile savehandler = exception_handler; |
| 2245 | struct jmploc jmploc; |
| 2246 | |
| 2247 | SAVE_INT(saveint); |
| 2248 | if (setjmp(jmploc.loc)) |
| 2249 | err = 1; |
| 2250 | else { |
| 2251 | exception_handler = &jmploc; |
| 2252 | setvar(name, val, flags); |
| 2253 | err = 0; |
| 2254 | } |
| 2255 | exception_handler = savehandler; |
| 2256 | RESTORE_INT(saveint); |
| 2257 | return err; |
| 2258 | } |
| 2259 | #endif |
| 2260 | |
| 2261 | /* |
| 2262 | * Unset the specified variable. |
| 2263 | */ |
| 2264 | static int |
| 2265 | unsetvar(const char *s) |
| 2266 | { |
| 2267 | struct var **vpp; |
| 2268 | struct var *vp; |
| 2269 | int retval; |
| 2270 | |
| 2271 | vpp = findvar(hashvar(s), s); |
| 2272 | vp = *vpp; |
| 2273 | retval = 2; |
| 2274 | if (vp) { |
| 2275 | int flags = vp->flags; |
| 2276 | |
| 2277 | retval = 1; |
| 2278 | if (flags & VREADONLY) |
| 2279 | goto out; |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 2280 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2281 | vp->flags &= ~VDYNAMIC; |
| 2282 | #endif |
| 2283 | if (flags & VUNSET) |
| 2284 | goto ok; |
| 2285 | if ((flags & VSTRFIXED) == 0) { |
| 2286 | INT_OFF; |
| 2287 | if ((flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2288 | free((char*)vp->var_text); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2289 | *vpp = vp->next; |
| 2290 | free(vp); |
| 2291 | INT_ON; |
| 2292 | } else { |
| 2293 | setvar(s, 0, 0); |
| 2294 | vp->flags &= ~VEXPORT; |
| 2295 | } |
| 2296 | ok: |
| 2297 | retval = 0; |
| 2298 | } |
| 2299 | out: |
| 2300 | return retval; |
| 2301 | } |
| 2302 | |
| 2303 | /* |
| 2304 | * Process a linked list of variable assignments. |
| 2305 | */ |
| 2306 | static void |
| 2307 | listsetvar(struct strlist *list_set_var, int flags) |
| 2308 | { |
| 2309 | struct strlist *lp = list_set_var; |
| 2310 | |
| 2311 | if (!lp) |
| 2312 | return; |
| 2313 | INT_OFF; |
| 2314 | do { |
| 2315 | setvareq(lp->text, flags); |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 2316 | lp = lp->next; |
| 2317 | } while (lp); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2318 | INT_ON; |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * Generate a list of variables satisfying the given conditions. |
| 2323 | */ |
| 2324 | static char ** |
| 2325 | listvars(int on, int off, char ***end) |
| 2326 | { |
| 2327 | struct var **vpp; |
| 2328 | struct var *vp; |
| 2329 | char **ep; |
| 2330 | int mask; |
| 2331 | |
| 2332 | STARTSTACKSTR(ep); |
| 2333 | vpp = vartab; |
| 2334 | mask = on | off; |
| 2335 | do { |
| 2336 | for (vp = *vpp; vp; vp = vp->next) { |
| 2337 | if ((vp->flags & mask) == on) { |
| 2338 | if (ep == stackstrend()) |
| 2339 | ep = growstackstr(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 2340 | *ep++ = (char*)vp->var_text; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | } while (++vpp < vartab + VTABSIZE); |
| 2344 | if (ep == stackstrend()) |
| 2345 | ep = growstackstr(); |
| 2346 | if (end) |
| 2347 | *end = ep; |
| 2348 | *ep++ = NULL; |
| 2349 | return grabstackstr(ep); |
| 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | /* ============ Path search helper |
| 2354 | * |
| 2355 | * The variable path (passed by reference) should be set to the start |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2356 | * of the path before the first call; path_advance will update |
| 2357 | * this value as it proceeds. Successive calls to path_advance will return |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2358 | * the possible path expansions in sequence. If an option (indicated by |
| 2359 | * a percent sign) appears in the path entry then the global variable |
| 2360 | * pathopt will be set to point to it; otherwise pathopt will be set to |
| 2361 | * NULL. |
| 2362 | */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2363 | static const char *pathopt; /* set by path_advance */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2364 | |
| 2365 | static char * |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2366 | path_advance(const char **path, const char *name) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2367 | { |
| 2368 | const char *p; |
| 2369 | char *q; |
| 2370 | const char *start; |
| 2371 | size_t len; |
| 2372 | |
| 2373 | if (*path == NULL) |
| 2374 | return NULL; |
| 2375 | start = *path; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2376 | for (p = start; *p && *p != ':' && *p != '%'; p++) |
| 2377 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2378 | len = p - start + strlen(name) + 2; /* "2" is for '/' and '\0' */ |
| 2379 | while (stackblocksize() < len) |
| 2380 | growstackblock(); |
| 2381 | q = stackblock(); |
| 2382 | if (p != start) { |
| 2383 | memcpy(q, start, p - start); |
| 2384 | q += p - start; |
| 2385 | *q++ = '/'; |
| 2386 | } |
| 2387 | strcpy(q, name); |
| 2388 | pathopt = NULL; |
| 2389 | if (*p == '%') { |
| 2390 | pathopt = ++p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 2391 | while (*p && *p != ':') |
| 2392 | p++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2393 | } |
| 2394 | if (*p == ':') |
| 2395 | *path = p + 1; |
| 2396 | else |
| 2397 | *path = NULL; |
| 2398 | return stalloc(len); |
| 2399 | } |
| 2400 | |
| 2401 | |
| 2402 | /* ============ Prompt */ |
| 2403 | |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 2404 | static smallint doprompt; /* if set, prompt the user */ |
| 2405 | static smallint needprompt; /* true if interactive and at start of line */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2406 | |
| 2407 | #if ENABLE_FEATURE_EDITING |
| 2408 | static line_input_t *line_input_state; |
| 2409 | static const char *cmdedit_prompt; |
| 2410 | static void |
| 2411 | putprompt(const char *s) |
| 2412 | { |
| 2413 | if (ENABLE_ASH_EXPAND_PRMT) { |
| 2414 | free((char*)cmdedit_prompt); |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 2415 | cmdedit_prompt = ckstrdup(s); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2416 | return; |
| 2417 | } |
| 2418 | cmdedit_prompt = s; |
| 2419 | } |
| 2420 | #else |
| 2421 | static void |
| 2422 | putprompt(const char *s) |
| 2423 | { |
| 2424 | out2str(s); |
| 2425 | } |
| 2426 | #endif |
| 2427 | |
| 2428 | #if ENABLE_ASH_EXPAND_PRMT |
| 2429 | /* expandstr() needs parsing machinery, so it is far away ahead... */ |
| 2430 | static const char *expandstr(const char *ps); |
| 2431 | #else |
| 2432 | #define expandstr(s) s |
| 2433 | #endif |
| 2434 | |
| 2435 | static void |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2436 | setprompt_if(smallint do_set, int whichprompt) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2437 | { |
| 2438 | const char *prompt; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 2439 | IF_ASH_EXPAND_PRMT(struct stackmark smark;) |
| 2440 | |
| 2441 | if (!do_set) |
| 2442 | return; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2443 | |
| 2444 | needprompt = 0; |
| 2445 | |
| 2446 | switch (whichprompt) { |
| 2447 | case 1: |
| 2448 | prompt = ps1val(); |
| 2449 | break; |
| 2450 | case 2: |
| 2451 | prompt = ps2val(); |
| 2452 | break; |
| 2453 | default: /* 0 */ |
| 2454 | prompt = nullstr; |
| 2455 | } |
| 2456 | #if ENABLE_ASH_EXPAND_PRMT |
| 2457 | setstackmark(&smark); |
| 2458 | stalloc(stackblocksize()); |
| 2459 | #endif |
| 2460 | putprompt(expandstr(prompt)); |
| 2461 | #if ENABLE_ASH_EXPAND_PRMT |
| 2462 | popstackmark(&smark); |
| 2463 | #endif |
| 2464 | } |
| 2465 | |
| 2466 | |
| 2467 | /* ============ The cd and pwd commands */ |
| 2468 | |
| 2469 | #define CD_PHYSICAL 1 |
| 2470 | #define CD_PRINT 2 |
| 2471 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2472 | static int |
| 2473 | cdopt(void) |
| 2474 | { |
| 2475 | int flags = 0; |
| 2476 | int i, j; |
| 2477 | |
| 2478 | j = 'L'; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 2479 | while ((i = nextopt("LP")) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2480 | if (i != j) { |
| 2481 | flags ^= CD_PHYSICAL; |
| 2482 | j = i; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | return flags; |
| 2487 | } |
| 2488 | |
| 2489 | /* |
| 2490 | * Update curdir (the name of the current directory) in response to a |
| 2491 | * cd command. |
| 2492 | */ |
| 2493 | static const char * |
| 2494 | updatepwd(const char *dir) |
| 2495 | { |
| 2496 | char *new; |
| 2497 | char *p; |
| 2498 | char *cdcomppath; |
| 2499 | const char *lim; |
| 2500 | |
| 2501 | cdcomppath = ststrdup(dir); |
| 2502 | STARTSTACKSTR(new); |
| 2503 | if (*dir != '/') { |
| 2504 | if (curdir == nullstr) |
| 2505 | return 0; |
| 2506 | new = stack_putstr(curdir, new); |
| 2507 | } |
| 2508 | new = makestrspace(strlen(dir) + 2, new); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 2509 | lim = (char *)stackblock() + 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2510 | if (*dir != '/') { |
| 2511 | if (new[-1] != '/') |
| 2512 | USTPUTC('/', new); |
| 2513 | if (new > lim && *lim == '/') |
| 2514 | lim++; |
| 2515 | } else { |
| 2516 | USTPUTC('/', new); |
| 2517 | cdcomppath++; |
| 2518 | if (dir[1] == '/' && dir[2] != '/') { |
| 2519 | USTPUTC('/', new); |
| 2520 | cdcomppath++; |
| 2521 | lim++; |
| 2522 | } |
| 2523 | } |
| 2524 | p = strtok(cdcomppath, "/"); |
| 2525 | while (p) { |
| 2526 | switch (*p) { |
| 2527 | case '.': |
| 2528 | if (p[1] == '.' && p[2] == '\0') { |
| 2529 | while (new > lim) { |
| 2530 | STUNPUTC(new); |
| 2531 | if (new[-1] == '/') |
| 2532 | break; |
| 2533 | } |
| 2534 | break; |
Denis Vlasenko | 16abcd9 | 2007-04-13 23:59:52 +0000 | [diff] [blame] | 2535 | } |
| 2536 | if (p[1] == '\0') |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2537 | break; |
| 2538 | /* fall through */ |
| 2539 | default: |
| 2540 | new = stack_putstr(p, new); |
| 2541 | USTPUTC('/', new); |
| 2542 | } |
| 2543 | p = strtok(0, "/"); |
| 2544 | } |
| 2545 | if (new > lim) |
| 2546 | STUNPUTC(new); |
| 2547 | *new = 0; |
| 2548 | return stackblock(); |
| 2549 | } |
| 2550 | |
| 2551 | /* |
| 2552 | * Find out what the current directory is. If we already know the current |
| 2553 | * directory, this routine returns immediately. |
| 2554 | */ |
| 2555 | static char * |
| 2556 | getpwd(void) |
| 2557 | { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 2558 | char *dir = getcwd(NULL, 0); /* huh, using glibc extension? */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2559 | return dir ? dir : nullstr; |
| 2560 | } |
| 2561 | |
| 2562 | static void |
| 2563 | setpwd(const char *val, int setold) |
| 2564 | { |
| 2565 | char *oldcur, *dir; |
| 2566 | |
| 2567 | oldcur = dir = curdir; |
| 2568 | |
| 2569 | if (setold) { |
| 2570 | setvar("OLDPWD", oldcur, VEXPORT); |
| 2571 | } |
| 2572 | INT_OFF; |
| 2573 | if (physdir != nullstr) { |
| 2574 | if (physdir != oldcur) |
| 2575 | free(physdir); |
| 2576 | physdir = nullstr; |
| 2577 | } |
| 2578 | if (oldcur == val || !val) { |
| 2579 | char *s = getpwd(); |
| 2580 | physdir = s; |
| 2581 | if (!val) |
| 2582 | dir = s; |
| 2583 | } else |
| 2584 | dir = ckstrdup(val); |
| 2585 | if (oldcur != dir && oldcur != nullstr) { |
| 2586 | free(oldcur); |
| 2587 | } |
| 2588 | curdir = dir; |
| 2589 | INT_ON; |
| 2590 | setvar("PWD", dir, VEXPORT); |
| 2591 | } |
| 2592 | |
| 2593 | static void hashcd(void); |
| 2594 | |
| 2595 | /* |
| 2596 | * Actually do the chdir. We also call hashcd to let the routines in exec.c |
| 2597 | * know that the current directory has changed. |
| 2598 | */ |
| 2599 | static int |
| 2600 | docd(const char *dest, int flags) |
| 2601 | { |
Denys Vlasenko | 4b1100e | 2010-03-05 14:10:54 +0100 | [diff] [blame] | 2602 | const char *dir = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2603 | int err; |
| 2604 | |
| 2605 | TRACE(("docd(\"%s\", %d) called\n", dest, flags)); |
| 2606 | |
| 2607 | INT_OFF; |
| 2608 | if (!(flags & CD_PHYSICAL)) { |
| 2609 | dir = updatepwd(dest); |
| 2610 | if (dir) |
| 2611 | dest = dir; |
| 2612 | } |
| 2613 | err = chdir(dest); |
| 2614 | if (err) |
| 2615 | goto out; |
| 2616 | setpwd(dir, 1); |
| 2617 | hashcd(); |
| 2618 | out: |
| 2619 | INT_ON; |
| 2620 | return err; |
| 2621 | } |
| 2622 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2623 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2624 | cdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2625 | { |
| 2626 | const char *dest; |
| 2627 | const char *path; |
| 2628 | const char *p; |
| 2629 | char c; |
| 2630 | struct stat statb; |
| 2631 | int flags; |
| 2632 | |
| 2633 | flags = cdopt(); |
| 2634 | dest = *argptr; |
| 2635 | if (!dest) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2636 | dest = bltinlookup("HOME"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2637 | else if (LONE_DASH(dest)) { |
| 2638 | dest = bltinlookup("OLDPWD"); |
| 2639 | flags |= CD_PRINT; |
| 2640 | } |
| 2641 | if (!dest) |
| 2642 | dest = nullstr; |
| 2643 | if (*dest == '/') |
| 2644 | goto step7; |
| 2645 | if (*dest == '.') { |
| 2646 | c = dest[1]; |
| 2647 | dotdot: |
| 2648 | switch (c) { |
| 2649 | case '\0': |
| 2650 | case '/': |
| 2651 | goto step6; |
| 2652 | case '.': |
| 2653 | c = dest[2]; |
| 2654 | if (c != '.') |
| 2655 | goto dotdot; |
| 2656 | } |
| 2657 | } |
| 2658 | if (!*dest) |
| 2659 | dest = "."; |
| 2660 | path = bltinlookup("CDPATH"); |
| 2661 | if (!path) { |
| 2662 | step6: |
| 2663 | step7: |
| 2664 | p = dest; |
| 2665 | goto docd; |
| 2666 | } |
| 2667 | do { |
| 2668 | c = *path; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 2669 | p = path_advance(&path, dest); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2670 | if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) { |
| 2671 | if (c && c != ':') |
| 2672 | flags |= CD_PRINT; |
| 2673 | docd: |
| 2674 | if (!docd(p, flags)) |
| 2675 | goto out; |
| 2676 | break; |
| 2677 | } |
| 2678 | } while (path); |
| 2679 | ash_msg_and_raise_error("can't cd to %s", dest); |
| 2680 | /* NOTREACHED */ |
| 2681 | out: |
| 2682 | if (flags & CD_PRINT) |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2683 | out1fmt("%s\n", curdir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2684 | return 0; |
| 2685 | } |
| 2686 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 2687 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2688 | pwdcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2689 | { |
| 2690 | int flags; |
| 2691 | const char *dir = curdir; |
| 2692 | |
| 2693 | flags = cdopt(); |
| 2694 | if (flags) { |
| 2695 | if (physdir == nullstr) |
| 2696 | setpwd(dir, 0); |
| 2697 | dir = physdir; |
| 2698 | } |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 2699 | out1fmt("%s\n", dir); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 2700 | return 0; |
| 2701 | } |
| 2702 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2703 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 2704 | /* ============ ... */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2705 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2706 | |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 2707 | #define IBUFSIZ (ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 1024) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2708 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2709 | /* Syntax classes */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2710 | #define CWORD 0 /* character is nothing special */ |
| 2711 | #define CNL 1 /* newline character */ |
| 2712 | #define CBACK 2 /* a backslash character */ |
| 2713 | #define CSQUOTE 3 /* single quote */ |
| 2714 | #define CDQUOTE 4 /* double quote */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2715 | #define CENDQUOTE 5 /* a terminating quote */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2716 | #define CBQUOTE 6 /* backwards single quote */ |
| 2717 | #define CVAR 7 /* a dollar sign */ |
| 2718 | #define CENDVAR 8 /* a '}' character */ |
| 2719 | #define CLP 9 /* a left paren in arithmetic */ |
| 2720 | #define CRP 10 /* a right paren in arithmetic */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2721 | #define CENDFILE 11 /* end of file */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 2722 | #define CCTL 12 /* like CWORD, except it must be escaped */ |
| 2723 | #define CSPCL 13 /* these terminate a word */ |
| 2724 | #define CIGN 14 /* character should be ignored */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2725 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2726 | #define PEOF 256 |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 2727 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2728 | # define PEOA 257 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2729 | #endif |
| 2730 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2731 | #define USE_SIT_FUNCTION ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2732 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2733 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2734 | # 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] | 2735 | #else |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2736 | # define SIT_ITEM(a,b,c,d) (a | (b << 4) | (c << 8)) |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2737 | #endif |
Denys Vlasenko | 068d386 | 2009-11-29 01:41:11 +0100 | [diff] [blame] | 2738 | static const uint16_t S_I_T[] = { |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2739 | #if ENABLE_ASH_ALIAS |
| 2740 | SIT_ITEM(CSPCL , CIGN , CIGN , CIGN ), /* 0, PEOA */ |
| 2741 | #endif |
| 2742 | SIT_ITEM(CSPCL , CWORD , CWORD, CWORD ), /* 1, ' ' */ |
| 2743 | SIT_ITEM(CNL , CNL , CNL , CNL ), /* 2, \n */ |
| 2744 | SIT_ITEM(CWORD , CCTL , CCTL , CWORD ), /* 3, !*-/:=?[]~ */ |
| 2745 | SIT_ITEM(CDQUOTE , CENDQUOTE, CWORD, CWORD ), /* 4, '"' */ |
| 2746 | SIT_ITEM(CVAR , CVAR , CWORD, CVAR ), /* 5, $ */ |
| 2747 | SIT_ITEM(CSQUOTE , CWORD , CENDQUOTE, CWORD), /* 6, "'" */ |
| 2748 | SIT_ITEM(CSPCL , CWORD , CWORD, CLP ), /* 7, ( */ |
| 2749 | SIT_ITEM(CSPCL , CWORD , CWORD, CRP ), /* 8, ) */ |
| 2750 | SIT_ITEM(CBACK , CBACK , CCTL , CBACK ), /* 9, \ */ |
| 2751 | SIT_ITEM(CBQUOTE , CBQUOTE , CWORD, CBQUOTE), /* 10, ` */ |
| 2752 | SIT_ITEM(CENDVAR , CENDVAR , CWORD, CENDVAR), /* 11, } */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2753 | #if !USE_SIT_FUNCTION |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2754 | SIT_ITEM(CENDFILE, CENDFILE , CENDFILE, CENDFILE),/* 12, PEOF */ |
| 2755 | SIT_ITEM(CWORD , CWORD , CWORD, CWORD ), /* 13, 0-9A-Za-z */ |
| 2756 | SIT_ITEM(CCTL , CCTL , CCTL , CCTL ) /* 14, CTLESC ... */ |
| 2757 | #endif |
| 2758 | #undef SIT_ITEM |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2759 | }; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2760 | /* Constants below must match table above */ |
| 2761 | enum { |
| 2762 | #if ENABLE_ASH_ALIAS |
| 2763 | CSPCL_CIGN_CIGN_CIGN , /* 0 */ |
| 2764 | #endif |
| 2765 | CSPCL_CWORD_CWORD_CWORD , /* 1 */ |
| 2766 | CNL_CNL_CNL_CNL , /* 2 */ |
| 2767 | CWORD_CCTL_CCTL_CWORD , /* 3 */ |
| 2768 | CDQUOTE_CENDQUOTE_CWORD_CWORD , /* 4 */ |
| 2769 | CVAR_CVAR_CWORD_CVAR , /* 5 */ |
| 2770 | CSQUOTE_CWORD_CENDQUOTE_CWORD , /* 6 */ |
| 2771 | CSPCL_CWORD_CWORD_CLP , /* 7 */ |
| 2772 | CSPCL_CWORD_CWORD_CRP , /* 8 */ |
| 2773 | CBACK_CBACK_CCTL_CBACK , /* 9 */ |
| 2774 | CBQUOTE_CBQUOTE_CWORD_CBQUOTE , /* 10 */ |
| 2775 | CENDVAR_CENDVAR_CWORD_CENDVAR , /* 11 */ |
| 2776 | CENDFILE_CENDFILE_CENDFILE_CENDFILE, /* 12 */ |
| 2777 | CWORD_CWORD_CWORD_CWORD , /* 13 */ |
| 2778 | CCTL_CCTL_CCTL_CCTL , /* 14 */ |
| 2779 | }; |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 2780 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2781 | /* c in SIT(c, syntax) must be an *unsigned char* or PEOA or PEOF, |
| 2782 | * caller must ensure proper cast on it if c is *char_ptr! |
| 2783 | */ |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2784 | /* Values for syntax param */ |
| 2785 | #define BASESYNTAX 0 /* not in quotes */ |
| 2786 | #define DQSYNTAX 1 /* in double quotes */ |
| 2787 | #define SQSYNTAX 2 /* in single quotes */ |
| 2788 | #define ARISYNTAX 3 /* in arithmetic */ |
| 2789 | #define PSSYNTAX 4 /* prompt. never passed to SIT() */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2790 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2791 | #if USE_SIT_FUNCTION |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2792 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 2793 | static int |
| 2794 | SIT(int c, int syntax) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2795 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2796 | static const char spec_symbls[] ALIGN1 = "\t\n !\"$&'()*-/:;<=>?[\\]`|}~"; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2797 | # if ENABLE_ASH_ALIAS |
| 2798 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2799 | 1, 2, 1, 3, 4, 5, 1, 6, /* "\t\n !\"$&'" */ |
| 2800 | 7, 8, 3, 3, 3, 3, 1, 1, /* "()*-/:;<" */ |
| 2801 | 3, 1, 3, 3, 9, 3, 10, 1, /* "=>?[\\]`|" */ |
| 2802 | 11, 3 /* "}~" */ |
| 2803 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2804 | # else |
| 2805 | static const uint8_t syntax_index_table[] ALIGN1 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 2806 | 0, 1, 0, 2, 3, 4, 0, 5, /* "\t\n !\"$&'" */ |
| 2807 | 6, 7, 2, 2, 2, 2, 0, 0, /* "()*-/:;<" */ |
| 2808 | 2, 0, 2, 2, 8, 2, 9, 0, /* "=>?[\\]`|" */ |
| 2809 | 10, 2 /* "}~" */ |
| 2810 | }; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2811 | # endif |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2812 | const char *s; |
| 2813 | int indx; |
| 2814 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2815 | if (c == PEOF) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2816 | return CENDFILE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2817 | # if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2818 | if (c == PEOA) |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2819 | indx = 0; |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2820 | else |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2821 | # endif |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2822 | { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2823 | /* Cast is purely for paranoia here, |
| 2824 | * just in case someone passed signed char to us */ |
| 2825 | if ((unsigned char)c >= CTL_FIRST |
| 2826 | && (unsigned char)c <= CTL_LAST |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2827 | ) { |
| 2828 | return CCTL; |
| 2829 | } |
| 2830 | s = strchrnul(spec_symbls, c); |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 2831 | if (*s == '\0') |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2832 | return CWORD; |
Denis Vlasenko | 0dfe1d2 | 2009-04-02 12:57:38 +0000 | [diff] [blame] | 2833 | indx = syntax_index_table[s - spec_symbls]; |
| 2834 | } |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2835 | return (S_I_T[indx] >> (syntax*4)) & 0xf; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 2838 | #else /* !USE_SIT_FUNCTION */ |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 2839 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 2840 | static const uint8_t syntax_index_table[] = { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 2841 | /* BASESYNTAX_DQSYNTAX_SQSYNTAX_ARISYNTAX */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 2842 | /* 0 */ CWORD_CWORD_CWORD_CWORD, |
| 2843 | /* 1 */ CWORD_CWORD_CWORD_CWORD, |
| 2844 | /* 2 */ CWORD_CWORD_CWORD_CWORD, |
| 2845 | /* 3 */ CWORD_CWORD_CWORD_CWORD, |
| 2846 | /* 4 */ CWORD_CWORD_CWORD_CWORD, |
| 2847 | /* 5 */ CWORD_CWORD_CWORD_CWORD, |
| 2848 | /* 6 */ CWORD_CWORD_CWORD_CWORD, |
| 2849 | /* 7 */ CWORD_CWORD_CWORD_CWORD, |
| 2850 | /* 8 */ CWORD_CWORD_CWORD_CWORD, |
| 2851 | /* 9 "\t" */ CSPCL_CWORD_CWORD_CWORD, |
| 2852 | /* 10 "\n" */ CNL_CNL_CNL_CNL, |
| 2853 | /* 11 */ CWORD_CWORD_CWORD_CWORD, |
| 2854 | /* 12 */ CWORD_CWORD_CWORD_CWORD, |
| 2855 | /* 13 */ CWORD_CWORD_CWORD_CWORD, |
| 2856 | /* 14 */ CWORD_CWORD_CWORD_CWORD, |
| 2857 | /* 15 */ CWORD_CWORD_CWORD_CWORD, |
| 2858 | /* 16 */ CWORD_CWORD_CWORD_CWORD, |
| 2859 | /* 17 */ CWORD_CWORD_CWORD_CWORD, |
| 2860 | /* 18 */ CWORD_CWORD_CWORD_CWORD, |
| 2861 | /* 19 */ CWORD_CWORD_CWORD_CWORD, |
| 2862 | /* 20 */ CWORD_CWORD_CWORD_CWORD, |
| 2863 | /* 21 */ CWORD_CWORD_CWORD_CWORD, |
| 2864 | /* 22 */ CWORD_CWORD_CWORD_CWORD, |
| 2865 | /* 23 */ CWORD_CWORD_CWORD_CWORD, |
| 2866 | /* 24 */ CWORD_CWORD_CWORD_CWORD, |
| 2867 | /* 25 */ CWORD_CWORD_CWORD_CWORD, |
| 2868 | /* 26 */ CWORD_CWORD_CWORD_CWORD, |
| 2869 | /* 27 */ CWORD_CWORD_CWORD_CWORD, |
| 2870 | /* 28 */ CWORD_CWORD_CWORD_CWORD, |
| 2871 | /* 29 */ CWORD_CWORD_CWORD_CWORD, |
| 2872 | /* 30 */ CWORD_CWORD_CWORD_CWORD, |
| 2873 | /* 31 */ CWORD_CWORD_CWORD_CWORD, |
| 2874 | /* 32 " " */ CSPCL_CWORD_CWORD_CWORD, |
| 2875 | /* 33 "!" */ CWORD_CCTL_CCTL_CWORD, |
| 2876 | /* 34 """ */ CDQUOTE_CENDQUOTE_CWORD_CWORD, |
| 2877 | /* 35 "#" */ CWORD_CWORD_CWORD_CWORD, |
| 2878 | /* 36 "$" */ CVAR_CVAR_CWORD_CVAR, |
| 2879 | /* 37 "%" */ CWORD_CWORD_CWORD_CWORD, |
| 2880 | /* 38 "&" */ CSPCL_CWORD_CWORD_CWORD, |
| 2881 | /* 39 "'" */ CSQUOTE_CWORD_CENDQUOTE_CWORD, |
| 2882 | /* 40 "(" */ CSPCL_CWORD_CWORD_CLP, |
| 2883 | /* 41 ")" */ CSPCL_CWORD_CWORD_CRP, |
| 2884 | /* 42 "*" */ CWORD_CCTL_CCTL_CWORD, |
| 2885 | /* 43 "+" */ CWORD_CWORD_CWORD_CWORD, |
| 2886 | /* 44 "," */ CWORD_CWORD_CWORD_CWORD, |
| 2887 | /* 45 "-" */ CWORD_CCTL_CCTL_CWORD, |
| 2888 | /* 46 "." */ CWORD_CWORD_CWORD_CWORD, |
| 2889 | /* 47 "/" */ CWORD_CCTL_CCTL_CWORD, |
| 2890 | /* 48 "0" */ CWORD_CWORD_CWORD_CWORD, |
| 2891 | /* 49 "1" */ CWORD_CWORD_CWORD_CWORD, |
| 2892 | /* 50 "2" */ CWORD_CWORD_CWORD_CWORD, |
| 2893 | /* 51 "3" */ CWORD_CWORD_CWORD_CWORD, |
| 2894 | /* 52 "4" */ CWORD_CWORD_CWORD_CWORD, |
| 2895 | /* 53 "5" */ CWORD_CWORD_CWORD_CWORD, |
| 2896 | /* 54 "6" */ CWORD_CWORD_CWORD_CWORD, |
| 2897 | /* 55 "7" */ CWORD_CWORD_CWORD_CWORD, |
| 2898 | /* 56 "8" */ CWORD_CWORD_CWORD_CWORD, |
| 2899 | /* 57 "9" */ CWORD_CWORD_CWORD_CWORD, |
| 2900 | /* 58 ":" */ CWORD_CCTL_CCTL_CWORD, |
| 2901 | /* 59 ";" */ CSPCL_CWORD_CWORD_CWORD, |
| 2902 | /* 60 "<" */ CSPCL_CWORD_CWORD_CWORD, |
| 2903 | /* 61 "=" */ CWORD_CCTL_CCTL_CWORD, |
| 2904 | /* 62 ">" */ CSPCL_CWORD_CWORD_CWORD, |
| 2905 | /* 63 "?" */ CWORD_CCTL_CCTL_CWORD, |
| 2906 | /* 64 "@" */ CWORD_CWORD_CWORD_CWORD, |
| 2907 | /* 65 "A" */ CWORD_CWORD_CWORD_CWORD, |
| 2908 | /* 66 "B" */ CWORD_CWORD_CWORD_CWORD, |
| 2909 | /* 67 "C" */ CWORD_CWORD_CWORD_CWORD, |
| 2910 | /* 68 "D" */ CWORD_CWORD_CWORD_CWORD, |
| 2911 | /* 69 "E" */ CWORD_CWORD_CWORD_CWORD, |
| 2912 | /* 70 "F" */ CWORD_CWORD_CWORD_CWORD, |
| 2913 | /* 71 "G" */ CWORD_CWORD_CWORD_CWORD, |
| 2914 | /* 72 "H" */ CWORD_CWORD_CWORD_CWORD, |
| 2915 | /* 73 "I" */ CWORD_CWORD_CWORD_CWORD, |
| 2916 | /* 74 "J" */ CWORD_CWORD_CWORD_CWORD, |
| 2917 | /* 75 "K" */ CWORD_CWORD_CWORD_CWORD, |
| 2918 | /* 76 "L" */ CWORD_CWORD_CWORD_CWORD, |
| 2919 | /* 77 "M" */ CWORD_CWORD_CWORD_CWORD, |
| 2920 | /* 78 "N" */ CWORD_CWORD_CWORD_CWORD, |
| 2921 | /* 79 "O" */ CWORD_CWORD_CWORD_CWORD, |
| 2922 | /* 80 "P" */ CWORD_CWORD_CWORD_CWORD, |
| 2923 | /* 81 "Q" */ CWORD_CWORD_CWORD_CWORD, |
| 2924 | /* 82 "R" */ CWORD_CWORD_CWORD_CWORD, |
| 2925 | /* 83 "S" */ CWORD_CWORD_CWORD_CWORD, |
| 2926 | /* 84 "T" */ CWORD_CWORD_CWORD_CWORD, |
| 2927 | /* 85 "U" */ CWORD_CWORD_CWORD_CWORD, |
| 2928 | /* 86 "V" */ CWORD_CWORD_CWORD_CWORD, |
| 2929 | /* 87 "W" */ CWORD_CWORD_CWORD_CWORD, |
| 2930 | /* 88 "X" */ CWORD_CWORD_CWORD_CWORD, |
| 2931 | /* 89 "Y" */ CWORD_CWORD_CWORD_CWORD, |
| 2932 | /* 90 "Z" */ CWORD_CWORD_CWORD_CWORD, |
| 2933 | /* 91 "[" */ CWORD_CCTL_CCTL_CWORD, |
| 2934 | /* 92 "\" */ CBACK_CBACK_CCTL_CBACK, |
| 2935 | /* 93 "]" */ CWORD_CCTL_CCTL_CWORD, |
| 2936 | /* 94 "^" */ CWORD_CWORD_CWORD_CWORD, |
| 2937 | /* 95 "_" */ CWORD_CWORD_CWORD_CWORD, |
| 2938 | /* 96 "`" */ CBQUOTE_CBQUOTE_CWORD_CBQUOTE, |
| 2939 | /* 97 "a" */ CWORD_CWORD_CWORD_CWORD, |
| 2940 | /* 98 "b" */ CWORD_CWORD_CWORD_CWORD, |
| 2941 | /* 99 "c" */ CWORD_CWORD_CWORD_CWORD, |
| 2942 | /* 100 "d" */ CWORD_CWORD_CWORD_CWORD, |
| 2943 | /* 101 "e" */ CWORD_CWORD_CWORD_CWORD, |
| 2944 | /* 102 "f" */ CWORD_CWORD_CWORD_CWORD, |
| 2945 | /* 103 "g" */ CWORD_CWORD_CWORD_CWORD, |
| 2946 | /* 104 "h" */ CWORD_CWORD_CWORD_CWORD, |
| 2947 | /* 105 "i" */ CWORD_CWORD_CWORD_CWORD, |
| 2948 | /* 106 "j" */ CWORD_CWORD_CWORD_CWORD, |
| 2949 | /* 107 "k" */ CWORD_CWORD_CWORD_CWORD, |
| 2950 | /* 108 "l" */ CWORD_CWORD_CWORD_CWORD, |
| 2951 | /* 109 "m" */ CWORD_CWORD_CWORD_CWORD, |
| 2952 | /* 110 "n" */ CWORD_CWORD_CWORD_CWORD, |
| 2953 | /* 111 "o" */ CWORD_CWORD_CWORD_CWORD, |
| 2954 | /* 112 "p" */ CWORD_CWORD_CWORD_CWORD, |
| 2955 | /* 113 "q" */ CWORD_CWORD_CWORD_CWORD, |
| 2956 | /* 114 "r" */ CWORD_CWORD_CWORD_CWORD, |
| 2957 | /* 115 "s" */ CWORD_CWORD_CWORD_CWORD, |
| 2958 | /* 116 "t" */ CWORD_CWORD_CWORD_CWORD, |
| 2959 | /* 117 "u" */ CWORD_CWORD_CWORD_CWORD, |
| 2960 | /* 118 "v" */ CWORD_CWORD_CWORD_CWORD, |
| 2961 | /* 119 "w" */ CWORD_CWORD_CWORD_CWORD, |
| 2962 | /* 120 "x" */ CWORD_CWORD_CWORD_CWORD, |
| 2963 | /* 121 "y" */ CWORD_CWORD_CWORD_CWORD, |
| 2964 | /* 122 "z" */ CWORD_CWORD_CWORD_CWORD, |
| 2965 | /* 123 "{" */ CWORD_CWORD_CWORD_CWORD, |
| 2966 | /* 124 "|" */ CSPCL_CWORD_CWORD_CWORD, |
| 2967 | /* 125 "}" */ CENDVAR_CENDVAR_CWORD_CENDVAR, |
| 2968 | /* 126 "~" */ CWORD_CCTL_CCTL_CWORD, |
| 2969 | /* 127 del */ CWORD_CWORD_CWORD_CWORD, |
| 2970 | /* 128 0x80 */ CWORD_CWORD_CWORD_CWORD, |
| 2971 | /* 129 CTLESC */ CCTL_CCTL_CCTL_CCTL, |
| 2972 | /* 130 CTLVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2973 | /* 131 CTLENDVAR */ CCTL_CCTL_CCTL_CCTL, |
| 2974 | /* 132 CTLBACKQ */ CCTL_CCTL_CCTL_CCTL, |
| 2975 | /* 133 CTLQUOTE */ CCTL_CCTL_CCTL_CCTL, |
| 2976 | /* 134 CTLARI */ CCTL_CCTL_CCTL_CCTL, |
| 2977 | /* 135 CTLENDARI */ CCTL_CCTL_CCTL_CCTL, |
| 2978 | /* 136 CTLQUOTEMARK */ CCTL_CCTL_CCTL_CCTL, |
| 2979 | /* 137 */ CWORD_CWORD_CWORD_CWORD, |
| 2980 | /* 138 */ CWORD_CWORD_CWORD_CWORD, |
| 2981 | /* 139 */ CWORD_CWORD_CWORD_CWORD, |
| 2982 | /* 140 */ CWORD_CWORD_CWORD_CWORD, |
| 2983 | /* 141 */ CWORD_CWORD_CWORD_CWORD, |
| 2984 | /* 142 */ CWORD_CWORD_CWORD_CWORD, |
| 2985 | /* 143 */ CWORD_CWORD_CWORD_CWORD, |
| 2986 | /* 144 */ CWORD_CWORD_CWORD_CWORD, |
| 2987 | /* 145 */ CWORD_CWORD_CWORD_CWORD, |
| 2988 | /* 146 */ CWORD_CWORD_CWORD_CWORD, |
| 2989 | /* 147 */ CWORD_CWORD_CWORD_CWORD, |
| 2990 | /* 148 */ CWORD_CWORD_CWORD_CWORD, |
| 2991 | /* 149 */ CWORD_CWORD_CWORD_CWORD, |
| 2992 | /* 150 */ CWORD_CWORD_CWORD_CWORD, |
| 2993 | /* 151 */ CWORD_CWORD_CWORD_CWORD, |
| 2994 | /* 152 */ CWORD_CWORD_CWORD_CWORD, |
| 2995 | /* 153 */ CWORD_CWORD_CWORD_CWORD, |
| 2996 | /* 154 */ CWORD_CWORD_CWORD_CWORD, |
| 2997 | /* 155 */ CWORD_CWORD_CWORD_CWORD, |
| 2998 | /* 156 */ CWORD_CWORD_CWORD_CWORD, |
| 2999 | /* 157 */ CWORD_CWORD_CWORD_CWORD, |
| 3000 | /* 158 */ CWORD_CWORD_CWORD_CWORD, |
| 3001 | /* 159 */ CWORD_CWORD_CWORD_CWORD, |
| 3002 | /* 160 */ CWORD_CWORD_CWORD_CWORD, |
| 3003 | /* 161 */ CWORD_CWORD_CWORD_CWORD, |
| 3004 | /* 162 */ CWORD_CWORD_CWORD_CWORD, |
| 3005 | /* 163 */ CWORD_CWORD_CWORD_CWORD, |
| 3006 | /* 164 */ CWORD_CWORD_CWORD_CWORD, |
| 3007 | /* 165 */ CWORD_CWORD_CWORD_CWORD, |
| 3008 | /* 166 */ CWORD_CWORD_CWORD_CWORD, |
| 3009 | /* 167 */ CWORD_CWORD_CWORD_CWORD, |
| 3010 | /* 168 */ CWORD_CWORD_CWORD_CWORD, |
| 3011 | /* 169 */ CWORD_CWORD_CWORD_CWORD, |
| 3012 | /* 170 */ CWORD_CWORD_CWORD_CWORD, |
| 3013 | /* 171 */ CWORD_CWORD_CWORD_CWORD, |
| 3014 | /* 172 */ CWORD_CWORD_CWORD_CWORD, |
| 3015 | /* 173 */ CWORD_CWORD_CWORD_CWORD, |
| 3016 | /* 174 */ CWORD_CWORD_CWORD_CWORD, |
| 3017 | /* 175 */ CWORD_CWORD_CWORD_CWORD, |
| 3018 | /* 176 */ CWORD_CWORD_CWORD_CWORD, |
| 3019 | /* 177 */ CWORD_CWORD_CWORD_CWORD, |
| 3020 | /* 178 */ CWORD_CWORD_CWORD_CWORD, |
| 3021 | /* 179 */ CWORD_CWORD_CWORD_CWORD, |
| 3022 | /* 180 */ CWORD_CWORD_CWORD_CWORD, |
| 3023 | /* 181 */ CWORD_CWORD_CWORD_CWORD, |
| 3024 | /* 182 */ CWORD_CWORD_CWORD_CWORD, |
| 3025 | /* 183 */ CWORD_CWORD_CWORD_CWORD, |
| 3026 | /* 184 */ CWORD_CWORD_CWORD_CWORD, |
| 3027 | /* 185 */ CWORD_CWORD_CWORD_CWORD, |
| 3028 | /* 186 */ CWORD_CWORD_CWORD_CWORD, |
| 3029 | /* 187 */ CWORD_CWORD_CWORD_CWORD, |
| 3030 | /* 188 */ CWORD_CWORD_CWORD_CWORD, |
| 3031 | /* 189 */ CWORD_CWORD_CWORD_CWORD, |
| 3032 | /* 190 */ CWORD_CWORD_CWORD_CWORD, |
| 3033 | /* 191 */ CWORD_CWORD_CWORD_CWORD, |
| 3034 | /* 192 */ CWORD_CWORD_CWORD_CWORD, |
| 3035 | /* 193 */ CWORD_CWORD_CWORD_CWORD, |
| 3036 | /* 194 */ CWORD_CWORD_CWORD_CWORD, |
| 3037 | /* 195 */ CWORD_CWORD_CWORD_CWORD, |
| 3038 | /* 196 */ CWORD_CWORD_CWORD_CWORD, |
| 3039 | /* 197 */ CWORD_CWORD_CWORD_CWORD, |
| 3040 | /* 198 */ CWORD_CWORD_CWORD_CWORD, |
| 3041 | /* 199 */ CWORD_CWORD_CWORD_CWORD, |
| 3042 | /* 200 */ CWORD_CWORD_CWORD_CWORD, |
| 3043 | /* 201 */ CWORD_CWORD_CWORD_CWORD, |
| 3044 | /* 202 */ CWORD_CWORD_CWORD_CWORD, |
| 3045 | /* 203 */ CWORD_CWORD_CWORD_CWORD, |
| 3046 | /* 204 */ CWORD_CWORD_CWORD_CWORD, |
| 3047 | /* 205 */ CWORD_CWORD_CWORD_CWORD, |
| 3048 | /* 206 */ CWORD_CWORD_CWORD_CWORD, |
| 3049 | /* 207 */ CWORD_CWORD_CWORD_CWORD, |
| 3050 | /* 208 */ CWORD_CWORD_CWORD_CWORD, |
| 3051 | /* 209 */ CWORD_CWORD_CWORD_CWORD, |
| 3052 | /* 210 */ CWORD_CWORD_CWORD_CWORD, |
| 3053 | /* 211 */ CWORD_CWORD_CWORD_CWORD, |
| 3054 | /* 212 */ CWORD_CWORD_CWORD_CWORD, |
| 3055 | /* 213 */ CWORD_CWORD_CWORD_CWORD, |
| 3056 | /* 214 */ CWORD_CWORD_CWORD_CWORD, |
| 3057 | /* 215 */ CWORD_CWORD_CWORD_CWORD, |
| 3058 | /* 216 */ CWORD_CWORD_CWORD_CWORD, |
| 3059 | /* 217 */ CWORD_CWORD_CWORD_CWORD, |
| 3060 | /* 218 */ CWORD_CWORD_CWORD_CWORD, |
| 3061 | /* 219 */ CWORD_CWORD_CWORD_CWORD, |
| 3062 | /* 220 */ CWORD_CWORD_CWORD_CWORD, |
| 3063 | /* 221 */ CWORD_CWORD_CWORD_CWORD, |
| 3064 | /* 222 */ CWORD_CWORD_CWORD_CWORD, |
| 3065 | /* 223 */ CWORD_CWORD_CWORD_CWORD, |
| 3066 | /* 224 */ CWORD_CWORD_CWORD_CWORD, |
| 3067 | /* 225 */ CWORD_CWORD_CWORD_CWORD, |
| 3068 | /* 226 */ CWORD_CWORD_CWORD_CWORD, |
| 3069 | /* 227 */ CWORD_CWORD_CWORD_CWORD, |
| 3070 | /* 228 */ CWORD_CWORD_CWORD_CWORD, |
| 3071 | /* 229 */ CWORD_CWORD_CWORD_CWORD, |
| 3072 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3073 | /* 231 */ CWORD_CWORD_CWORD_CWORD, |
| 3074 | /* 232 */ CWORD_CWORD_CWORD_CWORD, |
| 3075 | /* 233 */ CWORD_CWORD_CWORD_CWORD, |
| 3076 | /* 234 */ CWORD_CWORD_CWORD_CWORD, |
| 3077 | /* 235 */ CWORD_CWORD_CWORD_CWORD, |
| 3078 | /* 236 */ CWORD_CWORD_CWORD_CWORD, |
| 3079 | /* 237 */ CWORD_CWORD_CWORD_CWORD, |
| 3080 | /* 238 */ CWORD_CWORD_CWORD_CWORD, |
| 3081 | /* 239 */ CWORD_CWORD_CWORD_CWORD, |
| 3082 | /* 230 */ CWORD_CWORD_CWORD_CWORD, |
| 3083 | /* 241 */ CWORD_CWORD_CWORD_CWORD, |
| 3084 | /* 242 */ CWORD_CWORD_CWORD_CWORD, |
| 3085 | /* 243 */ CWORD_CWORD_CWORD_CWORD, |
| 3086 | /* 244 */ CWORD_CWORD_CWORD_CWORD, |
| 3087 | /* 245 */ CWORD_CWORD_CWORD_CWORD, |
| 3088 | /* 246 */ CWORD_CWORD_CWORD_CWORD, |
| 3089 | /* 247 */ CWORD_CWORD_CWORD_CWORD, |
| 3090 | /* 248 */ CWORD_CWORD_CWORD_CWORD, |
| 3091 | /* 249 */ CWORD_CWORD_CWORD_CWORD, |
| 3092 | /* 250 */ CWORD_CWORD_CWORD_CWORD, |
| 3093 | /* 251 */ CWORD_CWORD_CWORD_CWORD, |
| 3094 | /* 252 */ CWORD_CWORD_CWORD_CWORD, |
| 3095 | /* 253 */ CWORD_CWORD_CWORD_CWORD, |
| 3096 | /* 254 */ CWORD_CWORD_CWORD_CWORD, |
| 3097 | /* 255 */ CWORD_CWORD_CWORD_CWORD, |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3098 | /* PEOF */ CENDFILE_CENDFILE_CENDFILE_CENDFILE, |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 3099 | # if ENABLE_ASH_ALIAS |
| 3100 | /* PEOA */ CSPCL_CIGN_CIGN_CIGN, |
| 3101 | # endif |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3102 | }; |
| 3103 | |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 3104 | # 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] | 3105 | |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 3106 | #endif /* !USE_SIT_FUNCTION */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3107 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3108 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3109 | /* ============ Alias handling */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3110 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3111 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3112 | |
| 3113 | #define ALIASINUSE 1 |
| 3114 | #define ALIASDEAD 2 |
| 3115 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3116 | struct alias { |
| 3117 | struct alias *next; |
| 3118 | char *name; |
| 3119 | char *val; |
| 3120 | int flag; |
| 3121 | }; |
| 3122 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 3123 | |
| 3124 | static struct alias **atab; // [ATABSIZE]; |
| 3125 | #define INIT_G_alias() do { \ |
| 3126 | atab = xzalloc(ATABSIZE * sizeof(atab[0])); \ |
| 3127 | } while (0) |
| 3128 | |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3129 | |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 3130 | static struct alias ** |
| 3131 | __lookupalias(const char *name) { |
| 3132 | unsigned int hashval; |
| 3133 | struct alias **app; |
| 3134 | const char *p; |
| 3135 | unsigned int ch; |
| 3136 | |
| 3137 | p = name; |
| 3138 | |
| 3139 | ch = (unsigned char)*p; |
| 3140 | hashval = ch << 4; |
| 3141 | while (ch) { |
| 3142 | hashval += ch; |
| 3143 | ch = (unsigned char)*++p; |
| 3144 | } |
| 3145 | app = &atab[hashval % ATABSIZE]; |
| 3146 | |
| 3147 | for (; *app; app = &(*app)->next) { |
| 3148 | if (strcmp(name, (*app)->name) == 0) { |
| 3149 | break; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | return app; |
| 3154 | } |
| 3155 | |
| 3156 | static struct alias * |
| 3157 | lookupalias(const char *name, int check) |
| 3158 | { |
| 3159 | struct alias *ap = *__lookupalias(name); |
| 3160 | |
| 3161 | if (check && ap && (ap->flag & ALIASINUSE)) |
| 3162 | return NULL; |
| 3163 | return ap; |
| 3164 | } |
| 3165 | |
| 3166 | static struct alias * |
| 3167 | freealias(struct alias *ap) |
| 3168 | { |
| 3169 | struct alias *next; |
| 3170 | |
| 3171 | if (ap->flag & ALIASINUSE) { |
| 3172 | ap->flag |= ALIASDEAD; |
| 3173 | return ap; |
| 3174 | } |
| 3175 | |
| 3176 | next = ap->next; |
| 3177 | free(ap->name); |
| 3178 | free(ap->val); |
| 3179 | free(ap); |
| 3180 | return next; |
| 3181 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3182 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3183 | static void |
| 3184 | setalias(const char *name, const char *val) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3185 | { |
| 3186 | struct alias *ap, **app; |
| 3187 | |
| 3188 | app = __lookupalias(name); |
| 3189 | ap = *app; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3190 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3191 | if (ap) { |
| 3192 | if (!(ap->flag & ALIASINUSE)) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3193 | free(ap->val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3194 | } |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3195 | ap->val = ckstrdup(val); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3196 | ap->flag &= ~ALIASDEAD; |
| 3197 | } else { |
| 3198 | /* not found */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3199 | ap = ckzalloc(sizeof(struct alias)); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 3200 | ap->name = ckstrdup(name); |
| 3201 | ap->val = ckstrdup(val); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 3202 | /*ap->flag = 0; - ckzalloc did it */ |
| 3203 | /*ap->next = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3204 | *app = ap; |
| 3205 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3206 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3209 | static int |
| 3210 | unalias(const char *name) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3211 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3212 | struct alias **app; |
| 3213 | |
| 3214 | app = __lookupalias(name); |
| 3215 | |
| 3216 | if (*app) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3217 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3218 | *app = freealias(*app); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3219 | INT_ON; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3220 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3223 | return 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3226 | static void |
| 3227 | rmaliases(void) |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 3228 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3229 | struct alias *ap, **app; |
| 3230 | int i; |
| 3231 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3232 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3233 | for (i = 0; i < ATABSIZE; i++) { |
| 3234 | app = &atab[i]; |
| 3235 | for (ap = *app; ap; ap = *app) { |
| 3236 | *app = freealias(*app); |
| 3237 | if (ap == *app) { |
| 3238 | app = &ap->next; |
| 3239 | } |
| 3240 | } |
| 3241 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 3242 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 3245 | static void |
| 3246 | printalias(const struct alias *ap) |
| 3247 | { |
| 3248 | out1fmt("%s=%s\n", ap->name, single_quote(ap->val)); |
| 3249 | } |
| 3250 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3251 | /* |
| 3252 | * TODO - sort output |
| 3253 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3254 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3255 | aliascmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3256 | { |
| 3257 | char *n, *v; |
| 3258 | int ret = 0; |
| 3259 | struct alias *ap; |
| 3260 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3261 | if (!argv[1]) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3262 | int i; |
| 3263 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3264 | for (i = 0; i < ATABSIZE; i++) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3265 | for (ap = atab[i]; ap; ap = ap->next) { |
| 3266 | printalias(ap); |
| 3267 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3268 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3269 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3270 | } |
| 3271 | while ((n = *++argv) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 3272 | v = strchr(n+1, '='); |
| 3273 | if (v == NULL) { /* n+1: funny ksh stuff */ |
| 3274 | ap = *__lookupalias(n); |
| 3275 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3276 | fprintf(stderr, "%s: %s not found\n", "alias", n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3277 | ret = 1; |
| 3278 | } else |
| 3279 | printalias(ap); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 3280 | } else { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3281 | *v++ = '\0'; |
| 3282 | setalias(n, v); |
| 3283 | } |
| 3284 | } |
| 3285 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3286 | return ret; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3289 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3290 | unaliascmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3291 | { |
| 3292 | int i; |
| 3293 | |
| 3294 | while ((i = nextopt("a")) != '\0') { |
| 3295 | if (i == 'a') { |
| 3296 | rmaliases(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 3297 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | for (i = 0; *argptr; argptr++) { |
| 3301 | if (unalias(*argptr)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3302 | fprintf(stderr, "%s: %s not found\n", "unalias", *argptr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3303 | i = 1; |
| 3304 | } |
| 3305 | } |
| 3306 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 3307 | return i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3308 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 3309 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 3310 | #endif /* ASH_ALIAS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 3311 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 3312 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3313 | /* ============ jobs.c */ |
| 3314 | |
| 3315 | /* Mode argument to forkshell. Don't change FORK_FG or FORK_BG. */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3316 | #define FORK_FG 0 |
| 3317 | #define FORK_BG 1 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3318 | #define FORK_NOJOB 2 |
| 3319 | |
| 3320 | /* mode flags for showjob(s) */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 3321 | #define SHOW_ONLY_PGID 0x01 /* show only pgid (jobs -p) */ |
| 3322 | #define SHOW_PIDS 0x02 /* show individual pids, not just one line per job */ |
| 3323 | #define SHOW_CHANGED 0x04 /* only jobs whose state has changed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3324 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3325 | /* |
| 3326 | * A job structure contains information about a job. A job is either a |
| 3327 | * single process or a set of processes contained in a pipeline. In the |
| 3328 | * latter case, pidlist will be non-NULL, and will point to a -1 terminated |
| 3329 | * array of pids. |
| 3330 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3331 | struct procstat { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3332 | pid_t ps_pid; /* process id */ |
| 3333 | int ps_status; /* last process status from wait() */ |
| 3334 | char *ps_cmd; /* text of command being run */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3335 | }; |
| 3336 | |
| 3337 | struct job { |
| 3338 | struct procstat ps0; /* status of process */ |
| 3339 | struct procstat *ps; /* status or processes when more than one */ |
| 3340 | #if JOBS |
| 3341 | int stopstatus; /* status of a stopped job */ |
| 3342 | #endif |
| 3343 | uint32_t |
| 3344 | nprocs: 16, /* number of processes */ |
| 3345 | state: 8, |
| 3346 | #define JOBRUNNING 0 /* at least one proc running */ |
| 3347 | #define JOBSTOPPED 1 /* all procs are stopped */ |
| 3348 | #define JOBDONE 2 /* all procs are completed */ |
| 3349 | #if JOBS |
| 3350 | sigint: 1, /* job was killed by SIGINT */ |
| 3351 | jobctl: 1, /* job running under job control */ |
| 3352 | #endif |
| 3353 | waited: 1, /* true if this entry has been waited for */ |
| 3354 | used: 1, /* true if this entry is in used */ |
| 3355 | changed: 1; /* true if status has changed */ |
| 3356 | struct job *prev_job; /* previous job */ |
| 3357 | }; |
| 3358 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3359 | static struct job *makejob(/*union node *,*/ int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3360 | static int forkshell(struct job *, union node *, int); |
| 3361 | static int waitforjob(struct job *); |
| 3362 | |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3363 | #if !JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3364 | enum { doing_jobctl = 0 }; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 3365 | #define setjobctl(on) do {} while (0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3366 | #else |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3367 | static smallint doing_jobctl; //references:8 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3368 | static void setjobctl(int); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3369 | #endif |
| 3370 | |
| 3371 | /* |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3372 | * Ignore a signal. |
| 3373 | */ |
| 3374 | static void |
| 3375 | ignoresig(int signo) |
| 3376 | { |
| 3377 | /* Avoid unnecessary system calls. Is it already SIG_IGNed? */ |
| 3378 | if (sigmode[signo - 1] != S_IGN && sigmode[signo - 1] != S_HARD_IGN) { |
| 3379 | /* No, need to do it */ |
| 3380 | signal(signo, SIG_IGN); |
| 3381 | } |
| 3382 | sigmode[signo - 1] = S_HARD_IGN; |
| 3383 | } |
| 3384 | |
| 3385 | /* |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3386 | * Only one usage site - in setsignal() |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3387 | */ |
| 3388 | static void |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3389 | signal_handler(int signo) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3390 | { |
| 3391 | gotsig[signo - 1] = 1; |
| 3392 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3393 | if (signo == SIGINT && !trap[SIGINT]) { |
| 3394 | if (!suppress_int) { |
| 3395 | pending_sig = 0; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3396 | raise_interrupt(); /* does not return */ |
| 3397 | } |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3398 | pending_int = 1; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3399 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3400 | pending_sig = signo; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | /* |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3405 | * Set the signal handler for the specified signal. The routine figures |
| 3406 | * out what it should be set to. |
| 3407 | */ |
| 3408 | static void |
| 3409 | setsignal(int signo) |
| 3410 | { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3411 | char *t; |
| 3412 | char cur_act, new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3413 | struct sigaction act; |
| 3414 | |
| 3415 | t = trap[signo]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3416 | new_act = S_DFL; |
| 3417 | if (t != NULL) { /* trap for this sig is set */ |
| 3418 | new_act = S_CATCH; |
| 3419 | if (t[0] == '\0') /* trap is "": ignore this sig */ |
| 3420 | new_act = S_IGN; |
| 3421 | } |
| 3422 | |
| 3423 | if (rootshell && new_act == S_DFL) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3424 | switch (signo) { |
| 3425 | case SIGINT: |
| 3426 | if (iflag || minusc || sflag == 0) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3427 | new_act = S_CATCH; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3428 | break; |
| 3429 | case SIGQUIT: |
| 3430 | #if DEBUG |
| 3431 | if (debug) |
| 3432 | break; |
| 3433 | #endif |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3434 | /* man bash: |
| 3435 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 3436 | * commands run by bash have signal handlers |
| 3437 | * set to the values inherited by the shell |
| 3438 | * from its parent". */ |
| 3439 | new_act = S_IGN; |
| 3440 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3441 | case SIGTERM: |
| 3442 | if (iflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3443 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3444 | break; |
| 3445 | #if JOBS |
| 3446 | case SIGTSTP: |
| 3447 | case SIGTTOU: |
| 3448 | if (mflag) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3449 | new_act = S_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3450 | break; |
| 3451 | #endif |
| 3452 | } |
| 3453 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3454 | //TODO: if !rootshell, we reset SIGQUIT to DFL, |
| 3455 | //whereas we have to restore it to what shell got on entry |
| 3456 | //from the parent. See comment above |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3457 | |
| 3458 | t = &sigmode[signo - 1]; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3459 | cur_act = *t; |
| 3460 | if (cur_act == 0) { |
| 3461 | /* current setting is not yet known */ |
| 3462 | if (sigaction(signo, NULL, &act)) { |
| 3463 | /* pretend it worked; maybe we should give a warning, |
| 3464 | * but other shells don't. We don't alter sigmode, |
| 3465 | * so we retry every time. |
| 3466 | * btw, in Linux it never fails. --vda */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3467 | return; |
| 3468 | } |
| 3469 | if (act.sa_handler == SIG_IGN) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3470 | cur_act = S_HARD_IGN; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3471 | if (mflag |
| 3472 | && (signo == SIGTSTP || signo == SIGTTIN || signo == SIGTTOU) |
| 3473 | ) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3474 | cur_act = S_IGN; /* don't hard ignore these */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3475 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3476 | } |
| 3477 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3478 | if (cur_act == S_HARD_IGN || cur_act == new_act) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3479 | return; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3480 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 3481 | act.sa_handler = SIG_DFL; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3482 | switch (new_act) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3483 | case S_CATCH: |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 3484 | act.sa_handler = signal_handler; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3485 | act.sa_flags = 0; /* matters only if !DFL and !IGN */ |
| 3486 | sigfillset(&act.sa_mask); /* ditto */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3487 | break; |
| 3488 | case S_IGN: |
| 3489 | act.sa_handler = SIG_IGN; |
| 3490 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3491 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 3492 | sigaction_set(signo, &act); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3493 | |
| 3494 | *t = new_act; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | /* mode flags for set_curjob */ |
| 3498 | #define CUR_DELETE 2 |
| 3499 | #define CUR_RUNNING 1 |
| 3500 | #define CUR_STOPPED 0 |
| 3501 | |
| 3502 | /* mode flags for dowait */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3503 | #define DOWAIT_NONBLOCK WNOHANG |
| 3504 | #define DOWAIT_BLOCK 0 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3505 | |
| 3506 | #if JOBS |
| 3507 | /* pgrp of shell on invocation */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3508 | static int initialpgrp; //references:2 |
| 3509 | static int ttyfd = -1; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3510 | #endif |
| 3511 | /* array of jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3512 | static struct job *jobtab; //5 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3513 | /* size of array */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3514 | static unsigned njobs; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3515 | /* current job */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3516 | static struct job *curjob; //lots |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3517 | /* number of presumed living untracked jobs */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 3518 | static int jobless; //4 |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3519 | |
| 3520 | static void |
| 3521 | set_curjob(struct job *jp, unsigned mode) |
| 3522 | { |
| 3523 | struct job *jp1; |
| 3524 | struct job **jpp, **curp; |
| 3525 | |
| 3526 | /* first remove from list */ |
| 3527 | jpp = curp = &curjob; |
| 3528 | do { |
| 3529 | jp1 = *jpp; |
| 3530 | if (jp1 == jp) |
| 3531 | break; |
| 3532 | jpp = &jp1->prev_job; |
| 3533 | } while (1); |
| 3534 | *jpp = jp1->prev_job; |
| 3535 | |
| 3536 | /* Then re-insert in correct position */ |
| 3537 | jpp = curp; |
| 3538 | switch (mode) { |
| 3539 | default: |
| 3540 | #if DEBUG |
| 3541 | abort(); |
| 3542 | #endif |
| 3543 | case CUR_DELETE: |
| 3544 | /* job being deleted */ |
| 3545 | break; |
| 3546 | case CUR_RUNNING: |
| 3547 | /* newly created job or backgrounded job, |
| 3548 | put after all stopped jobs. */ |
| 3549 | do { |
| 3550 | jp1 = *jpp; |
| 3551 | #if JOBS |
| 3552 | if (!jp1 || jp1->state != JOBSTOPPED) |
| 3553 | #endif |
| 3554 | break; |
| 3555 | jpp = &jp1->prev_job; |
| 3556 | } while (1); |
| 3557 | /* FALLTHROUGH */ |
| 3558 | #if JOBS |
| 3559 | case CUR_STOPPED: |
| 3560 | #endif |
| 3561 | /* newly stopped job - becomes curjob */ |
| 3562 | jp->prev_job = *jpp; |
| 3563 | *jpp = jp; |
| 3564 | break; |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | #if JOBS || DEBUG |
| 3569 | static int |
| 3570 | jobno(const struct job *jp) |
| 3571 | { |
| 3572 | return jp - jobtab + 1; |
| 3573 | } |
| 3574 | #endif |
| 3575 | |
| 3576 | /* |
| 3577 | * Convert a job name to a job structure. |
| 3578 | */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 3579 | #if !JOBS |
| 3580 | #define getjob(name, getctl) getjob(name) |
| 3581 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3582 | static struct job * |
| 3583 | getjob(const char *name, int getctl) |
| 3584 | { |
| 3585 | struct job *jp; |
| 3586 | struct job *found; |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3587 | const char *err_msg = "%s: no such job"; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3588 | unsigned num; |
| 3589 | int c; |
| 3590 | const char *p; |
| 3591 | char *(*match)(const char *, const char *); |
| 3592 | |
| 3593 | jp = curjob; |
| 3594 | p = name; |
| 3595 | if (!p) |
| 3596 | goto currentjob; |
| 3597 | |
| 3598 | if (*p != '%') |
| 3599 | goto err; |
| 3600 | |
| 3601 | c = *++p; |
| 3602 | if (!c) |
| 3603 | goto currentjob; |
| 3604 | |
| 3605 | if (!p[1]) { |
| 3606 | if (c == '+' || c == '%') { |
| 3607 | currentjob: |
| 3608 | err_msg = "No current job"; |
| 3609 | goto check; |
| 3610 | } |
| 3611 | if (c == '-') { |
| 3612 | if (jp) |
| 3613 | jp = jp->prev_job; |
| 3614 | err_msg = "No previous job"; |
| 3615 | check: |
| 3616 | if (!jp) |
| 3617 | goto err; |
| 3618 | goto gotit; |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | if (is_number(p)) { |
| 3623 | num = atoi(p); |
| 3624 | if (num < njobs) { |
| 3625 | jp = jobtab + num - 1; |
| 3626 | if (jp->used) |
| 3627 | goto gotit; |
| 3628 | goto err; |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | match = prefix; |
| 3633 | if (*p == '?') { |
| 3634 | match = strstr; |
| 3635 | p++; |
| 3636 | } |
| 3637 | |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3638 | found = NULL; |
| 3639 | while (jp) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3640 | if (match(jp->ps[0].ps_cmd, p)) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3641 | if (found) |
| 3642 | goto err; |
| 3643 | found = jp; |
| 3644 | err_msg = "%s: ambiguous"; |
| 3645 | } |
| 3646 | jp = jp->prev_job; |
| 3647 | } |
Denys Vlasenko | ffc3920 | 2009-08-17 02:12:20 +0200 | [diff] [blame] | 3648 | if (!found) |
| 3649 | goto err; |
| 3650 | jp = found; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3651 | |
| 3652 | gotit: |
| 3653 | #if JOBS |
| 3654 | err_msg = "job %s not created under job control"; |
| 3655 | if (getctl && jp->jobctl == 0) |
| 3656 | goto err; |
| 3657 | #endif |
| 3658 | return jp; |
| 3659 | err: |
| 3660 | ash_msg_and_raise_error(err_msg, name); |
| 3661 | } |
| 3662 | |
| 3663 | /* |
| 3664 | * Mark a job structure as unused. |
| 3665 | */ |
| 3666 | static void |
| 3667 | freejob(struct job *jp) |
| 3668 | { |
| 3669 | struct procstat *ps; |
| 3670 | int i; |
| 3671 | |
| 3672 | INT_OFF; |
| 3673 | for (i = jp->nprocs, ps = jp->ps; --i >= 0; ps++) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3674 | if (ps->ps_cmd != nullstr) |
| 3675 | free(ps->ps_cmd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3676 | } |
| 3677 | if (jp->ps != &jp->ps0) |
| 3678 | free(jp->ps); |
| 3679 | jp->used = 0; |
| 3680 | set_curjob(jp, CUR_DELETE); |
| 3681 | INT_ON; |
| 3682 | } |
| 3683 | |
| 3684 | #if JOBS |
| 3685 | static void |
| 3686 | xtcsetpgrp(int fd, pid_t pgrp) |
| 3687 | { |
| 3688 | if (tcsetpgrp(fd, pgrp)) |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 3689 | ash_msg_and_raise_error("can't set tty process group (%m)"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /* |
| 3693 | * Turn job control on and off. |
| 3694 | * |
| 3695 | * Note: This code assumes that the third arg to ioctl is a character |
| 3696 | * pointer, which is true on Berkeley systems but not System V. Since |
| 3697 | * System V doesn't have job control yet, this isn't a problem now. |
| 3698 | * |
| 3699 | * Called with interrupts off. |
| 3700 | */ |
| 3701 | static void |
| 3702 | setjobctl(int on) |
| 3703 | { |
| 3704 | int fd; |
| 3705 | int pgrp; |
| 3706 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3707 | if (on == doing_jobctl || rootshell == 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3708 | return; |
| 3709 | if (on) { |
| 3710 | int ofd; |
| 3711 | ofd = fd = open(_PATH_TTY, O_RDWR); |
| 3712 | if (fd < 0) { |
| 3713 | /* BTW, bash will try to open(ttyname(0)) if open("/dev/tty") fails. |
| 3714 | * That sometimes helps to acquire controlling tty. |
| 3715 | * Obviously, a workaround for bugs when someone |
| 3716 | * failed to provide a controlling tty to bash! :) */ |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3717 | fd = 2; |
| 3718 | while (!isatty(fd)) |
| 3719 | if (--fd < 0) |
| 3720 | goto out; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3721 | } |
| 3722 | fd = fcntl(fd, F_DUPFD, 10); |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3723 | if (ofd >= 0) |
| 3724 | close(ofd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3725 | if (fd < 0) |
| 3726 | goto out; |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3727 | /* fd is a tty at this point */ |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 3728 | close_on_exec_on(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3729 | do { /* while we are in the background */ |
| 3730 | pgrp = tcgetpgrp(fd); |
| 3731 | if (pgrp < 0) { |
| 3732 | out: |
| 3733 | ash_msg("can't access tty; job control turned off"); |
| 3734 | mflag = on = 0; |
| 3735 | goto close; |
| 3736 | } |
| 3737 | if (pgrp == getpgrp()) |
| 3738 | break; |
| 3739 | killpg(0, SIGTTIN); |
| 3740 | } while (1); |
| 3741 | initialpgrp = pgrp; |
| 3742 | |
| 3743 | setsignal(SIGTSTP); |
| 3744 | setsignal(SIGTTOU); |
| 3745 | setsignal(SIGTTIN); |
| 3746 | pgrp = rootpid; |
| 3747 | setpgid(0, pgrp); |
| 3748 | xtcsetpgrp(fd, pgrp); |
| 3749 | } else { |
| 3750 | /* turning job control off */ |
| 3751 | fd = ttyfd; |
| 3752 | pgrp = initialpgrp; |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3753 | /* was xtcsetpgrp, but this can make exiting ash |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3754 | * loop forever if pty is already deleted */ |
Denis Vlasenko | 08c8c1d | 2007-04-28 22:39:02 +0000 | [diff] [blame] | 3755 | tcsetpgrp(fd, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3756 | setpgid(0, pgrp); |
| 3757 | setsignal(SIGTSTP); |
| 3758 | setsignal(SIGTTOU); |
| 3759 | setsignal(SIGTTIN); |
| 3760 | close: |
Denis Vlasenko | ed270a5 | 2007-11-26 05:37:07 +0000 | [diff] [blame] | 3761 | if (fd >= 0) |
| 3762 | close(fd); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3763 | fd = -1; |
| 3764 | } |
| 3765 | ttyfd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 3766 | doing_jobctl = on; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3769 | static int FAST_FUNC |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3770 | killcmd(int argc, char **argv) |
| 3771 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 3772 | int i = 1; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3773 | if (argv[1] && strcmp(argv[1], "-l") != 0) { |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3774 | do { |
| 3775 | if (argv[i][0] == '%') { |
| 3776 | struct job *jp = getjob(argv[i], 0); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3777 | unsigned pid = jp->ps[0].ps_pid; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3778 | /* Enough space for ' -NNN<nul>' */ |
| 3779 | argv[i] = alloca(sizeof(int)*3 + 3); |
| 3780 | /* kill_main has matching code to expect |
| 3781 | * leading space. Needed to not confuse |
| 3782 | * negative pids with "kill -SIGNAL_NO" syntax */ |
| 3783 | sprintf(argv[i], " -%u", pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3784 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3785 | } while (argv[++i]); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3786 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3787 | return kill_main(argc, argv); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | static void |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3791 | showpipe(struct job *jp /*, FILE *out*/) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3792 | { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3793 | struct procstat *ps; |
| 3794 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3795 | |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3796 | psend = jp->ps + jp->nprocs; |
| 3797 | for (ps = jp->ps + 1; ps < psend; ps++) |
| 3798 | printf(" | %s", ps->ps_cmd); |
| 3799 | outcslow('\n', stdout); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3800 | flush_stdout_stderr(); |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | static int |
| 3805 | restartjob(struct job *jp, int mode) |
| 3806 | { |
| 3807 | struct procstat *ps; |
| 3808 | int i; |
| 3809 | int status; |
| 3810 | pid_t pgid; |
| 3811 | |
| 3812 | INT_OFF; |
| 3813 | if (jp->state == JOBDONE) |
| 3814 | goto out; |
| 3815 | jp->state = JOBRUNNING; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3816 | pgid = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3817 | if (mode == FORK_FG) |
| 3818 | xtcsetpgrp(ttyfd, pgid); |
| 3819 | killpg(pgid, SIGCONT); |
| 3820 | ps = jp->ps; |
| 3821 | i = jp->nprocs; |
| 3822 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3823 | if (WIFSTOPPED(ps->ps_status)) { |
| 3824 | ps->ps_status = -1; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3825 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 3826 | ps++; |
| 3827 | } while (--i); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3828 | out: |
| 3829 | status = (mode == FORK_FG) ? waitforjob(jp) : 0; |
| 3830 | INT_ON; |
| 3831 | return status; |
| 3832 | } |
| 3833 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 3834 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 3835 | fg_bgcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3836 | { |
| 3837 | struct job *jp; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3838 | int mode; |
| 3839 | int retval; |
| 3840 | |
| 3841 | mode = (**argv == 'f') ? FORK_FG : FORK_BG; |
| 3842 | nextopt(nullstr); |
| 3843 | argv = argptr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3844 | do { |
| 3845 | jp = getjob(*argv, 1); |
| 3846 | if (mode == FORK_BG) { |
| 3847 | set_curjob(jp, CUR_RUNNING); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3848 | printf("[%d] ", jobno(jp)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3849 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3850 | out1str(jp->ps[0].ps_cmd); |
| 3851 | showpipe(jp /*, stdout*/); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3852 | retval = restartjob(jp, mode); |
| 3853 | } while (*argv && *++argv); |
| 3854 | return retval; |
| 3855 | } |
| 3856 | #endif |
| 3857 | |
| 3858 | static int |
| 3859 | sprint_status(char *s, int status, int sigonly) |
| 3860 | { |
| 3861 | int col; |
| 3862 | int st; |
| 3863 | |
| 3864 | col = 0; |
| 3865 | if (!WIFEXITED(status)) { |
| 3866 | #if JOBS |
| 3867 | if (WIFSTOPPED(status)) |
| 3868 | st = WSTOPSIG(status); |
| 3869 | else |
| 3870 | #endif |
| 3871 | st = WTERMSIG(status); |
| 3872 | if (sigonly) { |
| 3873 | if (st == SIGINT || st == SIGPIPE) |
| 3874 | goto out; |
| 3875 | #if JOBS |
| 3876 | if (WIFSTOPPED(status)) |
| 3877 | goto out; |
| 3878 | #endif |
| 3879 | } |
| 3880 | st &= 0x7f; |
| 3881 | col = fmtstr(s, 32, strsignal(st)); |
| 3882 | if (WCOREDUMP(status)) { |
| 3883 | col += fmtstr(s + col, 16, " (core dumped)"); |
| 3884 | } |
| 3885 | } else if (!sigonly) { |
| 3886 | st = WEXITSTATUS(status); |
| 3887 | if (st) |
| 3888 | col = fmtstr(s, 16, "Done(%d)", st); |
| 3889 | else |
| 3890 | col = fmtstr(s, 16, "Done"); |
| 3891 | } |
| 3892 | out: |
| 3893 | return col; |
| 3894 | } |
| 3895 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3896 | static int |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3897 | dowait(int wait_flags, struct job *job) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3898 | { |
| 3899 | int pid; |
| 3900 | int status; |
| 3901 | struct job *jp; |
| 3902 | struct job *thisjob; |
| 3903 | int state; |
| 3904 | |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 3905 | TRACE(("dowait(0x%x) called\n", wait_flags)); |
| 3906 | |
| 3907 | /* Do a wait system call. If job control is compiled in, we accept |
| 3908 | * stopped processes. wait_flags may have WNOHANG, preventing blocking. |
| 3909 | * NB: _not_ safe_waitpid, we need to detect EINTR */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3910 | if (doing_jobctl) |
| 3911 | wait_flags |= WUNTRACED; |
| 3912 | pid = waitpid(-1, &status, wait_flags); |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 3913 | TRACE(("wait returns pid=%d, status=0x%x, errno=%d(%s)\n", |
| 3914 | pid, status, errno, strerror(errno))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3915 | if (pid <= 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3916 | return pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3917 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3918 | INT_OFF; |
| 3919 | thisjob = NULL; |
| 3920 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3921 | struct procstat *ps; |
| 3922 | struct procstat *psend; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3923 | if (jp->state == JOBDONE) |
| 3924 | continue; |
| 3925 | state = JOBDONE; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3926 | ps = jp->ps; |
| 3927 | psend = ps + jp->nprocs; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3928 | do { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3929 | if (ps->ps_pid == pid) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3930 | TRACE(("Job %d: changing status of proc %d " |
| 3931 | "from 0x%x to 0x%x\n", |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3932 | jobno(jp), pid, ps->ps_status, status)); |
| 3933 | ps->ps_status = status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3934 | thisjob = jp; |
| 3935 | } |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3936 | if (ps->ps_status == -1) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3937 | state = JOBRUNNING; |
| 3938 | #if JOBS |
| 3939 | if (state == JOBRUNNING) |
| 3940 | continue; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3941 | if (WIFSTOPPED(ps->ps_status)) { |
| 3942 | jp->stopstatus = ps->ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3943 | state = JOBSTOPPED; |
| 3944 | } |
| 3945 | #endif |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 3946 | } while (++ps < psend); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3947 | if (thisjob) |
| 3948 | goto gotjob; |
| 3949 | } |
| 3950 | #if JOBS |
| 3951 | if (!WIFSTOPPED(status)) |
| 3952 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3953 | jobless--; |
| 3954 | goto out; |
| 3955 | |
| 3956 | gotjob: |
| 3957 | if (state != JOBRUNNING) { |
| 3958 | thisjob->changed = 1; |
| 3959 | |
| 3960 | if (thisjob->state != state) { |
| 3961 | TRACE(("Job %d: changing state from %d to %d\n", |
| 3962 | jobno(thisjob), thisjob->state, state)); |
| 3963 | thisjob->state = state; |
| 3964 | #if JOBS |
| 3965 | if (state == JOBSTOPPED) { |
| 3966 | set_curjob(thisjob, CUR_STOPPED); |
| 3967 | } |
| 3968 | #endif |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | out: |
| 3973 | INT_ON; |
| 3974 | |
| 3975 | if (thisjob && thisjob == job) { |
| 3976 | char s[48 + 1]; |
| 3977 | int len; |
| 3978 | |
| 3979 | len = sprint_status(s, status, 1); |
| 3980 | if (len) { |
| 3981 | s[len] = '\n'; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 3982 | s[len + 1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3983 | out2str(s); |
| 3984 | } |
| 3985 | } |
| 3986 | return pid; |
| 3987 | } |
| 3988 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3989 | static int |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3990 | blocking_wait_with_raise_on_sig(void) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3991 | { |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 3992 | pid_t pid = dowait(DOWAIT_BLOCK, NULL); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 3993 | if (pid <= 0 && pending_sig) |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 3994 | raise_exception(EXSIG); |
| 3995 | return pid; |
| 3996 | } |
| 3997 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 3998 | #if JOBS |
| 3999 | static void |
| 4000 | showjob(FILE *out, struct job *jp, int mode) |
| 4001 | { |
| 4002 | struct procstat *ps; |
| 4003 | struct procstat *psend; |
| 4004 | int col; |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4005 | int indent_col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4006 | char s[80]; |
| 4007 | |
| 4008 | ps = jp->ps; |
| 4009 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4010 | if (mode & SHOW_ONLY_PGID) { /* jobs -p */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4011 | /* just output process (group) id of pipeline */ |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4012 | fprintf(out, "%d\n", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4013 | return; |
| 4014 | } |
| 4015 | |
| 4016 | col = fmtstr(s, 16, "[%d] ", jobno(jp)); |
Denis Vlasenko | 40ba998 | 2007-07-14 00:48:29 +0000 | [diff] [blame] | 4017 | indent_col = col; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4018 | |
| 4019 | if (jp == curjob) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4020 | s[col - 3] = '+'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4021 | else if (curjob && jp == curjob->prev_job) |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4022 | s[col - 3] = '-'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4023 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4024 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4025 | col += fmtstr(s + col, 16, "%d ", ps->ps_pid); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4026 | |
| 4027 | psend = ps + jp->nprocs; |
| 4028 | |
| 4029 | if (jp->state == JOBRUNNING) { |
| 4030 | strcpy(s + col, "Running"); |
| 4031 | col += sizeof("Running") - 1; |
| 4032 | } else { |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4033 | int status = psend[-1].ps_status; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4034 | if (jp->state == JOBSTOPPED) |
| 4035 | status = jp->stopstatus; |
| 4036 | col += sprint_status(s + col, status, 0); |
| 4037 | } |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4038 | /* By now, "[JOBID]* [maybe PID] STATUS" is printed */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4039 | |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4040 | /* This loop either prints "<cmd1> | <cmd2> | <cmd3>" line |
| 4041 | * or prints several "PID | <cmdN>" lines, |
| 4042 | * depending on SHOW_PIDS bit. |
| 4043 | * We do not print status of individual processes |
| 4044 | * between PID and <cmdN>. bash does it, but not very well: |
| 4045 | * first line shows overall job status, not process status, |
| 4046 | * making it impossible to know 1st process status. |
| 4047 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4048 | goto start; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4049 | do { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4050 | /* for each process */ |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4051 | s[0] = '\0'; |
| 4052 | col = 33; |
| 4053 | if (mode & SHOW_PIDS) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4054 | 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] | 4055 | start: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4056 | fprintf(out, "%s%*c%s%s", |
| 4057 | s, |
| 4058 | 33 - col >= 0 ? 33 - col : 0, ' ', |
| 4059 | ps == jp->ps ? "" : "| ", |
| 4060 | ps->ps_cmd |
| 4061 | ); |
| 4062 | } while (++ps != psend); |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4063 | outcslow('\n', out); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4064 | |
| 4065 | jp->changed = 0; |
| 4066 | |
| 4067 | if (jp->state == JOBDONE) { |
| 4068 | TRACE(("showjob: freeing job %d\n", jobno(jp))); |
| 4069 | freejob(jp); |
| 4070 | } |
| 4071 | } |
| 4072 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4073 | /* |
| 4074 | * Print a list of jobs. If "change" is nonzero, only print jobs whose |
| 4075 | * statuses have changed since the last call to showjobs. |
| 4076 | */ |
| 4077 | static void |
| 4078 | showjobs(FILE *out, int mode) |
| 4079 | { |
| 4080 | struct job *jp; |
| 4081 | |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4082 | TRACE(("showjobs(0x%x) called\n", mode)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4083 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4084 | /* Handle all finished jobs */ |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4085 | while (dowait(DOWAIT_NONBLOCK, NULL) > 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4086 | continue; |
| 4087 | |
| 4088 | for (jp = curjob; jp; jp = jp->prev_job) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4089 | if (!(mode & SHOW_CHANGED) || jp->changed) { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4090 | showjob(out, jp, mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4091 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4092 | } |
| 4093 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4094 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4095 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4096 | jobscmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4097 | { |
| 4098 | int mode, m; |
| 4099 | |
| 4100 | mode = 0; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4101 | while ((m = nextopt("lp")) != '\0') { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4102 | if (m == 'l') |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4103 | mode |= SHOW_PIDS; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4104 | else |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4105 | mode |= SHOW_ONLY_PGID; |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | argv = argptr; |
| 4109 | if (*argv) { |
| 4110 | do |
Denys Vlasenko | a12af2d | 2009-08-23 22:10:04 +0200 | [diff] [blame] | 4111 | showjob(stdout, getjob(*argv, 0), mode); |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4112 | while (*++argv); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4113 | } else { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4114 | showjobs(stdout, mode); |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4115 | } |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4116 | |
| 4117 | return 0; |
| 4118 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4119 | #endif /* JOBS */ |
| 4120 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4121 | /* Called only on finished or stopped jobs (no members are running) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4122 | static int |
| 4123 | getstatus(struct job *job) |
| 4124 | { |
| 4125 | int status; |
| 4126 | int retval; |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4127 | struct procstat *ps; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4128 | |
Michael Abbott | 359da5e | 2009-12-04 23:03:29 +0100 | [diff] [blame] | 4129 | /* Fetch last member's status */ |
| 4130 | ps = job->ps + job->nprocs - 1; |
| 4131 | status = ps->ps_status; |
| 4132 | if (pipefail) { |
| 4133 | /* "set -o pipefail" mode: use last _nonzero_ status */ |
| 4134 | while (status == 0 && --ps >= job->ps) |
| 4135 | status = ps->ps_status; |
| 4136 | } |
| 4137 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4138 | retval = WEXITSTATUS(status); |
| 4139 | if (!WIFEXITED(status)) { |
| 4140 | #if JOBS |
| 4141 | retval = WSTOPSIG(status); |
| 4142 | if (!WIFSTOPPED(status)) |
| 4143 | #endif |
| 4144 | { |
| 4145 | /* XXX: limits number of signals */ |
| 4146 | retval = WTERMSIG(status); |
| 4147 | #if JOBS |
| 4148 | if (retval == SIGINT) |
| 4149 | job->sigint = 1; |
| 4150 | #endif |
| 4151 | } |
| 4152 | retval += 128; |
| 4153 | } |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 4154 | 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] | 4155 | jobno(job), job->nprocs, status, retval)); |
| 4156 | return retval; |
| 4157 | } |
| 4158 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 4159 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 4160 | waitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4161 | { |
| 4162 | struct job *job; |
| 4163 | int retval; |
| 4164 | struct job *jp; |
| 4165 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4166 | if (pending_sig) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4167 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4168 | |
| 4169 | nextopt(nullstr); |
| 4170 | retval = 0; |
| 4171 | |
| 4172 | argv = argptr; |
| 4173 | if (!*argv) { |
| 4174 | /* wait for all jobs */ |
| 4175 | for (;;) { |
| 4176 | jp = curjob; |
| 4177 | while (1) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4178 | if (!jp) /* no running procs */ |
| 4179 | goto ret; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4180 | if (jp->state == JOBRUNNING) |
| 4181 | break; |
| 4182 | jp->waited = 1; |
| 4183 | jp = jp->prev_job; |
| 4184 | } |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4185 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4186 | /* man bash: |
| 4187 | * "When bash is waiting for an asynchronous command via |
| 4188 | * the wait builtin, the reception of a signal for which a trap |
| 4189 | * has been set will cause the wait builtin to return immediately |
| 4190 | * with an exit status greater than 128, immediately after which |
| 4191 | * the trap is executed." |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4192 | * |
| 4193 | * blocking_wait_with_raise_on_sig raises signal handlers |
| 4194 | * if it gets no pid (pid < 0). However, |
| 4195 | * if child sends us a signal *and immediately exits*, |
| 4196 | * blocking_wait_with_raise_on_sig gets pid > 0 |
| 4197 | * and does not handle pending_sig. Check this case: */ |
| 4198 | if (pending_sig) |
| 4199 | raise_exception(EXSIG); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4200 | } |
| 4201 | } |
| 4202 | |
| 4203 | retval = 127; |
| 4204 | do { |
| 4205 | if (**argv != '%') { |
| 4206 | pid_t pid = number(*argv); |
| 4207 | job = curjob; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4208 | while (1) { |
| 4209 | if (!job) |
| 4210 | goto repeat; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4211 | if (job->ps[job->nprocs - 1].ps_pid == pid) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4212 | break; |
| 4213 | job = job->prev_job; |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4214 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4215 | } else |
| 4216 | job = getjob(*argv, 0); |
| 4217 | /* loop until process terminated or stopped */ |
| 4218 | while (job->state == JOBRUNNING) |
Denys Vlasenko | 7c1ed9f | 2010-05-17 04:42:40 +0200 | [diff] [blame] | 4219 | blocking_wait_with_raise_on_sig(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4220 | job->waited = 1; |
| 4221 | retval = getstatus(job); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4222 | repeat: ; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4223 | } while (*++argv); |
| 4224 | |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4225 | ret: |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4226 | return retval; |
| 4227 | } |
| 4228 | |
| 4229 | static struct job * |
| 4230 | growjobtab(void) |
| 4231 | { |
| 4232 | size_t len; |
| 4233 | ptrdiff_t offset; |
| 4234 | struct job *jp, *jq; |
| 4235 | |
| 4236 | len = njobs * sizeof(*jp); |
| 4237 | jq = jobtab; |
| 4238 | jp = ckrealloc(jq, len + 4 * sizeof(*jp)); |
| 4239 | |
| 4240 | offset = (char *)jp - (char *)jq; |
| 4241 | if (offset) { |
| 4242 | /* Relocate pointers */ |
| 4243 | size_t l = len; |
| 4244 | |
| 4245 | jq = (struct job *)((char *)jq + l); |
| 4246 | while (l) { |
| 4247 | l -= sizeof(*jp); |
| 4248 | jq--; |
| 4249 | #define joff(p) ((struct job *)((char *)(p) + l)) |
| 4250 | #define jmove(p) (p) = (void *)((char *)(p) + offset) |
| 4251 | if (joff(jp)->ps == &jq->ps0) |
| 4252 | jmove(joff(jp)->ps); |
| 4253 | if (joff(jp)->prev_job) |
| 4254 | jmove(joff(jp)->prev_job); |
| 4255 | } |
| 4256 | if (curjob) |
| 4257 | jmove(curjob); |
| 4258 | #undef joff |
| 4259 | #undef jmove |
| 4260 | } |
| 4261 | |
| 4262 | njobs += 4; |
| 4263 | jobtab = jp; |
| 4264 | jp = (struct job *)((char *)jp + len); |
| 4265 | jq = jp + 3; |
| 4266 | do { |
| 4267 | jq->used = 0; |
| 4268 | } while (--jq >= jp); |
| 4269 | return jp; |
| 4270 | } |
| 4271 | |
| 4272 | /* |
| 4273 | * Return a new job structure. |
| 4274 | * Called with interrupts off. |
| 4275 | */ |
| 4276 | static struct job * |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4277 | makejob(/*union node *node,*/ int nprocs) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4278 | { |
| 4279 | int i; |
| 4280 | struct job *jp; |
| 4281 | |
| 4282 | for (i = njobs, jp = jobtab; ; jp++) { |
| 4283 | if (--i < 0) { |
| 4284 | jp = growjobtab(); |
| 4285 | break; |
| 4286 | } |
| 4287 | if (jp->used == 0) |
| 4288 | break; |
| 4289 | if (jp->state != JOBDONE || !jp->waited) |
| 4290 | continue; |
| 4291 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4292 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4293 | continue; |
| 4294 | #endif |
| 4295 | freejob(jp); |
| 4296 | break; |
| 4297 | } |
| 4298 | memset(jp, 0, sizeof(*jp)); |
| 4299 | #if JOBS |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 4300 | /* jp->jobctl is a bitfield. |
| 4301 | * "jp->jobctl |= jobctl" likely to give awful code */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4302 | if (doing_jobctl) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4303 | jp->jobctl = 1; |
| 4304 | #endif |
| 4305 | jp->prev_job = curjob; |
| 4306 | curjob = jp; |
| 4307 | jp->used = 1; |
| 4308 | jp->ps = &jp->ps0; |
| 4309 | if (nprocs > 1) { |
| 4310 | jp->ps = ckmalloc(nprocs * sizeof(struct procstat)); |
| 4311 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 4312 | TRACE(("makejob(%d) returns %%%d\n", nprocs, |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4313 | jobno(jp))); |
| 4314 | return jp; |
| 4315 | } |
| 4316 | |
| 4317 | #if JOBS |
| 4318 | /* |
| 4319 | * Return a string identifying a command (to be printed by the |
| 4320 | * jobs command). |
| 4321 | */ |
| 4322 | static char *cmdnextc; |
| 4323 | |
| 4324 | static void |
| 4325 | cmdputs(const char *s) |
| 4326 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4327 | static const char vstype[VSTYPE + 1][3] = { |
| 4328 | "", "}", "-", "+", "?", "=", |
| 4329 | "%", "%%", "#", "##" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4330 | IF_ASH_BASH_COMPAT(, ":", "/", "//") |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 4331 | }; |
| 4332 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4333 | const char *p, *str; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4334 | char cc[2]; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4335 | char *nextc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 4336 | unsigned char c; |
| 4337 | unsigned char subtype = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4338 | int quoted = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4339 | |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4340 | cc[1] = '\0'; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4341 | nextc = makestrspace((strlen(s) + 1) * 8, cmdnextc); |
| 4342 | p = s; |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4343 | while ((c = *p++) != '\0') { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 4344 | str = NULL; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4345 | switch (c) { |
| 4346 | case CTLESC: |
| 4347 | c = *p++; |
| 4348 | break; |
| 4349 | case CTLVAR: |
| 4350 | subtype = *p++; |
| 4351 | if ((subtype & VSTYPE) == VSLENGTH) |
| 4352 | str = "${#"; |
| 4353 | else |
| 4354 | str = "${"; |
| 4355 | if (!(subtype & VSQUOTE) == !(quoted & 1)) |
| 4356 | goto dostr; |
| 4357 | quoted ^= 1; |
| 4358 | c = '"'; |
| 4359 | break; |
| 4360 | case CTLENDVAR: |
| 4361 | str = "\"}" + !(quoted & 1); |
| 4362 | quoted >>= 1; |
| 4363 | subtype = 0; |
| 4364 | goto dostr; |
| 4365 | case CTLBACKQ: |
| 4366 | str = "$(...)"; |
| 4367 | goto dostr; |
| 4368 | case CTLBACKQ+CTLQUOTE: |
| 4369 | str = "\"$(...)\""; |
| 4370 | goto dostr; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 4371 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4372 | case CTLARI: |
| 4373 | str = "$(("; |
| 4374 | goto dostr; |
| 4375 | case CTLENDARI: |
| 4376 | str = "))"; |
| 4377 | goto dostr; |
| 4378 | #endif |
| 4379 | case CTLQUOTEMARK: |
| 4380 | quoted ^= 1; |
| 4381 | c = '"'; |
| 4382 | break; |
| 4383 | case '=': |
| 4384 | if (subtype == 0) |
| 4385 | break; |
| 4386 | if ((subtype & VSTYPE) != VSNORMAL) |
| 4387 | quoted <<= 1; |
| 4388 | str = vstype[subtype & VSTYPE]; |
| 4389 | if (subtype & VSNUL) |
| 4390 | c = ':'; |
| 4391 | else |
| 4392 | goto checkstr; |
| 4393 | break; |
| 4394 | case '\'': |
| 4395 | case '\\': |
| 4396 | case '"': |
| 4397 | case '$': |
| 4398 | /* These can only happen inside quotes */ |
| 4399 | cc[0] = c; |
| 4400 | str = cc; |
| 4401 | c = '\\'; |
| 4402 | break; |
| 4403 | default: |
| 4404 | break; |
| 4405 | } |
| 4406 | USTPUTC(c, nextc); |
| 4407 | checkstr: |
| 4408 | if (!str) |
| 4409 | continue; |
| 4410 | dostr: |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 4411 | while ((c = *str++) != '\0') { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4412 | USTPUTC(c, nextc); |
| 4413 | } |
Denys Vlasenko | 46a1477 | 2009-12-10 21:27:13 +0100 | [diff] [blame] | 4414 | } /* while *p++ not NUL */ |
| 4415 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4416 | if (quoted & 1) { |
| 4417 | USTPUTC('"', nextc); |
| 4418 | } |
| 4419 | *nextc = 0; |
| 4420 | cmdnextc = nextc; |
| 4421 | } |
| 4422 | |
| 4423 | /* cmdtxt() and cmdlist() call each other */ |
| 4424 | static void cmdtxt(union node *n); |
| 4425 | |
| 4426 | static void |
| 4427 | cmdlist(union node *np, int sep) |
| 4428 | { |
| 4429 | for (; np; np = np->narg.next) { |
| 4430 | if (!sep) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4431 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4432 | cmdtxt(np); |
| 4433 | if (sep && np->narg.next) |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 4434 | cmdputs(" "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | static void |
| 4439 | cmdtxt(union node *n) |
| 4440 | { |
| 4441 | union node *np; |
| 4442 | struct nodelist *lp; |
| 4443 | const char *p; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4444 | |
| 4445 | if (!n) |
| 4446 | return; |
| 4447 | switch (n->type) { |
| 4448 | default: |
| 4449 | #if DEBUG |
| 4450 | abort(); |
| 4451 | #endif |
| 4452 | case NPIPE: |
| 4453 | lp = n->npipe.cmdlist; |
| 4454 | for (;;) { |
| 4455 | cmdtxt(lp->n); |
| 4456 | lp = lp->next; |
| 4457 | if (!lp) |
| 4458 | break; |
| 4459 | cmdputs(" | "); |
| 4460 | } |
| 4461 | break; |
| 4462 | case NSEMI: |
| 4463 | p = "; "; |
| 4464 | goto binop; |
| 4465 | case NAND: |
| 4466 | p = " && "; |
| 4467 | goto binop; |
| 4468 | case NOR: |
| 4469 | p = " || "; |
| 4470 | binop: |
| 4471 | cmdtxt(n->nbinary.ch1); |
| 4472 | cmdputs(p); |
| 4473 | n = n->nbinary.ch2; |
| 4474 | goto donode; |
| 4475 | case NREDIR: |
| 4476 | case NBACKGND: |
| 4477 | n = n->nredir.n; |
| 4478 | goto donode; |
| 4479 | case NNOT: |
| 4480 | cmdputs("!"); |
| 4481 | n = n->nnot.com; |
| 4482 | donode: |
| 4483 | cmdtxt(n); |
| 4484 | break; |
| 4485 | case NIF: |
| 4486 | cmdputs("if "); |
| 4487 | cmdtxt(n->nif.test); |
| 4488 | cmdputs("; then "); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4489 | if (n->nif.elsepart) { |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4490 | cmdtxt(n->nif.ifpart); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4491 | cmdputs("; else "); |
| 4492 | n = n->nif.elsepart; |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 4493 | } else { |
| 4494 | n = n->nif.ifpart; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4495 | } |
| 4496 | p = "; fi"; |
| 4497 | goto dotail; |
| 4498 | case NSUBSHELL: |
| 4499 | cmdputs("("); |
| 4500 | n = n->nredir.n; |
| 4501 | p = ")"; |
| 4502 | goto dotail; |
| 4503 | case NWHILE: |
| 4504 | p = "while "; |
| 4505 | goto until; |
| 4506 | case NUNTIL: |
| 4507 | p = "until "; |
| 4508 | until: |
| 4509 | cmdputs(p); |
| 4510 | cmdtxt(n->nbinary.ch1); |
| 4511 | n = n->nbinary.ch2; |
| 4512 | p = "; done"; |
| 4513 | dodo: |
| 4514 | cmdputs("; do "); |
| 4515 | dotail: |
| 4516 | cmdtxt(n); |
| 4517 | goto dotail2; |
| 4518 | case NFOR: |
| 4519 | cmdputs("for "); |
| 4520 | cmdputs(n->nfor.var); |
| 4521 | cmdputs(" in "); |
| 4522 | cmdlist(n->nfor.args, 1); |
| 4523 | n = n->nfor.body; |
| 4524 | p = "; done"; |
| 4525 | goto dodo; |
| 4526 | case NDEFUN: |
| 4527 | cmdputs(n->narg.text); |
| 4528 | p = "() { ... }"; |
| 4529 | goto dotail2; |
| 4530 | case NCMD: |
| 4531 | cmdlist(n->ncmd.args, 1); |
| 4532 | cmdlist(n->ncmd.redirect, 0); |
| 4533 | break; |
| 4534 | case NARG: |
| 4535 | p = n->narg.text; |
| 4536 | dotail2: |
| 4537 | cmdputs(p); |
| 4538 | break; |
| 4539 | case NHERE: |
| 4540 | case NXHERE: |
| 4541 | p = "<<..."; |
| 4542 | goto dotail2; |
| 4543 | case NCASE: |
| 4544 | cmdputs("case "); |
| 4545 | cmdputs(n->ncase.expr->narg.text); |
| 4546 | cmdputs(" in "); |
| 4547 | for (np = n->ncase.cases; np; np = np->nclist.next) { |
| 4548 | cmdtxt(np->nclist.pattern); |
| 4549 | cmdputs(") "); |
| 4550 | cmdtxt(np->nclist.body); |
| 4551 | cmdputs(";; "); |
| 4552 | } |
| 4553 | p = "esac"; |
| 4554 | goto dotail2; |
| 4555 | case NTO: |
| 4556 | p = ">"; |
| 4557 | goto redir; |
| 4558 | case NCLOBBER: |
| 4559 | p = ">|"; |
| 4560 | goto redir; |
| 4561 | case NAPPEND: |
| 4562 | p = ">>"; |
| 4563 | goto redir; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 4564 | #if ENABLE_ASH_BASH_COMPAT |
| 4565 | case NTO2: |
| 4566 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4567 | case NTOFD: |
| 4568 | p = ">&"; |
| 4569 | goto redir; |
| 4570 | case NFROM: |
| 4571 | p = "<"; |
| 4572 | goto redir; |
| 4573 | case NFROMFD: |
| 4574 | p = "<&"; |
| 4575 | goto redir; |
| 4576 | case NFROMTO: |
| 4577 | p = "<>"; |
| 4578 | redir: |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4579 | cmdputs(utoa(n->nfile.fd)); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4580 | cmdputs(p); |
| 4581 | if (n->type == NTOFD || n->type == NFROMFD) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 4582 | cmdputs(utoa(n->ndup.dupfd)); |
| 4583 | break; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4584 | } |
| 4585 | n = n->nfile.fname; |
| 4586 | goto donode; |
| 4587 | } |
| 4588 | } |
| 4589 | |
| 4590 | static char * |
| 4591 | commandtext(union node *n) |
| 4592 | { |
| 4593 | char *name; |
| 4594 | |
| 4595 | STARTSTACKSTR(cmdnextc); |
| 4596 | cmdtxt(n); |
| 4597 | name = stackblock(); |
| 4598 | TRACE(("commandtext: name %p, end %p\n\t\"%s\"\n", |
| 4599 | name, cmdnextc, cmdnextc)); |
| 4600 | return ckstrdup(name); |
| 4601 | } |
| 4602 | #endif /* JOBS */ |
| 4603 | |
| 4604 | /* |
| 4605 | * Fork off a subshell. If we are doing job control, give the subshell its |
| 4606 | * own process group. Jp is a job structure that the job is to be added to. |
| 4607 | * N is the command that will be evaluated by the child. Both jp and n may |
| 4608 | * be NULL. The mode parameter can be one of the following: |
| 4609 | * FORK_FG - Fork off a foreground process. |
| 4610 | * FORK_BG - Fork off a background process. |
| 4611 | * FORK_NOJOB - Like FORK_FG, but don't give the process its own |
| 4612 | * process group even if job control is on. |
| 4613 | * |
| 4614 | * When job control is turned off, background processes have their standard |
| 4615 | * input redirected to /dev/null (except for the second and later processes |
| 4616 | * in a pipeline). |
| 4617 | * |
| 4618 | * Called with interrupts off. |
| 4619 | */ |
| 4620 | /* |
| 4621 | * Clear traps on a fork. |
| 4622 | */ |
| 4623 | static void |
| 4624 | clear_traps(void) |
| 4625 | { |
| 4626 | char **tp; |
| 4627 | |
| 4628 | for (tp = trap; tp < &trap[NSIG]; tp++) { |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4629 | if (*tp && **tp) { /* trap not NULL or "" (SIG_IGN) */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4630 | INT_OFF; |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4631 | if (trap_ptr == trap) |
| 4632 | free(*tp); |
| 4633 | /* else: it "belongs" to trap_ptr vector, don't free */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4634 | *tp = NULL; |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 4635 | if ((tp - trap) != 0) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4636 | setsignal(tp - trap); |
| 4637 | INT_ON; |
| 4638 | } |
| 4639 | } |
Alexander Shishkin | ccb9771 | 2010-07-25 13:07:39 +0200 | [diff] [blame] | 4640 | may_have_traps = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4641 | } |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4642 | |
| 4643 | /* Lives far away from here, needed for forkchild */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4644 | static void closescript(void); |
Denis Vlasenko | 4177022 | 2007-10-07 18:02:52 +0000 | [diff] [blame] | 4645 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4646 | /* Called after fork(), in child */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4647 | static NOINLINE void |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4648 | forkchild(struct job *jp, union node *n, int mode) |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4649 | { |
| 4650 | int oldlvl; |
| 4651 | |
| 4652 | TRACE(("Child shell %d\n", getpid())); |
| 4653 | oldlvl = shlvl; |
| 4654 | shlvl++; |
| 4655 | |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4656 | /* man bash: "Non-builtin commands run by bash have signal handlers |
| 4657 | * set to the values inherited by the shell from its parent". |
| 4658 | * Do we do it correctly? */ |
| 4659 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4660 | closescript(); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4661 | |
| 4662 | if (mode == FORK_NOJOB /* is it `xxx` ? */ |
| 4663 | && n && n->type == NCMD /* is it single cmd? */ |
| 4664 | /* && n->ncmd.args->type == NARG - always true? */ |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4665 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "trap") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4666 | && n->ncmd.args->narg.next == NULL /* "trap" with no arguments */ |
| 4667 | /* && n->ncmd.args->narg.backquote == NULL - do we need to check this? */ |
| 4668 | ) { |
| 4669 | TRACE(("Trap hack\n")); |
| 4670 | /* Awful hack for `trap` or $(trap). |
| 4671 | * |
| 4672 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 4673 | * contains an example where "trap" is executed in a subshell: |
| 4674 | * |
| 4675 | * save_traps=$(trap) |
| 4676 | * ... |
| 4677 | * eval "$save_traps" |
| 4678 | * |
| 4679 | * Standard does not say that "trap" in subshell shall print |
| 4680 | * parent shell's traps. It only says that its output |
| 4681 | * must have suitable form, but then, in the above example |
| 4682 | * (which is not supposed to be normative), it implies that. |
| 4683 | * |
| 4684 | * bash (and probably other shell) does implement it |
| 4685 | * (traps are reset to defaults, but "trap" still shows them), |
| 4686 | * but as a result, "trap" logic is hopelessly messed up: |
| 4687 | * |
| 4688 | * # trap |
| 4689 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 4690 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 4691 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 4692 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 4693 | * trap -- 'echo Ho' SIGWINCH |
| 4694 | * # echo `(trap)` <--- in subshell in subshell - output |
| 4695 | * trap -- 'echo Ho' SIGWINCH |
| 4696 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 4697 | * trap -- 'echo Ho' SIGWINCH |
| 4698 | * |
| 4699 | * The rules when to forget and when to not forget traps |
| 4700 | * get really complex and nonsensical. |
| 4701 | * |
| 4702 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 4703 | */ |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4704 | /* Save trap handler strings for trap builtin to print */ |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 4705 | trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap)); |
Denys Vlasenko | 8f88d85 | 2009-09-25 12:12:53 +0200 | [diff] [blame] | 4706 | /* Fall through into clearing traps */ |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4707 | } |
Denys Vlasenko | e305c28 | 2009-09-25 02:12:27 +0200 | [diff] [blame] | 4708 | clear_traps(); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4709 | #if JOBS |
| 4710 | /* do job control only in root shell */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4711 | doing_jobctl = 0; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4712 | if (mode != FORK_NOJOB && jp->jobctl && !oldlvl) { |
| 4713 | pid_t pgrp; |
| 4714 | |
| 4715 | if (jp->nprocs == 0) |
| 4716 | pgrp = getpid(); |
| 4717 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4718 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4719 | /* this can fail because we are doing it in the parent also */ |
| 4720 | setpgid(0, pgrp); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4721 | if (mode == FORK_FG) |
| 4722 | xtcsetpgrp(ttyfd, pgrp); |
| 4723 | setsignal(SIGTSTP); |
| 4724 | setsignal(SIGTTOU); |
| 4725 | } else |
| 4726 | #endif |
| 4727 | if (mode == FORK_BG) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4728 | /* man bash: "When job control is not in effect, |
| 4729 | * asynchronous commands ignore SIGINT and SIGQUIT" */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4730 | ignoresig(SIGINT); |
| 4731 | ignoresig(SIGQUIT); |
| 4732 | if (jp->nprocs == 0) { |
| 4733 | close(0); |
| 4734 | if (open(bb_dev_null, O_RDONLY) != 0) |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 4735 | ash_msg_and_raise_error("can't open '%s'", bb_dev_null); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4736 | } |
| 4737 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4738 | if (!oldlvl) { |
| 4739 | if (iflag) { /* why if iflag only? */ |
| 4740 | setsignal(SIGINT); |
| 4741 | setsignal(SIGTERM); |
| 4742 | } |
| 4743 | /* man bash: |
| 4744 | * "In all cases, bash ignores SIGQUIT. Non-builtin |
| 4745 | * commands run by bash have signal handlers |
| 4746 | * set to the values inherited by the shell |
| 4747 | * from its parent". |
| 4748 | * Take care of the second rule: */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4749 | setsignal(SIGQUIT); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4750 | } |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4751 | #if JOBS |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4752 | if (n && n->type == NCMD |
Denys Vlasenko | 7426920 | 2010-02-21 01:26:42 +0100 | [diff] [blame] | 4753 | && n->ncmd.args && strcmp(n->ncmd.args->narg.text, "jobs") == 0 |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4754 | ) { |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4755 | TRACE(("Job hack\n")); |
Denys Vlasenko | 844f990 | 2009-09-23 03:25:52 +0200 | [diff] [blame] | 4756 | /* "jobs": we do not want to clear job list for it, |
| 4757 | * instead we remove only _its_ own_ job from job list. |
| 4758 | * This makes "jobs .... | cat" more useful. |
| 4759 | */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4760 | freejob(curjob); |
| 4761 | return; |
| 4762 | } |
| 4763 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4764 | for (jp = curjob; jp; jp = jp->prev_job) |
| 4765 | freejob(jp); |
| 4766 | jobless = 0; |
| 4767 | } |
| 4768 | |
Denis Vlasenko | bdc406d | 2007-07-15 01:13:25 +0000 | [diff] [blame] | 4769 | /* Called after fork(), in parent */ |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 4770 | #if !JOBS |
| 4771 | #define forkparent(jp, n, mode, pid) forkparent(jp, mode, pid) |
| 4772 | #endif |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4773 | static void |
| 4774 | forkparent(struct job *jp, union node *n, int mode, pid_t pid) |
| 4775 | { |
| 4776 | TRACE(("In parent shell: child = %d\n", pid)); |
| 4777 | if (!jp) { |
Denis Vlasenko | 36fc3cd | 2008-01-29 09:23:49 +0000 | [diff] [blame] | 4778 | while (jobless && dowait(DOWAIT_NONBLOCK, NULL) > 0) |
| 4779 | continue; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4780 | jobless++; |
| 4781 | return; |
| 4782 | } |
| 4783 | #if JOBS |
| 4784 | if (mode != FORK_NOJOB && jp->jobctl) { |
| 4785 | int pgrp; |
| 4786 | |
| 4787 | if (jp->nprocs == 0) |
| 4788 | pgrp = pid; |
| 4789 | else |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4790 | pgrp = jp->ps[0].ps_pid; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4791 | /* This can fail because we are doing it in the child also */ |
| 4792 | setpgid(pid, pgrp); |
| 4793 | } |
| 4794 | #endif |
| 4795 | if (mode == FORK_BG) { |
| 4796 | backgndpid = pid; /* set $! */ |
| 4797 | set_curjob(jp, CUR_RUNNING); |
| 4798 | } |
| 4799 | if (jp) { |
| 4800 | struct procstat *ps = &jp->ps[jp->nprocs++]; |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4801 | ps->ps_pid = pid; |
| 4802 | ps->ps_status = -1; |
| 4803 | ps->ps_cmd = nullstr; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4804 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 4805 | if (doing_jobctl && n) |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 4806 | ps->ps_cmd = commandtext(n); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4807 | #endif |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | static int |
| 4812 | forkshell(struct job *jp, union node *n, int mode) |
| 4813 | { |
| 4814 | int pid; |
| 4815 | |
| 4816 | TRACE(("forkshell(%%%d, %p, %d) called\n", jobno(jp), n, mode)); |
| 4817 | pid = fork(); |
| 4818 | if (pid < 0) { |
| 4819 | TRACE(("Fork failed, errno=%d", errno)); |
| 4820 | if (jp) |
| 4821 | freejob(jp); |
Denis Vlasenko | fa0b56d | 2008-07-01 16:09:07 +0000 | [diff] [blame] | 4822 | ash_msg_and_raise_error("can't fork"); |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4823 | } |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4824 | if (pid == 0) { |
| 4825 | CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */ |
Denys Vlasenko | e56f22a | 2009-07-24 00:16:59 +0200 | [diff] [blame] | 4826 | forkchild(jp, n, mode); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4827 | } else { |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4828 | forkparent(jp, n, mode, pid); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4829 | } |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4830 | return pid; |
| 4831 | } |
| 4832 | |
| 4833 | /* |
| 4834 | * Wait for job to finish. |
| 4835 | * |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4836 | * Under job control we have the problem that while a child process |
| 4837 | * is running interrupts generated by the user are sent to the child |
| 4838 | * but not to the shell. This means that an infinite loop started by |
| 4839 | * an interactive user may be hard to kill. With job control turned off, |
| 4840 | * an interactive user may place an interactive program inside a loop. |
| 4841 | * If the interactive program catches interrupts, the user doesn't want |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4842 | * these interrupts to also abort the loop. The approach we take here |
| 4843 | * is to have the shell ignore interrupt signals while waiting for a |
| 4844 | * foreground process to terminate, and then send itself an interrupt |
| 4845 | * signal if the child process was terminated by an interrupt signal. |
| 4846 | * Unfortunately, some programs want to do a bit of cleanup and then |
| 4847 | * exit on interrupt; unless these processes terminate themselves by |
| 4848 | * sending a signal to themselves (instead of calling exit) they will |
| 4849 | * confuse this approach. |
| 4850 | * |
| 4851 | * Called with interrupts off. |
| 4852 | */ |
| 4853 | static int |
| 4854 | waitforjob(struct job *jp) |
| 4855 | { |
| 4856 | int st; |
| 4857 | |
| 4858 | TRACE(("waitforjob(%%%d) called\n", jobno(jp))); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4859 | |
| 4860 | INT_OFF; |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4861 | while (jp->state == JOBRUNNING) { |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4862 | /* In non-interactive shells, we _can_ get |
| 4863 | * a keyboard signal here and be EINTRed, |
| 4864 | * but we just loop back, waiting for command to complete. |
| 4865 | * |
| 4866 | * man bash: |
| 4867 | * "If bash is waiting for a command to complete and receives |
| 4868 | * a signal for which a trap has been set, the trap |
| 4869 | * will not be executed until the command completes." |
| 4870 | * |
| 4871 | * Reality is that even if trap is not set, bash |
| 4872 | * will not act on the signal until command completes. |
| 4873 | * Try this. sleep5intoff.c: |
| 4874 | * #include <signal.h> |
| 4875 | * #include <unistd.h> |
| 4876 | * int main() { |
| 4877 | * sigset_t set; |
| 4878 | * sigemptyset(&set); |
| 4879 | * sigaddset(&set, SIGINT); |
| 4880 | * sigaddset(&set, SIGQUIT); |
| 4881 | * sigprocmask(SIG_BLOCK, &set, NULL); |
| 4882 | * sleep(5); |
| 4883 | * return 0; |
| 4884 | * } |
| 4885 | * $ bash -c './sleep5intoff; echo hi' |
| 4886 | * ^C^C^C^C <--- pressing ^C once a second |
| 4887 | * $ _ |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4888 | * $ bash -c './sleep5intoff; echo hi' |
| 4889 | * ^\^\^\^\hi <--- pressing ^\ (SIGQUIT) |
| 4890 | * $ _ |
| 4891 | */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4892 | dowait(DOWAIT_BLOCK, jp); |
| 4893 | } |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 4894 | INT_ON; |
| 4895 | |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4896 | st = getstatus(jp); |
| 4897 | #if JOBS |
| 4898 | if (jp->jobctl) { |
| 4899 | xtcsetpgrp(ttyfd, rootpid); |
| 4900 | /* |
| 4901 | * This is truly gross. |
| 4902 | * If we're doing job control, then we did a TIOCSPGRP which |
| 4903 | * caused us (the shell) to no longer be in the controlling |
| 4904 | * session -- so we wouldn't have seen any ^C/SIGINT. So, we |
| 4905 | * intuit from the subprocess exit status whether a SIGINT |
| 4906 | * occurred, and if so interrupt ourselves. Yuck. - mycroft |
| 4907 | */ |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 4908 | if (jp->sigint) /* TODO: do the same with all signals */ |
| 4909 | raise(SIGINT); /* ... by raise(jp->sig) instead? */ |
Denis Vlasenko | a891507 | 2007-02-23 21:10:06 +0000 | [diff] [blame] | 4910 | } |
| 4911 | if (jp->state == JOBDONE) |
| 4912 | #endif |
| 4913 | freejob(jp); |
| 4914 | return st; |
| 4915 | } |
| 4916 | |
| 4917 | /* |
| 4918 | * return 1 if there are stopped jobs, otherwise 0 |
| 4919 | */ |
| 4920 | static int |
| 4921 | stoppedjobs(void) |
| 4922 | { |
| 4923 | struct job *jp; |
| 4924 | int retval; |
| 4925 | |
| 4926 | retval = 0; |
| 4927 | if (job_warning) |
| 4928 | goto out; |
| 4929 | jp = curjob; |
| 4930 | if (jp && jp->state == JOBSTOPPED) { |
| 4931 | out2str("You have stopped jobs.\n"); |
| 4932 | job_warning = 2; |
| 4933 | retval++; |
| 4934 | } |
| 4935 | out: |
| 4936 | return retval; |
| 4937 | } |
| 4938 | |
| 4939 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4940 | /* ============ redir.c |
| 4941 | * |
| 4942 | * Code for dealing with input/output redirection. |
| 4943 | */ |
| 4944 | |
| 4945 | #define EMPTY -2 /* marks an unused slot in redirtab */ |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 4946 | #define CLOSED -3 /* marks a slot of previously-closed fd */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 4947 | |
| 4948 | /* |
| 4949 | * Open a file in noclobber mode. |
| 4950 | * The code was copied from bash. |
| 4951 | */ |
| 4952 | static int |
| 4953 | noclobberopen(const char *fname) |
| 4954 | { |
| 4955 | int r, fd; |
| 4956 | struct stat finfo, finfo2; |
| 4957 | |
| 4958 | /* |
| 4959 | * If the file exists and is a regular file, return an error |
| 4960 | * immediately. |
| 4961 | */ |
| 4962 | r = stat(fname, &finfo); |
| 4963 | if (r == 0 && S_ISREG(finfo.st_mode)) { |
| 4964 | errno = EEXIST; |
| 4965 | return -1; |
| 4966 | } |
| 4967 | |
| 4968 | /* |
| 4969 | * If the file was not present (r != 0), make sure we open it |
| 4970 | * exclusively so that if it is created before we open it, our open |
| 4971 | * will fail. Make sure that we do not truncate an existing file. |
| 4972 | * Note that we don't turn on O_EXCL unless the stat failed -- if the |
| 4973 | * file was not a regular file, we leave O_EXCL off. |
| 4974 | */ |
| 4975 | if (r != 0) |
| 4976 | return open(fname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
| 4977 | fd = open(fname, O_WRONLY|O_CREAT, 0666); |
| 4978 | |
| 4979 | /* If the open failed, return the file descriptor right away. */ |
| 4980 | if (fd < 0) |
| 4981 | return fd; |
| 4982 | |
| 4983 | /* |
| 4984 | * OK, the open succeeded, but the file may have been changed from a |
| 4985 | * non-regular file to a regular file between the stat and the open. |
| 4986 | * We are assuming that the O_EXCL open handles the case where FILENAME |
| 4987 | * did not exist and is symlinked to an existing file between the stat |
| 4988 | * and open. |
| 4989 | */ |
| 4990 | |
| 4991 | /* |
| 4992 | * If we can open it and fstat the file descriptor, and neither check |
| 4993 | * revealed that it was a regular file, and the file has not been |
| 4994 | * replaced, return the file descriptor. |
| 4995 | */ |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 4996 | if (fstat(fd, &finfo2) == 0 |
| 4997 | && !S_ISREG(finfo2.st_mode) |
| 4998 | && finfo.st_dev == finfo2.st_dev |
| 4999 | && finfo.st_ino == finfo2.st_ino |
| 5000 | ) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5001 | return fd; |
Denys Vlasenko | 8d3e225 | 2010-08-31 12:42:06 +0200 | [diff] [blame] | 5002 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5003 | |
| 5004 | /* The file has been replaced. badness. */ |
| 5005 | close(fd); |
| 5006 | errno = EEXIST; |
| 5007 | return -1; |
| 5008 | } |
| 5009 | |
| 5010 | /* |
| 5011 | * Handle here documents. Normally we fork off a process to write the |
| 5012 | * data to a pipe. If the document is short, we can stuff the data in |
| 5013 | * the pipe without forking. |
| 5014 | */ |
| 5015 | /* openhere needs this forward reference */ |
| 5016 | static void expandhere(union node *arg, int fd); |
| 5017 | static int |
| 5018 | openhere(union node *redir) |
| 5019 | { |
| 5020 | int pip[2]; |
| 5021 | size_t len = 0; |
| 5022 | |
| 5023 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5024 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5025 | if (redir->type == NHERE) { |
| 5026 | len = strlen(redir->nhere.doc->narg.text); |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5027 | if (len <= PIPE_BUF) { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5028 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
| 5029 | goto out; |
| 5030 | } |
| 5031 | } |
| 5032 | if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5033 | /* child */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5034 | close(pip[0]); |
Denis Vlasenko | f8535cc | 2008-12-03 10:36:26 +0000 | [diff] [blame] | 5035 | ignoresig(SIGINT); //signal(SIGINT, SIG_IGN); |
| 5036 | ignoresig(SIGQUIT); //signal(SIGQUIT, SIG_IGN); |
| 5037 | ignoresig(SIGHUP); //signal(SIGHUP, SIG_IGN); |
| 5038 | ignoresig(SIGTSTP); //signal(SIGTSTP, SIG_IGN); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5039 | signal(SIGPIPE, SIG_DFL); |
| 5040 | if (redir->type == NHERE) |
| 5041 | full_write(pip[1], redir->nhere.doc->narg.text, len); |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5042 | else /* NXHERE */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5043 | expandhere(redir->nhere.doc, pip[1]); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 5044 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5045 | } |
| 5046 | out: |
| 5047 | close(pip[1]); |
| 5048 | return pip[0]; |
| 5049 | } |
| 5050 | |
| 5051 | static int |
| 5052 | openredirect(union node *redir) |
| 5053 | { |
| 5054 | char *fname; |
| 5055 | int f; |
| 5056 | |
| 5057 | switch (redir->nfile.type) { |
| 5058 | case NFROM: |
| 5059 | fname = redir->nfile.expfname; |
| 5060 | f = open(fname, O_RDONLY); |
| 5061 | if (f < 0) |
| 5062 | goto eopen; |
| 5063 | break; |
| 5064 | case NFROMTO: |
| 5065 | fname = redir->nfile.expfname; |
Andreas Bühmann | da75f44 | 2010-06-24 04:32:37 +0200 | [diff] [blame] | 5066 | f = open(fname, O_RDWR|O_CREAT, 0666); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5067 | if (f < 0) |
| 5068 | goto ecreate; |
| 5069 | break; |
| 5070 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5071 | #if ENABLE_ASH_BASH_COMPAT |
| 5072 | case NTO2: |
| 5073 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5074 | /* Take care of noclobber mode. */ |
| 5075 | if (Cflag) { |
| 5076 | fname = redir->nfile.expfname; |
| 5077 | f = noclobberopen(fname); |
| 5078 | if (f < 0) |
| 5079 | goto ecreate; |
| 5080 | break; |
| 5081 | } |
| 5082 | /* FALLTHROUGH */ |
| 5083 | case NCLOBBER: |
| 5084 | fname = redir->nfile.expfname; |
| 5085 | f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); |
| 5086 | if (f < 0) |
| 5087 | goto ecreate; |
| 5088 | break; |
| 5089 | case NAPPEND: |
| 5090 | fname = redir->nfile.expfname; |
| 5091 | f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666); |
| 5092 | if (f < 0) |
| 5093 | goto ecreate; |
| 5094 | break; |
| 5095 | default: |
| 5096 | #if DEBUG |
| 5097 | abort(); |
| 5098 | #endif |
| 5099 | /* Fall through to eliminate warning. */ |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5100 | /* Our single caller does this itself */ |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5101 | // case NTOFD: |
| 5102 | // case NFROMFD: |
| 5103 | // f = -1; |
| 5104 | // break; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5105 | case NHERE: |
| 5106 | case NXHERE: |
| 5107 | f = openhere(redir); |
| 5108 | break; |
| 5109 | } |
| 5110 | |
| 5111 | return f; |
| 5112 | ecreate: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5113 | 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] | 5114 | eopen: |
Bernhard Reutner-Fischer | a53de7f | 2008-07-21 13:46:54 +0000 | [diff] [blame] | 5115 | 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] | 5116 | } |
| 5117 | |
| 5118 | /* |
| 5119 | * Copy a file descriptor to be >= to. Returns -1 |
| 5120 | * if the source file descriptor is closed, EMPTY if there are no unused |
| 5121 | * file descriptors left. |
| 5122 | */ |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5123 | /* 0x800..00: bit to set in "to" to request dup2 instead of fcntl(F_DUPFD). |
| 5124 | * 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] | 5125 | enum { |
| 5126 | COPYFD_EXACT = (int)~(INT_MAX), |
| 5127 | COPYFD_RESTORE = (int)((unsigned)COPYFD_EXACT >> 1), |
| 5128 | }; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5129 | static int |
| 5130 | copyfd(int from, int to) |
| 5131 | { |
| 5132 | int newfd; |
| 5133 | |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5134 | if (to & COPYFD_EXACT) { |
| 5135 | to &= ~COPYFD_EXACT; |
| 5136 | /*if (from != to)*/ |
| 5137 | newfd = dup2(from, to); |
| 5138 | } else { |
| 5139 | newfd = fcntl(from, F_DUPFD, to); |
| 5140 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5141 | if (newfd < 0) { |
| 5142 | if (errno == EMFILE) |
| 5143 | return EMPTY; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5144 | /* Happens when source fd is not open: try "echo >&99" */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5145 | ash_msg_and_raise_error("%d: %m", from); |
| 5146 | } |
| 5147 | return newfd; |
| 5148 | } |
| 5149 | |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5150 | /* Struct def and variable are moved down to the first usage site */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5151 | struct two_fd_t { |
| 5152 | int orig, copy; |
| 5153 | }; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5154 | struct redirtab { |
| 5155 | struct redirtab *next; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5156 | int nullredirs; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5157 | int pair_count; |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 5158 | struct two_fd_t two_fd[]; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5159 | }; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5160 | #define redirlist (G_var.redirlist) |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5161 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5162 | static int need_to_remember(struct redirtab *rp, int fd) |
| 5163 | { |
| 5164 | int i; |
| 5165 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5166 | if (!rp) /* remembering was not requested */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5167 | return 0; |
| 5168 | |
| 5169 | for (i = 0; i < rp->pair_count; i++) { |
| 5170 | if (rp->two_fd[i].orig == fd) { |
| 5171 | /* already remembered */ |
| 5172 | return 0; |
| 5173 | } |
| 5174 | } |
| 5175 | return 1; |
| 5176 | } |
| 5177 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5178 | /* "hidden" fd is a fd used to read scripts, or a copy of such */ |
| 5179 | static int is_hidden_fd(struct redirtab *rp, int fd) |
| 5180 | { |
| 5181 | int i; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5182 | struct parsefile *pf; |
| 5183 | |
| 5184 | if (fd == -1) |
| 5185 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5186 | /* Check open scripts' fds */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5187 | pf = g_parsefile; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5188 | while (pf) { |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5189 | /* We skip pf_fd == 0 case because of the following case: |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5190 | * $ ash # running ash interactively |
| 5191 | * $ . ./script.sh |
| 5192 | * and in script.sh: "exec 9>&0". |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5193 | * Even though top-level pf_fd _is_ 0, |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5194 | * it's still ok to use it: "read" builtin uses it, |
| 5195 | * why should we cripple "exec" builtin? |
| 5196 | */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 5197 | if (pf->pf_fd > 0 && fd == pf->pf_fd) { |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5198 | return 1; |
| 5199 | } |
| 5200 | pf = pf->prev; |
| 5201 | } |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5202 | |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5203 | if (!rp) |
| 5204 | return 0; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5205 | /* Check saved fds of redirects */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5206 | fd |= COPYFD_RESTORE; |
| 5207 | for (i = 0; i < rp->pair_count; i++) { |
| 5208 | if (rp->two_fd[i].copy == fd) { |
| 5209 | return 1; |
| 5210 | } |
| 5211 | } |
| 5212 | return 0; |
| 5213 | } |
| 5214 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5215 | /* |
| 5216 | * Process a list of redirection commands. If the REDIR_PUSH flag is set, |
| 5217 | * old file descriptors are stashed away so that the redirection can be |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5218 | * undone by calling popredir. |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5219 | */ |
| 5220 | /* flags passed to redirect */ |
| 5221 | #define REDIR_PUSH 01 /* save previous values of file descriptors */ |
| 5222 | #define REDIR_SAVEFD2 03 /* set preverrout */ |
| 5223 | static void |
| 5224 | redirect(union node *redir, int flags) |
| 5225 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5226 | struct redirtab *sv; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5227 | int sv_pos; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5228 | int i; |
| 5229 | int fd; |
| 5230 | int newfd; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5231 | int copied_fd2 = -1; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5232 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5233 | g_nullredirs++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5234 | if (!redir) { |
| 5235 | return; |
| 5236 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5237 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5238 | sv = NULL; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5239 | sv_pos = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5240 | INT_OFF; |
| 5241 | if (flags & REDIR_PUSH) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5242 | union node *tmp = redir; |
| 5243 | do { |
| 5244 | sv_pos++; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5245 | #if ENABLE_ASH_BASH_COMPAT |
Chris Metcalf | c3c1fb6 | 2010-01-08 13:18:06 +0100 | [diff] [blame] | 5246 | if (tmp->nfile.type == NTO2) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5247 | sv_pos++; |
| 5248 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5249 | tmp = tmp->nfile.next; |
| 5250 | } while (tmp); |
| 5251 | sv = ckmalloc(sizeof(*sv) + sv_pos * sizeof(sv->two_fd[0])); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5252 | sv->next = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5253 | sv->pair_count = sv_pos; |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5254 | redirlist = sv; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5255 | sv->nullredirs = g_nullredirs - 1; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5256 | g_nullredirs = 0; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5257 | while (sv_pos > 0) { |
| 5258 | sv_pos--; |
| 5259 | sv->two_fd[sv_pos].orig = sv->two_fd[sv_pos].copy = EMPTY; |
| 5260 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5261 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5262 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5263 | do { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5264 | int right_fd = -1; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5265 | fd = redir->nfile.fd; |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5266 | if (redir->nfile.type == NTOFD || redir->nfile.type == NFROMFD) { |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5267 | right_fd = redir->ndup.dupfd; |
| 5268 | //bb_error_msg("doing %d > %d", fd, right_fd); |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5269 | /* redirect from/to same file descriptor? */ |
| 5270 | if (right_fd == fd) |
| 5271 | continue; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5272 | /* "echo >&10" and 10 is a fd opened to a sh script? */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5273 | if (is_hidden_fd(sv, right_fd)) { |
| 5274 | errno = EBADF; /* as if it is closed */ |
| 5275 | ash_msg_and_raise_error("%d: %m", right_fd); |
| 5276 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5277 | newfd = -1; |
| 5278 | } else { |
| 5279 | newfd = openredirect(redir); /* always >= 0 */ |
| 5280 | if (fd == newfd) { |
| 5281 | /* Descriptor wasn't open before redirect. |
| 5282 | * Mark it for close in the future */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5283 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5284 | goto remember_to_close; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5285 | } |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5286 | continue; |
| 5287 | } |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5288 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5289 | #if ENABLE_ASH_BASH_COMPAT |
| 5290 | redirect_more: |
| 5291 | #endif |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5292 | if (need_to_remember(sv, fd)) { |
Denis Vlasenko | 0b76964 | 2008-07-24 07:54:57 +0000 | [diff] [blame] | 5293 | /* Copy old descriptor */ |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 5294 | /* Careful to not accidentally "save" |
| 5295 | * to the same fd as right side fd in N>&M */ |
| 5296 | int minfd = right_fd < 10 ? 10 : right_fd + 1; |
| 5297 | i = fcntl(fd, F_DUPFD, minfd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5298 | /* You'd expect copy to be CLOEXECed. Currently these extra "saved" fds |
| 5299 | * are closed in popredir() in the child, preventing them from leaking |
| 5300 | * into child. (popredir() also cleans up the mess in case of failures) |
| 5301 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5302 | if (i == -1) { |
| 5303 | i = errno; |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5304 | if (i != EBADF) { |
| 5305 | /* Strange error (e.g. "too many files" EMFILE?) */ |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5306 | if (newfd >= 0) |
| 5307 | close(newfd); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5308 | errno = i; |
| 5309 | ash_msg_and_raise_error("%d: %m", fd); |
| 5310 | /* NOTREACHED */ |
| 5311 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5312 | /* EBADF: it is not open - good, remember to close it */ |
| 5313 | remember_to_close: |
| 5314 | i = CLOSED; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5315 | } else { /* fd is open, save its copy */ |
| 5316 | /* "exec fd>&-" should not close fds |
| 5317 | * which point to script file(s). |
| 5318 | * Force them to be restored afterwards */ |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 5319 | if (is_hidden_fd(sv, fd)) |
| 5320 | i |= COPYFD_RESTORE; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5321 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5322 | if (fd == 2) |
| 5323 | copied_fd2 = i; |
| 5324 | sv->two_fd[sv_pos].orig = fd; |
| 5325 | sv->two_fd[sv_pos].copy = i; |
| 5326 | sv_pos++; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5327 | } |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5328 | if (newfd < 0) { |
| 5329 | /* NTOFD/NFROMFD: copy redir->ndup.dupfd to fd */ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5330 | if (redir->ndup.dupfd < 0) { /* "fd>&-" */ |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5331 | /* Don't want to trigger debugging */ |
| 5332 | if (fd != -1) |
| 5333 | close(fd); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5334 | } else { |
| 5335 | copyfd(redir->ndup.dupfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5336 | } |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5337 | } else if (fd != newfd) { /* move newfd to fd */ |
| 5338 | copyfd(newfd, fd | COPYFD_EXACT); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5339 | #if ENABLE_ASH_BASH_COMPAT |
| 5340 | if (!(redir->nfile.type == NTO2 && fd == 2)) |
| 5341 | #endif |
| 5342 | close(newfd); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5343 | } |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 5344 | #if ENABLE_ASH_BASH_COMPAT |
| 5345 | if (redir->nfile.type == NTO2 && fd == 1) { |
| 5346 | /* We already redirected it to fd 1, now copy it to 2 */ |
| 5347 | newfd = 1; |
| 5348 | fd = 2; |
| 5349 | goto redirect_more; |
| 5350 | } |
| 5351 | #endif |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 5352 | } while ((redir = redir->nfile.next) != NULL); |
Denis Vlasenko | 8d924ec | 2008-07-24 11:34:27 +0000 | [diff] [blame] | 5353 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5354 | INT_ON; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5355 | if ((flags & REDIR_SAVEFD2) && copied_fd2 >= 0) |
| 5356 | preverrout_fd = copied_fd2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | /* |
| 5360 | * Undo the effects of the last redirection. |
| 5361 | */ |
| 5362 | static void |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5363 | popredir(int drop, int restore) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5364 | { |
| 5365 | struct redirtab *rp; |
| 5366 | int i; |
| 5367 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5368 | if (--g_nullredirs >= 0) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5369 | return; |
| 5370 | INT_OFF; |
| 5371 | rp = redirlist; |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5372 | for (i = 0; i < rp->pair_count; i++) { |
| 5373 | int fd = rp->two_fd[i].orig; |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5374 | int copy = rp->two_fd[i].copy; |
| 5375 | if (copy == CLOSED) { |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5376 | if (!drop) |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 5377 | close(fd); |
Denis Vlasenko | 7d75a96 | 2007-11-22 08:16:57 +0000 | [diff] [blame] | 5378 | continue; |
| 5379 | } |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5380 | if (copy != EMPTY) { |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5381 | if (!drop || (restore && (copy & COPYFD_RESTORE))) { |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5382 | copy &= ~COPYFD_RESTORE; |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5383 | /*close(fd);*/ |
Denis Vlasenko | 22f7414 | 2008-07-24 22:34:43 +0000 | [diff] [blame] | 5384 | copyfd(copy, fd | COPYFD_EXACT); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5385 | } |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 5386 | close(copy & ~COPYFD_RESTORE); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5387 | } |
| 5388 | } |
| 5389 | redirlist = rp->next; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5390 | g_nullredirs = rp->nullredirs; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5391 | free(rp); |
| 5392 | INT_ON; |
| 5393 | } |
| 5394 | |
| 5395 | /* |
| 5396 | * Undo all redirections. Called on error or interrupt. |
| 5397 | */ |
| 5398 | |
| 5399 | /* |
| 5400 | * Discard all saved file descriptors. |
| 5401 | */ |
| 5402 | static void |
| 5403 | clearredir(int drop) |
| 5404 | { |
| 5405 | for (;;) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 5406 | g_nullredirs = 0; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5407 | if (!redirlist) |
| 5408 | break; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 5409 | popredir(drop, /*restore:*/ 0); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | static int |
| 5414 | redirectsafe(union node *redir, int flags) |
| 5415 | { |
| 5416 | int err; |
| 5417 | volatile int saveint; |
| 5418 | struct jmploc *volatile savehandler = exception_handler; |
| 5419 | struct jmploc jmploc; |
| 5420 | |
| 5421 | SAVE_INT(saveint); |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5422 | /* "echo 9>/dev/null; echo >&9; echo result: $?" - result should be 1, not 2! */ |
| 5423 | err = setjmp(jmploc.loc); // huh?? was = setjmp(jmploc.loc) * 2; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5424 | if (!err) { |
| 5425 | exception_handler = &jmploc; |
| 5426 | redirect(redir, flags); |
| 5427 | } |
| 5428 | exception_handler = savehandler; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 5429 | if (err && exception_type != EXERROR) |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 5430 | longjmp(exception_handler->loc, 1); |
| 5431 | RESTORE_INT(saveint); |
| 5432 | return err; |
| 5433 | } |
| 5434 | |
| 5435 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5436 | /* ============ Routines to expand arguments to commands |
| 5437 | * |
| 5438 | * We have to deal with backquotes, shell variables, and file metacharacters. |
| 5439 | */ |
| 5440 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5441 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5442 | static arith_t |
| 5443 | ash_arith(const char *s) |
| 5444 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5445 | arith_state_t math_state; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5446 | arith_t result; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5447 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5448 | math_state.lookupvar = lookupvar; |
| 5449 | math_state.setvar = setvar2; |
| 5450 | //math_state.endofname = endofname; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5451 | |
| 5452 | INT_OFF; |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5453 | result = arith(&math_state, s); |
| 5454 | if (math_state.errcode < 0) { |
| 5455 | if (math_state.errcode == -3) |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5456 | ash_msg_and_raise_error("exponent less than 0"); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5457 | if (math_state.errcode == -2) |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5458 | ash_msg_and_raise_error("divide by zero"); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 5459 | if (math_state.errcode == -5) |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5460 | ash_msg_and_raise_error("expression recursion loop detected"); |
| 5461 | raise_error_syntax(s); |
| 5462 | } |
| 5463 | INT_ON; |
| 5464 | |
| 5465 | return result; |
| 5466 | } |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5467 | #endif |
| 5468 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5469 | /* |
| 5470 | * expandarg flags |
| 5471 | */ |
| 5472 | #define EXP_FULL 0x1 /* perform word splitting & file globbing */ |
| 5473 | #define EXP_TILDE 0x2 /* do normal tilde expansion */ |
| 5474 | #define EXP_VARTILDE 0x4 /* expand tildes in an assignment */ |
| 5475 | #define EXP_REDIR 0x8 /* file glob for a redirection (1 match only) */ |
| 5476 | #define EXP_CASE 0x10 /* keeps quotes around for CASE pattern */ |
| 5477 | #define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */ |
| 5478 | #define EXP_VARTILDE2 0x40 /* expand tildes after colons only */ |
| 5479 | #define EXP_WORD 0x80 /* expand word in parameter expansion */ |
| 5480 | #define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */ |
| 5481 | /* |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5482 | * rmescape() flags |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5483 | */ |
| 5484 | #define RMESCAPE_ALLOC 0x1 /* Allocate a new string */ |
| 5485 | #define RMESCAPE_GLOB 0x2 /* Add backslashes for glob */ |
| 5486 | #define RMESCAPE_QUOTED 0x4 /* Remove CTLESC unless in quotes */ |
| 5487 | #define RMESCAPE_GROW 0x8 /* Grow strings instead of stalloc */ |
| 5488 | #define RMESCAPE_HEAP 0x10 /* Malloc strings instead of stalloc */ |
| 5489 | |
| 5490 | /* |
| 5491 | * Structure specifying which parts of the string should be searched |
| 5492 | * for IFS characters. |
| 5493 | */ |
| 5494 | struct ifsregion { |
| 5495 | struct ifsregion *next; /* next region in list */ |
| 5496 | int begoff; /* offset of start of region */ |
| 5497 | int endoff; /* offset of end of region */ |
| 5498 | int nulonly; /* search for nul bytes only */ |
| 5499 | }; |
| 5500 | |
| 5501 | struct arglist { |
| 5502 | struct strlist *list; |
| 5503 | struct strlist **lastp; |
| 5504 | }; |
| 5505 | |
| 5506 | /* output of current string */ |
| 5507 | static char *expdest; |
| 5508 | /* list of back quote expressions */ |
| 5509 | static struct nodelist *argbackq; |
| 5510 | /* first struct in list of ifs regions */ |
| 5511 | static struct ifsregion ifsfirst; |
| 5512 | /* last struct in list */ |
| 5513 | static struct ifsregion *ifslastp; |
| 5514 | /* holds expanded arg list */ |
| 5515 | static struct arglist exparg; |
| 5516 | |
| 5517 | /* |
| 5518 | * Our own itoa(). |
| 5519 | */ |
| 5520 | static int |
| 5521 | cvtnum(arith_t num) |
| 5522 | { |
| 5523 | int len; |
| 5524 | |
| 5525 | expdest = makestrspace(32, expdest); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5526 | len = fmtstr(expdest, 32, arith_t_fmt, num); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5527 | STADJUST(len, expdest); |
| 5528 | return len; |
| 5529 | } |
| 5530 | |
| 5531 | static size_t |
| 5532 | esclen(const char *start, const char *p) |
| 5533 | { |
| 5534 | size_t esc = 0; |
| 5535 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5536 | while (p > start && (unsigned char)*--p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5537 | esc++; |
| 5538 | } |
| 5539 | return esc; |
| 5540 | } |
| 5541 | |
| 5542 | /* |
| 5543 | * Remove any CTLESC characters from a string. |
| 5544 | */ |
| 5545 | static char * |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5546 | rmescapes(char *str, int flag) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5547 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5548 | static const char qchars[] ALIGN1 = { CTLESC, CTLQUOTEMARK, '\0' }; |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 5549 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5550 | char *p, *q, *r; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5551 | unsigned inquotes; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5552 | unsigned protect_against_glob; |
| 5553 | unsigned globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5554 | |
| 5555 | p = strpbrk(str, qchars); |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5556 | if (!p) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5557 | return str; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5558 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5559 | q = p; |
| 5560 | r = str; |
| 5561 | if (flag & RMESCAPE_ALLOC) { |
| 5562 | size_t len = p - str; |
| 5563 | size_t fulllen = len + strlen(p) + 1; |
| 5564 | |
| 5565 | if (flag & RMESCAPE_GROW) { |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5566 | int strloc = str - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5567 | r = makestrspace(fulllen, expdest); |
Colin Watson | 3963d94 | 2010-04-26 14:21:27 +0200 | [diff] [blame] | 5568 | /* p and str may be invalidated by makestrspace */ |
| 5569 | str = (char *)stackblock() + strloc; |
| 5570 | p = str + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5571 | } else if (flag & RMESCAPE_HEAP) { |
| 5572 | r = ckmalloc(fulllen); |
| 5573 | } else { |
| 5574 | r = stalloc(fulllen); |
| 5575 | } |
| 5576 | q = r; |
| 5577 | if (len > 0) { |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5578 | q = (char *)memcpy(q, str, len) + len; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5579 | } |
| 5580 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5581 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5582 | inquotes = (flag & RMESCAPE_QUOTED) ^ RMESCAPE_QUOTED; |
| 5583 | globbing = flag & RMESCAPE_GLOB; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5584 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5585 | while (*p) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5586 | if ((unsigned char)*p == CTLQUOTEMARK) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5587 | // TODO: if no RMESCAPE_QUOTED in flags, inquotes never becomes 0 |
| 5588 | // (alternates between RMESCAPE_QUOTED and ~RMESCAPE_QUOTED). Is it ok? |
| 5589 | // Note: both inquotes and protect_against_glob only affect whether |
| 5590 | // CTLESC,<ch> gets converted to <ch> or to \<ch> |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5591 | inquotes = ~inquotes; |
| 5592 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5593 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5594 | continue; |
| 5595 | } |
| 5596 | if (*p == '\\') { |
| 5597 | /* naked back slash */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5598 | protect_against_glob = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5599 | goto copy; |
| 5600 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5601 | if ((unsigned char)*p == CTLESC) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5602 | p++; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5603 | if (protect_against_glob && inquotes && *p != '/') { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5604 | *q++ = '\\'; |
| 5605 | } |
| 5606 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5607 | protect_against_glob = globbing; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5608 | copy: |
| 5609 | *q++ = *p++; |
| 5610 | } |
| 5611 | *q = '\0'; |
| 5612 | if (flag & RMESCAPE_GROW) { |
| 5613 | expdest = r; |
| 5614 | STADJUST(q - r + 1, expdest); |
| 5615 | } |
| 5616 | return r; |
| 5617 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5618 | #define pmatch(a, b) !fnmatch((a), (b), 0) |
| 5619 | |
| 5620 | /* |
| 5621 | * Prepare a pattern for a expmeta (internal glob(3)) call. |
| 5622 | * |
| 5623 | * Returns an stalloced string. |
| 5624 | */ |
| 5625 | static char * |
| 5626 | preglob(const char *pattern, int quoted, int flag) |
| 5627 | { |
| 5628 | flag |= RMESCAPE_GLOB; |
| 5629 | if (quoted) { |
| 5630 | flag |= RMESCAPE_QUOTED; |
| 5631 | } |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5632 | return rmescapes((char *)pattern, flag); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | /* |
| 5636 | * Put a string on the stack. |
| 5637 | */ |
| 5638 | static void |
| 5639 | memtodest(const char *p, size_t len, int syntax, int quotes) |
| 5640 | { |
| 5641 | char *q = expdest; |
| 5642 | |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5643 | q = makestrspace(quotes ? len * 2 : len, q); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5644 | |
| 5645 | while (len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5646 | unsigned char c = *p++; |
| 5647 | if (c == '\0') |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5648 | continue; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5649 | if (quotes) { |
| 5650 | int n = SIT(c, syntax); |
| 5651 | if (n == CCTL || n == CBACK) |
| 5652 | USTPUTC(CTLESC, q); |
| 5653 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5654 | USTPUTC(c, q); |
| 5655 | } |
| 5656 | |
| 5657 | expdest = q; |
| 5658 | } |
| 5659 | |
| 5660 | static void |
| 5661 | strtodest(const char *p, int syntax, int quotes) |
| 5662 | { |
| 5663 | memtodest(p, strlen(p), syntax, quotes); |
| 5664 | } |
| 5665 | |
| 5666 | /* |
| 5667 | * Record the fact that we have to scan this region of the |
| 5668 | * string for IFS characters. |
| 5669 | */ |
| 5670 | static void |
| 5671 | recordregion(int start, int end, int nulonly) |
| 5672 | { |
| 5673 | struct ifsregion *ifsp; |
| 5674 | |
| 5675 | if (ifslastp == NULL) { |
| 5676 | ifsp = &ifsfirst; |
| 5677 | } else { |
| 5678 | INT_OFF; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 5679 | ifsp = ckzalloc(sizeof(*ifsp)); |
| 5680 | /*ifsp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5681 | ifslastp->next = ifsp; |
| 5682 | INT_ON; |
| 5683 | } |
| 5684 | ifslastp = ifsp; |
| 5685 | ifslastp->begoff = start; |
| 5686 | ifslastp->endoff = end; |
| 5687 | ifslastp->nulonly = nulonly; |
| 5688 | } |
| 5689 | |
| 5690 | static void |
| 5691 | removerecordregions(int endoff) |
| 5692 | { |
| 5693 | if (ifslastp == NULL) |
| 5694 | return; |
| 5695 | |
| 5696 | if (ifsfirst.endoff > endoff) { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5697 | while (ifsfirst.next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5698 | struct ifsregion *ifsp; |
| 5699 | INT_OFF; |
| 5700 | ifsp = ifsfirst.next->next; |
| 5701 | free(ifsfirst.next); |
| 5702 | ifsfirst.next = ifsp; |
| 5703 | INT_ON; |
| 5704 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5705 | if (ifsfirst.begoff > endoff) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5706 | ifslastp = NULL; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5707 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5708 | ifslastp = &ifsfirst; |
| 5709 | ifsfirst.endoff = endoff; |
| 5710 | } |
| 5711 | return; |
| 5712 | } |
| 5713 | |
| 5714 | ifslastp = &ifsfirst; |
| 5715 | while (ifslastp->next && ifslastp->next->begoff < endoff) |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5716 | ifslastp = ifslastp->next; |
| 5717 | while (ifslastp->next) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5718 | struct ifsregion *ifsp; |
| 5719 | INT_OFF; |
| 5720 | ifsp = ifslastp->next->next; |
| 5721 | free(ifslastp->next); |
| 5722 | ifslastp->next = ifsp; |
| 5723 | INT_ON; |
| 5724 | } |
| 5725 | if (ifslastp->endoff > endoff) |
| 5726 | ifslastp->endoff = endoff; |
| 5727 | } |
| 5728 | |
| 5729 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5730 | exptilde(char *startp, char *p, int flags) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5731 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5732 | unsigned char c; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5733 | char *name; |
| 5734 | struct passwd *pw; |
| 5735 | const char *home; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 5736 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5737 | int startloc; |
| 5738 | |
| 5739 | name = p + 1; |
| 5740 | |
| 5741 | while ((c = *++p) != '\0') { |
| 5742 | switch (c) { |
| 5743 | case CTLESC: |
| 5744 | return startp; |
| 5745 | case CTLQUOTEMARK: |
| 5746 | return startp; |
| 5747 | case ':': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5748 | if (flags & EXP_VARTILDE) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5749 | goto done; |
| 5750 | break; |
| 5751 | case '/': |
| 5752 | case CTLENDVAR: |
| 5753 | goto done; |
| 5754 | } |
| 5755 | } |
| 5756 | done: |
| 5757 | *p = '\0'; |
| 5758 | if (*name == '\0') { |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 5759 | home = lookupvar("HOME"); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5760 | } else { |
| 5761 | pw = getpwnam(name); |
| 5762 | if (pw == NULL) |
| 5763 | goto lose; |
| 5764 | home = pw->pw_dir; |
| 5765 | } |
| 5766 | if (!home || !*home) |
| 5767 | goto lose; |
| 5768 | *p = c; |
| 5769 | startloc = expdest - (char *)stackblock(); |
| 5770 | strtodest(home, SQSYNTAX, quotes); |
| 5771 | recordregion(startloc, expdest - (char *)stackblock(), 0); |
| 5772 | return p; |
| 5773 | lose: |
| 5774 | *p = c; |
| 5775 | return startp; |
| 5776 | } |
| 5777 | |
| 5778 | /* |
| 5779 | * Execute a command inside back quotes. If it's a builtin command, we |
| 5780 | * want to save its output in a block obtained from malloc. Otherwise |
| 5781 | * we fork off a subprocess and get the output of the command via a pipe. |
| 5782 | * Should be called with interrupts off. |
| 5783 | */ |
| 5784 | struct backcmd { /* result of evalbackcmd */ |
| 5785 | int fd; /* file descriptor to read from */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5786 | int nleft; /* number of chars in buffer */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 5787 | char *buf; /* buffer */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5788 | struct job *jp; /* job structure for command */ |
| 5789 | }; |
| 5790 | |
| 5791 | /* These forward decls are needed to use "eval" code for backticks handling: */ |
Denis Vlasenko | 448d30e | 2008-06-27 00:24:11 +0000 | [diff] [blame] | 5792 | static uint8_t back_exitstatus; /* exit status of backquoted command */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5793 | #define EV_EXIT 01 /* exit after evaluating tree */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 5794 | static void evaltree(union node *, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5795 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 5796 | static void FAST_FUNC |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5797 | evalbackcmd(union node *n, struct backcmd *result) |
| 5798 | { |
| 5799 | int saveherefd; |
| 5800 | |
| 5801 | result->fd = -1; |
| 5802 | result->buf = NULL; |
| 5803 | result->nleft = 0; |
| 5804 | result->jp = NULL; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5805 | if (n == NULL) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5806 | goto out; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5807 | |
| 5808 | saveherefd = herefd; |
| 5809 | herefd = -1; |
| 5810 | |
| 5811 | { |
| 5812 | int pip[2]; |
| 5813 | struct job *jp; |
| 5814 | |
| 5815 | if (pipe(pip) < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 5816 | ash_msg_and_raise_error("pipe call failed"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 5817 | jp = makejob(/*n,*/ 1); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5818 | if (forkshell(jp, n, FORK_NOJOB) == 0) { |
| 5819 | FORCE_INT_ON; |
| 5820 | close(pip[0]); |
| 5821 | if (pip[1] != 1) { |
Denis Vlasenko | 5a86731 | 2008-07-24 19:46:38 +0000 | [diff] [blame] | 5822 | /*close(1);*/ |
| 5823 | copyfd(pip[1], 1 | COPYFD_EXACT); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5824 | close(pip[1]); |
| 5825 | } |
| 5826 | eflag = 0; |
| 5827 | evaltree(n, EV_EXIT); /* actually evaltreenr... */ |
| 5828 | /* NOTREACHED */ |
| 5829 | } |
| 5830 | close(pip[1]); |
| 5831 | result->fd = pip[0]; |
| 5832 | result->jp = jp; |
| 5833 | } |
| 5834 | herefd = saveherefd; |
| 5835 | out: |
| 5836 | TRACE(("evalbackcmd done: fd=%d buf=0x%x nleft=%d jp=0x%x\n", |
| 5837 | result->fd, result->buf, result->nleft, result->jp)); |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * Expand stuff in backwards quotes. |
| 5842 | */ |
| 5843 | static void |
| 5844 | expbackq(union node *cmd, int quoted, int quotes) |
| 5845 | { |
| 5846 | struct backcmd in; |
| 5847 | int i; |
| 5848 | char buf[128]; |
| 5849 | char *p; |
| 5850 | char *dest; |
| 5851 | int startloc; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 5852 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5853 | struct stackmark smark; |
| 5854 | |
| 5855 | INT_OFF; |
| 5856 | setstackmark(&smark); |
| 5857 | dest = expdest; |
| 5858 | startloc = dest - (char *)stackblock(); |
| 5859 | grabstackstr(dest); |
| 5860 | evalbackcmd(cmd, &in); |
| 5861 | popstackmark(&smark); |
| 5862 | |
| 5863 | p = in.buf; |
| 5864 | i = in.nleft; |
| 5865 | if (i == 0) |
| 5866 | goto read; |
| 5867 | for (;;) { |
| 5868 | memtodest(p, i, syntax, quotes); |
| 5869 | read: |
| 5870 | if (in.fd < 0) |
| 5871 | break; |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 5872 | i = nonblock_safe_read(in.fd, buf, sizeof(buf)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5873 | TRACE(("expbackq: read returns %d\n", i)); |
| 5874 | if (i <= 0) |
| 5875 | break; |
| 5876 | p = buf; |
| 5877 | } |
| 5878 | |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 5879 | free(in.buf); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5880 | if (in.fd >= 0) { |
| 5881 | close(in.fd); |
| 5882 | back_exitstatus = waitforjob(in.jp); |
| 5883 | } |
| 5884 | INT_ON; |
| 5885 | |
| 5886 | /* Eat all trailing newlines */ |
| 5887 | dest = expdest; |
| 5888 | for (; dest > (char *)stackblock() && dest[-1] == '\n';) |
| 5889 | STUNPUTC(dest); |
| 5890 | expdest = dest; |
| 5891 | |
| 5892 | if (quoted == 0) |
| 5893 | recordregion(startloc, dest - (char *)stackblock(), 0); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 5894 | TRACE(("evalbackq: size:%d:'%.*s'\n", |
| 5895 | (int)((dest - (char *)stackblock()) - startloc), |
| 5896 | (int)((dest - (char *)stackblock()) - startloc), |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5897 | stackblock() + startloc)); |
| 5898 | } |
| 5899 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5900 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5901 | /* |
| 5902 | * Expand arithmetic expression. Backup to start of expression, |
| 5903 | * evaluate, place result in (backed up) result, adjust string position. |
| 5904 | */ |
| 5905 | static void |
| 5906 | expari(int quotes) |
| 5907 | { |
| 5908 | char *p, *start; |
| 5909 | int begoff; |
| 5910 | int flag; |
| 5911 | int len; |
| 5912 | |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 5913 | /* ifsfree(); */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5914 | |
| 5915 | /* |
| 5916 | * This routine is slightly over-complicated for |
| 5917 | * efficiency. Next we scan backwards looking for the |
| 5918 | * start of arithmetic. |
| 5919 | */ |
| 5920 | start = stackblock(); |
| 5921 | p = expdest - 1; |
| 5922 | *p = '\0'; |
| 5923 | p--; |
| 5924 | do { |
| 5925 | int esc; |
| 5926 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5927 | while ((unsigned char)*p != CTLARI) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5928 | p--; |
| 5929 | #if DEBUG |
| 5930 | if (p < start) { |
| 5931 | ash_msg_and_raise_error("missing CTLARI (shouldn't happen)"); |
| 5932 | } |
| 5933 | #endif |
| 5934 | } |
| 5935 | |
| 5936 | esc = esclen(start, p); |
| 5937 | if (!(esc % 2)) { |
| 5938 | break; |
| 5939 | } |
| 5940 | |
| 5941 | p -= esc + 1; |
| 5942 | } while (1); |
| 5943 | |
| 5944 | begoff = p - start; |
| 5945 | |
| 5946 | removerecordregions(begoff); |
| 5947 | |
| 5948 | flag = p[1]; |
| 5949 | |
| 5950 | expdest = p; |
| 5951 | |
| 5952 | if (quotes) |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 5953 | rmescapes(p + 2, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5954 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 5955 | len = cvtnum(ash_arith(p + 2)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5956 | |
| 5957 | if (flag != '"') |
| 5958 | recordregion(begoff, begoff + len, 0); |
| 5959 | } |
| 5960 | #endif |
| 5961 | |
| 5962 | /* argstr needs it */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5963 | static char *evalvar(char *p, int flags, struct strlist *var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5964 | |
| 5965 | /* |
| 5966 | * Perform variable and command substitution. If EXP_FULL is set, output CTLESC |
| 5967 | * characters to allow for further processing. Otherwise treat |
| 5968 | * $@ like $* since no splitting will be performed. |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 5969 | * |
| 5970 | * var_str_list (can be NULL) is a list of "VAR=val" strings which take precedence |
| 5971 | * over shell varables. Needed for "A=a B=$A; echo $B" case - we use it |
| 5972 | * for correct expansion of "B=$A" word. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5973 | */ |
| 5974 | static void |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5975 | argstr(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5976 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 5977 | static const char spclchars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5978 | '=', |
| 5979 | ':', |
| 5980 | CTLQUOTEMARK, |
| 5981 | CTLENDVAR, |
| 5982 | CTLESC, |
| 5983 | CTLVAR, |
| 5984 | CTLBACKQ, |
| 5985 | CTLBACKQ | CTLQUOTE, |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 5986 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5987 | CTLENDARI, |
| 5988 | #endif |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 5989 | '\0' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5990 | }; |
| 5991 | const char *reject = spclchars; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5992 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); /* do CTLESC */ |
| 5993 | int breakall = flags & EXP_WORD; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5994 | int inquotes; |
| 5995 | size_t length; |
| 5996 | int startloc; |
| 5997 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 5998 | if (!(flags & EXP_VARTILDE)) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 5999 | reject += 2; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6000 | } else if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6001 | reject++; |
| 6002 | } |
| 6003 | inquotes = 0; |
| 6004 | length = 0; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6005 | if (flags & EXP_TILDE) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6006 | char *q; |
| 6007 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6008 | flags &= ~EXP_TILDE; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6009 | tilde: |
| 6010 | q = p; |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6011 | if ((unsigned char)*q == CTLESC && (flags & EXP_QWORD)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6012 | q++; |
| 6013 | if (*q == '~') |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6014 | p = exptilde(p, q, flags); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6015 | } |
| 6016 | start: |
| 6017 | startloc = expdest - (char *)stackblock(); |
| 6018 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6019 | unsigned char c; |
| 6020 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6021 | length += strcspn(p + length, reject); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6022 | c = p[length]; |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6023 | if (c) { |
| 6024 | if (!(c & 0x80) |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 6025 | IF_SH_MATH_SUPPORT(|| c == CTLENDARI) |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6026 | ) { |
| 6027 | /* c == '=' || c == ':' || c == CTLENDARI */ |
| 6028 | length++; |
| 6029 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6030 | } |
| 6031 | if (length > 0) { |
| 6032 | int newloc; |
| 6033 | expdest = stack_nputstr(p, length, expdest); |
| 6034 | newloc = expdest - (char *)stackblock(); |
| 6035 | if (breakall && !inquotes && newloc > startloc) { |
| 6036 | recordregion(startloc, newloc, 0); |
| 6037 | } |
| 6038 | startloc = newloc; |
| 6039 | } |
| 6040 | p += length + 1; |
| 6041 | length = 0; |
| 6042 | |
| 6043 | switch (c) { |
| 6044 | case '\0': |
| 6045 | goto breakloop; |
| 6046 | case '=': |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6047 | if (flags & EXP_VARTILDE2) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6048 | p--; |
| 6049 | continue; |
| 6050 | } |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6051 | flags |= EXP_VARTILDE2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6052 | reject++; |
| 6053 | /* fall through */ |
| 6054 | case ':': |
| 6055 | /* |
| 6056 | * sort of a hack - expand tildes in variable |
| 6057 | * assignments (after the first '=' and after ':'s). |
| 6058 | */ |
| 6059 | if (*--p == '~') { |
| 6060 | goto tilde; |
| 6061 | } |
| 6062 | continue; |
| 6063 | } |
| 6064 | |
| 6065 | switch (c) { |
| 6066 | case CTLENDVAR: /* ??? */ |
| 6067 | goto breakloop; |
| 6068 | case CTLQUOTEMARK: |
| 6069 | /* "$@" syntax adherence hack */ |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6070 | if (!inquotes |
| 6071 | && memcmp(p, dolatstr, 4) == 0 |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6072 | && ( p[4] == (char)CTLQUOTEMARK |
| 6073 | || (p[4] == (char)CTLENDVAR && p[5] == (char)CTLQUOTEMARK) |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6074 | ) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6075 | ) { |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6076 | p = evalvar(p + 1, flags, /* var_str_list: */ NULL) + 1; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6077 | goto start; |
| 6078 | } |
| 6079 | inquotes = !inquotes; |
| 6080 | addquote: |
| 6081 | if (quotes) { |
| 6082 | p--; |
| 6083 | length++; |
| 6084 | startloc++; |
| 6085 | } |
| 6086 | break; |
| 6087 | case CTLESC: |
| 6088 | startloc++; |
| 6089 | length++; |
| 6090 | goto addquote; |
| 6091 | case CTLVAR: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6092 | p = evalvar(p, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6093 | goto start; |
| 6094 | case CTLBACKQ: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6095 | c = '\0'; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6096 | case CTLBACKQ|CTLQUOTE: |
| 6097 | expbackq(argbackq->n, c, quotes); |
| 6098 | argbackq = argbackq->next; |
| 6099 | goto start; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6100 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6101 | case CTLENDARI: |
| 6102 | p--; |
| 6103 | expari(quotes); |
| 6104 | goto start; |
| 6105 | #endif |
| 6106 | } |
| 6107 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 6108 | breakloop: ; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6109 | } |
| 6110 | |
| 6111 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6112 | scanleft(char *startp, char *rmesc, char *rmescend UNUSED_PARAM, |
| 6113 | char *pattern, int quotes, int zero) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6114 | { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6115 | char *loc, *loc2; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6116 | char c; |
| 6117 | |
| 6118 | loc = startp; |
| 6119 | loc2 = rmesc; |
| 6120 | do { |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6121 | int match; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6122 | const char *s = loc2; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6123 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6124 | c = *loc2; |
| 6125 | if (zero) { |
| 6126 | *loc2 = '\0'; |
| 6127 | s = rmesc; |
| 6128 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6129 | match = pmatch(pattern, s); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6130 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6131 | *loc2 = c; |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6132 | if (match) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6133 | return loc; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6134 | if (quotes && (unsigned char)*loc == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6135 | loc++; |
| 6136 | loc++; |
| 6137 | loc2++; |
| 6138 | } while (c); |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6139 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | static char * |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6143 | scanright(char *startp, char *rmesc, char *rmescend, |
| 6144 | char *pattern, int quotes, int match_at_start) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6145 | { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6146 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6147 | int try2optimize = match_at_start; |
| 6148 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6149 | int esc = 0; |
| 6150 | char *loc; |
| 6151 | char *loc2; |
| 6152 | |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6153 | /* If we called by "${v/pattern/repl}" or "${v//pattern/repl}": |
| 6154 | * startp="escaped_value_of_v" rmesc="raw_value_of_v" |
| 6155 | * rmescend=""(ptr to NUL in rmesc) pattern="pattern" quotes=match_at_start=1 |
| 6156 | * Logic: |
| 6157 | * loc starts at NUL at the end of startp, loc2 starts at the end of rmesc, |
| 6158 | * and on each iteration they go back two/one char until they reach the beginning. |
| 6159 | * We try to find a match in "raw_value_of_v", "raw_value_of_", "raw_value_of" etc. |
| 6160 | */ |
| 6161 | /* TODO: document in what other circumstances we are called. */ |
| 6162 | |
| 6163 | for (loc = pattern - 1, loc2 = rmescend; loc >= startp; loc2--) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6164 | int match; |
| 6165 | char c = *loc2; |
| 6166 | const char *s = loc2; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6167 | if (match_at_start) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6168 | *loc2 = '\0'; |
| 6169 | s = rmesc; |
| 6170 | } |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6171 | match = pmatch(pattern, s); |
| 6172 | //bb_error_msg("pmatch(pattern:'%s',s:'%s'):%d", pattern, s, match); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6173 | *loc2 = c; |
| 6174 | if (match) |
| 6175 | return loc; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6176 | #if !ENABLE_ASH_OPTIMIZE_FOR_SIZE |
| 6177 | if (try2optimize) { |
| 6178 | /* Maybe we can optimize this: |
| 6179 | * if pattern ends with unescaped *, we can avoid checking |
| 6180 | * shorter strings: if "foo*" doesnt match "raw_value_of_v", |
| 6181 | * it wont match truncated "raw_value_of_" strings too. |
| 6182 | */ |
| 6183 | unsigned plen = strlen(pattern); |
| 6184 | /* Does it end with "*"? */ |
| 6185 | if (plen != 0 && pattern[--plen] == '*') { |
| 6186 | /* "xxxx*" is not escaped */ |
| 6187 | /* "xxx\*" is escaped */ |
| 6188 | /* "xx\\*" is not escaped */ |
| 6189 | /* "x\\\*" is escaped */ |
| 6190 | int slashes = 0; |
| 6191 | while (plen != 0 && pattern[--plen] == '\\') |
| 6192 | slashes++; |
| 6193 | if (!(slashes & 1)) |
| 6194 | break; /* ends with unescaped "*" */ |
| 6195 | } |
| 6196 | try2optimize = 0; |
| 6197 | } |
| 6198 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6199 | loc--; |
| 6200 | if (quotes) { |
| 6201 | if (--esc < 0) { |
| 6202 | esc = esclen(startp, loc); |
| 6203 | } |
| 6204 | if (esc % 2) { |
| 6205 | esc--; |
| 6206 | loc--; |
| 6207 | } |
| 6208 | } |
| 6209 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6210 | return NULL; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 6213 | static void varunset(const char *, const char *, const char *, int) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6214 | static void |
| 6215 | varunset(const char *end, const char *var, const char *umsg, int varflags) |
| 6216 | { |
| 6217 | const char *msg; |
| 6218 | const char *tail; |
| 6219 | |
| 6220 | tail = nullstr; |
| 6221 | msg = "parameter not set"; |
| 6222 | if (umsg) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6223 | if ((unsigned char)*end == CTLENDVAR) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6224 | if (varflags & VSNUL) |
| 6225 | tail = " or null"; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6226 | } else { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6227 | msg = umsg; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6228 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6229 | } |
Denys Vlasenko | 09dd6ec | 2010-08-07 02:44:33 +0200 | [diff] [blame] | 6230 | 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] | 6231 | } |
| 6232 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6233 | #if ENABLE_ASH_BASH_COMPAT |
| 6234 | static char * |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6235 | parse_sub_pattern(char *arg, int varflags) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6236 | { |
| 6237 | char *idx, *repl = NULL; |
| 6238 | unsigned char c; |
| 6239 | |
Denys Vlasenko | 1614900 | 2010-08-06 22:06:21 +0200 | [diff] [blame] | 6240 | //char *org_arg = arg; |
Denys Vlasenko | 33bbb27 | 2010-08-07 22:24:36 +0200 | [diff] [blame] | 6241 | //bb_error_msg("arg:'%s' varflags:%x", arg, varflags); |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6242 | idx = arg; |
| 6243 | while (1) { |
| 6244 | c = *arg; |
| 6245 | if (!c) |
| 6246 | break; |
| 6247 | if (c == '/') { |
| 6248 | /* Only the first '/' seen is our separator */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6249 | if (!repl) { |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6250 | repl = idx + 1; |
| 6251 | c = '\0'; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6252 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6253 | } |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6254 | *idx++ = c; |
Denis Vlasenko | 2659c63 | 2008-06-14 06:04:59 +0000 | [diff] [blame] | 6255 | arg++; |
Denys Vlasenko | 33bbb27 | 2010-08-07 22:24:36 +0200 | [diff] [blame] | 6256 | /* |
| 6257 | * Example: v='ab\c'; echo ${v/\\b/_\\_\z_} |
| 6258 | * The result is a_\_z_c (not a\_\_z_c)! |
| 6259 | * |
| 6260 | * Enable debug prints in this function and you'll see: |
| 6261 | * ash: arg:'\\b/_\\_z_' varflags:d |
| 6262 | * ash: pattern:'\\b' repl:'_\_z_' |
| 6263 | * That is, \\b is interpreted as \\b, but \\_ as \_! |
| 6264 | * IOW: search pattern and replace string treat backslashes |
| 6265 | * differently! That is the reason why we check repl below: |
| 6266 | */ |
| 6267 | if (c == '\\' && *arg == '\\' && repl && !(varflags & VSQUOTE)) |
| 6268 | arg++; /* skip both '\', not just first one */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6269 | } |
Denis Vlasenko | 29038c0 | 2008-06-14 06:14:02 +0000 | [diff] [blame] | 6270 | *idx = c; /* NUL */ |
Denys Vlasenko | 1614900 | 2010-08-06 22:06:21 +0200 | [diff] [blame] | 6271 | //bb_error_msg("pattern:'%s' repl:'%s'", org_arg, repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6272 | |
| 6273 | return repl; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6274 | } |
| 6275 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6276 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6277 | static const char * |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6278 | subevalvar(char *p, char *varname, int strloc, int subtype, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6279 | int startloc, int varflags, int quotes, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6280 | { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6281 | struct nodelist *saveargbackq = argbackq; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6282 | char *startp; |
| 6283 | char *loc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6284 | char *rmesc, *rmescend; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6285 | char *str; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6286 | IF_ASH_BASH_COMPAT(const char *repl = NULL;) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6287 | IF_ASH_BASH_COMPAT(int pos, len, orig_len;) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6288 | int saveherefd = herefd; |
| 6289 | int amount, workloc, resetloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6290 | int zero; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6291 | char *(*scan)(char*, char*, char*, char*, int, int); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6292 | |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6293 | //bb_error_msg("subevalvar(p:'%s',varname:'%s',strloc:%d,subtype:%d,startloc:%d,varflags:%x,quotes:%d)", |
| 6294 | // p, varname, strloc, subtype, startloc, varflags, quotes); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6295 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6296 | herefd = -1; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6297 | argstr(p, (subtype != VSASSIGN && subtype != VSQUESTION) ? EXP_CASE : 0, |
| 6298 | var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6299 | STPUTC('\0', expdest); |
| 6300 | herefd = saveherefd; |
| 6301 | argbackq = saveargbackq; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6302 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6303 | |
| 6304 | switch (subtype) { |
| 6305 | case VSASSIGN: |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6306 | setvar(varname, startp, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6307 | amount = startp - expdest; |
| 6308 | STADJUST(amount, expdest); |
| 6309 | return startp; |
| 6310 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6311 | case VSQUESTION: |
| 6312 | varunset(p, varname, startp, varflags); |
| 6313 | /* NOTREACHED */ |
| 6314 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6315 | #if ENABLE_ASH_BASH_COMPAT |
| 6316 | case VSSUBSTR: |
| 6317 | loc = str = stackblock() + strloc; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6318 | /* Read POS in ${var:POS:LEN} */ |
| 6319 | pos = atoi(loc); /* number(loc) errors out on "1:4" */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6320 | len = str - startp - 1; |
| 6321 | |
| 6322 | /* *loc != '\0', guaranteed by parser */ |
| 6323 | if (quotes) { |
| 6324 | char *ptr; |
| 6325 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6326 | /* Adjust the length by the number of escapes */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6327 | for (ptr = startp; ptr < (str - 1); ptr++) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6328 | if ((unsigned char)*ptr == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6329 | len--; |
| 6330 | ptr++; |
| 6331 | } |
| 6332 | } |
| 6333 | } |
| 6334 | orig_len = len; |
| 6335 | |
| 6336 | if (*loc++ == ':') { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6337 | /* ${var::LEN} */ |
| 6338 | len = number(loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6339 | } else { |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6340 | /* Skip POS in ${var:POS:LEN} */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6341 | len = orig_len; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6342 | while (*loc && *loc != ':') { |
| 6343 | /* TODO? |
| 6344 | * bash complains on: var=qwe; echo ${var:1a:123} |
| 6345 | if (!isdigit(*loc)) |
| 6346 | ash_msg_and_raise_error(msg_illnum, str); |
| 6347 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6348 | loc++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6349 | } |
| 6350 | if (*loc++ == ':') { |
| 6351 | len = number(loc); |
| 6352 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6353 | } |
| 6354 | if (pos >= orig_len) { |
| 6355 | pos = 0; |
| 6356 | len = 0; |
| 6357 | } |
| 6358 | if (len > (orig_len - pos)) |
| 6359 | len = orig_len - pos; |
| 6360 | |
| 6361 | for (str = startp; pos; str++, pos--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6362 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6363 | str++; |
| 6364 | } |
| 6365 | for (loc = startp; len; len--) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6366 | if (quotes && (unsigned char)*str == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6367 | *loc++ = *str++; |
| 6368 | *loc++ = *str++; |
| 6369 | } |
| 6370 | *loc = '\0'; |
| 6371 | amount = loc - expdest; |
| 6372 | STADJUST(amount, expdest); |
| 6373 | return loc; |
| 6374 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6375 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6376 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6377 | resetloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6378 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6379 | /* We'll comeback here if we grow the stack while handling |
| 6380 | * a VSREPLACE or VSREPLACEALL, since our pointers into the |
| 6381 | * stack will need rebasing, and we'll need to remove our work |
| 6382 | * areas each time |
| 6383 | */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6384 | IF_ASH_BASH_COMPAT(restart:) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6385 | |
| 6386 | amount = expdest - ((char *)stackblock() + resetloc); |
| 6387 | STADJUST(-amount, expdest); |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6388 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6389 | |
| 6390 | rmesc = startp; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6391 | rmescend = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6392 | if (quotes) { |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 6393 | rmesc = rmescapes(startp, RMESCAPE_ALLOC | RMESCAPE_GROW); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6394 | if (rmesc != startp) { |
| 6395 | rmescend = expdest; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6396 | startp = (char *)stackblock() + startloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6397 | } |
| 6398 | } |
| 6399 | rmescend--; |
Denis Vlasenko | 29eb359 | 2008-05-18 14:06:08 +0000 | [diff] [blame] | 6400 | str = (char *)stackblock() + strloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6401 | preglob(str, varflags & VSQUOTE, 0); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6402 | workloc = expdest - (char *)stackblock(); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6403 | |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6404 | #if ENABLE_ASH_BASH_COMPAT |
| 6405 | if (subtype == VSREPLACE || subtype == VSREPLACEALL) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6406 | char *idx, *end; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6407 | |
Denis Vlasenko | d6855d1 | 2008-09-27 14:03:25 +0000 | [diff] [blame] | 6408 | if (!repl) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6409 | repl = parse_sub_pattern(str, varflags); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6410 | //bb_error_msg("repl:'%s'", repl); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6411 | if (!repl) |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6412 | repl = nullstr; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
| 6415 | /* If there's no pattern to match, return the expansion unmolested */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6416 | if (str[0] == '\0') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6417 | return NULL; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6418 | |
| 6419 | len = 0; |
| 6420 | idx = startp; |
| 6421 | end = str - 1; |
| 6422 | while (idx < end) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6423 | try_to_match: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6424 | loc = scanright(idx, rmesc, rmescend, str, quotes, 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6425 | //bb_error_msg("scanright('%s'):'%s'", str, loc); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6426 | if (!loc) { |
| 6427 | /* No match, advance */ |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6428 | char *restart_detect = stackblock(); |
| 6429 | skip_matching: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6430 | STPUTC(*idx, expdest); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6431 | if (quotes && (unsigned char)*idx == CTLESC) { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6432 | idx++; |
| 6433 | len++; |
| 6434 | STPUTC(*idx, expdest); |
| 6435 | } |
| 6436 | if (stackblock() != restart_detect) |
| 6437 | goto restart; |
| 6438 | idx++; |
| 6439 | len++; |
| 6440 | rmesc++; |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6441 | /* continue; - prone to quadratic behavior, smarter code: */ |
| 6442 | if (idx >= end) |
| 6443 | break; |
| 6444 | if (str[0] == '*') { |
| 6445 | /* Pattern is "*foo". If "*foo" does not match "long_string", |
| 6446 | * it would never match "ong_string" etc, no point in trying. |
| 6447 | */ |
| 6448 | goto skip_matching; |
| 6449 | } |
| 6450 | goto try_to_match; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6451 | } |
| 6452 | |
| 6453 | if (subtype == VSREPLACEALL) { |
| 6454 | while (idx < loc) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6455 | if (quotes && (unsigned char)*idx == CTLESC) |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6456 | idx++; |
| 6457 | idx++; |
| 6458 | rmesc++; |
| 6459 | } |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6460 | } else { |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6461 | idx = loc; |
Denis Vlasenko | 81c3a1d | 2008-12-03 11:59:12 +0000 | [diff] [blame] | 6462 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6463 | |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6464 | //bb_error_msg("repl:'%s'", repl); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6465 | for (loc = (char*)repl; *loc; loc++) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6466 | char *restart_detect = stackblock(); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6467 | if (quotes && *loc == '\\') { |
| 6468 | STPUTC(CTLESC, expdest); |
| 6469 | len++; |
| 6470 | } |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6471 | STPUTC(*loc, expdest); |
| 6472 | if (stackblock() != restart_detect) |
| 6473 | goto restart; |
| 6474 | len++; |
| 6475 | } |
| 6476 | |
| 6477 | if (subtype == VSREPLACE) { |
Denys Vlasenko | f02c82f | 2010-08-06 19:14:47 +0200 | [diff] [blame] | 6478 | //bb_error_msg("tail:'%s', quotes:%x", idx, quotes); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6479 | while (*idx) { |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6480 | char *restart_detect = stackblock(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6481 | STPUTC(*idx, expdest); |
| 6482 | if (stackblock() != restart_detect) |
| 6483 | goto restart; |
| 6484 | len++; |
| 6485 | idx++; |
| 6486 | } |
| 6487 | break; |
| 6488 | } |
| 6489 | } |
| 6490 | |
| 6491 | /* We've put the replaced text into a buffer at workloc, now |
| 6492 | * move it to the right place and adjust the stack. |
| 6493 | */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6494 | STPUTC('\0', expdest); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6495 | startp = (char *)stackblock() + startloc; |
| 6496 | memmove(startp, (char *)stackblock() + workloc, len + 1); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6497 | //bb_error_msg("startp:'%s'", startp); |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6498 | amount = expdest - (startp + len); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6499 | STADJUST(-amount, expdest); |
| 6500 | return startp; |
| 6501 | } |
| 6502 | #endif /* ENABLE_ASH_BASH_COMPAT */ |
| 6503 | |
| 6504 | subtype -= VSTRIMRIGHT; |
| 6505 | #if DEBUG |
| 6506 | if (subtype < 0 || subtype > 7) |
| 6507 | abort(); |
| 6508 | #endif |
Denys Vlasenko | b76356b | 2010-03-13 16:19:04 +0100 | [diff] [blame] | 6509 | /* zero = (subtype == VSTRIMLEFT || subtype == VSTRIMLEFTMAX) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6510 | zero = subtype >> 1; |
| 6511 | /* VSTRIMLEFT/VSTRIMRIGHTMAX -> scanleft */ |
| 6512 | scan = (subtype & 1) ^ zero ? scanleft : scanright; |
| 6513 | |
| 6514 | loc = scan(startp, rmesc, rmescend, str, quotes, zero); |
| 6515 | if (loc) { |
| 6516 | if (zero) { |
| 6517 | memmove(startp, loc, str - loc); |
| 6518 | loc = startp + (str - loc) - 1; |
| 6519 | } |
| 6520 | *loc = '\0'; |
| 6521 | amount = loc - expdest; |
| 6522 | STADJUST(amount, expdest); |
| 6523 | } |
| 6524 | return loc; |
| 6525 | } |
| 6526 | |
| 6527 | /* |
| 6528 | * Add the value of a specialized variable to the stack string. |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6529 | * name parameter (examples): |
| 6530 | * ash -c 'echo $1' name:'1=' |
| 6531 | * ash -c 'echo $qwe' name:'qwe=' |
| 6532 | * ash -c 'echo $$' name:'$=' |
| 6533 | * ash -c 'echo ${$}' name:'$=' |
| 6534 | * ash -c 'echo ${$##q}' name:'$=q' |
| 6535 | * ash -c 'echo ${#$}' name:'$=' |
| 6536 | * note: examples with bad shell syntax: |
| 6537 | * ash -c 'echo ${#$1}' name:'$=1' |
| 6538 | * ash -c 'echo ${#1#}' name:'1=#' |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6539 | */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 6540 | static NOINLINE ssize_t |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6541 | varvalue(char *name, int varflags, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6542 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6543 | const char *p; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6544 | int num; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6545 | int i; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6546 | int sepq = 0; |
| 6547 | ssize_t len = 0; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6548 | int subtype = varflags & VSTYPE; |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6549 | int quotes = flags & (EXP_FULL | EXP_CASE | EXP_REDIR); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6550 | int quoted = varflags & VSQUOTE; |
| 6551 | int syntax = quoted ? DQSYNTAX : BASESYNTAX; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6552 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6553 | switch (*name) { |
| 6554 | case '$': |
| 6555 | num = rootpid; |
| 6556 | goto numvar; |
| 6557 | case '?': |
| 6558 | num = exitstatus; |
| 6559 | goto numvar; |
| 6560 | case '#': |
| 6561 | num = shellparam.nparam; |
| 6562 | goto numvar; |
| 6563 | case '!': |
| 6564 | num = backgndpid; |
| 6565 | if (num == 0) |
| 6566 | return -1; |
| 6567 | numvar: |
| 6568 | len = cvtnum(num); |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6569 | goto check_1char_name; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6570 | case '-': |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6571 | expdest = makestrspace(NOPTS, expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6572 | for (i = NOPTS - 1; i >= 0; i--) { |
| 6573 | if (optlist[i]) { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6574 | USTPUTC(optletters(i), expdest); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6575 | len++; |
| 6576 | } |
| 6577 | } |
Denys Vlasenko | 4d8873f | 2009-10-04 03:14:41 +0200 | [diff] [blame] | 6578 | check_1char_name: |
| 6579 | #if 0 |
| 6580 | /* handles cases similar to ${#$1} */ |
| 6581 | if (name[2] != '\0') |
| 6582 | raise_error_syntax("bad substitution"); |
| 6583 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6584 | break; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6585 | case '@': { |
| 6586 | char **ap; |
| 6587 | int sep; |
| 6588 | |
| 6589 | if (quoted && (flags & EXP_FULL)) { |
| 6590 | /* note: this is not meant as PEOF value */ |
| 6591 | sep = 1 << CHAR_BIT; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6592 | goto param; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6593 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6594 | /* fall through */ |
| 6595 | case '*': |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6596 | sep = ifsset() ? (unsigned char)(ifsval()[0]) : ' '; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6597 | i = SIT(sep, syntax); |
| 6598 | if (quotes && (i == CCTL || i == CBACK)) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6599 | sepq = 1; |
| 6600 | param: |
| 6601 | ap = shellparam.p; |
| 6602 | if (!ap) |
| 6603 | return -1; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 6604 | while ((p = *ap++) != NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6605 | size_t partlen; |
| 6606 | |
| 6607 | partlen = strlen(p); |
| 6608 | len += partlen; |
| 6609 | |
| 6610 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6611 | memtodest(p, partlen, syntax, quotes); |
| 6612 | |
| 6613 | if (*ap && sep) { |
| 6614 | char *q; |
| 6615 | |
| 6616 | len++; |
| 6617 | if (subtype == VSPLUS || subtype == VSLENGTH) { |
| 6618 | continue; |
| 6619 | } |
| 6620 | q = expdest; |
| 6621 | if (sepq) |
| 6622 | STPUTC(CTLESC, q); |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6623 | /* note: may put NUL despite sep != 0 |
| 6624 | * (see sep = 1 << CHAR_BIT above) */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6625 | STPUTC(sep, q); |
| 6626 | expdest = q; |
| 6627 | } |
| 6628 | } |
| 6629 | return len; |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6630 | } /* case '@' and '*' */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6631 | case '0': |
| 6632 | case '1': |
| 6633 | case '2': |
| 6634 | case '3': |
| 6635 | case '4': |
| 6636 | case '5': |
| 6637 | case '6': |
| 6638 | case '7': |
| 6639 | case '8': |
| 6640 | case '9': |
Denys Vlasenko | a00329c | 2009-08-30 20:05:10 +0200 | [diff] [blame] | 6641 | num = atoi(name); /* number(name) fails on ${N#str} etc */ |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6642 | if (num < 0 || num > shellparam.nparam) |
| 6643 | return -1; |
| 6644 | p = num ? shellparam.p[num - 1] : arg0; |
| 6645 | goto value; |
| 6646 | default: |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6647 | /* NB: name has form "VAR=..." */ |
| 6648 | |
| 6649 | /* "A=a B=$A" case: var_str_list is a list of "A=a" strings |
| 6650 | * which should be considered before we check variables. */ |
| 6651 | if (var_str_list) { |
| 6652 | unsigned name_len = (strchrnul(name, '=') - name) + 1; |
| 6653 | p = NULL; |
| 6654 | do { |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 6655 | char *str, *eq; |
| 6656 | str = var_str_list->text; |
| 6657 | eq = strchr(str, '='); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6658 | if (!eq) /* stop at first non-assignment */ |
| 6659 | break; |
| 6660 | eq++; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 6661 | if (name_len == (unsigned)(eq - str) |
Denys Vlasenko | 8eda4a9 | 2009-11-30 12:16:17 +0100 | [diff] [blame] | 6662 | && strncmp(str, name, name_len) == 0 |
| 6663 | ) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6664 | p = eq; |
| 6665 | /* goto value; - WRONG! */ |
| 6666 | /* think "A=1 A=2 B=$A" */ |
| 6667 | } |
| 6668 | var_str_list = var_str_list->next; |
| 6669 | } while (var_str_list); |
| 6670 | if (p) |
| 6671 | goto value; |
| 6672 | } |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6673 | p = lookupvar(name); |
| 6674 | value: |
| 6675 | if (!p) |
| 6676 | return -1; |
| 6677 | |
| 6678 | len = strlen(p); |
| 6679 | if (!(subtype == VSPLUS || subtype == VSLENGTH)) |
| 6680 | memtodest(p, len, syntax, quotes); |
| 6681 | return len; |
| 6682 | } |
| 6683 | |
| 6684 | if (subtype == VSPLUS || subtype == VSLENGTH) |
| 6685 | STADJUST(-len, expdest); |
| 6686 | return len; |
| 6687 | } |
| 6688 | |
| 6689 | /* |
| 6690 | * Expand a variable, and return a pointer to the next character in the |
| 6691 | * input string. |
| 6692 | */ |
| 6693 | static char * |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6694 | evalvar(char *p, int flags, struct strlist *var_str_list) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6695 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6696 | char varflags; |
| 6697 | char subtype; |
| 6698 | char quoted; |
| 6699 | char easy; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6700 | char *var; |
| 6701 | int patloc; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6702 | int startloc; |
| 6703 | ssize_t varlen; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6704 | |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6705 | varflags = (unsigned char) *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6706 | subtype = varflags & VSTYPE; |
| 6707 | quoted = varflags & VSQUOTE; |
| 6708 | var = p; |
| 6709 | easy = (!quoted || (*var == '@' && shellparam.nparam)); |
| 6710 | startloc = expdest - (char *)stackblock(); |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 6711 | p = strchr(p, '=') + 1; //TODO: use var_end(p)? |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6712 | |
| 6713 | again: |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6714 | varlen = varvalue(var, varflags, flags, var_str_list); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6715 | if (varflags & VSNUL) |
| 6716 | varlen--; |
| 6717 | |
| 6718 | if (subtype == VSPLUS) { |
| 6719 | varlen = -1 - varlen; |
| 6720 | goto vsplus; |
| 6721 | } |
| 6722 | |
| 6723 | if (subtype == VSMINUS) { |
| 6724 | vsplus: |
| 6725 | if (varlen < 0) { |
| 6726 | argstr( |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 6727 | p, |
| 6728 | flags | (quoted ? EXP_TILDE|EXP_QWORD : EXP_TILDE|EXP_WORD), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6729 | var_str_list |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6730 | ); |
| 6731 | goto end; |
| 6732 | } |
| 6733 | if (easy) |
| 6734 | goto record; |
| 6735 | goto end; |
| 6736 | } |
| 6737 | |
| 6738 | if (subtype == VSASSIGN || subtype == VSQUESTION) { |
| 6739 | if (varlen < 0) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6740 | if (subevalvar(p, var, /* strloc: */ 0, |
| 6741 | subtype, startloc, varflags, |
| 6742 | /* quotes: */ 0, |
| 6743 | var_str_list) |
| 6744 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6745 | varflags &= ~VSNUL; |
| 6746 | /* |
| 6747 | * Remove any recorded regions beyond |
| 6748 | * start of variable |
| 6749 | */ |
| 6750 | removerecordregions(startloc); |
| 6751 | goto again; |
| 6752 | } |
| 6753 | goto end; |
| 6754 | } |
| 6755 | if (easy) |
| 6756 | goto record; |
| 6757 | goto end; |
| 6758 | } |
| 6759 | |
| 6760 | if (varlen < 0 && uflag) |
| 6761 | varunset(p, var, 0, 0); |
| 6762 | |
| 6763 | if (subtype == VSLENGTH) { |
| 6764 | cvtnum(varlen > 0 ? varlen : 0); |
| 6765 | goto record; |
| 6766 | } |
| 6767 | |
| 6768 | if (subtype == VSNORMAL) { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6769 | if (easy) |
| 6770 | goto record; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6771 | goto end; |
| 6772 | } |
| 6773 | |
| 6774 | #if DEBUG |
| 6775 | switch (subtype) { |
| 6776 | case VSTRIMLEFT: |
| 6777 | case VSTRIMLEFTMAX: |
| 6778 | case VSTRIMRIGHT: |
| 6779 | case VSTRIMRIGHTMAX: |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 6780 | #if ENABLE_ASH_BASH_COMPAT |
| 6781 | case VSSUBSTR: |
| 6782 | case VSREPLACE: |
| 6783 | case VSREPLACEALL: |
| 6784 | #endif |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6785 | break; |
| 6786 | default: |
| 6787 | abort(); |
| 6788 | } |
| 6789 | #endif |
| 6790 | |
| 6791 | if (varlen >= 0) { |
| 6792 | /* |
| 6793 | * Terminate the string and start recording the pattern |
| 6794 | * right after it |
| 6795 | */ |
| 6796 | STPUTC('\0', expdest); |
| 6797 | patloc = expdest - (char *)stackblock(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 6798 | if (NULL == subevalvar(p, /* varname: */ NULL, patloc, subtype, |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6799 | startloc, varflags, |
Denys Vlasenko | 1166d7b | 2009-09-16 16:20:31 +0200 | [diff] [blame] | 6800 | //TODO: | EXP_REDIR too? All other such places do it too |
Denys Vlasenko | b0d6338 | 2009-09-16 16:18:32 +0200 | [diff] [blame] | 6801 | /* quotes: */ flags & (EXP_FULL | EXP_CASE), |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6802 | var_str_list) |
| 6803 | ) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6804 | int amount = expdest - ( |
| 6805 | (char *)stackblock() + patloc - 1 |
| 6806 | ); |
| 6807 | STADJUST(-amount, expdest); |
| 6808 | } |
| 6809 | /* Remove any recorded regions beyond start of variable */ |
| 6810 | removerecordregions(startloc); |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 6811 | record: |
| 6812 | recordregion(startloc, expdest - (char *)stackblock(), quoted); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6813 | } |
| 6814 | |
| 6815 | end: |
| 6816 | if (subtype != VSNORMAL) { /* skip to end of alternative */ |
| 6817 | int nesting = 1; |
| 6818 | for (;;) { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6819 | unsigned char c = *p++; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6820 | if (c == CTLESC) |
| 6821 | p++; |
| 6822 | else if (c == CTLBACKQ || c == (CTLBACKQ|CTLQUOTE)) { |
| 6823 | if (varlen >= 0) |
| 6824 | argbackq = argbackq->next; |
| 6825 | } else if (c == CTLVAR) { |
| 6826 | if ((*p++ & VSTYPE) != VSNORMAL) |
| 6827 | nesting++; |
| 6828 | } else if (c == CTLENDVAR) { |
| 6829 | if (--nesting == 0) |
| 6830 | break; |
| 6831 | } |
| 6832 | } |
| 6833 | } |
| 6834 | return p; |
| 6835 | } |
| 6836 | |
| 6837 | /* |
| 6838 | * Break the argument string into pieces based upon IFS and add the |
| 6839 | * strings to the argument list. The regions of the string to be |
| 6840 | * searched for IFS characters have been stored by recordregion. |
| 6841 | */ |
| 6842 | static void |
| 6843 | ifsbreakup(char *string, struct arglist *arglist) |
| 6844 | { |
| 6845 | struct ifsregion *ifsp; |
| 6846 | struct strlist *sp; |
| 6847 | char *start; |
| 6848 | char *p; |
| 6849 | char *q; |
| 6850 | const char *ifs, *realifs; |
| 6851 | int ifsspc; |
| 6852 | int nulonly; |
| 6853 | |
| 6854 | start = string; |
| 6855 | if (ifslastp != NULL) { |
| 6856 | ifsspc = 0; |
| 6857 | nulonly = 0; |
| 6858 | realifs = ifsset() ? ifsval() : defifs; |
| 6859 | ifsp = &ifsfirst; |
| 6860 | do { |
| 6861 | p = string + ifsp->begoff; |
| 6862 | nulonly = ifsp->nulonly; |
| 6863 | ifs = nulonly ? nullstr : realifs; |
| 6864 | ifsspc = 0; |
| 6865 | while (p < string + ifsp->endoff) { |
| 6866 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6867 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6868 | p++; |
| 6869 | if (!strchr(ifs, *p)) { |
| 6870 | p++; |
| 6871 | continue; |
| 6872 | } |
| 6873 | if (!nulonly) |
| 6874 | ifsspc = (strchr(defifs, *p) != NULL); |
| 6875 | /* Ignore IFS whitespace at start */ |
| 6876 | if (q == start && ifsspc) { |
| 6877 | p++; |
| 6878 | start = p; |
| 6879 | continue; |
| 6880 | } |
| 6881 | *q = '\0'; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6882 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6883 | sp->text = start; |
| 6884 | *arglist->lastp = sp; |
| 6885 | arglist->lastp = &sp->next; |
| 6886 | p++; |
| 6887 | if (!nulonly) { |
| 6888 | for (;;) { |
| 6889 | if (p >= string + ifsp->endoff) { |
| 6890 | break; |
| 6891 | } |
| 6892 | q = p; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 6893 | if ((unsigned char)*p == CTLESC) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6894 | p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 6895 | if (strchr(ifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6896 | p = q; |
| 6897 | break; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6898 | } |
| 6899 | if (strchr(defifs, *p) == NULL) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6900 | if (ifsspc) { |
| 6901 | p++; |
| 6902 | ifsspc = 0; |
| 6903 | } else { |
| 6904 | p = q; |
| 6905 | break; |
| 6906 | } |
| 6907 | } else |
| 6908 | p++; |
| 6909 | } |
| 6910 | } |
| 6911 | start = p; |
| 6912 | } /* while */ |
| 6913 | ifsp = ifsp->next; |
| 6914 | } while (ifsp != NULL); |
| 6915 | if (nulonly) |
| 6916 | goto add; |
| 6917 | } |
| 6918 | |
| 6919 | if (!*start) |
| 6920 | return; |
| 6921 | |
| 6922 | add: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6923 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6924 | sp->text = start; |
| 6925 | *arglist->lastp = sp; |
| 6926 | arglist->lastp = &sp->next; |
| 6927 | } |
| 6928 | |
| 6929 | static void |
| 6930 | ifsfree(void) |
| 6931 | { |
| 6932 | struct ifsregion *p; |
| 6933 | |
| 6934 | INT_OFF; |
| 6935 | p = ifsfirst.next; |
| 6936 | do { |
| 6937 | struct ifsregion *ifsp; |
| 6938 | ifsp = p->next; |
| 6939 | free(p); |
| 6940 | p = ifsp; |
| 6941 | } while (p); |
| 6942 | ifslastp = NULL; |
| 6943 | ifsfirst.next = NULL; |
| 6944 | INT_ON; |
| 6945 | } |
| 6946 | |
| 6947 | /* |
| 6948 | * Add a file name to the list. |
| 6949 | */ |
| 6950 | static void |
| 6951 | addfname(const char *name) |
| 6952 | { |
| 6953 | struct strlist *sp; |
| 6954 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 6955 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6956 | sp->text = ststrdup(name); |
| 6957 | *exparg.lastp = sp; |
| 6958 | exparg.lastp = &sp->next; |
| 6959 | } |
| 6960 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6961 | /* |
| 6962 | * Do metacharacter (i.e. *, ?, [...]) expansion. |
| 6963 | */ |
| 6964 | static void |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 6965 | expmeta(char *expdir, char *enddir, char *name) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 6966 | { |
| 6967 | char *p; |
| 6968 | const char *cp; |
| 6969 | char *start; |
| 6970 | char *endname; |
| 6971 | int metaflag; |
| 6972 | struct stat statb; |
| 6973 | DIR *dirp; |
| 6974 | struct dirent *dp; |
| 6975 | int atend; |
| 6976 | int matchdot; |
| 6977 | |
| 6978 | metaflag = 0; |
| 6979 | start = name; |
| 6980 | for (p = name; *p; p++) { |
| 6981 | if (*p == '*' || *p == '?') |
| 6982 | metaflag = 1; |
| 6983 | else if (*p == '[') { |
| 6984 | char *q = p + 1; |
| 6985 | if (*q == '!') |
| 6986 | q++; |
| 6987 | for (;;) { |
| 6988 | if (*q == '\\') |
| 6989 | q++; |
| 6990 | if (*q == '/' || *q == '\0') |
| 6991 | break; |
| 6992 | if (*++q == ']') { |
| 6993 | metaflag = 1; |
| 6994 | break; |
| 6995 | } |
| 6996 | } |
| 6997 | } else if (*p == '\\') |
| 6998 | p++; |
| 6999 | else if (*p == '/') { |
| 7000 | if (metaflag) |
| 7001 | goto out; |
| 7002 | start = p + 1; |
| 7003 | } |
| 7004 | } |
| 7005 | out: |
| 7006 | if (metaflag == 0) { /* we've reached the end of the file name */ |
| 7007 | if (enddir != expdir) |
| 7008 | metaflag++; |
| 7009 | p = name; |
| 7010 | do { |
| 7011 | if (*p == '\\') |
| 7012 | p++; |
| 7013 | *enddir++ = *p; |
| 7014 | } while (*p++); |
| 7015 | if (metaflag == 0 || lstat(expdir, &statb) >= 0) |
| 7016 | addfname(expdir); |
| 7017 | return; |
| 7018 | } |
| 7019 | endname = p; |
| 7020 | if (name < start) { |
| 7021 | p = name; |
| 7022 | do { |
| 7023 | if (*p == '\\') |
| 7024 | p++; |
| 7025 | *enddir++ = *p++; |
| 7026 | } while (p < start); |
| 7027 | } |
| 7028 | if (enddir == expdir) { |
| 7029 | cp = "."; |
| 7030 | } else if (enddir == expdir + 1 && *expdir == '/') { |
| 7031 | cp = "/"; |
| 7032 | } else { |
| 7033 | cp = expdir; |
| 7034 | enddir[-1] = '\0'; |
| 7035 | } |
| 7036 | dirp = opendir(cp); |
| 7037 | if (dirp == NULL) |
| 7038 | return; |
| 7039 | if (enddir != expdir) |
| 7040 | enddir[-1] = '/'; |
| 7041 | if (*endname == 0) { |
| 7042 | atend = 1; |
| 7043 | } else { |
| 7044 | atend = 0; |
| 7045 | *endname++ = '\0'; |
| 7046 | } |
| 7047 | matchdot = 0; |
| 7048 | p = start; |
| 7049 | if (*p == '\\') |
| 7050 | p++; |
| 7051 | if (*p == '.') |
| 7052 | matchdot++; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7053 | while (!pending_int && (dp = readdir(dirp)) != NULL) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7054 | if (dp->d_name[0] == '.' && !matchdot) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7055 | continue; |
| 7056 | if (pmatch(start, dp->d_name)) { |
| 7057 | if (atend) { |
| 7058 | strcpy(enddir, dp->d_name); |
| 7059 | addfname(expdir); |
| 7060 | } else { |
| 7061 | for (p = enddir, cp = dp->d_name; (*p++ = *cp++) != '\0';) |
| 7062 | continue; |
| 7063 | p[-1] = '/'; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7064 | expmeta(expdir, p, endname); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7065 | } |
| 7066 | } |
| 7067 | } |
| 7068 | closedir(dirp); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 7069 | if (!atend) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7070 | endname[-1] = '/'; |
| 7071 | } |
| 7072 | |
| 7073 | static struct strlist * |
| 7074 | msort(struct strlist *list, int len) |
| 7075 | { |
| 7076 | struct strlist *p, *q = NULL; |
| 7077 | struct strlist **lpp; |
| 7078 | int half; |
| 7079 | int n; |
| 7080 | |
| 7081 | if (len <= 1) |
| 7082 | return list; |
| 7083 | half = len >> 1; |
| 7084 | p = list; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 7085 | for (n = half; --n >= 0;) { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7086 | q = p; |
| 7087 | p = p->next; |
| 7088 | } |
| 7089 | q->next = NULL; /* terminate first half of list */ |
| 7090 | q = msort(list, half); /* sort first half of list */ |
| 7091 | p = msort(p, len - half); /* sort second half */ |
| 7092 | lpp = &list; |
| 7093 | for (;;) { |
| 7094 | #if ENABLE_LOCALE_SUPPORT |
| 7095 | if (strcoll(p->text, q->text) < 0) |
| 7096 | #else |
| 7097 | if (strcmp(p->text, q->text) < 0) |
| 7098 | #endif |
| 7099 | { |
| 7100 | *lpp = p; |
| 7101 | lpp = &p->next; |
| 7102 | p = *lpp; |
| 7103 | if (p == NULL) { |
| 7104 | *lpp = q; |
| 7105 | break; |
| 7106 | } |
| 7107 | } else { |
| 7108 | *lpp = q; |
| 7109 | lpp = &q->next; |
| 7110 | q = *lpp; |
| 7111 | if (q == NULL) { |
| 7112 | *lpp = p; |
| 7113 | break; |
| 7114 | } |
| 7115 | } |
| 7116 | } |
| 7117 | return list; |
| 7118 | } |
| 7119 | |
| 7120 | /* |
| 7121 | * Sort the results of file name expansion. It calculates the number of |
| 7122 | * strings to sort and then calls msort (short for merge sort) to do the |
| 7123 | * work. |
| 7124 | */ |
| 7125 | static struct strlist * |
| 7126 | expsort(struct strlist *str) |
| 7127 | { |
| 7128 | int len; |
| 7129 | struct strlist *sp; |
| 7130 | |
| 7131 | len = 0; |
| 7132 | for (sp = str; sp; sp = sp->next) |
| 7133 | len++; |
| 7134 | return msort(str, len); |
| 7135 | } |
| 7136 | |
| 7137 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7138 | expandmeta(struct strlist *str /*, int flag*/) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7139 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 7140 | static const char metachars[] ALIGN1 = { |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7141 | '*', '?', '[', 0 |
| 7142 | }; |
| 7143 | /* TODO - EXP_REDIR */ |
| 7144 | |
| 7145 | while (str) { |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7146 | char *expdir; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7147 | struct strlist **savelastp; |
| 7148 | struct strlist *sp; |
| 7149 | char *p; |
| 7150 | |
| 7151 | if (fflag) |
| 7152 | goto nometa; |
| 7153 | if (!strpbrk(str->text, metachars)) |
| 7154 | goto nometa; |
| 7155 | savelastp = exparg.lastp; |
| 7156 | |
| 7157 | INT_OFF; |
| 7158 | p = preglob(str->text, 0, RMESCAPE_ALLOC | RMESCAPE_HEAP); |
| 7159 | { |
| 7160 | int i = strlen(str->text); |
| 7161 | expdir = ckmalloc(i < 2048 ? 2048 : i); /* XXX */ |
| 7162 | } |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 7163 | expmeta(expdir, expdir, p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7164 | free(expdir); |
| 7165 | if (p != str->text) |
| 7166 | free(p); |
| 7167 | INT_ON; |
| 7168 | if (exparg.lastp == savelastp) { |
| 7169 | /* |
| 7170 | * no matches |
| 7171 | */ |
| 7172 | nometa: |
| 7173 | *exparg.lastp = str; |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7174 | rmescapes(str->text, 0); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7175 | exparg.lastp = &str->next; |
| 7176 | } else { |
| 7177 | *exparg.lastp = NULL; |
| 7178 | *savelastp = sp = expsort(*savelastp); |
| 7179 | while (sp->next != NULL) |
| 7180 | sp = sp->next; |
| 7181 | exparg.lastp = &sp->next; |
| 7182 | } |
| 7183 | str = str->next; |
| 7184 | } |
| 7185 | } |
| 7186 | |
| 7187 | /* |
| 7188 | * Perform variable substitution and command substitution on an argument, |
| 7189 | * placing the resulting list of arguments in arglist. If EXP_FULL is true, |
| 7190 | * perform splitting and file name expansion. When arglist is NULL, perform |
| 7191 | * here document expansion. |
| 7192 | */ |
| 7193 | static void |
| 7194 | expandarg(union node *arg, struct arglist *arglist, int flag) |
| 7195 | { |
| 7196 | struct strlist *sp; |
| 7197 | char *p; |
| 7198 | |
| 7199 | argbackq = arg->narg.backquote; |
| 7200 | STARTSTACKSTR(expdest); |
| 7201 | ifsfirst.next = NULL; |
| 7202 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7203 | argstr(arg->narg.text, flag, |
| 7204 | /* var_str_list: */ arglist ? arglist->list : NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7205 | p = _STPUTC('\0', expdest); |
| 7206 | expdest = p - 1; |
| 7207 | if (arglist == NULL) { |
| 7208 | return; /* here document expanded */ |
| 7209 | } |
| 7210 | p = grabstackstr(p); |
| 7211 | exparg.lastp = &exparg.list; |
| 7212 | /* |
| 7213 | * TODO - EXP_REDIR |
| 7214 | */ |
| 7215 | if (flag & EXP_FULL) { |
| 7216 | ifsbreakup(p, &exparg); |
| 7217 | *exparg.lastp = NULL; |
| 7218 | exparg.lastp = &exparg.list; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7219 | expandmeta(exparg.list /*, flag*/); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7220 | } else { |
| 7221 | if (flag & EXP_REDIR) /*XXX - for now, just remove escapes */ |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 7222 | rmescapes(p, 0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7223 | sp = stzalloc(sizeof(*sp)); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7224 | sp->text = p; |
| 7225 | *exparg.lastp = sp; |
| 7226 | exparg.lastp = &sp->next; |
| 7227 | } |
| 7228 | if (ifsfirst.next) |
| 7229 | ifsfree(); |
| 7230 | *exparg.lastp = NULL; |
| 7231 | if (exparg.list) { |
| 7232 | *arglist->lastp = exparg.list; |
| 7233 | arglist->lastp = exparg.lastp; |
| 7234 | } |
| 7235 | } |
| 7236 | |
| 7237 | /* |
| 7238 | * Expand shell variables and backquotes inside a here document. |
| 7239 | */ |
| 7240 | static void |
| 7241 | expandhere(union node *arg, int fd) |
| 7242 | { |
| 7243 | herefd = fd; |
| 7244 | expandarg(arg, (struct arglist *)NULL, 0); |
| 7245 | full_write(fd, stackblock(), expdest - (char *)stackblock()); |
| 7246 | } |
| 7247 | |
| 7248 | /* |
| 7249 | * Returns true if the pattern matches the string. |
| 7250 | */ |
| 7251 | static int |
| 7252 | patmatch(char *pattern, const char *string) |
| 7253 | { |
| 7254 | return pmatch(preglob(pattern, 0, 0), string); |
| 7255 | } |
| 7256 | |
| 7257 | /* |
| 7258 | * See if a pattern matches in a case statement. |
| 7259 | */ |
| 7260 | static int |
| 7261 | casematch(union node *pattern, char *val) |
| 7262 | { |
| 7263 | struct stackmark smark; |
| 7264 | int result; |
| 7265 | |
| 7266 | setstackmark(&smark); |
| 7267 | argbackq = pattern->narg.backquote; |
| 7268 | STARTSTACKSTR(expdest); |
| 7269 | ifslastp = NULL; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 7270 | argstr(pattern->narg.text, EXP_TILDE | EXP_CASE, |
| 7271 | /* var_str_list: */ NULL); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7272 | STACKSTRNUL(expdest); |
| 7273 | result = patmatch(stackblock(), val); |
| 7274 | popstackmark(&smark); |
| 7275 | return result; |
| 7276 | } |
| 7277 | |
| 7278 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7279 | /* ============ find_command */ |
| 7280 | |
| 7281 | struct builtincmd { |
| 7282 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7283 | int (*builtin)(int, char **) FAST_FUNC; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7284 | /* unsigned flags; */ |
| 7285 | }; |
| 7286 | #define IS_BUILTIN_SPECIAL(b) ((b)->name[0] & 1) |
Denis Vlasenko | e26b278 | 2008-02-12 07:40:29 +0000 | [diff] [blame] | 7287 | /* "regular" builtins always take precedence over commands, |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7288 | * regardless of PATH=....%builtin... position */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7289 | #define IS_BUILTIN_REGULAR(b) ((b)->name[0] & 2) |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7290 | #define IS_BUILTIN_ASSIGN(b) ((b)->name[0] & 4) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7291 | |
| 7292 | struct cmdentry { |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7293 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7294 | union param { |
| 7295 | int index; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7296 | /* index >= 0 for commands without path (slashes) */ |
| 7297 | /* (TODO: what exactly does the value mean? PATH position?) */ |
| 7298 | /* index == -1 for commands with slashes */ |
| 7299 | /* index == (-2 - applet_no) for NOFORK applets */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7300 | const struct builtincmd *cmd; |
| 7301 | struct funcnode *func; |
| 7302 | } u; |
| 7303 | }; |
| 7304 | /* values of cmdtype */ |
| 7305 | #define CMDUNKNOWN -1 /* no entry in table for command */ |
| 7306 | #define CMDNORMAL 0 /* command is an executable program */ |
| 7307 | #define CMDFUNCTION 1 /* command is a shell function */ |
| 7308 | #define CMDBUILTIN 2 /* command is a shell builtin */ |
| 7309 | |
| 7310 | /* action to find_command() */ |
| 7311 | #define DO_ERR 0x01 /* prints errors */ |
| 7312 | #define DO_ABS 0x02 /* checks absolute paths */ |
| 7313 | #define DO_NOFUNC 0x04 /* don't return shell functions, for command */ |
| 7314 | #define DO_ALTPATH 0x08 /* using alternate path */ |
| 7315 | #define DO_ALTBLTIN 0x20 /* %builtin in alt. path */ |
| 7316 | |
| 7317 | static void find_command(char *, struct cmdentry *, int, const char *); |
| 7318 | |
| 7319 | |
| 7320 | /* ============ Hashing commands */ |
| 7321 | |
| 7322 | /* |
| 7323 | * When commands are first encountered, they are entered in a hash table. |
| 7324 | * This ensures that a full path search will not have to be done for them |
| 7325 | * on each invocation. |
| 7326 | * |
| 7327 | * We should investigate converting to a linear search, even though that |
| 7328 | * would make the command name "hash" a misnomer. |
| 7329 | */ |
| 7330 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7331 | struct tblentry { |
| 7332 | struct tblentry *next; /* next entry in hash chain */ |
| 7333 | union param param; /* definition of builtin function */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7334 | smallint cmdtype; /* CMDxxx */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7335 | char rehash; /* if set, cd done since entry created */ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7336 | char cmdname[1]; /* name of command */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7337 | }; |
| 7338 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 7339 | static struct tblentry **cmdtable; |
| 7340 | #define INIT_G_cmdtable() do { \ |
| 7341 | cmdtable = xzalloc(CMDTABLESIZE * sizeof(cmdtable[0])); \ |
| 7342 | } while (0) |
| 7343 | |
| 7344 | static int builtinloc = -1; /* index in path of %builtin, or -1 */ |
| 7345 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7346 | |
| 7347 | static void |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7348 | 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] | 7349 | { |
| 7350 | int repeated = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7351 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7352 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7353 | if (applet_no >= 0) { |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7354 | if (APPLET_IS_NOEXEC(applet_no)) { |
Denys Vlasenko | 7df28bb | 2010-06-18 14:23:47 +0200 | [diff] [blame] | 7355 | clearenv(); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7356 | while (*envp) |
| 7357 | putenv(*envp++); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7358 | run_applet_no_and_exit(applet_no, argv); |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 7359 | } |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7360 | /* re-exec ourselves with the new arguments */ |
| 7361 | execve(bb_busybox_exec_path, argv, envp); |
| 7362 | /* If they called chroot or otherwise made the binary no longer |
| 7363 | * executable, fall through */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7364 | } |
| 7365 | #endif |
| 7366 | |
| 7367 | repeat: |
| 7368 | #ifdef SYSV |
| 7369 | do { |
| 7370 | execve(cmd, argv, envp); |
| 7371 | } while (errno == EINTR); |
| 7372 | #else |
| 7373 | execve(cmd, argv, envp); |
| 7374 | #endif |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7375 | if (repeated) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7376 | free(argv); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7377 | return; |
| 7378 | } |
| 7379 | if (errno == ENOEXEC) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7380 | char **ap; |
| 7381 | char **new; |
| 7382 | |
| 7383 | for (ap = argv; *ap; ap++) |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7384 | continue; |
| 7385 | ap = new = ckmalloc((ap - argv + 2) * sizeof(ap[0])); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7386 | ap[1] = cmd; |
Denis Vlasenko | c44ab01 | 2007-04-09 03:11:58 +0000 | [diff] [blame] | 7387 | ap[0] = cmd = (char *)DEFAULT_SHELL; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7388 | ap += 2; |
| 7389 | argv++; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7390 | while ((*ap++ = *argv++) != NULL) |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7391 | continue; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7392 | argv = new; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7393 | repeated++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7394 | goto repeat; |
| 7395 | } |
| 7396 | } |
| 7397 | |
| 7398 | /* |
| 7399 | * Exec a program. Never returns. If you change this routine, you may |
| 7400 | * have to change the find_command routine as well. |
| 7401 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7402 | static void shellexec(char **, const char *, int) NORETURN; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7403 | static void |
| 7404 | shellexec(char **argv, const char *path, int idx) |
| 7405 | { |
| 7406 | char *cmdname; |
| 7407 | int e; |
| 7408 | char **envp; |
| 7409 | int exerrno; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7410 | #if ENABLE_FEATURE_SH_STANDALONE |
| 7411 | int applet_no = -1; |
| 7412 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7413 | |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 7414 | clearredir(/*drop:*/ 1); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 7415 | envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL); |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7416 | if (strchr(argv[0], '/') != NULL |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 7417 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 7418 | || (applet_no = find_applet_by_name(argv[0])) >= 0 |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7419 | #endif |
| 7420 | ) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7421 | tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7422 | e = errno; |
| 7423 | } else { |
| 7424 | e = ENOENT; |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7425 | while ((cmdname = path_advance(&path, argv[0])) != NULL) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7426 | if (--idx < 0 && pathopt == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7427 | tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdname, argv, envp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7428 | if (errno != ENOENT && errno != ENOTDIR) |
| 7429 | e = errno; |
| 7430 | } |
| 7431 | stunalloc(cmdname); |
| 7432 | } |
| 7433 | } |
| 7434 | |
| 7435 | /* Map to POSIX errors */ |
| 7436 | switch (e) { |
| 7437 | case EACCES: |
| 7438 | exerrno = 126; |
| 7439 | break; |
| 7440 | case ENOENT: |
| 7441 | exerrno = 127; |
| 7442 | break; |
| 7443 | default: |
| 7444 | exerrno = 2; |
| 7445 | break; |
| 7446 | } |
| 7447 | exitstatus = exerrno; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 7448 | TRACE(("shellexec failed for %s, errno %d, suppress_int %d\n", |
| 7449 | argv[0], e, suppress_int)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7450 | ash_msg_and_raise(EXEXEC, "%s: %s", argv[0], errmsg(e, "not found")); |
| 7451 | /* NOTREACHED */ |
| 7452 | } |
| 7453 | |
| 7454 | static void |
| 7455 | printentry(struct tblentry *cmdp) |
| 7456 | { |
| 7457 | int idx; |
| 7458 | const char *path; |
| 7459 | char *name; |
| 7460 | |
| 7461 | idx = cmdp->param.index; |
| 7462 | path = pathval(); |
| 7463 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7464 | name = path_advance(&path, cmdp->cmdname); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7465 | stunalloc(name); |
| 7466 | } while (--idx >= 0); |
| 7467 | out1fmt("%s%s\n", name, (cmdp->rehash ? "*" : nullstr)); |
| 7468 | } |
| 7469 | |
| 7470 | /* |
| 7471 | * Clear out command entries. The argument specifies the first entry in |
| 7472 | * PATH which has changed. |
| 7473 | */ |
| 7474 | static void |
| 7475 | clearcmdentry(int firstchange) |
| 7476 | { |
| 7477 | struct tblentry **tblp; |
| 7478 | struct tblentry **pp; |
| 7479 | struct tblentry *cmdp; |
| 7480 | |
| 7481 | INT_OFF; |
| 7482 | for (tblp = cmdtable; tblp < &cmdtable[CMDTABLESIZE]; tblp++) { |
| 7483 | pp = tblp; |
| 7484 | while ((cmdp = *pp) != NULL) { |
| 7485 | if ((cmdp->cmdtype == CMDNORMAL && |
| 7486 | cmdp->param.index >= firstchange) |
| 7487 | || (cmdp->cmdtype == CMDBUILTIN && |
| 7488 | builtinloc >= firstchange) |
| 7489 | ) { |
| 7490 | *pp = cmdp->next; |
| 7491 | free(cmdp); |
| 7492 | } else { |
| 7493 | pp = &cmdp->next; |
| 7494 | } |
| 7495 | } |
| 7496 | } |
| 7497 | INT_ON; |
| 7498 | } |
| 7499 | |
| 7500 | /* |
| 7501 | * Locate a command in the command hash table. If "add" is nonzero, |
| 7502 | * add the command to the table if it is not already present. The |
| 7503 | * variable "lastcmdentry" is set to point to the address of the link |
| 7504 | * pointing to the entry, so that delete_cmd_entry can delete the |
| 7505 | * entry. |
| 7506 | * |
| 7507 | * Interrupts must be off if called with add != 0. |
| 7508 | */ |
| 7509 | static struct tblentry **lastcmdentry; |
| 7510 | |
| 7511 | static struct tblentry * |
| 7512 | cmdlookup(const char *name, int add) |
| 7513 | { |
| 7514 | unsigned int hashval; |
| 7515 | const char *p; |
| 7516 | struct tblentry *cmdp; |
| 7517 | struct tblentry **pp; |
| 7518 | |
| 7519 | p = name; |
| 7520 | hashval = (unsigned char)*p << 4; |
| 7521 | while (*p) |
| 7522 | hashval += (unsigned char)*p++; |
| 7523 | hashval &= 0x7FFF; |
| 7524 | pp = &cmdtable[hashval % CMDTABLESIZE]; |
| 7525 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7526 | if (strcmp(cmdp->cmdname, name) == 0) |
| 7527 | break; |
| 7528 | pp = &cmdp->next; |
| 7529 | } |
| 7530 | if (add && cmdp == NULL) { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7531 | cmdp = *pp = ckzalloc(sizeof(struct tblentry) |
| 7532 | + strlen(name) |
| 7533 | /* + 1 - already done because |
| 7534 | * tblentry::cmdname is char[1] */); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 7535 | /*cmdp->next = NULL; - ckzalloc did it */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7536 | cmdp->cmdtype = CMDUNKNOWN; |
| 7537 | strcpy(cmdp->cmdname, name); |
| 7538 | } |
| 7539 | lastcmdentry = pp; |
| 7540 | return cmdp; |
| 7541 | } |
| 7542 | |
| 7543 | /* |
| 7544 | * Delete the command entry returned on the last lookup. |
| 7545 | */ |
| 7546 | static void |
| 7547 | delete_cmd_entry(void) |
| 7548 | { |
| 7549 | struct tblentry *cmdp; |
| 7550 | |
| 7551 | INT_OFF; |
| 7552 | cmdp = *lastcmdentry; |
| 7553 | *lastcmdentry = cmdp->next; |
| 7554 | if (cmdp->cmdtype == CMDFUNCTION) |
| 7555 | freefunc(cmdp->param.func); |
| 7556 | free(cmdp); |
| 7557 | INT_ON; |
| 7558 | } |
| 7559 | |
| 7560 | /* |
| 7561 | * Add a new command entry, replacing any existing command entry for |
| 7562 | * the same name - except special builtins. |
| 7563 | */ |
| 7564 | static void |
| 7565 | addcmdentry(char *name, struct cmdentry *entry) |
| 7566 | { |
| 7567 | struct tblentry *cmdp; |
| 7568 | |
| 7569 | cmdp = cmdlookup(name, 1); |
| 7570 | if (cmdp->cmdtype == CMDFUNCTION) { |
| 7571 | freefunc(cmdp->param.func); |
| 7572 | } |
| 7573 | cmdp->cmdtype = entry->cmdtype; |
| 7574 | cmdp->param = entry->u; |
| 7575 | cmdp->rehash = 0; |
| 7576 | } |
| 7577 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7578 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7579 | hashcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7580 | { |
| 7581 | struct tblentry **pp; |
| 7582 | struct tblentry *cmdp; |
| 7583 | int c; |
| 7584 | struct cmdentry entry; |
| 7585 | char *name; |
| 7586 | |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7587 | if (nextopt("r") != '\0') { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7588 | clearcmdentry(0); |
| 7589 | return 0; |
| 7590 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7591 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7592 | if (*argptr == NULL) { |
| 7593 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7594 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
| 7595 | if (cmdp->cmdtype == CMDNORMAL) |
| 7596 | printentry(cmdp); |
| 7597 | } |
| 7598 | } |
| 7599 | return 0; |
| 7600 | } |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7601 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7602 | c = 0; |
| 7603 | while ((name = *argptr) != NULL) { |
| 7604 | cmdp = cmdlookup(name, 0); |
| 7605 | if (cmdp != NULL |
| 7606 | && (cmdp->cmdtype == CMDNORMAL |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7607 | || (cmdp->cmdtype == CMDBUILTIN && builtinloc >= 0)) |
| 7608 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7609 | delete_cmd_entry(); |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7610 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7611 | find_command(name, &entry, DO_ERR, pathval()); |
| 7612 | if (entry.cmdtype == CMDUNKNOWN) |
| 7613 | c = 1; |
| 7614 | argptr++; |
| 7615 | } |
| 7616 | return c; |
| 7617 | } |
| 7618 | |
| 7619 | /* |
| 7620 | * Called when a cd is done. Marks all commands so the next time they |
| 7621 | * are executed they will be rehashed. |
| 7622 | */ |
| 7623 | static void |
| 7624 | hashcd(void) |
| 7625 | { |
| 7626 | struct tblentry **pp; |
| 7627 | struct tblentry *cmdp; |
| 7628 | |
| 7629 | for (pp = cmdtable; pp < &cmdtable[CMDTABLESIZE]; pp++) { |
| 7630 | for (cmdp = *pp; cmdp; cmdp = cmdp->next) { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7631 | if (cmdp->cmdtype == CMDNORMAL |
| 7632 | || (cmdp->cmdtype == CMDBUILTIN |
| 7633 | && !IS_BUILTIN_REGULAR(cmdp->param.cmd) |
| 7634 | && builtinloc > 0) |
| 7635 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7636 | cmdp->rehash = 1; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7637 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7638 | } |
| 7639 | } |
| 7640 | } |
| 7641 | |
| 7642 | /* |
| 7643 | * Fix command hash table when PATH changed. |
| 7644 | * Called before PATH is changed. The argument is the new value of PATH; |
| 7645 | * pathval() still returns the old value at this point. |
| 7646 | * Called with interrupts off. |
| 7647 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7648 | static void FAST_FUNC |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7649 | changepath(const char *new) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7650 | { |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7651 | const char *old; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7652 | int firstchange; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7653 | int idx; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7654 | int idx_bltin; |
| 7655 | |
| 7656 | old = pathval(); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7657 | firstchange = 9999; /* assume no change */ |
| 7658 | idx = 0; |
| 7659 | idx_bltin = -1; |
| 7660 | for (;;) { |
| 7661 | if (*old != *new) { |
| 7662 | firstchange = idx; |
| 7663 | if ((*old == '\0' && *new == ':') |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7664 | || (*old == ':' && *new == '\0') |
| 7665 | ) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7666 | firstchange++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7667 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7668 | old = new; /* ignore subsequent differences */ |
| 7669 | } |
| 7670 | if (*new == '\0') |
| 7671 | break; |
| 7672 | if (*new == '%' && idx_bltin < 0 && prefix(new + 1, "builtin")) |
| 7673 | idx_bltin = idx; |
Denis Vlasenko | 5c3d2b3 | 2008-02-03 22:01:08 +0000 | [diff] [blame] | 7674 | if (*new == ':') |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7675 | idx++; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 7676 | new++; |
| 7677 | old++; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7678 | } |
| 7679 | if (builtinloc < 0 && idx_bltin >= 0) |
| 7680 | builtinloc = idx_bltin; /* zap builtins */ |
| 7681 | if (builtinloc >= 0 && idx_bltin < 0) |
| 7682 | firstchange = 0; |
| 7683 | clearcmdentry(firstchange); |
| 7684 | builtinloc = idx_bltin; |
| 7685 | } |
| 7686 | |
| 7687 | #define TEOF 0 |
| 7688 | #define TNL 1 |
| 7689 | #define TREDIR 2 |
| 7690 | #define TWORD 3 |
| 7691 | #define TSEMI 4 |
| 7692 | #define TBACKGND 5 |
| 7693 | #define TAND 6 |
| 7694 | #define TOR 7 |
| 7695 | #define TPIPE 8 |
| 7696 | #define TLP 9 |
| 7697 | #define TRP 10 |
| 7698 | #define TENDCASE 11 |
| 7699 | #define TENDBQUOTE 12 |
| 7700 | #define TNOT 13 |
| 7701 | #define TCASE 14 |
| 7702 | #define TDO 15 |
| 7703 | #define TDONE 16 |
| 7704 | #define TELIF 17 |
| 7705 | #define TELSE 18 |
| 7706 | #define TESAC 19 |
| 7707 | #define TFI 20 |
| 7708 | #define TFOR 21 |
| 7709 | #define TIF 22 |
| 7710 | #define TIN 23 |
| 7711 | #define TTHEN 24 |
| 7712 | #define TUNTIL 25 |
| 7713 | #define TWHILE 26 |
| 7714 | #define TBEGIN 27 |
| 7715 | #define TEND 28 |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 7716 | typedef smallint token_id_t; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7717 | |
| 7718 | /* first char is indicating which tokens mark the end of a list */ |
| 7719 | static const char *const tokname_array[] = { |
| 7720 | "\1end of file", |
| 7721 | "\0newline", |
| 7722 | "\0redirection", |
| 7723 | "\0word", |
| 7724 | "\0;", |
| 7725 | "\0&", |
| 7726 | "\0&&", |
| 7727 | "\0||", |
| 7728 | "\0|", |
| 7729 | "\0(", |
| 7730 | "\1)", |
| 7731 | "\1;;", |
| 7732 | "\1`", |
| 7733 | #define KWDOFFSET 13 |
| 7734 | /* the following are keywords */ |
| 7735 | "\0!", |
| 7736 | "\0case", |
| 7737 | "\1do", |
| 7738 | "\1done", |
| 7739 | "\1elif", |
| 7740 | "\1else", |
| 7741 | "\1esac", |
| 7742 | "\1fi", |
| 7743 | "\0for", |
| 7744 | "\0if", |
| 7745 | "\0in", |
| 7746 | "\1then", |
| 7747 | "\0until", |
| 7748 | "\0while", |
| 7749 | "\0{", |
| 7750 | "\1}", |
| 7751 | }; |
| 7752 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7753 | /* Wrapper around strcmp for qsort/bsearch/... */ |
| 7754 | static int |
| 7755 | pstrcmp(const void *a, const void *b) |
| 7756 | { |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 7757 | return strcmp((char*) a, (*(char**) b) + 1); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7758 | } |
| 7759 | |
| 7760 | static const char *const * |
| 7761 | findkwd(const char *s) |
| 7762 | { |
| 7763 | return bsearch(s, tokname_array + KWDOFFSET, |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 7764 | ARRAY_SIZE(tokname_array) - KWDOFFSET, |
| 7765 | sizeof(tokname_array[0]), pstrcmp); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7766 | } |
| 7767 | |
| 7768 | /* |
| 7769 | * Locate and print what a word is... |
| 7770 | */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7771 | static int |
| 7772 | describe_command(char *command, int describe_command_verbose) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7773 | { |
| 7774 | struct cmdentry entry; |
| 7775 | struct tblentry *cmdp; |
| 7776 | #if ENABLE_ASH_ALIAS |
| 7777 | const struct alias *ap; |
| 7778 | #endif |
| 7779 | const char *path = pathval(); |
| 7780 | |
| 7781 | if (describe_command_verbose) { |
| 7782 | out1str(command); |
| 7783 | } |
| 7784 | |
| 7785 | /* First look at the keywords */ |
| 7786 | if (findkwd(command)) { |
| 7787 | out1str(describe_command_verbose ? " is a shell keyword" : command); |
| 7788 | goto out; |
| 7789 | } |
| 7790 | |
| 7791 | #if ENABLE_ASH_ALIAS |
| 7792 | /* Then look at the aliases */ |
| 7793 | ap = lookupalias(command, 0); |
| 7794 | if (ap != NULL) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7795 | if (!describe_command_verbose) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7796 | out1str("alias "); |
| 7797 | printalias(ap); |
| 7798 | return 0; |
| 7799 | } |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7800 | out1fmt(" is an alias for %s", ap->val); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7801 | goto out; |
| 7802 | } |
| 7803 | #endif |
| 7804 | /* Then check if it is a tracked alias */ |
| 7805 | cmdp = cmdlookup(command, 0); |
| 7806 | if (cmdp != NULL) { |
| 7807 | entry.cmdtype = cmdp->cmdtype; |
| 7808 | entry.u = cmdp->param; |
| 7809 | } else { |
| 7810 | /* Finally use brute force */ |
| 7811 | find_command(command, &entry, DO_ABS, path); |
| 7812 | } |
| 7813 | |
| 7814 | switch (entry.cmdtype) { |
| 7815 | case CMDNORMAL: { |
| 7816 | int j = entry.u.index; |
| 7817 | char *p; |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 7818 | if (j < 0) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7819 | p = command; |
| 7820 | } else { |
| 7821 | do { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 7822 | p = path_advance(&path, command); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7823 | stunalloc(p); |
| 7824 | } while (--j >= 0); |
| 7825 | } |
| 7826 | if (describe_command_verbose) { |
| 7827 | out1fmt(" is%s %s", |
| 7828 | (cmdp ? " a tracked alias for" : nullstr), p |
| 7829 | ); |
| 7830 | } else { |
| 7831 | out1str(p); |
| 7832 | } |
| 7833 | break; |
| 7834 | } |
| 7835 | |
| 7836 | case CMDFUNCTION: |
| 7837 | if (describe_command_verbose) { |
| 7838 | out1str(" is a shell function"); |
| 7839 | } else { |
| 7840 | out1str(command); |
| 7841 | } |
| 7842 | break; |
| 7843 | |
| 7844 | case CMDBUILTIN: |
| 7845 | if (describe_command_verbose) { |
| 7846 | out1fmt(" is a %sshell builtin", |
| 7847 | IS_BUILTIN_SPECIAL(entry.u.cmd) ? |
| 7848 | "special " : nullstr |
| 7849 | ); |
| 7850 | } else { |
| 7851 | out1str(command); |
| 7852 | } |
| 7853 | break; |
| 7854 | |
| 7855 | default: |
| 7856 | if (describe_command_verbose) { |
| 7857 | out1str(": not found\n"); |
| 7858 | } |
| 7859 | return 127; |
| 7860 | } |
| 7861 | out: |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 7862 | out1str("\n"); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7863 | return 0; |
| 7864 | } |
| 7865 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7866 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7867 | typecmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7868 | { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7869 | int i = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7870 | int err = 0; |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7871 | int verbose = 1; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7872 | |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7873 | /* type -p ... ? (we don't bother checking for 'p') */ |
Denis Vlasenko | 1fc6238 | 2007-06-25 22:55:34 +0000 | [diff] [blame] | 7874 | if (argv[1] && argv[1][0] == '-') { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7875 | i++; |
| 7876 | verbose = 0; |
| 7877 | } |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 7878 | while (argv[i]) { |
Denis Vlasenko | 46846e2 | 2007-05-20 13:08:31 +0000 | [diff] [blame] | 7879 | err |= describe_command(argv[i++], verbose); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7880 | } |
| 7881 | return err; |
| 7882 | } |
| 7883 | |
| 7884 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7885 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 7886 | commandcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7887 | { |
| 7888 | int c; |
| 7889 | enum { |
| 7890 | VERIFY_BRIEF = 1, |
| 7891 | VERIFY_VERBOSE = 2, |
| 7892 | } verify = 0; |
| 7893 | |
| 7894 | while ((c = nextopt("pvV")) != '\0') |
| 7895 | if (c == 'V') |
| 7896 | verify |= VERIFY_VERBOSE; |
| 7897 | else if (c == 'v') |
| 7898 | verify |= VERIFY_BRIEF; |
| 7899 | #if DEBUG |
| 7900 | else if (c != 'p') |
| 7901 | abort(); |
| 7902 | #endif |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7903 | /* Mimic bash: just "command -v" doesn't complain, it's a nop */ |
| 7904 | if (verify && (*argptr != NULL)) { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7905 | return describe_command(*argptr, verify - VERIFY_BRIEF); |
Denis Vlasenko | e7067e3 | 2008-07-11 23:09:34 +0000 | [diff] [blame] | 7906 | } |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7907 | |
| 7908 | return 0; |
| 7909 | } |
| 7910 | #endif |
| 7911 | |
| 7912 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 7913 | /* ============ eval.c */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7914 | |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7915 | static int funcblocksize; /* size of structures in function */ |
| 7916 | static int funcstringsize; /* size of strings in node */ |
| 7917 | static void *funcblock; /* block to allocate function from */ |
| 7918 | static char *funcstring; /* block to allocate strings from */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7919 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7920 | /* flags in argument to evaltree */ |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7921 | #define EV_EXIT 01 /* exit after evaluating tree */ |
| 7922 | #define EV_TESTED 02 /* exit status is checked; ignore -e flag */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 7923 | #define EV_BACKCMD 04 /* command executing within back quotes */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 7924 | |
Denys Vlasenko | 0e5e4ea | 2009-10-11 00:36:20 +0200 | [diff] [blame] | 7925 | static const uint8_t nodesize[N_NUMBER] = { |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7926 | [NCMD ] = SHELL_ALIGN(sizeof(struct ncmd)), |
| 7927 | [NPIPE ] = SHELL_ALIGN(sizeof(struct npipe)), |
| 7928 | [NREDIR ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7929 | [NBACKGND ] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7930 | [NSUBSHELL] = SHELL_ALIGN(sizeof(struct nredir)), |
| 7931 | [NAND ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7932 | [NOR ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7933 | [NSEMI ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7934 | [NIF ] = SHELL_ALIGN(sizeof(struct nif)), |
| 7935 | [NWHILE ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7936 | [NUNTIL ] = SHELL_ALIGN(sizeof(struct nbinary)), |
| 7937 | [NFOR ] = SHELL_ALIGN(sizeof(struct nfor)), |
| 7938 | [NCASE ] = SHELL_ALIGN(sizeof(struct ncase)), |
| 7939 | [NCLIST ] = SHELL_ALIGN(sizeof(struct nclist)), |
| 7940 | [NDEFUN ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7941 | [NARG ] = SHELL_ALIGN(sizeof(struct narg)), |
| 7942 | [NTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7943 | #if ENABLE_ASH_BASH_COMPAT |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7944 | [NTO2 ] = SHELL_ALIGN(sizeof(struct nfile)), |
Denis Vlasenko | cc5feab | 2008-11-22 01:32:40 +0000 | [diff] [blame] | 7945 | #endif |
Denis Vlasenko | 340299a | 2008-11-21 10:36:36 +0000 | [diff] [blame] | 7946 | [NCLOBBER ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7947 | [NFROM ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7948 | [NFROMTO ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7949 | [NAPPEND ] = SHELL_ALIGN(sizeof(struct nfile)), |
| 7950 | [NTOFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7951 | [NFROMFD ] = SHELL_ALIGN(sizeof(struct ndup)), |
| 7952 | [NHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7953 | [NXHERE ] = SHELL_ALIGN(sizeof(struct nhere)), |
| 7954 | [NNOT ] = SHELL_ALIGN(sizeof(struct nnot)), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 7955 | }; |
| 7956 | |
| 7957 | static void calcsize(union node *n); |
| 7958 | |
| 7959 | static void |
| 7960 | sizenodelist(struct nodelist *lp) |
| 7961 | { |
| 7962 | while (lp) { |
| 7963 | funcblocksize += SHELL_ALIGN(sizeof(struct nodelist)); |
| 7964 | calcsize(lp->n); |
| 7965 | lp = lp->next; |
| 7966 | } |
| 7967 | } |
| 7968 | |
| 7969 | static void |
| 7970 | calcsize(union node *n) |
| 7971 | { |
| 7972 | if (n == NULL) |
| 7973 | return; |
| 7974 | funcblocksize += nodesize[n->type]; |
| 7975 | switch (n->type) { |
| 7976 | case NCMD: |
| 7977 | calcsize(n->ncmd.redirect); |
| 7978 | calcsize(n->ncmd.args); |
| 7979 | calcsize(n->ncmd.assign); |
| 7980 | break; |
| 7981 | case NPIPE: |
| 7982 | sizenodelist(n->npipe.cmdlist); |
| 7983 | break; |
| 7984 | case NREDIR: |
| 7985 | case NBACKGND: |
| 7986 | case NSUBSHELL: |
| 7987 | calcsize(n->nredir.redirect); |
| 7988 | calcsize(n->nredir.n); |
| 7989 | break; |
| 7990 | case NAND: |
| 7991 | case NOR: |
| 7992 | case NSEMI: |
| 7993 | case NWHILE: |
| 7994 | case NUNTIL: |
| 7995 | calcsize(n->nbinary.ch2); |
| 7996 | calcsize(n->nbinary.ch1); |
| 7997 | break; |
| 7998 | case NIF: |
| 7999 | calcsize(n->nif.elsepart); |
| 8000 | calcsize(n->nif.ifpart); |
| 8001 | calcsize(n->nif.test); |
| 8002 | break; |
| 8003 | case NFOR: |
| 8004 | funcstringsize += strlen(n->nfor.var) + 1; |
| 8005 | calcsize(n->nfor.body); |
| 8006 | calcsize(n->nfor.args); |
| 8007 | break; |
| 8008 | case NCASE: |
| 8009 | calcsize(n->ncase.cases); |
| 8010 | calcsize(n->ncase.expr); |
| 8011 | break; |
| 8012 | case NCLIST: |
| 8013 | calcsize(n->nclist.body); |
| 8014 | calcsize(n->nclist.pattern); |
| 8015 | calcsize(n->nclist.next); |
| 8016 | break; |
| 8017 | case NDEFUN: |
| 8018 | case NARG: |
| 8019 | sizenodelist(n->narg.backquote); |
| 8020 | funcstringsize += strlen(n->narg.text) + 1; |
| 8021 | calcsize(n->narg.next); |
| 8022 | break; |
| 8023 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8024 | #if ENABLE_ASH_BASH_COMPAT |
| 8025 | case NTO2: |
| 8026 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8027 | case NCLOBBER: |
| 8028 | case NFROM: |
| 8029 | case NFROMTO: |
| 8030 | case NAPPEND: |
| 8031 | calcsize(n->nfile.fname); |
| 8032 | calcsize(n->nfile.next); |
| 8033 | break; |
| 8034 | case NTOFD: |
| 8035 | case NFROMFD: |
| 8036 | calcsize(n->ndup.vname); |
| 8037 | calcsize(n->ndup.next); |
| 8038 | break; |
| 8039 | case NHERE: |
| 8040 | case NXHERE: |
| 8041 | calcsize(n->nhere.doc); |
| 8042 | calcsize(n->nhere.next); |
| 8043 | break; |
| 8044 | case NNOT: |
| 8045 | calcsize(n->nnot.com); |
| 8046 | break; |
| 8047 | }; |
| 8048 | } |
| 8049 | |
| 8050 | static char * |
| 8051 | nodeckstrdup(char *s) |
| 8052 | { |
| 8053 | char *rtn = funcstring; |
| 8054 | |
| 8055 | strcpy(funcstring, s); |
| 8056 | funcstring += strlen(s) + 1; |
| 8057 | return rtn; |
| 8058 | } |
| 8059 | |
| 8060 | static union node *copynode(union node *); |
| 8061 | |
| 8062 | static struct nodelist * |
| 8063 | copynodelist(struct nodelist *lp) |
| 8064 | { |
| 8065 | struct nodelist *start; |
| 8066 | struct nodelist **lpp; |
| 8067 | |
| 8068 | lpp = &start; |
| 8069 | while (lp) { |
| 8070 | *lpp = funcblock; |
| 8071 | funcblock = (char *) funcblock + SHELL_ALIGN(sizeof(struct nodelist)); |
| 8072 | (*lpp)->n = copynode(lp->n); |
| 8073 | lp = lp->next; |
| 8074 | lpp = &(*lpp)->next; |
| 8075 | } |
| 8076 | *lpp = NULL; |
| 8077 | return start; |
| 8078 | } |
| 8079 | |
| 8080 | static union node * |
| 8081 | copynode(union node *n) |
| 8082 | { |
| 8083 | union node *new; |
| 8084 | |
| 8085 | if (n == NULL) |
| 8086 | return NULL; |
| 8087 | new = funcblock; |
| 8088 | funcblock = (char *) funcblock + nodesize[n->type]; |
| 8089 | |
| 8090 | switch (n->type) { |
| 8091 | case NCMD: |
| 8092 | new->ncmd.redirect = copynode(n->ncmd.redirect); |
| 8093 | new->ncmd.args = copynode(n->ncmd.args); |
| 8094 | new->ncmd.assign = copynode(n->ncmd.assign); |
| 8095 | break; |
| 8096 | case NPIPE: |
| 8097 | new->npipe.cmdlist = copynodelist(n->npipe.cmdlist); |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8098 | new->npipe.pipe_backgnd = n->npipe.pipe_backgnd; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8099 | break; |
| 8100 | case NREDIR: |
| 8101 | case NBACKGND: |
| 8102 | case NSUBSHELL: |
| 8103 | new->nredir.redirect = copynode(n->nredir.redirect); |
| 8104 | new->nredir.n = copynode(n->nredir.n); |
| 8105 | break; |
| 8106 | case NAND: |
| 8107 | case NOR: |
| 8108 | case NSEMI: |
| 8109 | case NWHILE: |
| 8110 | case NUNTIL: |
| 8111 | new->nbinary.ch2 = copynode(n->nbinary.ch2); |
| 8112 | new->nbinary.ch1 = copynode(n->nbinary.ch1); |
| 8113 | break; |
| 8114 | case NIF: |
| 8115 | new->nif.elsepart = copynode(n->nif.elsepart); |
| 8116 | new->nif.ifpart = copynode(n->nif.ifpart); |
| 8117 | new->nif.test = copynode(n->nif.test); |
| 8118 | break; |
| 8119 | case NFOR: |
| 8120 | new->nfor.var = nodeckstrdup(n->nfor.var); |
| 8121 | new->nfor.body = copynode(n->nfor.body); |
| 8122 | new->nfor.args = copynode(n->nfor.args); |
| 8123 | break; |
| 8124 | case NCASE: |
| 8125 | new->ncase.cases = copynode(n->ncase.cases); |
| 8126 | new->ncase.expr = copynode(n->ncase.expr); |
| 8127 | break; |
| 8128 | case NCLIST: |
| 8129 | new->nclist.body = copynode(n->nclist.body); |
| 8130 | new->nclist.pattern = copynode(n->nclist.pattern); |
| 8131 | new->nclist.next = copynode(n->nclist.next); |
| 8132 | break; |
| 8133 | case NDEFUN: |
| 8134 | case NARG: |
| 8135 | new->narg.backquote = copynodelist(n->narg.backquote); |
| 8136 | new->narg.text = nodeckstrdup(n->narg.text); |
| 8137 | new->narg.next = copynode(n->narg.next); |
| 8138 | break; |
| 8139 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8140 | #if ENABLE_ASH_BASH_COMPAT |
| 8141 | case NTO2: |
| 8142 | #endif |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8143 | case NCLOBBER: |
| 8144 | case NFROM: |
| 8145 | case NFROMTO: |
| 8146 | case NAPPEND: |
| 8147 | new->nfile.fname = copynode(n->nfile.fname); |
| 8148 | new->nfile.fd = n->nfile.fd; |
| 8149 | new->nfile.next = copynode(n->nfile.next); |
| 8150 | break; |
| 8151 | case NTOFD: |
| 8152 | case NFROMFD: |
| 8153 | new->ndup.vname = copynode(n->ndup.vname); |
| 8154 | new->ndup.dupfd = n->ndup.dupfd; |
| 8155 | new->ndup.fd = n->ndup.fd; |
| 8156 | new->ndup.next = copynode(n->ndup.next); |
| 8157 | break; |
| 8158 | case NHERE: |
| 8159 | case NXHERE: |
| 8160 | new->nhere.doc = copynode(n->nhere.doc); |
| 8161 | new->nhere.fd = n->nhere.fd; |
| 8162 | new->nhere.next = copynode(n->nhere.next); |
| 8163 | break; |
| 8164 | case NNOT: |
| 8165 | new->nnot.com = copynode(n->nnot.com); |
| 8166 | break; |
| 8167 | }; |
| 8168 | new->type = n->type; |
| 8169 | return new; |
| 8170 | } |
| 8171 | |
| 8172 | /* |
| 8173 | * Make a copy of a parse tree. |
| 8174 | */ |
| 8175 | static struct funcnode * |
| 8176 | copyfunc(union node *n) |
| 8177 | { |
| 8178 | struct funcnode *f; |
| 8179 | size_t blocksize; |
| 8180 | |
| 8181 | funcblocksize = offsetof(struct funcnode, n); |
| 8182 | funcstringsize = 0; |
| 8183 | calcsize(n); |
| 8184 | blocksize = funcblocksize; |
| 8185 | f = ckmalloc(blocksize + funcstringsize); |
| 8186 | funcblock = (char *) f + offsetof(struct funcnode, n); |
| 8187 | funcstring = (char *) f + blocksize; |
| 8188 | copynode(n); |
| 8189 | f->count = 0; |
| 8190 | return f; |
| 8191 | } |
| 8192 | |
| 8193 | /* |
| 8194 | * Define a shell function. |
| 8195 | */ |
| 8196 | static void |
| 8197 | defun(char *name, union node *func) |
| 8198 | { |
| 8199 | struct cmdentry entry; |
| 8200 | |
| 8201 | INT_OFF; |
| 8202 | entry.cmdtype = CMDFUNCTION; |
| 8203 | entry.u.func = copyfunc(func); |
| 8204 | addcmdentry(name, &entry); |
| 8205 | INT_ON; |
| 8206 | } |
| 8207 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8208 | /* Reasons for skipping commands (see comment on breakcmd routine) */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8209 | #define SKIPBREAK (1 << 0) |
| 8210 | #define SKIPCONT (1 << 1) |
| 8211 | #define SKIPFUNC (1 << 2) |
| 8212 | #define SKIPFILE (1 << 3) |
| 8213 | #define SKIPEVAL (1 << 4) |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8214 | static smallint evalskip; /* set to SKIPxxx if we are skipping commands */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8215 | static int skipcount; /* number of levels to skip */ |
| 8216 | static int funcnest; /* depth of function calls */ |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 8217 | static int loopnest; /* current loop nesting level */ |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8218 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8219 | /* Forward decl way out to parsing code - dotrap needs it */ |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8220 | static int evalstring(char *s, int mask); |
| 8221 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8222 | /* Called to execute a trap. |
| 8223 | * Single callsite - at the end of evaltree(). |
| 8224 | * If we return non-zero, exaltree raises EXEXIT exception. |
| 8225 | * |
| 8226 | * Perhaps we should avoid entering new trap handlers |
| 8227 | * while we are executing a trap handler. [is it a TODO?] |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8228 | */ |
| 8229 | static int |
| 8230 | dotrap(void) |
| 8231 | { |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8232 | uint8_t *g; |
| 8233 | int sig; |
| 8234 | uint8_t savestatus; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8235 | |
| 8236 | savestatus = exitstatus; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8237 | pending_sig = 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8238 | xbarrier(); |
| 8239 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8240 | TRACE(("dotrap entered\n")); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8241 | for (sig = 1, g = gotsig; sig < NSIG; sig++, g++) { |
| 8242 | int want_exexit; |
| 8243 | char *t; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8244 | |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8245 | if (*g == 0) |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8246 | continue; |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8247 | t = trap[sig]; |
| 8248 | /* non-trapped SIGINT is handled separately by raise_interrupt, |
| 8249 | * don't upset it by resetting gotsig[SIGINT-1] */ |
| 8250 | if (sig == SIGINT && !t) |
| 8251 | continue; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8252 | |
| 8253 | TRACE(("sig %d is active, will run handler '%s'\n", sig, t)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8254 | *g = 0; |
| 8255 | if (!t) |
| 8256 | continue; |
| 8257 | want_exexit = evalstring(t, SKIPEVAL); |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8258 | exitstatus = savestatus; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8259 | if (want_exexit) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8260 | TRACE(("dotrap returns %d\n", want_exexit)); |
Denis Vlasenko | 4b87570 | 2009-03-19 13:30:04 +0000 | [diff] [blame] | 8261 | return want_exexit; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8262 | } |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8263 | } |
| 8264 | |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8265 | TRACE(("dotrap returns 0\n")); |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 8266 | return 0; |
Denis Vlasenko | fc06f29 | 2007-02-23 21:09:35 +0000 | [diff] [blame] | 8267 | } |
| 8268 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 8269 | /* forward declarations - evaluation is fairly recursive business... */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8270 | static void evalloop(union node *, int); |
| 8271 | static void evalfor(union node *, int); |
| 8272 | static void evalcase(union node *, int); |
| 8273 | static void evalsubshell(union node *, int); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8274 | static void expredir(union node *); |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8275 | static void evalpipe(union node *, int); |
| 8276 | static void evalcommand(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8277 | static int evalbltin(const struct builtincmd *, int, char **); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8278 | static void prehash(union node *); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 8279 | |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8280 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8281 | * Evaluate a parse tree. The value is left in the global variable |
| 8282 | * exitstatus. |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8283 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8284 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8285 | evaltree(union node *n, int flags) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8286 | { |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8287 | struct jmploc *volatile savehandler = exception_handler; |
| 8288 | struct jmploc jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8289 | int checkexit = 0; |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8290 | void (*evalfn)(union node *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8291 | int status; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8292 | int int_level; |
| 8293 | |
| 8294 | SAVE_INT(int_level); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8295 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8296 | if (n == NULL) { |
| 8297 | TRACE(("evaltree(NULL) called\n")); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8298 | goto out1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8299 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8300 | TRACE(("evaltree(%p: %d, %d) called\n", n, n->type, flags)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8301 | |
| 8302 | exception_handler = &jmploc; |
| 8303 | { |
| 8304 | int err = setjmp(jmploc.loc); |
| 8305 | if (err) { |
| 8306 | /* if it was a signal, check for trap handlers */ |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8307 | if (exception_type == EXSIG) { |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8308 | TRACE(("exception %d (EXSIG) in evaltree, err=%d\n", |
| 8309 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8310 | goto out; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8311 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8312 | /* continue on the way out */ |
Denis Vlasenko | b21f379 | 2009-03-19 23:09:58 +0000 | [diff] [blame] | 8313 | TRACE(("exception %d in evaltree, propagating err=%d\n", |
| 8314 | exception_type, err)); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8315 | exception_handler = savehandler; |
| 8316 | longjmp(exception_handler->loc, err); |
| 8317 | } |
| 8318 | } |
| 8319 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8320 | switch (n->type) { |
| 8321 | default: |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 8322 | #if DEBUG |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8323 | out1fmt("Node type = %d\n", n->type); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8324 | fflush_all(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8325 | break; |
| 8326 | #endif |
| 8327 | case NNOT: |
| 8328 | evaltree(n->nnot.com, EV_TESTED); |
| 8329 | status = !exitstatus; |
| 8330 | goto setstatus; |
| 8331 | case NREDIR: |
| 8332 | expredir(n->nredir.redirect); |
| 8333 | status = redirectsafe(n->nredir.redirect, REDIR_PUSH); |
| 8334 | if (!status) { |
| 8335 | evaltree(n->nredir.n, flags & EV_TESTED); |
| 8336 | status = exitstatus; |
| 8337 | } |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 8338 | popredir(/*drop:*/ 0, /*restore:*/ 0 /* not sure */); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8339 | goto setstatus; |
| 8340 | case NCMD: |
| 8341 | evalfn = evalcommand; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8342 | checkexit: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8343 | if (eflag && !(flags & EV_TESTED)) |
| 8344 | checkexit = ~0; |
| 8345 | goto calleval; |
| 8346 | case NFOR: |
| 8347 | evalfn = evalfor; |
| 8348 | goto calleval; |
| 8349 | case NWHILE: |
| 8350 | case NUNTIL: |
| 8351 | evalfn = evalloop; |
| 8352 | goto calleval; |
| 8353 | case NSUBSHELL: |
| 8354 | case NBACKGND: |
| 8355 | evalfn = evalsubshell; |
| 8356 | goto calleval; |
| 8357 | case NPIPE: |
| 8358 | evalfn = evalpipe; |
| 8359 | goto checkexit; |
| 8360 | case NCASE: |
| 8361 | evalfn = evalcase; |
| 8362 | goto calleval; |
| 8363 | case NAND: |
| 8364 | case NOR: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8365 | case NSEMI: { |
| 8366 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8367 | #if NAND + 1 != NOR |
| 8368 | #error NAND + 1 != NOR |
| 8369 | #endif |
| 8370 | #if NOR + 1 != NSEMI |
| 8371 | #error NOR + 1 != NSEMI |
| 8372 | #endif |
Denis Vlasenko | 87d5fd9 | 2008-07-26 13:48:35 +0000 | [diff] [blame] | 8373 | unsigned is_or = n->type - NAND; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8374 | evaltree( |
| 8375 | n->nbinary.ch1, |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8376 | (flags | ((is_or >> 1) - 1)) & EV_TESTED |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8377 | ); |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8378 | if (!exitstatus == is_or) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8379 | break; |
| 8380 | if (!evalskip) { |
| 8381 | n = n->nbinary.ch2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8382 | evaln: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8383 | evalfn = evaltree; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8384 | calleval: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8385 | evalfn(n, flags); |
| 8386 | break; |
| 8387 | } |
| 8388 | break; |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8389 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8390 | case NIF: |
| 8391 | evaltree(n->nif.test, EV_TESTED); |
| 8392 | if (evalskip) |
| 8393 | break; |
| 8394 | if (exitstatus == 0) { |
| 8395 | n = n->nif.ifpart; |
| 8396 | goto evaln; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8397 | } |
| 8398 | if (n->nif.elsepart) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8399 | n = n->nif.elsepart; |
| 8400 | goto evaln; |
| 8401 | } |
| 8402 | goto success; |
| 8403 | case NDEFUN: |
| 8404 | defun(n->narg.text, n->narg.next); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8405 | success: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8406 | status = 0; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8407 | setstatus: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8408 | exitstatus = status; |
| 8409 | break; |
| 8410 | } |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8411 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8412 | out: |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 8413 | exception_handler = savehandler; |
| 8414 | out1: |
| 8415 | if (checkexit & exitstatus) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8416 | evalskip |= SKIPEVAL; |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 8417 | else if (pending_sig && dotrap()) |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8418 | goto exexit; |
| 8419 | |
| 8420 | if (flags & EV_EXIT) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8421 | exexit: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8422 | raise_exception(EXEXIT); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 8423 | } |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 8424 | |
| 8425 | RESTORE_INT(int_level); |
| 8426 | TRACE(("leaving evaltree (no interrupts)\n")); |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 8427 | } |
| 8428 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8429 | #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3) |
| 8430 | static |
| 8431 | #endif |
| 8432 | void evaltreenr(union node *, int) __attribute__ ((alias("evaltree"),__noreturn__)); |
| 8433 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8434 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8435 | evalloop(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8436 | { |
| 8437 | int status; |
| 8438 | |
| 8439 | loopnest++; |
| 8440 | status = 0; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8441 | flags &= EV_TESTED; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8442 | for (;;) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8443 | int i; |
| 8444 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8445 | evaltree(n->nbinary.ch1, EV_TESTED); |
| 8446 | if (evalskip) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8447 | skipping: |
| 8448 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8449 | evalskip = 0; |
| 8450 | continue; |
| 8451 | } |
| 8452 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8453 | evalskip = 0; |
| 8454 | break; |
| 8455 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8456 | i = exitstatus; |
| 8457 | if (n->type != NWHILE) |
| 8458 | i = !i; |
| 8459 | if (i != 0) |
| 8460 | break; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8461 | evaltree(n->nbinary.ch2, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8462 | status = exitstatus; |
| 8463 | if (evalskip) |
| 8464 | goto skipping; |
| 8465 | } |
| 8466 | loopnest--; |
| 8467 | exitstatus = status; |
| 8468 | } |
| 8469 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8470 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8471 | evalfor(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8472 | { |
| 8473 | struct arglist arglist; |
| 8474 | union node *argp; |
| 8475 | struct strlist *sp; |
| 8476 | struct stackmark smark; |
| 8477 | |
| 8478 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8479 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8480 | arglist.lastp = &arglist.list; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8481 | for (argp = n->nfor.args; argp; argp = argp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8482 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE | EXP_RECORD); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8483 | /* XXX */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8484 | if (evalskip) |
| 8485 | goto out; |
| 8486 | } |
| 8487 | *arglist.lastp = NULL; |
| 8488 | |
| 8489 | exitstatus = 0; |
| 8490 | loopnest++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8491 | flags &= EV_TESTED; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8492 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8493 | setvar(n->nfor.var, sp->text, 0); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8494 | evaltree(n->nfor.body, flags); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8495 | if (evalskip) { |
| 8496 | if (evalskip == SKIPCONT && --skipcount <= 0) { |
| 8497 | evalskip = 0; |
| 8498 | continue; |
| 8499 | } |
| 8500 | if (evalskip == SKIPBREAK && --skipcount <= 0) |
| 8501 | evalskip = 0; |
| 8502 | break; |
| 8503 | } |
| 8504 | } |
| 8505 | loopnest--; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8506 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8507 | popstackmark(&smark); |
| 8508 | } |
| 8509 | |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8510 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8511 | evalcase(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8512 | { |
| 8513 | union node *cp; |
| 8514 | union node *patp; |
| 8515 | struct arglist arglist; |
| 8516 | struct stackmark smark; |
| 8517 | |
| 8518 | setstackmark(&smark); |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8519 | arglist.list = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8520 | arglist.lastp = &arglist.list; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8521 | expandarg(n->ncase.expr, &arglist, EXP_TILDE); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8522 | exitstatus = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8523 | for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) { |
| 8524 | for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8525 | if (casematch(patp, arglist.list->text)) { |
| 8526 | if (evalskip == 0) { |
| 8527 | evaltree(cp->nclist.body, flags); |
| 8528 | } |
| 8529 | goto out; |
| 8530 | } |
| 8531 | } |
| 8532 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8533 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8534 | popstackmark(&smark); |
| 8535 | } |
| 8536 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8537 | /* |
| 8538 | * Kick off a subshell to evaluate a tree. |
| 8539 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8540 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8541 | evalsubshell(union node *n, int flags) |
| 8542 | { |
| 8543 | struct job *jp; |
| 8544 | int backgnd = (n->type == NBACKGND); |
| 8545 | int status; |
| 8546 | |
| 8547 | expredir(n->nredir.redirect); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8548 | if (!backgnd && (flags & EV_EXIT) && !may_have_traps) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8549 | goto nofork; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8550 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8551 | jp = makejob(/*n,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8552 | if (forkshell(jp, n, backgnd) == 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8553 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8554 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8555 | flags |= EV_EXIT; |
| 8556 | if (backgnd) |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 8557 | flags &= ~EV_TESTED; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 8558 | nofork: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8559 | redirect(n->nredir.redirect, 0); |
| 8560 | evaltreenr(n->nredir.n, flags); |
| 8561 | /* never returns */ |
| 8562 | } |
| 8563 | status = 0; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8564 | if (!backgnd) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8565 | status = waitforjob(jp); |
| 8566 | exitstatus = status; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8567 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8570 | /* |
| 8571 | * Compute the names of the files in a redirection list. |
| 8572 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 8573 | static void fixredir(union node *, const char *, int); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8574 | static void |
| 8575 | expredir(union node *n) |
| 8576 | { |
| 8577 | union node *redir; |
| 8578 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8579 | for (redir = n; redir; redir = redir->nfile.next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8580 | struct arglist fn; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8581 | |
Denis Vlasenko | c12d51e | 2008-02-19 23:31:05 +0000 | [diff] [blame] | 8582 | fn.list = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8583 | fn.lastp = &fn.list; |
| 8584 | switch (redir->type) { |
| 8585 | case NFROMTO: |
| 8586 | case NFROM: |
| 8587 | case NTO: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8588 | #if ENABLE_ASH_BASH_COMPAT |
| 8589 | case NTO2: |
| 8590 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8591 | case NCLOBBER: |
| 8592 | case NAPPEND: |
| 8593 | expandarg(redir->nfile.fname, &fn, EXP_TILDE | EXP_REDIR); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8594 | #if ENABLE_ASH_BASH_COMPAT |
| 8595 | store_expfname: |
| 8596 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8597 | redir->nfile.expfname = fn.list->text; |
| 8598 | break; |
| 8599 | case NFROMFD: |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8600 | case NTOFD: /* >& */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8601 | if (redir->ndup.vname) { |
| 8602 | expandarg(redir->ndup.vname, &fn, EXP_FULL | EXP_TILDE); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8603 | if (fn.list == NULL) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8604 | ash_msg_and_raise_error("redir error"); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 8605 | #if ENABLE_ASH_BASH_COMPAT |
| 8606 | //FIXME: we used expandarg with different args! |
| 8607 | if (!isdigit_str9(fn.list->text)) { |
| 8608 | /* >&file, not >&fd */ |
| 8609 | if (redir->nfile.fd != 1) /* 123>&file - BAD */ |
| 8610 | ash_msg_and_raise_error("redir error"); |
| 8611 | redir->type = NTO2; |
| 8612 | goto store_expfname; |
| 8613 | } |
| 8614 | #endif |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8615 | fixredir(redir, fn.list->text, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8616 | } |
| 8617 | break; |
| 8618 | } |
| 8619 | } |
| 8620 | } |
| 8621 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8622 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8623 | * Evaluate a pipeline. All the processes in the pipeline are children |
| 8624 | * of the process creating the pipeline. (This differs from some versions |
| 8625 | * of the shell, which make the last process in a pipeline the parent |
| 8626 | * of all the rest.) |
| 8627 | */ |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 8628 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8629 | evalpipe(union node *n, int flags) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8630 | { |
| 8631 | struct job *jp; |
| 8632 | struct nodelist *lp; |
| 8633 | int pipelen; |
| 8634 | int prevfd; |
| 8635 | int pip[2]; |
| 8636 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8637 | TRACE(("evalpipe(0x%lx) called\n", (long)n)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8638 | pipelen = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8639 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8640 | pipelen++; |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8641 | flags |= EV_EXIT; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8642 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8643 | jp = makejob(/*n,*/ pipelen); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8644 | prevfd = -1; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 8645 | for (lp = n->npipe.cmdlist; lp; lp = lp->next) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8646 | prehash(lp->n); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8647 | pip[1] = -1; |
| 8648 | if (lp->next) { |
| 8649 | if (pipe(pip) < 0) { |
| 8650 | close(prevfd); |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 8651 | ash_msg_and_raise_error("pipe call failed"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8652 | } |
| 8653 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8654 | if (forkshell(jp, lp->n, n->npipe.pipe_backgnd) == 0) { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8655 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8656 | if (pip[1] >= 0) { |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8657 | close(pip[0]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8658 | } |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 8659 | if (prevfd > 0) { |
| 8660 | dup2(prevfd, 0); |
| 8661 | close(prevfd); |
| 8662 | } |
| 8663 | if (pip[1] > 1) { |
| 8664 | dup2(pip[1], 1); |
| 8665 | close(pip[1]); |
| 8666 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8667 | evaltreenr(lp->n, flags); |
| 8668 | /* never returns */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8669 | } |
| 8670 | if (prevfd >= 0) |
| 8671 | close(prevfd); |
| 8672 | prevfd = pip[0]; |
Denis Vlasenko | b9e70dd | 2009-03-20 01:24:08 +0000 | [diff] [blame] | 8673 | /* Don't want to trigger debugging */ |
| 8674 | if (pip[1] != -1) |
| 8675 | close(pip[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8676 | } |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 8677 | if (n->npipe.pipe_backgnd == 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8678 | exitstatus = waitforjob(jp); |
| 8679 | TRACE(("evalpipe: job done exit status %d\n", exitstatus)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8680 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 8681 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 8682 | } |
| 8683 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8684 | /* |
| 8685 | * Controls whether the shell is interactive or not. |
| 8686 | */ |
| 8687 | static void |
| 8688 | setinteractive(int on) |
| 8689 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8690 | static smallint is_interactive; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8691 | |
| 8692 | if (++on == is_interactive) |
| 8693 | return; |
| 8694 | is_interactive = on; |
| 8695 | setsignal(SIGINT); |
| 8696 | setsignal(SIGQUIT); |
| 8697 | setsignal(SIGTERM); |
| 8698 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
| 8699 | if (is_interactive > 1) { |
| 8700 | /* Looks like they want an interactive shell */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8701 | static smallint did_banner; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8702 | |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8703 | if (!did_banner) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8704 | /* note: ash and hush share this string */ |
| 8705 | out1fmt("\n\n%s %s\n" |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8706 | "Enter 'help' for a list of built-in commands." |
| 8707 | "\n\n", |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 8708 | bb_banner, |
| 8709 | "built-in shell (ash)" |
| 8710 | ); |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 8711 | did_banner = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8712 | } |
| 8713 | } |
| 8714 | #endif |
| 8715 | } |
| 8716 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8717 | static void |
| 8718 | optschanged(void) |
| 8719 | { |
| 8720 | #if DEBUG |
| 8721 | opentrace(); |
| 8722 | #endif |
| 8723 | setinteractive(iflag); |
| 8724 | setjobctl(mflag); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 8725 | #if ENABLE_FEATURE_EDITING_VI |
| 8726 | if (viflag) |
| 8727 | line_input_state->flags |= VI_MODE; |
| 8728 | else |
| 8729 | line_input_state->flags &= ~VI_MODE; |
| 8730 | #else |
| 8731 | viflag = 0; /* forcibly keep the option off */ |
| 8732 | #endif |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 8733 | } |
| 8734 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8735 | static struct localvar *localvars; |
| 8736 | |
| 8737 | /* |
| 8738 | * Called after a function returns. |
| 8739 | * Interrupts must be off. |
| 8740 | */ |
| 8741 | static void |
| 8742 | poplocalvars(void) |
| 8743 | { |
| 8744 | struct localvar *lvp; |
| 8745 | struct var *vp; |
| 8746 | |
| 8747 | while ((lvp = localvars) != NULL) { |
| 8748 | localvars = lvp->next; |
| 8749 | vp = lvp->vp; |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 8750 | TRACE(("poplocalvar %s\n", vp ? vp->text : "-")); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8751 | if (vp == NULL) { /* $- saved */ |
| 8752 | memcpy(optlist, lvp->text, sizeof(optlist)); |
| 8753 | free((char*)lvp->text); |
| 8754 | optschanged(); |
| 8755 | } else if ((lvp->flags & (VUNSET|VSTRFIXED)) == VUNSET) { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8756 | unsetvar(vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8757 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8758 | if (vp->var_func) |
| 8759 | vp->var_func(var_end(lvp->text)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8760 | if ((vp->flags & (VTEXTFIXED|VSTACK)) == 0) |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8761 | free((char*)vp->var_text); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8762 | vp->flags = lvp->flags; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8763 | vp->var_text = lvp->text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8764 | } |
| 8765 | free(lvp); |
| 8766 | } |
| 8767 | } |
| 8768 | |
| 8769 | static int |
| 8770 | evalfun(struct funcnode *func, int argc, char **argv, int flags) |
| 8771 | { |
| 8772 | volatile struct shparam saveparam; |
| 8773 | struct localvar *volatile savelocalvars; |
| 8774 | struct jmploc *volatile savehandler; |
| 8775 | struct jmploc jmploc; |
| 8776 | int e; |
| 8777 | |
| 8778 | saveparam = shellparam; |
| 8779 | savelocalvars = localvars; |
| 8780 | e = setjmp(jmploc.loc); |
| 8781 | if (e) { |
| 8782 | goto funcdone; |
| 8783 | } |
| 8784 | INT_OFF; |
| 8785 | savehandler = exception_handler; |
| 8786 | exception_handler = &jmploc; |
| 8787 | localvars = NULL; |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8788 | shellparam.malloced = 0; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8789 | func->count++; |
| 8790 | funcnest++; |
| 8791 | INT_ON; |
| 8792 | shellparam.nparam = argc - 1; |
| 8793 | shellparam.p = argv + 1; |
| 8794 | #if ENABLE_ASH_GETOPTS |
| 8795 | shellparam.optind = 1; |
| 8796 | shellparam.optoff = -1; |
| 8797 | #endif |
| 8798 | evaltree(&func->n, flags & EV_TESTED); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 8799 | funcdone: |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8800 | INT_OFF; |
| 8801 | funcnest--; |
| 8802 | freefunc(func); |
| 8803 | poplocalvars(); |
| 8804 | localvars = savelocalvars; |
| 8805 | freeparam(&shellparam); |
| 8806 | shellparam = saveparam; |
| 8807 | exception_handler = savehandler; |
| 8808 | INT_ON; |
| 8809 | evalskip &= ~SKIPFUNC; |
| 8810 | return e; |
| 8811 | } |
| 8812 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 8813 | #if ENABLE_ASH_CMDCMD |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 8814 | static char ** |
| 8815 | parse_command_args(char **argv, const char **path) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8816 | { |
| 8817 | char *cp, c; |
| 8818 | |
| 8819 | for (;;) { |
| 8820 | cp = *++argv; |
| 8821 | if (!cp) |
| 8822 | return 0; |
| 8823 | if (*cp++ != '-') |
| 8824 | break; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 8825 | c = *cp++; |
| 8826 | if (!c) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8827 | break; |
| 8828 | if (c == '-' && !*cp) { |
| 8829 | argv++; |
| 8830 | break; |
| 8831 | } |
| 8832 | do { |
| 8833 | switch (c) { |
| 8834 | case 'p': |
Denis Vlasenko | f5f75c5 | 2007-06-12 22:35:19 +0000 | [diff] [blame] | 8835 | *path = bb_default_path; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8836 | break; |
| 8837 | default: |
| 8838 | /* run 'typecmd' for other options */ |
| 8839 | return 0; |
| 8840 | } |
Denis Vlasenko | 9650f36 | 2007-02-23 01:04:37 +0000 | [diff] [blame] | 8841 | c = *cp++; |
| 8842 | } while (c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 8843 | } |
| 8844 | return argv; |
| 8845 | } |
| 8846 | #endif |
| 8847 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8848 | /* |
| 8849 | * Make a variable a local variable. When a variable is made local, it's |
| 8850 | * value and flags are saved in a localvar structure. The saved values |
| 8851 | * will be restored when the shell function returns. We handle the name |
| 8852 | * "-" as a special case. |
| 8853 | */ |
| 8854 | static void |
| 8855 | mklocal(char *name) |
| 8856 | { |
| 8857 | struct localvar *lvp; |
| 8858 | struct var **vpp; |
| 8859 | struct var *vp; |
| 8860 | |
| 8861 | INT_OFF; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 8862 | lvp = ckzalloc(sizeof(struct localvar)); |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8863 | if (LONE_DASH(name)) { |
| 8864 | char *p; |
| 8865 | p = ckmalloc(sizeof(optlist)); |
| 8866 | lvp->text = memcpy(p, optlist, sizeof(optlist)); |
| 8867 | vp = NULL; |
| 8868 | } else { |
| 8869 | char *eq; |
| 8870 | |
| 8871 | vpp = hashvar(name); |
| 8872 | vp = *findvar(vpp, name); |
| 8873 | eq = strchr(name, '='); |
| 8874 | if (vp == NULL) { |
| 8875 | if (eq) |
| 8876 | setvareq(name, VSTRFIXED); |
| 8877 | else |
| 8878 | setvar(name, NULL, VSTRFIXED); |
| 8879 | vp = *vpp; /* the new variable */ |
| 8880 | lvp->flags = VUNSET; |
| 8881 | } else { |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 8882 | lvp->text = vp->var_text; |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8883 | lvp->flags = vp->flags; |
| 8884 | vp->flags |= VSTRFIXED|VTEXTFIXED; |
| 8885 | if (eq) |
| 8886 | setvareq(name, 0); |
| 8887 | } |
| 8888 | } |
| 8889 | lvp->vp = vp; |
| 8890 | lvp->next = localvars; |
| 8891 | localvars = lvp; |
| 8892 | INT_ON; |
| 8893 | } |
| 8894 | |
| 8895 | /* |
| 8896 | * The "local" command. |
| 8897 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8898 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8899 | localcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8900 | { |
| 8901 | char *name; |
| 8902 | |
| 8903 | argv = argptr; |
| 8904 | while ((name = *argv++) != NULL) { |
| 8905 | mklocal(name); |
| 8906 | } |
| 8907 | return 0; |
| 8908 | } |
| 8909 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8910 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8911 | falsecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8912 | { |
| 8913 | return 1; |
| 8914 | } |
| 8915 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8916 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8917 | truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8918 | { |
| 8919 | return 0; |
| 8920 | } |
| 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 | execcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8924 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 8925 | if (argv[1]) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8926 | iflag = 0; /* exit on error */ |
| 8927 | mflag = 0; |
| 8928 | optschanged(); |
| 8929 | shellexec(argv + 1, pathval(), 0); |
| 8930 | } |
| 8931 | return 0; |
| 8932 | } |
| 8933 | |
| 8934 | /* |
| 8935 | * The return command. |
| 8936 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8937 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 8938 | returncmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 8939 | { |
| 8940 | /* |
| 8941 | * If called outside a function, do what ksh does; |
| 8942 | * skip the rest of the file. |
| 8943 | */ |
| 8944 | evalskip = funcnest ? SKIPFUNC : SKIPFILE; |
| 8945 | return argv[1] ? number(argv[1]) : exitstatus; |
| 8946 | } |
| 8947 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8948 | /* Forward declarations for builtintab[] */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8949 | static int breakcmd(int, char **) FAST_FUNC; |
| 8950 | static int dotcmd(int, char **) FAST_FUNC; |
| 8951 | static int evalcmd(int, char **) FAST_FUNC; |
| 8952 | static int exitcmd(int, char **) FAST_FUNC; |
| 8953 | static int exportcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8954 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8955 | static int getoptscmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8956 | #endif |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8957 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8958 | static int helpcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 8959 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 8960 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8961 | static int letcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8962 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8963 | static int readcmd(int, char **) FAST_FUNC; |
| 8964 | static int setcmd(int, char **) FAST_FUNC; |
| 8965 | static int shiftcmd(int, char **) FAST_FUNC; |
| 8966 | static int timescmd(int, char **) FAST_FUNC; |
| 8967 | static int trapcmd(int, char **) FAST_FUNC; |
| 8968 | static int umaskcmd(int, char **) FAST_FUNC; |
| 8969 | static int unsetcmd(int, char **) FAST_FUNC; |
| 8970 | static int ulimitcmd(int, char **) FAST_FUNC; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 8971 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 8972 | #define BUILTIN_NOSPEC "0" |
| 8973 | #define BUILTIN_SPECIAL "1" |
| 8974 | #define BUILTIN_REGULAR "2" |
| 8975 | #define BUILTIN_SPEC_REG "3" |
| 8976 | #define BUILTIN_ASSIGN "4" |
| 8977 | #define BUILTIN_SPEC_ASSG "5" |
| 8978 | #define BUILTIN_REG_ASSG "6" |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8979 | #define BUILTIN_SPEC_REG_ASSG "7" |
| 8980 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8981 | /* Stubs for calling non-FAST_FUNC's */ |
Denys Vlasenko | 2634bf3 | 2009-06-09 18:40:07 +0200 | [diff] [blame] | 8982 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8983 | 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] | 8984 | #endif |
| 8985 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8986 | 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] | 8987 | #endif |
| 8988 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8989 | 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] | 8990 | #endif |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 8991 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 8992 | /* Keep these in proper order since it is searched via bsearch() */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8993 | static const struct builtincmd builtintab[] = { |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8994 | { BUILTIN_SPEC_REG "." , dotcmd }, |
| 8995 | { BUILTIN_SPEC_REG ":" , truecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 8996 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8997 | { BUILTIN_REGULAR "[" , testcmd }, |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 8998 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 8999 | { BUILTIN_REGULAR "[[" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9000 | #endif |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9001 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9002 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9003 | { BUILTIN_REG_ASSG "alias" , aliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9004 | #endif |
| 9005 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9006 | { BUILTIN_REGULAR "bg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9007 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9008 | { BUILTIN_SPEC_REG "break" , breakcmd }, |
| 9009 | { BUILTIN_REGULAR "cd" , cdcmd }, |
| 9010 | { BUILTIN_NOSPEC "chdir" , cdcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9011 | #if ENABLE_ASH_CMDCMD |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9012 | { BUILTIN_REGULAR "command" , commandcmd }, |
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_SPEC_REG "continue", breakcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9015 | #if ENABLE_ASH_BUILTIN_ECHO |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9016 | { BUILTIN_REGULAR "echo" , echocmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9017 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9018 | { BUILTIN_SPEC_REG "eval" , evalcmd }, |
| 9019 | { BUILTIN_SPEC_REG "exec" , execcmd }, |
| 9020 | { BUILTIN_SPEC_REG "exit" , exitcmd }, |
| 9021 | { BUILTIN_SPEC_REG_ASSG "export" , exportcmd }, |
| 9022 | { BUILTIN_REGULAR "false" , falsecmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9023 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9024 | { BUILTIN_REGULAR "fg" , fg_bgcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9025 | #endif |
| 9026 | #if ENABLE_ASH_GETOPTS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9027 | { BUILTIN_REGULAR "getopts" , getoptscmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9028 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9029 | { BUILTIN_NOSPEC "hash" , hashcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9030 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9031 | { BUILTIN_NOSPEC "help" , helpcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9032 | #endif |
| 9033 | #if JOBS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9034 | { BUILTIN_REGULAR "jobs" , jobscmd }, |
| 9035 | { BUILTIN_REGULAR "kill" , killcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9036 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 9037 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9038 | { BUILTIN_NOSPEC "let" , letcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9039 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9040 | { BUILTIN_ASSIGN "local" , localcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9041 | #if ENABLE_ASH_BUILTIN_PRINTF |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9042 | { BUILTIN_REGULAR "printf" , printfcmd }, |
Denis Vlasenko | cd2663f | 2008-06-01 22:36:39 +0000 | [diff] [blame] | 9043 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9044 | { BUILTIN_NOSPEC "pwd" , pwdcmd }, |
| 9045 | { BUILTIN_REGULAR "read" , readcmd }, |
| 9046 | { BUILTIN_SPEC_REG_ASSG "readonly", exportcmd }, |
| 9047 | { BUILTIN_SPEC_REG "return" , returncmd }, |
| 9048 | { BUILTIN_SPEC_REG "set" , setcmd }, |
| 9049 | { BUILTIN_SPEC_REG "shift" , shiftcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9050 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9051 | { BUILTIN_SPEC_REG "source" , dotcmd }, |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 9052 | #endif |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9053 | #if ENABLE_ASH_BUILTIN_TEST |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9054 | { BUILTIN_REGULAR "test" , testcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9055 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9056 | { BUILTIN_SPEC_REG "times" , timescmd }, |
| 9057 | { BUILTIN_SPEC_REG "trap" , trapcmd }, |
| 9058 | { BUILTIN_REGULAR "true" , truecmd }, |
| 9059 | { BUILTIN_NOSPEC "type" , typecmd }, |
| 9060 | { BUILTIN_NOSPEC "ulimit" , ulimitcmd }, |
| 9061 | { BUILTIN_REGULAR "umask" , umaskcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9062 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9063 | { BUILTIN_REGULAR "unalias" , unaliascmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9064 | #endif |
Denys Vlasenko | 023a08f | 2010-03-26 15:53:33 +0100 | [diff] [blame] | 9065 | { BUILTIN_SPEC_REG "unset" , unsetcmd }, |
| 9066 | { BUILTIN_REGULAR "wait" , waitcmd }, |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9067 | }; |
| 9068 | |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 9069 | /* Should match the above table! */ |
| 9070 | #define COMMANDCMD (builtintab + \ |
| 9071 | 2 + \ |
| 9072 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9073 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9074 | 1 * ENABLE_ASH_ALIAS + \ |
| 9075 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9076 | 3) |
| 9077 | #define EXECCMD (builtintab + \ |
| 9078 | 2 + \ |
| 9079 | 1 * ENABLE_ASH_BUILTIN_TEST + \ |
| 9080 | 1 * ENABLE_ASH_BUILTIN_TEST * ENABLE_ASH_BASH_COMPAT + \ |
| 9081 | 1 * ENABLE_ASH_ALIAS + \ |
| 9082 | 1 * ENABLE_ASH_JOB_CONTROL + \ |
| 9083 | 3 + \ |
| 9084 | 1 * ENABLE_ASH_CMDCMD + \ |
| 9085 | 1 + \ |
| 9086 | ENABLE_ASH_BUILTIN_ECHO + \ |
| 9087 | 1) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9088 | |
| 9089 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9090 | * Search the table of builtin commands. |
| 9091 | */ |
| 9092 | static struct builtincmd * |
| 9093 | find_builtin(const char *name) |
| 9094 | { |
| 9095 | struct builtincmd *bp; |
| 9096 | |
| 9097 | bp = bsearch( |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 9098 | name, builtintab, ARRAY_SIZE(builtintab), sizeof(builtintab[0]), |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 9099 | pstrcmp |
| 9100 | ); |
| 9101 | return bp; |
| 9102 | } |
| 9103 | |
| 9104 | /* |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9105 | * Execute a simple command. |
| 9106 | */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9107 | static int |
| 9108 | isassignment(const char *p) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9109 | { |
| 9110 | const char *q = endofname(p); |
| 9111 | if (p == q) |
| 9112 | return 0; |
| 9113 | return *q == '='; |
| 9114 | } |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9115 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9116 | bltincmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9117 | { |
| 9118 | /* Preserve exitstatus of a previous possible redirection |
| 9119 | * as POSIX mandates */ |
| 9120 | return back_exitstatus; |
| 9121 | } |
Denys Vlasenko | 641dd7b | 2009-06-11 19:30:19 +0200 | [diff] [blame] | 9122 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9123 | evalcommand(union node *cmd, int flags) |
| 9124 | { |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9125 | static const struct builtincmd null_bltin = { |
| 9126 | "\0\0", bltincmd /* why three NULs? */ |
Denis Vlasenko | 4fe15f3 | 2007-02-23 01:05:26 +0000 | [diff] [blame] | 9127 | }; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9128 | struct stackmark smark; |
| 9129 | union node *argp; |
| 9130 | struct arglist arglist; |
| 9131 | struct arglist varlist; |
| 9132 | char **argv; |
| 9133 | int argc; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9134 | const struct strlist *sp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9135 | struct cmdentry cmdentry; |
| 9136 | struct job *jp; |
| 9137 | char *lastarg; |
| 9138 | const char *path; |
| 9139 | int spclbltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9140 | int status; |
| 9141 | char **nargv; |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9142 | struct builtincmd *bcmd; |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9143 | smallint cmd_is_exec; |
| 9144 | smallint pseudovarflag = 0; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9145 | |
| 9146 | /* First expand the arguments. */ |
| 9147 | TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags)); |
| 9148 | setstackmark(&smark); |
| 9149 | back_exitstatus = 0; |
| 9150 | |
| 9151 | cmdentry.cmdtype = CMDBUILTIN; |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9152 | cmdentry.u.cmd = &null_bltin; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9153 | varlist.lastp = &varlist.list; |
| 9154 | *varlist.lastp = NULL; |
| 9155 | arglist.lastp = &arglist.list; |
| 9156 | *arglist.lastp = NULL; |
| 9157 | |
| 9158 | argc = 0; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9159 | if (cmd->ncmd.args) { |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9160 | bcmd = find_builtin(cmd->ncmd.args->narg.text); |
| 9161 | pseudovarflag = bcmd && IS_BUILTIN_ASSIGN(bcmd); |
| 9162 | } |
| 9163 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9164 | for (argp = cmd->ncmd.args; argp; argp = argp->narg.next) { |
| 9165 | struct strlist **spp; |
| 9166 | |
| 9167 | spp = arglist.lastp; |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 9168 | if (pseudovarflag && isassignment(argp->narg.text)) |
Paul Fox | c3850c8 | 2005-07-20 18:23:39 +0000 | [diff] [blame] | 9169 | expandarg(argp, &arglist, EXP_VARTILDE); |
| 9170 | else |
| 9171 | expandarg(argp, &arglist, EXP_FULL | EXP_TILDE); |
| 9172 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9173 | for (sp = *spp; sp; sp = sp->next) |
| 9174 | argc++; |
| 9175 | } |
| 9176 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9177 | argv = nargv = stalloc(sizeof(char *) * (argc + 1)); |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9178 | for (sp = arglist.list; sp; sp = sp->next) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9179 | TRACE(("evalcommand arg: %s\n", sp->text)); |
| 9180 | *nargv++ = sp->text; |
| 9181 | } |
| 9182 | *nargv = NULL; |
| 9183 | |
| 9184 | lastarg = NULL; |
| 9185 | if (iflag && funcnest == 0 && argc > 0) |
| 9186 | lastarg = nargv[-1]; |
| 9187 | |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9188 | preverrout_fd = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9189 | expredir(cmd->ncmd.redirect); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9190 | status = redirectsafe(cmd->ncmd.redirect, REDIR_PUSH | REDIR_SAVEFD2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9191 | |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9192 | path = vpath.var_text; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9193 | for (argp = cmd->ncmd.assign; argp; argp = argp->narg.next) { |
| 9194 | struct strlist **spp; |
| 9195 | char *p; |
| 9196 | |
| 9197 | spp = varlist.lastp; |
| 9198 | expandarg(argp, &varlist, EXP_VARTILDE); |
| 9199 | |
| 9200 | /* |
| 9201 | * Modify the command lookup path, if a PATH= assignment |
| 9202 | * is present |
| 9203 | */ |
| 9204 | p = (*spp)->text; |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 9205 | if (varcmp(p, path) == 0) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9206 | path = p; |
| 9207 | } |
| 9208 | |
| 9209 | /* Print the command if xflag is set. */ |
| 9210 | if (xflag) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9211 | int n; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9212 | const char *p = " %s" + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9213 | |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9214 | fdprintf(preverrout_fd, p, expandstr(ps4val())); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9215 | sp = varlist.list; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9216 | for (n = 0; n < 2; n++) { |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9217 | while (sp) { |
Denis Vlasenko | 0de37e1 | 2007-10-17 11:08:53 +0000 | [diff] [blame] | 9218 | fdprintf(preverrout_fd, p, sp->text); |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9219 | sp = sp->next; |
Denys Vlasenko | fd33e17 | 2010-06-26 22:55:44 +0200 | [diff] [blame] | 9220 | p = " %s"; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9221 | } |
| 9222 | sp = arglist.list; |
| 9223 | } |
Denis Vlasenko | 0e6f661 | 2008-02-15 15:02:15 +0000 | [diff] [blame] | 9224 | safe_write(preverrout_fd, "\n", 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9225 | } |
| 9226 | |
| 9227 | cmd_is_exec = 0; |
| 9228 | spclbltin = -1; |
| 9229 | |
| 9230 | /* Now locate the command. */ |
| 9231 | if (argc) { |
| 9232 | const char *oldpath; |
| 9233 | int cmd_flag = DO_ERR; |
| 9234 | |
| 9235 | path += 5; |
| 9236 | oldpath = path; |
| 9237 | for (;;) { |
| 9238 | find_command(argv[0], &cmdentry, cmd_flag, path); |
| 9239 | if (cmdentry.cmdtype == CMDUNKNOWN) { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 9240 | flush_stdout_stderr(); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9241 | status = 127; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9242 | goto bail; |
| 9243 | } |
| 9244 | |
| 9245 | /* implement bltin and command here */ |
| 9246 | if (cmdentry.cmdtype != CMDBUILTIN) |
| 9247 | break; |
| 9248 | if (spclbltin < 0) |
| 9249 | spclbltin = IS_BUILTIN_SPECIAL(cmdentry.u.cmd); |
| 9250 | if (cmdentry.u.cmd == EXECCMD) |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9251 | cmd_is_exec = 1; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9252 | #if ENABLE_ASH_CMDCMD |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9253 | if (cmdentry.u.cmd == COMMANDCMD) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9254 | path = oldpath; |
| 9255 | nargv = parse_command_args(argv, &path); |
| 9256 | if (!nargv) |
| 9257 | break; |
| 9258 | argc -= nargv - argv; |
| 9259 | argv = nargv; |
| 9260 | cmd_flag |= DO_NOFUNC; |
| 9261 | } else |
| 9262 | #endif |
| 9263 | break; |
| 9264 | } |
| 9265 | } |
| 9266 | |
| 9267 | if (status) { |
| 9268 | /* We have a redirection error. */ |
| 9269 | if (spclbltin > 0) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9270 | raise_exception(EXERROR); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9271 | bail: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9272 | exitstatus = status; |
| 9273 | goto out; |
| 9274 | } |
| 9275 | |
| 9276 | /* Execute the command. */ |
| 9277 | switch (cmdentry.cmdtype) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9278 | default: { |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9279 | |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9280 | #if ENABLE_FEATURE_SH_NOFORK |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9281 | /* (1) BUG: if variables are set, we need to fork, or save/restore them |
| 9282 | * around run_nofork_applet() call. |
| 9283 | * (2) Should this check also be done in forkshell()? |
| 9284 | * (perhaps it should, so that "VAR=VAL nofork" at least avoids exec...) |
| 9285 | */ |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9286 | /* find_command() encodes applet_no as (-2 - applet_no) */ |
| 9287 | int applet_no = (- cmdentry.u.index - 2); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9288 | if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9289 | listsetvar(varlist.list, VEXPORT|VSTACK); |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 9290 | /* run <applet>_main() */ |
| 9291 | exitstatus = run_nofork_applet(applet_no, argv); |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9292 | break; |
| 9293 | } |
Denis Vlasenko | 9bc80d7 | 2008-04-12 20:07:53 +0000 | [diff] [blame] | 9294 | #endif |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9295 | /* Can we avoid forking off? For example, very last command |
| 9296 | * in a script or a subshell does not need forking, |
| 9297 | * we can just exec it. |
| 9298 | */ |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9299 | if (!(flags & EV_EXIT) || may_have_traps) { |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9300 | /* No, forking off a child is necessary */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9301 | INT_OFF; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9302 | jp = makejob(/*cmd,*/ 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9303 | if (forkshell(jp, cmd, FORK_FG) != 0) { |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9304 | /* parent */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9305 | exitstatus = waitforjob(jp); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9306 | INT_ON; |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 9307 | TRACE(("forked child exited with %d\n", exitstatus)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9308 | break; |
| 9309 | } |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 9310 | /* child */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9311 | FORCE_INT_ON; |
Denys Vlasenko | c7f95d2 | 2010-05-18 15:52:23 +0200 | [diff] [blame] | 9312 | /* fall through to exec'ing external program */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9313 | } |
| 9314 | listsetvar(varlist.list, VEXPORT|VSTACK); |
| 9315 | shellexec(argv, path, cmdentry.u.index); |
| 9316 | /* NOTREACHED */ |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9317 | } /* default */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9318 | case CMDBUILTIN: |
| 9319 | cmdenviron = varlist.list; |
| 9320 | if (cmdenviron) { |
| 9321 | struct strlist *list = cmdenviron; |
| 9322 | int i = VNOSET; |
| 9323 | if (spclbltin > 0 || argc == 0) { |
| 9324 | i = 0; |
| 9325 | if (cmd_is_exec && argc > 1) |
| 9326 | i = VEXPORT; |
| 9327 | } |
| 9328 | listsetvar(list, i); |
| 9329 | } |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9330 | /* Tight loop with builtins only: |
| 9331 | * "while kill -0 $child; do true; done" |
| 9332 | * will never exit even if $child died, unless we do this |
| 9333 | * to reap the zombie and make kill detect that it's gone: */ |
| 9334 | dowait(DOWAIT_NONBLOCK, NULL); |
| 9335 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9336 | if (evalbltin(cmdentry.u.cmd, argc, argv)) { |
| 9337 | int exit_status; |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 9338 | int i = exception_type; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9339 | if (i == EXEXIT) |
| 9340 | goto raise; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9341 | exit_status = 2; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9342 | if (i == EXINT) |
Denis Vlasenko | 991a1da | 2008-02-10 19:02:53 +0000 | [diff] [blame] | 9343 | exit_status = 128 + SIGINT; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9344 | if (i == EXSIG) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9345 | exit_status = 128 + pending_sig; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9346 | exitstatus = exit_status; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9347 | if (i == EXINT || spclbltin > 0) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9348 | raise: |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9349 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9350 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9351 | FORCE_INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9352 | } |
| 9353 | break; |
| 9354 | |
| 9355 | case CMDFUNCTION: |
| 9356 | listsetvar(varlist.list, 0); |
Denis Vlasenko | be54d6b | 2008-10-27 14:25:52 +0000 | [diff] [blame] | 9357 | /* See above for the rationale */ |
| 9358 | dowait(DOWAIT_NONBLOCK, NULL); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9359 | if (evalfun(cmdentry.u.func, argc, argv, flags)) |
| 9360 | goto raise; |
| 9361 | break; |
Denys Vlasenko | 42c4b2e | 2010-05-18 16:13:56 +0200 | [diff] [blame] | 9362 | |
| 9363 | } /* switch */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9364 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9365 | out: |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 9366 | popredir(/*drop:*/ cmd_is_exec, /*restore:*/ cmd_is_exec); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9367 | if (lastarg) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9368 | /* dsl: I think this is intended to be used to support |
| 9369 | * '_' in 'vi' command mode during line editing... |
| 9370 | * However I implemented that within libedit itself. |
| 9371 | */ |
| 9372 | setvar("_", lastarg, 0); |
Denis Vlasenko | 6514c5e | 2008-07-24 13:41:37 +0000 | [diff] [blame] | 9373 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9374 | popstackmark(&smark); |
| 9375 | } |
| 9376 | |
| 9377 | static int |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9378 | evalbltin(const struct builtincmd *cmd, int argc, char **argv) |
| 9379 | { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9380 | char *volatile savecmdname; |
| 9381 | struct jmploc *volatile savehandler; |
| 9382 | struct jmploc jmploc; |
| 9383 | int i; |
| 9384 | |
| 9385 | savecmdname = commandname; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9386 | i = setjmp(jmploc.loc); |
| 9387 | if (i) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9388 | goto cmddone; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9389 | savehandler = exception_handler; |
| 9390 | exception_handler = &jmploc; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9391 | commandname = argv[0]; |
| 9392 | argptr = argv + 1; |
| 9393 | optptr = NULL; /* initialize nextopt */ |
| 9394 | exitstatus = (*cmd->builtin)(argc, argv); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9395 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9396 | cmddone: |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 9397 | exitstatus |= ferror(stdout); |
Rob Landley | f296f0b | 2006-07-06 01:09:21 +0000 | [diff] [blame] | 9398 | clearerr(stdout); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9399 | commandname = savecmdname; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 9400 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9401 | |
| 9402 | return i; |
| 9403 | } |
| 9404 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9405 | static int |
| 9406 | goodname(const char *p) |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9407 | { |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 9408 | return endofname(p)[0] == '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9409 | } |
| 9410 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9411 | |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9412 | /* |
| 9413 | * Search for a command. This is called before we fork so that the |
| 9414 | * 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] | 9415 | * the child. The check for "goodname" is an overly conservative |
| 9416 | * check that the name will not be subject to expansion. |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9417 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9418 | static void |
| 9419 | prehash(union node *n) |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9420 | { |
| 9421 | struct cmdentry entry; |
| 9422 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 9423 | if (n->type == NCMD && n->ncmd.args && goodname(n->ncmd.args->narg.text)) |
| 9424 | find_command(n->ncmd.args->narg.text, &entry, 0, pathval()); |
Glenn L McGrath | 50812ff | 2002-08-23 13:14:48 +0000 | [diff] [blame] | 9425 | } |
| 9426 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9427 | |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9428 | /* ============ Builtin commands |
| 9429 | * |
| 9430 | * Builtin commands whose functions are closely tied to evaluation |
| 9431 | * are implemented here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9432 | */ |
| 9433 | |
| 9434 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9435 | * Handle break and continue commands. Break, continue, and return are |
| 9436 | * all handled by setting the evalskip flag. The evaluation routines |
| 9437 | * above all check this flag, and if it is set they start skipping |
| 9438 | * commands rather than executing them. The variable skipcount is |
| 9439 | * the number of loops to break/continue, or the number of function |
| 9440 | * levels to return. (The latter is always 1.) It should probably |
| 9441 | * be an error to break out of more loops than exist, but it isn't |
| 9442 | * in the standard shell so we don't make it one here. |
| 9443 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9444 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9445 | breakcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9446 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 9447 | int n = argv[1] ? number(argv[1]) : 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9448 | |
Aaron Lehmann | 2aef3a6 | 2001-12-31 06:03:12 +0000 | [diff] [blame] | 9449 | if (n <= 0) |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 9450 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9451 | if (n > loopnest) |
| 9452 | n = loopnest; |
| 9453 | if (n > 0) { |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 9454 | evalskip = (**argv == 'c') ? SKIPCONT : SKIPBREAK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9455 | skipcount = n; |
| 9456 | } |
| 9457 | return 0; |
| 9458 | } |
| 9459 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9460 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9461 | /* ============ input.c |
| 9462 | * |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 9463 | * This implements the input routines used by the parser. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9464 | */ |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9465 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9466 | enum { |
| 9467 | INPUT_PUSH_FILE = 1, |
| 9468 | INPUT_NOFILE_OK = 2, |
| 9469 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9470 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9471 | static smallint checkkwd; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 9472 | /* values of checkkwd variable */ |
| 9473 | #define CHKALIAS 0x1 |
| 9474 | #define CHKKWD 0x2 |
| 9475 | #define CHKNL 0x4 |
| 9476 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9477 | /* |
| 9478 | * Push a string back onto the input at this current parsefile level. |
| 9479 | * We handle aliases this way. |
| 9480 | */ |
| 9481 | #if !ENABLE_ASH_ALIAS |
| 9482 | #define pushstring(s, ap) pushstring(s) |
| 9483 | #endif |
| 9484 | static void |
| 9485 | pushstring(char *s, struct alias *ap) |
| 9486 | { |
| 9487 | struct strpush *sp; |
| 9488 | int len; |
| 9489 | |
| 9490 | len = strlen(s); |
| 9491 | INT_OFF; |
| 9492 | if (g_parsefile->strpush) { |
| 9493 | sp = ckzalloc(sizeof(*sp)); |
| 9494 | sp->prev = g_parsefile->strpush; |
| 9495 | } else { |
| 9496 | sp = &(g_parsefile->basestrpush); |
| 9497 | } |
| 9498 | g_parsefile->strpush = sp; |
| 9499 | sp->prev_string = g_parsefile->next_to_pgetc; |
| 9500 | sp->prev_left_in_line = g_parsefile->left_in_line; |
| 9501 | #if ENABLE_ASH_ALIAS |
| 9502 | sp->ap = ap; |
| 9503 | if (ap) { |
| 9504 | ap->flag |= ALIASINUSE; |
| 9505 | sp->string = s; |
| 9506 | } |
| 9507 | #endif |
| 9508 | g_parsefile->next_to_pgetc = s; |
| 9509 | g_parsefile->left_in_line = len; |
| 9510 | INT_ON; |
| 9511 | } |
| 9512 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9513 | static void |
| 9514 | popstring(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9515 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9516 | struct strpush *sp = g_parsefile->strpush; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9517 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9518 | INT_OFF; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9519 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9520 | if (sp->ap) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9521 | if (g_parsefile->next_to_pgetc[-1] == ' ' |
| 9522 | || g_parsefile->next_to_pgetc[-1] == '\t' |
| 9523 | ) { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9524 | checkkwd |= CHKALIAS; |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9525 | } |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9526 | if (sp->string != sp->ap->val) { |
| 9527 | free(sp->string); |
| 9528 | } |
| 9529 | sp->ap->flag &= ~ALIASINUSE; |
| 9530 | if (sp->ap->flag & ALIASDEAD) { |
| 9531 | unalias(sp->ap->name); |
| 9532 | } |
Glenn L McGrath | 28939ad | 2004-07-21 10:20:19 +0000 | [diff] [blame] | 9533 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9534 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9535 | g_parsefile->next_to_pgetc = sp->prev_string; |
| 9536 | g_parsefile->left_in_line = sp->prev_left_in_line; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9537 | g_parsefile->strpush = sp->prev; |
| 9538 | if (sp != &(g_parsefile->basestrpush)) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9539 | free(sp); |
| 9540 | INT_ON; |
| 9541 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9542 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9543 | //FIXME: BASH_COMPAT with "...&" does TWO pungetc(): |
| 9544 | //it peeks whether it is &>, and then pushes back both chars. |
| 9545 | //This function needs to save last *next_to_pgetc to buf[0] |
| 9546 | //to make two pungetc() reliable. Currently, |
| 9547 | // pgetc (out of buf: does preadfd), pgetc, pungetc, pungetc won't work... |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 9548 | static int |
| 9549 | preadfd(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9550 | { |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9551 | int nr; |
Denis Vlasenko | 6a0ad25 | 2008-07-25 13:34:05 +0000 | [diff] [blame] | 9552 | char *buf = g_parsefile->buf; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9553 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9554 | g_parsefile->next_to_pgetc = buf; |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9555 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 9556 | retry: |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9557 | if (!iflag || g_parsefile->pf_fd != STDIN_FILENO) |
| 9558 | nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9559 | else { |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 9560 | #if ENABLE_FEATURE_TAB_COMPLETION |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9561 | line_input_state->path_lookup = pathval(); |
Eric Andersen | 4a79c0e | 2004-09-08 10:01:07 +0000 | [diff] [blame] | 9562 | #endif |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 9563 | nr = read_line_input(cmdedit_prompt, buf, IBUFSIZ, line_input_state); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9564 | if (nr == 0) { |
| 9565 | /* Ctrl+C pressed */ |
| 9566 | if (trap[SIGINT]) { |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9567 | buf[0] = '\n'; |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9568 | buf[1] = '\0'; |
Glenn L McGrath | 16e45d7 | 2004-02-04 08:24:39 +0000 | [diff] [blame] | 9569 | raise(SIGINT); |
| 9570 | return 1; |
| 9571 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9572 | goto retry; |
| 9573 | } |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 9574 | if (nr < 0 && errno == 0) { |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9575 | /* Ctrl+D pressed */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9576 | nr = 0; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 9577 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9578 | } |
| 9579 | #else |
Denys Vlasenko | 161bb8f | 2010-06-06 05:07:11 +0200 | [diff] [blame] | 9580 | nr = nonblock_safe_read(g_parsefile->pf_fd, buf, IBUFSIZ - 1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9581 | #endif |
| 9582 | |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9583 | #if 0 |
| 9584 | /* nonblock_safe_read() handles this problem */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9585 | if (nr < 0) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9586 | if (parsefile->fd == 0 && errno == EWOULDBLOCK) { |
Denis Vlasenko | d37f222 | 2007-08-19 13:42:08 +0000 | [diff] [blame] | 9587 | int flags = fcntl(0, F_GETFL); |
Denis Vlasenko | 9cb220b | 2007-12-09 10:03:28 +0000 | [diff] [blame] | 9588 | if (flags >= 0 && (flags & O_NONBLOCK)) { |
| 9589 | flags &= ~O_NONBLOCK; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9590 | if (fcntl(0, F_SETFL, flags) >= 0) { |
| 9591 | out2str("sh: turning off NDELAY mode\n"); |
| 9592 | goto retry; |
| 9593 | } |
| 9594 | } |
| 9595 | } |
| 9596 | } |
Denis Vlasenko | e376d45 | 2008-02-20 22:23:24 +0000 | [diff] [blame] | 9597 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9598 | return nr; |
| 9599 | } |
| 9600 | |
| 9601 | /* |
| 9602 | * Refill the input buffer and return the next input character: |
| 9603 | * |
| 9604 | * 1) If a string was pushed back on the input, pop it; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9605 | * 2) If an EOF was pushed back (g_parsefile->left_in_line < -BIGNUM) |
| 9606 | * or we are reading from a string so we can't refill the buffer, |
| 9607 | * return EOF. |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 9608 | * 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] | 9609 | * 4) Process input up to the next newline, deleting nul characters. |
| 9610 | */ |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9611 | //#define pgetc_debug(...) bb_error_msg(__VA_ARGS__) |
| 9612 | #define pgetc_debug(...) ((void)0) |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9613 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9614 | preadbuffer(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9615 | { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9616 | char *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9617 | int more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9618 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9619 | while (g_parsefile->strpush) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9620 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9621 | if (g_parsefile->left_in_line == -1 |
| 9622 | && g_parsefile->strpush->ap |
| 9623 | && g_parsefile->next_to_pgetc[-1] != ' ' |
| 9624 | && g_parsefile->next_to_pgetc[-1] != '\t' |
Denis Vlasenko | 1689840 | 2008-11-25 01:34:52 +0000 | [diff] [blame] | 9625 | ) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9626 | pgetc_debug("preadbuffer PEOA"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9627 | return PEOA; |
| 9628 | } |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 9629 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9630 | popstring(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9631 | /* try "pgetc" now: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9632 | pgetc_debug("preadbuffer internal pgetc at %d:%p'%s'", |
| 9633 | g_parsefile->left_in_line, |
| 9634 | g_parsefile->next_to_pgetc, |
| 9635 | g_parsefile->next_to_pgetc); |
| 9636 | if (--g_parsefile->left_in_line >= 0) |
| 9637 | return (unsigned char)(*g_parsefile->next_to_pgetc++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9638 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9639 | /* on both branches above g_parsefile->left_in_line < 0. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9640 | * "pgetc" needs refilling. |
| 9641 | */ |
| 9642 | |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9643 | /* -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] | 9644 | * pungetc() may increment it a few times. |
Denis Vlasenko | e27dafd | 2008-11-28 04:01:03 +0000 | [diff] [blame] | 9645 | * Assuming it won't increment it to less than -90. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9646 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9647 | if (g_parsefile->left_in_line < -90 || g_parsefile->buf == NULL) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9648 | pgetc_debug("preadbuffer PEOF1"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9649 | /* even in failure keep left_in_line and next_to_pgetc |
| 9650 | * in lock step, for correct multi-layer pungetc. |
| 9651 | * left_in_line was decremented before preadbuffer(), |
| 9652 | * must inc next_to_pgetc: */ |
| 9653 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9654 | return PEOF; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9655 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9656 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9657 | more = g_parsefile->left_in_buffer; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9658 | if (more <= 0) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9659 | flush_stdout_stderr(); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9660 | again: |
| 9661 | more = preadfd(); |
| 9662 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9663 | /* don't try reading again */ |
| 9664 | g_parsefile->left_in_line = -99; |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9665 | pgetc_debug("preadbuffer PEOF2"); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9666 | g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9667 | return PEOF; |
| 9668 | } |
| 9669 | } |
| 9670 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9671 | /* Find out where's the end of line. |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9672 | * Set g_parsefile->left_in_line |
| 9673 | * and g_parsefile->left_in_buffer acordingly. |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9674 | * NUL chars are deleted. |
| 9675 | */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9676 | q = g_parsefile->next_to_pgetc; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9677 | for (;;) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9678 | char c; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9679 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9680 | more--; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9681 | |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9682 | c = *q; |
| 9683 | if (c == '\0') { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9684 | memmove(q, q + 1, more); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9685 | } else { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9686 | q++; |
| 9687 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9688 | 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] | 9689 | break; |
| 9690 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9691 | } |
| 9692 | |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9693 | if (more <= 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9694 | g_parsefile->left_in_line = q - g_parsefile->next_to_pgetc - 1; |
| 9695 | if (g_parsefile->left_in_line < 0) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9696 | goto again; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9697 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9698 | } |
| 9699 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9700 | g_parsefile->left_in_buffer = more; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9701 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9702 | if (vflag) { |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9703 | char save = *q; |
| 9704 | *q = '\0'; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9705 | out2str(g_parsefile->next_to_pgetc); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9706 | *q = save; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9707 | } |
| 9708 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9709 | pgetc_debug("preadbuffer at %d:%p'%s'", |
| 9710 | g_parsefile->left_in_line, |
| 9711 | g_parsefile->next_to_pgetc, |
| 9712 | g_parsefile->next_to_pgetc); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9713 | return (unsigned char)*g_parsefile->next_to_pgetc++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9714 | } |
| 9715 | |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9716 | #define pgetc_as_macro() \ |
| 9717 | (--g_parsefile->left_in_line >= 0 \ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 9718 | ? (unsigned char)*g_parsefile->next_to_pgetc++ \ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9719 | : preadbuffer() \ |
| 9720 | ) |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9721 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9722 | static int |
| 9723 | pgetc(void) |
| 9724 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9725 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9726 | g_parsefile->left_in_line, |
| 9727 | g_parsefile->next_to_pgetc, |
| 9728 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9729 | return pgetc_as_macro(); |
| 9730 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9731 | |
| 9732 | #if ENABLE_ASH_OPTIMIZE_FOR_SIZE |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9733 | # define pgetc_fast() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9734 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9735 | # define pgetc_fast() pgetc_as_macro() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9736 | #endif |
| 9737 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9738 | #if ENABLE_ASH_ALIAS |
| 9739 | static int |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9740 | pgetc_without_PEOA(void) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9741 | { |
| 9742 | int c; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9743 | do { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9744 | pgetc_debug("pgetc_fast at %d:%p'%s'", |
| 9745 | g_parsefile->left_in_line, |
| 9746 | g_parsefile->next_to_pgetc, |
| 9747 | g_parsefile->next_to_pgetc); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 9748 | c = pgetc_fast(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9749 | } while (c == PEOA); |
| 9750 | return c; |
| 9751 | } |
| 9752 | #else |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9753 | # define pgetc_without_PEOA() pgetc() |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9754 | #endif |
| 9755 | |
| 9756 | /* |
| 9757 | * Read a line from the script. |
| 9758 | */ |
| 9759 | static char * |
| 9760 | pfgets(char *line, int len) |
| 9761 | { |
| 9762 | char *p = line; |
| 9763 | int nleft = len; |
| 9764 | int c; |
| 9765 | |
| 9766 | while (--nleft > 0) { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 9767 | c = pgetc_without_PEOA(); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9768 | if (c == PEOF) { |
| 9769 | if (p == line) |
| 9770 | return NULL; |
| 9771 | break; |
| 9772 | } |
| 9773 | *p++ = c; |
| 9774 | if (c == '\n') |
| 9775 | break; |
| 9776 | } |
| 9777 | *p = '\0'; |
| 9778 | return line; |
| 9779 | } |
| 9780 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9781 | /* |
| 9782 | * Undo the last call to pgetc. Only one character may be pushed back. |
| 9783 | * PEOF may be pushed back. |
| 9784 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9785 | static void |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9786 | pungetc(void) |
| 9787 | { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9788 | g_parsefile->left_in_line++; |
| 9789 | g_parsefile->next_to_pgetc--; |
| 9790 | pgetc_debug("pushed back to %d:%p'%s'", |
| 9791 | g_parsefile->left_in_line, |
| 9792 | g_parsefile->next_to_pgetc, |
| 9793 | g_parsefile->next_to_pgetc); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9794 | } |
| 9795 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9796 | /* |
| 9797 | * To handle the "." command, a stack of input files is used. Pushfile |
| 9798 | * adds a new entry to the stack and popfile restores the previous level. |
| 9799 | */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9800 | static void |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9801 | pushfile(void) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9802 | { |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9803 | struct parsefile *pf; |
| 9804 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9805 | pf = ckzalloc(sizeof(*pf)); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9806 | pf->prev = g_parsefile; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9807 | pf->pf_fd = -1; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 9808 | /*pf->strpush = NULL; - ckzalloc did it */ |
| 9809 | /*pf->basestrpush.prev = NULL;*/ |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9810 | g_parsefile = pf; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9811 | } |
| 9812 | |
| 9813 | static void |
| 9814 | popfile(void) |
| 9815 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9816 | struct parsefile *pf = g_parsefile; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9817 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9818 | INT_OFF; |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9819 | if (pf->pf_fd >= 0) |
| 9820 | close(pf->pf_fd); |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 9821 | free(pf->buf); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9822 | while (pf->strpush) |
| 9823 | popstring(); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9824 | g_parsefile = pf->prev; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9825 | free(pf); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9826 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9827 | } |
| 9828 | |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9829 | /* |
| 9830 | * Return to top level. |
| 9831 | */ |
| 9832 | static void |
| 9833 | popallfiles(void) |
| 9834 | { |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9835 | while (g_parsefile != &basepf) |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9836 | popfile(); |
| 9837 | } |
| 9838 | |
| 9839 | /* |
| 9840 | * Close the file(s) that the shell is reading commands from. Called |
| 9841 | * after a fork is done. |
| 9842 | */ |
| 9843 | static void |
| 9844 | closescript(void) |
| 9845 | { |
| 9846 | popallfiles(); |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9847 | if (g_parsefile->pf_fd > 0) { |
| 9848 | close(g_parsefile->pf_fd); |
| 9849 | g_parsefile->pf_fd = 0; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9850 | } |
| 9851 | } |
| 9852 | |
| 9853 | /* |
| 9854 | * Like setinputfile, but takes an open file descriptor. Call this with |
| 9855 | * interrupts off. |
| 9856 | */ |
| 9857 | static void |
| 9858 | setinputfd(int fd, int push) |
| 9859 | { |
Denis Vlasenko | 96e1b38 | 2007-09-30 23:50:48 +0000 | [diff] [blame] | 9860 | close_on_exec_on(fd); |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9861 | if (push) { |
| 9862 | pushfile(); |
Denis Vlasenko | 727752d | 2008-11-28 03:41:47 +0000 | [diff] [blame] | 9863 | g_parsefile->buf = NULL; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9864 | } |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 9865 | g_parsefile->pf_fd = fd; |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9866 | if (g_parsefile->buf == NULL) |
| 9867 | g_parsefile->buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9868 | g_parsefile->left_in_buffer = 0; |
| 9869 | g_parsefile->left_in_line = 0; |
| 9870 | g_parsefile->linno = 1; |
Denis Vlasenko | 4d2183b | 2007-02-23 01:05:38 +0000 | [diff] [blame] | 9871 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9872 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9873 | /* |
| 9874 | * Set the input to take input from a file. If push is set, push the |
| 9875 | * old input onto the stack first. |
| 9876 | */ |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9877 | static int |
| 9878 | setinputfile(const char *fname, int flags) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9879 | { |
| 9880 | int fd; |
| 9881 | int fd2; |
| 9882 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9883 | INT_OFF; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9884 | fd = open(fname, O_RDONLY); |
| 9885 | if (fd < 0) { |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9886 | if (flags & INPUT_NOFILE_OK) |
| 9887 | goto out; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 9888 | ash_msg_and_raise_error("can't open '%s'", fname); |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9889 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9890 | if (fd < 10) { |
| 9891 | fd2 = copyfd(fd, 10); |
| 9892 | close(fd); |
| 9893 | if (fd2 < 0) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 9894 | ash_msg_and_raise_error("out of file descriptors"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9895 | fd = fd2; |
| 9896 | } |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9897 | setinputfd(fd, flags & INPUT_PUSH_FILE); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 9898 | out: |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9899 | INT_ON; |
"Vladimir N. Oleynik" | fb29b46 | 2006-01-15 14:21:01 +0000 | [diff] [blame] | 9900 | return fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9901 | } |
| 9902 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9903 | /* |
| 9904 | * Like setinputfile, but takes input from a string. |
| 9905 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9906 | static void |
| 9907 | setinputstring(char *string) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 9908 | { |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9909 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9910 | pushfile(); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9911 | g_parsefile->next_to_pgetc = string; |
| 9912 | g_parsefile->left_in_line = strlen(string); |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 9913 | g_parsefile->buf = NULL; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 9914 | g_parsefile->linno = 1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 9915 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9916 | } |
| 9917 | |
| 9918 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9919 | /* ============ mail.c |
| 9920 | * |
| 9921 | * Routines to check for mail. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9922 | */ |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9923 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9924 | #if ENABLE_ASH_MAIL |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9925 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9926 | #define MAXMBOXES 10 |
| 9927 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9928 | /* times of mailboxes */ |
| 9929 | static time_t mailtime[MAXMBOXES]; |
| 9930 | /* Set if MAIL or MAILPATH is changed. */ |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9931 | static smallint mail_var_path_changed; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9932 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9933 | /* |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9934 | * Print appropriate message(s) if mail has arrived. |
| 9935 | * If mail_var_path_changed is set, |
| 9936 | * then the value of MAIL has mail_var_path_changed, |
| 9937 | * so we just update the values. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9938 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9939 | static void |
| 9940 | chkmail(void) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9941 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9942 | const char *mpath; |
| 9943 | char *p; |
| 9944 | char *q; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9945 | time_t *mtp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9946 | struct stackmark smark; |
| 9947 | struct stat statb; |
| 9948 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9949 | setstackmark(&smark); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9950 | mpath = mpathset() ? mpathval() : mailval(); |
| 9951 | for (mtp = mailtime; mtp < mailtime + MAXMBOXES; mtp++) { |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 9952 | p = path_advance(&mpath, nullstr); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9953 | if (p == NULL) |
| 9954 | break; |
| 9955 | if (*p == '\0') |
| 9956 | continue; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 9957 | for (q = p; *q; q++) |
| 9958 | continue; |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 9959 | #if DEBUG |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9960 | if (q[-1] != '/') |
| 9961 | abort(); |
| 9962 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9963 | q[-1] = '\0'; /* delete trailing '/' */ |
| 9964 | if (stat(p, &statb) < 0) { |
| 9965 | *mtp = 0; |
| 9966 | continue; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9967 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9968 | if (!mail_var_path_changed && statb.st_mtime != *mtp) { |
| 9969 | fprintf( |
Denys Vlasenko | ea8b252 | 2010-06-02 12:57:26 +0200 | [diff] [blame] | 9970 | stderr, "%s\n", |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9971 | pathopt ? pathopt : "you have mail" |
| 9972 | ); |
| 9973 | } |
| 9974 | *mtp = statb.st_mtime; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9975 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9976 | mail_var_path_changed = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9977 | popstackmark(&smark); |
| 9978 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9979 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 9980 | static void FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 9981 | changemail(const char *val UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9982 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 9983 | mail_var_path_changed = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9984 | } |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 9985 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 9986 | #endif /* ASH_MAIL */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 9987 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 9988 | |
| 9989 | /* ============ ??? */ |
| 9990 | |
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 | * Set the shell parameters. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9993 | */ |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9994 | static void |
| 9995 | setparam(char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 9996 | { |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 9997 | char **newparam; |
| 9998 | char **ap; |
| 9999 | int nparam; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10000 | |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10001 | for (nparam = 0; argv[nparam]; nparam++) |
| 10002 | continue; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10003 | ap = newparam = ckmalloc((nparam + 1) * sizeof(*ap)); |
| 10004 | while (*argv) { |
| 10005 | *ap++ = ckstrdup(*argv++); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10006 | } |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10007 | *ap = NULL; |
| 10008 | freeparam(&shellparam); |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10009 | shellparam.malloced = 1; |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10010 | shellparam.nparam = nparam; |
| 10011 | shellparam.p = newparam; |
| 10012 | #if ENABLE_ASH_GETOPTS |
| 10013 | shellparam.optind = 1; |
| 10014 | shellparam.optoff = -1; |
| 10015 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10016 | } |
| 10017 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10018 | /* |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 10019 | * Process shell options. The global variable argptr contains a pointer |
| 10020 | * to the argument list; we advance it past the options. |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10021 | * |
| 10022 | * SUSv3 section 2.8.1 "Consequences of Shell Errors" says: |
| 10023 | * For a non-interactive shell, an error condition encountered |
| 10024 | * by a special built-in ... shall cause the shell to write a diagnostic message |
| 10025 | * to standard error and exit as shown in the following table: |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 10026 | * Error Special Built-In |
Denis Vlasenko | 94e87bc | 2008-02-14 16:51:58 +0000 | [diff] [blame] | 10027 | * ... |
| 10028 | * Utility syntax error (option or operand error) Shall exit |
| 10029 | * ... |
| 10030 | * However, in bug 1142 (http://busybox.net/bugs/view.php?id=1142) |
| 10031 | * we see that bash does not do that (set "finishes" with error code 1 instead, |
| 10032 | * and shell continues), and people rely on this behavior! |
| 10033 | * Testcase: |
| 10034 | * set -o barfoo 2>/dev/null |
| 10035 | * echo $? |
| 10036 | * |
| 10037 | * Oh well. Let's mimic that. |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 10038 | */ |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10039 | static int |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10040 | plus_minus_o(char *name, int val) |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10041 | { |
| 10042 | int i; |
| 10043 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10044 | if (name) { |
| 10045 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10046 | if (strcmp(name, optnames(i)) == 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10047 | optlist[i] = val; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10048 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10049 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10050 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10051 | ash_msg("illegal option %co %s", val ? '-' : '+', name); |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10052 | return 1; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10053 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10054 | for (i = 0; i < NOPTS; i++) { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10055 | if (val) { |
| 10056 | out1fmt("%-16s%s\n", optnames(i), optlist[i] ? "on" : "off"); |
| 10057 | } else { |
| 10058 | out1fmt("set %co %s\n", optlist[i] ? '-' : '+', optnames(i)); |
| 10059 | } |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10060 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10061 | return 0; |
Eric Andersen | 6248355 | 2001-07-10 06:09:16 +0000 | [diff] [blame] | 10062 | } |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10063 | static void |
| 10064 | setoption(int flag, int val) |
| 10065 | { |
| 10066 | int i; |
| 10067 | |
| 10068 | for (i = 0; i < NOPTS; i++) { |
| 10069 | if (optletters(i) == flag) { |
| 10070 | optlist[i] = val; |
| 10071 | return; |
| 10072 | } |
| 10073 | } |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10074 | ash_msg_and_raise_error("illegal option %c%c", val ? '-' : '+', flag); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10075 | /* NOTREACHED */ |
| 10076 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10077 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10078 | options(int cmdline) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10079 | { |
| 10080 | char *p; |
| 10081 | int val; |
| 10082 | int c; |
| 10083 | |
| 10084 | if (cmdline) |
| 10085 | minusc = NULL; |
| 10086 | while ((p = *argptr) != NULL) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10087 | c = *p++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10088 | if (c != '-' && c != '+') |
| 10089 | break; |
| 10090 | argptr++; |
| 10091 | val = 0; /* val = 0 if c == '+' */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10092 | if (c == '-') { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10093 | val = 1; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10094 | if (p[0] == '\0' || LONE_DASH(p)) { |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10095 | if (!cmdline) { |
| 10096 | /* "-" means turn off -x and -v */ |
| 10097 | if (p[0] == '\0') |
| 10098 | xflag = vflag = 0; |
| 10099 | /* "--" means reset params */ |
| 10100 | else if (*argptr == NULL) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10101 | setparam(argptr); |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10102 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10103 | break; /* "-" or "--" terminates options */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10104 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10105 | } |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10106 | /* first char was + or - */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10107 | while ((c = *p++) != '\0') { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10108 | /* bash 3.2 indeed handles -c CMD and +c CMD the same */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10109 | if (c == 'c' && cmdline) { |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10110 | minusc = p; /* command is after shell args */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10111 | } else if (c == 'o') { |
Denis Vlasenko | dddfaff | 2008-05-06 15:30:27 +0000 | [diff] [blame] | 10112 | if (plus_minus_o(*argptr, val)) { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10113 | /* it already printed err message */ |
| 10114 | return 1; /* error */ |
| 10115 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10116 | if (*argptr) |
| 10117 | argptr++; |
Denis Vlasenko | 8fdc4b7 | 2007-07-14 11:33:10 +0000 | [diff] [blame] | 10118 | } else if (cmdline && (c == 'l')) { /* -l or +l == --login */ |
| 10119 | isloginsh = 1; |
| 10120 | /* bash does not accept +-login, we also won't */ |
| 10121 | } else if (cmdline && val && (c == '-')) { /* long options */ |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10122 | if (strcmp(p, "login") == 0) |
Robert Griebl | 64f70cc | 2002-05-14 23:22:06 +0000 | [diff] [blame] | 10123 | isloginsh = 1; |
| 10124 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10125 | } else { |
| 10126 | setoption(c, val); |
| 10127 | } |
| 10128 | } |
| 10129 | } |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10130 | return 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10131 | } |
| 10132 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10133 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10134 | * The shift builtin command. |
| 10135 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10136 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10137 | shiftcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10138 | { |
| 10139 | int n; |
| 10140 | char **ap1, **ap2; |
| 10141 | |
| 10142 | n = 1; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10143 | if (argv[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10144 | n = number(argv[1]); |
| 10145 | if (n > shellparam.nparam) |
Denis Vlasenko | c90e1be | 2008-07-30 15:35:05 +0000 | [diff] [blame] | 10146 | n = 0; /* bash compat, was = shellparam.nparam; */ |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10147 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10148 | shellparam.nparam -= n; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10149 | for (ap1 = shellparam.p; --n >= 0; ap1++) { |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 10150 | if (shellparam.malloced) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10151 | free(*ap1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10152 | } |
| 10153 | ap2 = shellparam.p; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 10154 | while ((*ap2++ = *ap1++) != NULL) |
| 10155 | continue; |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10156 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10157 | shellparam.optind = 1; |
| 10158 | shellparam.optoff = -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10159 | #endif |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10160 | INT_ON; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10161 | return 0; |
| 10162 | } |
| 10163 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10164 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10165 | * POSIX requires that 'set' (but not export or readonly) output the |
| 10166 | * variables in lexicographic order - by the locale's collating order (sigh). |
| 10167 | * Maybe we could keep them in an ordered balanced binary tree |
| 10168 | * instead of hashed lists. |
| 10169 | * For now just roll 'em through qsort for printing... |
| 10170 | */ |
| 10171 | static int |
| 10172 | showvars(const char *sep_prefix, int on, int off) |
| 10173 | { |
| 10174 | const char *sep; |
| 10175 | char **ep, **epend; |
| 10176 | |
| 10177 | ep = listvars(on, off, &epend); |
| 10178 | qsort(ep, epend - ep, sizeof(char *), vpcmp); |
| 10179 | |
Denis Vlasenko | 2de3d9f | 2007-02-23 21:10:23 +0000 | [diff] [blame] | 10180 | sep = *sep_prefix ? " " : sep_prefix; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10181 | |
| 10182 | for (; ep < epend; ep++) { |
| 10183 | const char *p; |
| 10184 | const char *q; |
| 10185 | |
| 10186 | p = strchrnul(*ep, '='); |
| 10187 | q = nullstr; |
| 10188 | if (*p) |
| 10189 | q = single_quote(++p); |
| 10190 | out1fmt("%s%s%.*s%s\n", sep_prefix, sep, (int)(p - *ep), *ep, q); |
| 10191 | } |
| 10192 | return 0; |
| 10193 | } |
| 10194 | |
| 10195 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10196 | * The set command builtin. |
| 10197 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10198 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10199 | setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10200 | { |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10201 | int retval; |
| 10202 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 10203 | if (!argv[1]) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10204 | return showvars(nullstr, 0, VUNSET); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10205 | INT_OFF; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10206 | retval = 1; |
| 10207 | if (!options(0)) { /* if no parse error... */ |
| 10208 | retval = 0; |
| 10209 | optschanged(); |
| 10210 | if (*argptr != NULL) { |
| 10211 | setparam(argptr); |
| 10212 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10213 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10214 | INT_ON; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 10215 | return retval; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10216 | } |
| 10217 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10218 | #if ENABLE_ASH_RANDOM_SUPPORT |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10219 | static void FAST_FUNC |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 10220 | change_random(const char *value) |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10221 | { |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10222 | uint32_t t; |
| 10223 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10224 | if (value == NULL) { |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10225 | /* "get", generate */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10226 | t = next_random(&random_gen); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10227 | /* set without recursion */ |
Denys Vlasenko | 8837c5d | 2010-06-02 12:56:18 +0200 | [diff] [blame] | 10228 | setvar(vrandom.var_text, utoa(t), VNOFUNC); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10229 | vrandom.flags &= ~VNOFUNC; |
| 10230 | } else { |
| 10231 | /* set/reset */ |
Denys Vlasenko | 3ea2e82 | 2009-10-09 20:59:04 +0200 | [diff] [blame] | 10232 | t = strtoul(value, NULL, 10); |
| 10233 | INIT_RANDOM_T(&random_gen, (t ? t : 1), t); |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10234 | } |
Eric Andersen | ef02f82 | 2004-03-11 13:34:24 +0000 | [diff] [blame] | 10235 | } |
Eric Andersen | 16767e2 | 2004-03-16 05:14:10 +0000 | [diff] [blame] | 10236 | #endif |
| 10237 | |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10238 | #if ENABLE_ASH_GETOPTS |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10239 | static int |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10240 | getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10241 | { |
| 10242 | char *p, *q; |
| 10243 | char c = '?'; |
| 10244 | int done = 0; |
| 10245 | int err = 0; |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10246 | char s[12]; |
| 10247 | char **optnext; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10248 | |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10249 | if (*param_optind < 1) |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10250 | return 1; |
| 10251 | optnext = optfirst + *param_optind - 1; |
| 10252 | |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 10253 | if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10254 | p = NULL; |
| 10255 | else |
Eric Andersen | a48b0a3 | 2003-10-22 10:56:47 +0000 | [diff] [blame] | 10256 | p = optnext[-1] + *optoff; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10257 | if (p == NULL || *p == '\0') { |
| 10258 | /* Current word is done, advance */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10259 | p = *optnext; |
| 10260 | if (p == NULL || *p != '-' || *++p == '\0') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10261 | atend: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10262 | p = NULL; |
| 10263 | done = 1; |
| 10264 | goto out; |
| 10265 | } |
| 10266 | optnext++; |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 10267 | if (LONE_DASH(p)) /* check for "--" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10268 | goto atend; |
| 10269 | } |
| 10270 | |
| 10271 | c = *p++; |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 10272 | for (q = optstr; *q != c;) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10273 | if (*q == '\0') { |
| 10274 | if (optstr[0] == ':') { |
| 10275 | s[0] = c; |
| 10276 | s[1] = '\0'; |
| 10277 | err |= setvarsafe("OPTARG", s, 0); |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10278 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10279 | fprintf(stderr, "Illegal option -%c\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10280 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10281 | } |
| 10282 | c = '?'; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10283 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10284 | } |
| 10285 | if (*++q == ':') |
| 10286 | q++; |
| 10287 | } |
| 10288 | |
| 10289 | if (*++q == ':') { |
| 10290 | if (*p == '\0' && (p = *optnext) == NULL) { |
| 10291 | if (optstr[0] == ':') { |
| 10292 | s[0] = c; |
| 10293 | s[1] = '\0'; |
| 10294 | err |= setvarsafe("OPTARG", s, 0); |
| 10295 | c = ':'; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10296 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10297 | fprintf(stderr, "No arg for -%c option\n", c); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10298 | unsetvar("OPTARG"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10299 | c = '?'; |
| 10300 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10301 | goto out; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10302 | } |
| 10303 | |
| 10304 | if (p == *optnext) |
| 10305 | optnext++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10306 | err |= setvarsafe("OPTARG", p, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10307 | p = NULL; |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10308 | } else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10309 | err |= setvarsafe("OPTARG", nullstr, 0); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10310 | out: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10311 | *optoff = p ? p - *(optnext - 1) : -1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10312 | *param_optind = optnext - optfirst + 1; |
| 10313 | fmtstr(s, sizeof(s), "%d", *param_optind); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10314 | err |= setvarsafe("OPTIND", s, VNOFUNC); |
| 10315 | s[0] = c; |
| 10316 | s[1] = '\0'; |
| 10317 | err |= setvarsafe(optvar, s, 0); |
| 10318 | if (err) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10319 | *param_optind = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10320 | *optoff = -1; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 10321 | flush_stdout_stderr(); |
| 10322 | raise_exception(EXERROR); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10323 | } |
| 10324 | return done; |
| 10325 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10326 | |
| 10327 | /* |
| 10328 | * The getopts builtin. Shellparam.optnext points to the next argument |
| 10329 | * to be processed. Shellparam.optptr points to the next character to |
| 10330 | * be processed in the current argument. If shellparam.optnext is NULL, |
| 10331 | * then it's the first time getopts has been called. |
| 10332 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 10333 | static int FAST_FUNC |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10334 | getoptscmd(int argc, char **argv) |
| 10335 | { |
| 10336 | char **optbase; |
| 10337 | |
| 10338 | if (argc < 3) |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 10339 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10340 | if (argc == 3) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10341 | optbase = shellparam.p; |
| 10342 | if (shellparam.optind > shellparam.nparam + 1) { |
| 10343 | shellparam.optind = 1; |
| 10344 | shellparam.optoff = -1; |
| 10345 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10346 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10347 | optbase = &argv[3]; |
| 10348 | if (shellparam.optind > argc - 2) { |
| 10349 | shellparam.optind = 1; |
| 10350 | shellparam.optoff = -1; |
| 10351 | } |
| 10352 | } |
| 10353 | |
| 10354 | return getopts(argv[1], argv[2], optbase, &shellparam.optind, |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10355 | &shellparam.optoff); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10356 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10357 | #endif /* ASH_GETOPTS */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10358 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10359 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10360 | /* ============ Shell parser */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10361 | |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 10362 | struct heredoc { |
| 10363 | struct heredoc *next; /* next here document in list */ |
| 10364 | union node *here; /* redirection node */ |
| 10365 | char *eofmark; /* string indicating end of input */ |
| 10366 | smallint striptabs; /* if set, strip leading tabs */ |
| 10367 | }; |
| 10368 | |
| 10369 | static smallint tokpushback; /* last token pushed back */ |
| 10370 | static smallint parsebackquote; /* nonzero if we are inside backquotes */ |
| 10371 | static smallint quoteflag; /* set if (part of) last token was quoted */ |
| 10372 | static token_id_t lasttoken; /* last token read (integer id Txxx) */ |
| 10373 | static struct heredoc *heredoclist; /* list of here documents to read */ |
| 10374 | static char *wordtext; /* text of last word returned by readtoken */ |
| 10375 | static struct nodelist *backquotelist; |
| 10376 | static union node *redirnode; |
| 10377 | static struct heredoc *heredoc; |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 10378 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10379 | static const char * |
| 10380 | tokname(char *buf, int tok) |
| 10381 | { |
| 10382 | if (tok < TSEMI) |
| 10383 | return tokname_array[tok] + 1; |
| 10384 | sprintf(buf, "\"%s\"", tokname_array[tok] + 1); |
| 10385 | return buf; |
| 10386 | } |
| 10387 | |
| 10388 | /* raise_error_unexpected_syntax: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10389 | * Called when an unexpected token is read during the parse. The argument |
| 10390 | * is the token that is expected, or -1 if more than one type of token can |
| 10391 | * occur at this point. |
| 10392 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 10393 | static void raise_error_unexpected_syntax(int) NORETURN; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10394 | static void |
| 10395 | raise_error_unexpected_syntax(int token) |
| 10396 | { |
| 10397 | char msg[64]; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10398 | char buf[16]; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10399 | int l; |
| 10400 | |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10401 | l = sprintf(msg, "unexpected %s", tokname(buf, lasttoken)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10402 | if (token >= 0) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10403 | sprintf(msg + l, " (expecting %s)", tokname(buf, token)); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10404 | raise_error_syntax(msg); |
| 10405 | /* NOTREACHED */ |
| 10406 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10407 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10408 | #define EOFMARKLEN 79 |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10409 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10410 | /* parsing is heavily cross-recursive, need these forward decls */ |
| 10411 | static union node *andor(void); |
| 10412 | static union node *pipeline(void); |
| 10413 | static union node *parse_command(void); |
| 10414 | static void parseheredoc(void); |
| 10415 | static char peektoken(void); |
| 10416 | static int readtoken(void); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10417 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10418 | static union node * |
| 10419 | list(int nlflag) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10420 | { |
| 10421 | union node *n1, *n2, *n3; |
| 10422 | int tok; |
| 10423 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10424 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10425 | if (nlflag == 2 && peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10426 | return NULL; |
| 10427 | n1 = NULL; |
| 10428 | for (;;) { |
| 10429 | n2 = andor(); |
| 10430 | tok = readtoken(); |
| 10431 | if (tok == TBACKGND) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10432 | if (n2->type == NPIPE) { |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10433 | n2->npipe.pipe_backgnd = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10434 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10435 | if (n2->type != NREDIR) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10436 | n3 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10437 | n3->nredir.n = n2; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10438 | /*n3->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10439 | n2 = n3; |
| 10440 | } |
| 10441 | n2->type = NBACKGND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10442 | } |
| 10443 | } |
| 10444 | if (n1 == NULL) { |
| 10445 | n1 = n2; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10446 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10447 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10448 | n3->type = NSEMI; |
| 10449 | n3->nbinary.ch1 = n1; |
| 10450 | n3->nbinary.ch2 = n2; |
| 10451 | n1 = n3; |
| 10452 | } |
| 10453 | switch (tok) { |
| 10454 | case TBACKGND: |
| 10455 | case TSEMI: |
| 10456 | tok = readtoken(); |
| 10457 | /* fall through */ |
| 10458 | case TNL: |
| 10459 | if (tok == TNL) { |
| 10460 | parseheredoc(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10461 | if (nlflag == 1) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10462 | return n1; |
| 10463 | } else { |
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 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10466 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 10467 | if (peektoken()) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10468 | return n1; |
| 10469 | break; |
| 10470 | case TEOF: |
| 10471 | if (heredoclist) |
| 10472 | parseheredoc(); |
| 10473 | else |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10474 | pungetc(); /* push back EOF on input */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10475 | return n1; |
| 10476 | default: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10477 | if (nlflag == 1) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10478 | raise_error_unexpected_syntax(-1); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10479 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10480 | return n1; |
| 10481 | } |
| 10482 | } |
| 10483 | } |
| 10484 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10485 | static union node * |
| 10486 | andor(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10487 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10488 | union node *n1, *n2, *n3; |
| 10489 | int t; |
| 10490 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10491 | n1 = pipeline(); |
| 10492 | for (;;) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10493 | t = readtoken(); |
| 10494 | if (t == TAND) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10495 | t = NAND; |
| 10496 | } else if (t == TOR) { |
| 10497 | t = NOR; |
| 10498 | } else { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10499 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10500 | return n1; |
| 10501 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10502 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10503 | n2 = pipeline(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10504 | n3 = stzalloc(sizeof(struct nbinary)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10505 | n3->type = t; |
| 10506 | n3->nbinary.ch1 = n1; |
| 10507 | n3->nbinary.ch2 = n2; |
| 10508 | n1 = n3; |
| 10509 | } |
| 10510 | } |
| 10511 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10512 | static union node * |
| 10513 | pipeline(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10514 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10515 | union node *n1, *n2, *pipenode; |
| 10516 | struct nodelist *lp, *prev; |
| 10517 | int negate; |
| 10518 | |
| 10519 | negate = 0; |
| 10520 | TRACE(("pipeline: entered\n")); |
| 10521 | if (readtoken() == TNOT) { |
| 10522 | negate = !negate; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10523 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10524 | } else |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10525 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10526 | n1 = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10527 | if (readtoken() == TPIPE) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10528 | pipenode = stzalloc(sizeof(struct npipe)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10529 | pipenode->type = NPIPE; |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10530 | /*pipenode->npipe.pipe_backgnd = 0; - stzalloc did it */ |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10531 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10532 | pipenode->npipe.cmdlist = lp; |
| 10533 | lp->n = n1; |
| 10534 | do { |
| 10535 | prev = lp; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10536 | lp = stzalloc(sizeof(struct nodelist)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10537 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10538 | lp->n = parse_command(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10539 | prev->next = lp; |
| 10540 | } while (readtoken() == TPIPE); |
| 10541 | lp->next = NULL; |
| 10542 | n1 = pipenode; |
| 10543 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10544 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10545 | if (negate) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10546 | n2 = stzalloc(sizeof(struct nnot)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10547 | n2->type = NNOT; |
| 10548 | n2->nnot.com = n1; |
| 10549 | return n2; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 10550 | } |
| 10551 | return n1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10552 | } |
| 10553 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10554 | static union node * |
| 10555 | makename(void) |
| 10556 | { |
| 10557 | union node *n; |
| 10558 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10559 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10560 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10561 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10562 | n->narg.text = wordtext; |
| 10563 | n->narg.backquote = backquotelist; |
| 10564 | return n; |
| 10565 | } |
| 10566 | |
| 10567 | static void |
| 10568 | fixredir(union node *n, const char *text, int err) |
| 10569 | { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10570 | int fd; |
| 10571 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10572 | TRACE(("Fix redir %s %d\n", text, err)); |
| 10573 | if (!err) |
| 10574 | n->ndup.vname = NULL; |
| 10575 | |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 10576 | fd = bb_strtou(text, NULL, 10); |
| 10577 | if (!errno && fd >= 0) |
| 10578 | n->ndup.dupfd = fd; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10579 | else if (LONE_DASH(text)) |
| 10580 | n->ndup.dupfd = -1; |
| 10581 | else { |
| 10582 | if (err) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10583 | raise_error_syntax("bad fd number"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10584 | n->ndup.vname = makename(); |
| 10585 | } |
| 10586 | } |
| 10587 | |
| 10588 | /* |
| 10589 | * Returns true if the text contains nothing to expand (no dollar signs |
| 10590 | * or backquotes). |
| 10591 | */ |
| 10592 | static int |
Denis Vlasenko | 68819d1 | 2008-12-15 11:26:36 +0000 | [diff] [blame] | 10593 | noexpand(const char *text) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10594 | { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10595 | unsigned char c; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10596 | |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10597 | while ((c = *text++) != '\0') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10598 | if (c == CTLQUOTEMARK) |
| 10599 | continue; |
| 10600 | if (c == CTLESC) |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 10601 | text++; |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 10602 | else if (SIT(c, BASESYNTAX) == CCTL) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10603 | return 0; |
| 10604 | } |
| 10605 | return 1; |
| 10606 | } |
| 10607 | |
| 10608 | static void |
| 10609 | parsefname(void) |
| 10610 | { |
| 10611 | union node *n = redirnode; |
| 10612 | |
| 10613 | if (readtoken() != TWORD) |
| 10614 | raise_error_unexpected_syntax(-1); |
| 10615 | if (n->type == NHERE) { |
| 10616 | struct heredoc *here = heredoc; |
| 10617 | struct heredoc *p; |
| 10618 | int i; |
| 10619 | |
| 10620 | if (quoteflag == 0) |
| 10621 | n->type = NXHERE; |
| 10622 | TRACE(("Here document %d\n", n->type)); |
| 10623 | if (!noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10624 | raise_error_syntax("illegal eof marker for << redirection"); |
Denys Vlasenko | b6c8434 | 2009-08-29 20:23:20 +0200 | [diff] [blame] | 10625 | rmescapes(wordtext, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10626 | here->eofmark = wordtext; |
| 10627 | here->next = NULL; |
| 10628 | if (heredoclist == NULL) |
| 10629 | heredoclist = here; |
| 10630 | else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10631 | for (p = heredoclist; p->next; p = p->next) |
| 10632 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10633 | p->next = here; |
| 10634 | } |
| 10635 | } else if (n->type == NTOFD || n->type == NFROMFD) { |
| 10636 | fixredir(n, wordtext, 0); |
| 10637 | } else { |
| 10638 | n->nfile.fname = makename(); |
| 10639 | } |
| 10640 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10641 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10642 | static union node * |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10643 | simplecmd(void) |
| 10644 | { |
| 10645 | union node *args, **app; |
| 10646 | union node *n = NULL; |
| 10647 | union node *vars, **vpp; |
| 10648 | union node **rpp, *redir; |
| 10649 | int savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10650 | #if ENABLE_ASH_BASH_COMPAT |
| 10651 | smallint double_brackets_flag = 0; |
| 10652 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10653 | |
| 10654 | args = NULL; |
| 10655 | app = &args; |
| 10656 | vars = NULL; |
| 10657 | vpp = &vars; |
| 10658 | redir = NULL; |
| 10659 | rpp = &redir; |
| 10660 | |
| 10661 | savecheckkwd = CHKALIAS; |
| 10662 | for (;;) { |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10663 | int t; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10664 | checkkwd = savecheckkwd; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10665 | t = readtoken(); |
| 10666 | switch (t) { |
| 10667 | #if ENABLE_ASH_BASH_COMPAT |
| 10668 | case TAND: /* "&&" */ |
| 10669 | case TOR: /* "||" */ |
| 10670 | if (!double_brackets_flag) { |
| 10671 | tokpushback = 1; |
| 10672 | goto out; |
| 10673 | } |
| 10674 | wordtext = (char *) (t == TAND ? "-a" : "-o"); |
| 10675 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10676 | case TWORD: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10677 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10678 | n->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10679 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10680 | n->narg.text = wordtext; |
Denis Vlasenko | 80591b0 | 2008-03-25 07:49:43 +0000 | [diff] [blame] | 10681 | #if ENABLE_ASH_BASH_COMPAT |
| 10682 | if (strcmp("[[", wordtext) == 0) |
| 10683 | double_brackets_flag = 1; |
| 10684 | else if (strcmp("]]", wordtext) == 0) |
| 10685 | double_brackets_flag = 0; |
| 10686 | #endif |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10687 | n->narg.backquote = backquotelist; |
| 10688 | if (savecheckkwd && isassignment(wordtext)) { |
| 10689 | *vpp = n; |
| 10690 | vpp = &n->narg.next; |
| 10691 | } else { |
| 10692 | *app = n; |
| 10693 | app = &n->narg.next; |
| 10694 | savecheckkwd = 0; |
| 10695 | } |
| 10696 | break; |
| 10697 | case TREDIR: |
| 10698 | *rpp = n = redirnode; |
| 10699 | rpp = &n->nfile.next; |
| 10700 | parsefname(); /* read name of redirection file */ |
| 10701 | break; |
| 10702 | case TLP: |
| 10703 | if (args && app == &args->narg.next |
| 10704 | && !vars && !redir |
| 10705 | ) { |
| 10706 | struct builtincmd *bcmd; |
| 10707 | const char *name; |
| 10708 | |
| 10709 | /* We have a function */ |
| 10710 | if (readtoken() != TRP) |
| 10711 | raise_error_unexpected_syntax(TRP); |
| 10712 | name = n->narg.text; |
| 10713 | if (!goodname(name) |
| 10714 | || ((bcmd = find_builtin(name)) && IS_BUILTIN_SPECIAL(bcmd)) |
| 10715 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10716 | raise_error_syntax("bad function name"); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10717 | } |
| 10718 | n->type = NDEFUN; |
| 10719 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
| 10720 | n->narg.next = parse_command(); |
| 10721 | return n; |
| 10722 | } |
| 10723 | /* fall through */ |
| 10724 | default: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10725 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10726 | goto out; |
| 10727 | } |
| 10728 | } |
| 10729 | out: |
| 10730 | *app = NULL; |
| 10731 | *vpp = NULL; |
| 10732 | *rpp = NULL; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10733 | n = stzalloc(sizeof(struct ncmd)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10734 | n->type = NCMD; |
| 10735 | n->ncmd.args = args; |
| 10736 | n->ncmd.assign = vars; |
| 10737 | n->ncmd.redirect = redir; |
| 10738 | return n; |
| 10739 | } |
| 10740 | |
| 10741 | static union node * |
| 10742 | parse_command(void) |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 10743 | { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10744 | union node *n1, *n2; |
| 10745 | union node *ap, **app; |
| 10746 | union node *cp, **cpp; |
| 10747 | union node *redir, **rpp; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10748 | union node **rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10749 | int t; |
| 10750 | |
| 10751 | redir = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10752 | rpp2 = &redir; |
Eric Andersen | 88cec25 | 2001-09-06 17:35:20 +0000 | [diff] [blame] | 10753 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10754 | switch (readtoken()) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10755 | default: |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10756 | raise_error_unexpected_syntax(-1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10757 | /* NOTREACHED */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10758 | case TIF: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10759 | n1 = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10760 | n1->type = NIF; |
| 10761 | n1->nif.test = list(0); |
| 10762 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10763 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10764 | n1->nif.ifpart = list(0); |
| 10765 | n2 = n1; |
| 10766 | while (readtoken() == TELIF) { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10767 | n2->nif.elsepart = stzalloc(sizeof(struct nif)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10768 | n2 = n2->nif.elsepart; |
| 10769 | n2->type = NIF; |
| 10770 | n2->nif.test = list(0); |
| 10771 | if (readtoken() != TTHEN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10772 | raise_error_unexpected_syntax(TTHEN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10773 | n2->nif.ifpart = list(0); |
| 10774 | } |
| 10775 | if (lasttoken == TELSE) |
| 10776 | n2->nif.elsepart = list(0); |
| 10777 | else { |
| 10778 | n2->nif.elsepart = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10779 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10780 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10781 | t = TFI; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10782 | break; |
| 10783 | case TWHILE: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10784 | case TUNTIL: { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10785 | int got; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10786 | n1 = stzalloc(sizeof(struct nbinary)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10787 | n1->type = (lasttoken == TWHILE) ? NWHILE : NUNTIL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10788 | n1->nbinary.ch1 = list(0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 10789 | got = readtoken(); |
| 10790 | if (got != TDO) { |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 10791 | TRACE(("expecting DO got '%s' %s\n", tokname_array[got] + 1, |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 10792 | got == TWORD ? wordtext : "")); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10793 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10794 | } |
| 10795 | n1->nbinary.ch2 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10796 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10797 | break; |
| 10798 | } |
| 10799 | case TFOR: |
Denis Vlasenko | 2dc240c | 2008-07-24 06:07:50 +0000 | [diff] [blame] | 10800 | if (readtoken() != TWORD || quoteflag || !goodname(wordtext)) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 10801 | raise_error_syntax("bad for loop variable"); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10802 | n1 = stzalloc(sizeof(struct nfor)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10803 | n1->type = NFOR; |
| 10804 | n1->nfor.var = wordtext; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10805 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10806 | if (readtoken() == TIN) { |
| 10807 | app = ≈ |
| 10808 | while (readtoken() == TWORD) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10809 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10810 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10811 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10812 | n2->narg.text = wordtext; |
| 10813 | n2->narg.backquote = backquotelist; |
| 10814 | *app = n2; |
| 10815 | app = &n2->narg.next; |
| 10816 | } |
| 10817 | *app = NULL; |
| 10818 | n1->nfor.args = ap; |
| 10819 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10820 | raise_error_unexpected_syntax(-1); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10821 | } else { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10822 | n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10823 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10824 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10825 | n2->narg.text = (char *)dolatstr; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10826 | /*n2->narg.backquote = NULL;*/ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10827 | n1->nfor.args = n2; |
| 10828 | /* |
| 10829 | * Newline or semicolon here is optional (but note |
| 10830 | * that the original Bourne shell only allowed NL). |
| 10831 | */ |
| 10832 | if (lasttoken != TNL && lasttoken != TSEMI) |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10833 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10834 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10835 | checkkwd = CHKNL | CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10836 | if (readtoken() != TDO) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10837 | raise_error_unexpected_syntax(TDO); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10838 | n1->nfor.body = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10839 | t = TDONE; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10840 | break; |
| 10841 | case TCASE: |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10842 | n1 = stzalloc(sizeof(struct ncase)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10843 | n1->type = NCASE; |
| 10844 | if (readtoken() != TWORD) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10845 | raise_error_unexpected_syntax(TWORD); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10846 | n1->ncase.expr = n2 = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10847 | n2->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10848 | /*n2->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10849 | n2->narg.text = wordtext; |
| 10850 | n2->narg.backquote = backquotelist; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10851 | do { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10852 | checkkwd = CHKKWD | CHKALIAS; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10853 | } while (readtoken() == TNL); |
| 10854 | if (lasttoken != TIN) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10855 | raise_error_unexpected_syntax(TIN); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10856 | cpp = &n1->ncase.cases; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10857 | next_case: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10858 | checkkwd = CHKNL | CHKKWD; |
| 10859 | t = readtoken(); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 10860 | while (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10861 | if (lasttoken == TLP) |
| 10862 | readtoken(); |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 10863 | *cpp = cp = stzalloc(sizeof(struct nclist)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10864 | cp->type = NCLIST; |
| 10865 | app = &cp->nclist.pattern; |
| 10866 | for (;;) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10867 | *app = ap = stzalloc(sizeof(struct narg)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10868 | ap->type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10869 | /*ap->narg.next = NULL; - stzalloc did it */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10870 | ap->narg.text = wordtext; |
| 10871 | ap->narg.backquote = backquotelist; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10872 | if (readtoken() != TPIPE) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10873 | break; |
| 10874 | app = &ap->narg.next; |
| 10875 | readtoken(); |
| 10876 | } |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10877 | //ap->narg.next = NULL; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10878 | if (lasttoken != TRP) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10879 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10880 | cp->nclist.body = list(2); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10881 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10882 | cpp = &cp->nclist.next; |
| 10883 | |
| 10884 | checkkwd = CHKNL | CHKKWD; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10885 | t = readtoken(); |
| 10886 | if (t != TESAC) { |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10887 | if (t != TENDCASE) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10888 | raise_error_unexpected_syntax(TENDCASE); |
| 10889 | goto next_case; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10890 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10891 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10892 | *cpp = NULL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10893 | goto redir; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10894 | case TLP: |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10895 | n1 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10896 | n1->type = NSUBSHELL; |
| 10897 | n1->nredir.n = list(0); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10898 | /*n1->nredir.redirect = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10899 | t = TRP; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10900 | break; |
| 10901 | case TBEGIN: |
| 10902 | n1 = list(0); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10903 | t = TEND; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10904 | break; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10905 | case TWORD: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10906 | case TREDIR: |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10907 | tokpushback = 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10908 | return simplecmd(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10909 | } |
| 10910 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10911 | if (readtoken() != t) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 10912 | raise_error_unexpected_syntax(t); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10913 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 10914 | redir: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10915 | /* Now check for redirection which may follow command */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 10916 | checkkwd = CHKKWD | CHKALIAS; |
| 10917 | rpp = rpp2; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10918 | while (readtoken() == TREDIR) { |
| 10919 | *rpp = n2 = redirnode; |
| 10920 | rpp = &n2->nfile.next; |
| 10921 | parsefname(); |
| 10922 | } |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 10923 | tokpushback = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10924 | *rpp = NULL; |
| 10925 | if (redir) { |
| 10926 | if (n1->type != NSUBSHELL) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 10927 | n2 = stzalloc(sizeof(struct nredir)); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10928 | n2->type = NREDIR; |
| 10929 | n2->nredir.n = n1; |
| 10930 | n1 = n2; |
| 10931 | } |
| 10932 | n1->nredir.redirect = redir; |
| 10933 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10934 | return n1; |
| 10935 | } |
| 10936 | |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 10937 | #if ENABLE_ASH_BASH_COMPAT |
| 10938 | static int decode_dollar_squote(void) |
| 10939 | { |
| 10940 | static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567"; |
| 10941 | int c, cnt; |
| 10942 | char *p; |
| 10943 | char buf[4]; |
| 10944 | |
| 10945 | c = pgetc(); |
| 10946 | p = strchr(C_escapes, c); |
| 10947 | if (p) { |
| 10948 | buf[0] = c; |
| 10949 | p = buf; |
| 10950 | cnt = 3; |
| 10951 | if ((unsigned char)(c - '0') <= 7) { /* \ooo */ |
| 10952 | do { |
| 10953 | c = pgetc(); |
| 10954 | *++p = c; |
| 10955 | } while ((unsigned char)(c - '0') <= 7 && --cnt); |
| 10956 | pungetc(); |
| 10957 | } else if (c == 'x') { /* \xHH */ |
| 10958 | do { |
| 10959 | c = pgetc(); |
| 10960 | *++p = c; |
| 10961 | } while (isxdigit(c) && --cnt); |
| 10962 | pungetc(); |
| 10963 | if (cnt == 3) { /* \x but next char is "bad" */ |
| 10964 | c = 'x'; |
| 10965 | goto unrecognized; |
| 10966 | } |
| 10967 | } else { /* simple seq like \\ or \t */ |
| 10968 | p++; |
| 10969 | } |
| 10970 | *p = '\0'; |
| 10971 | p = buf; |
| 10972 | c = bb_process_escape_sequence((void*)&p); |
| 10973 | } else { /* unrecognized "\z": print both chars unless ' or " */ |
| 10974 | if (c != '\'' && c != '"') { |
| 10975 | unrecognized: |
| 10976 | c |= 0x100; /* "please encode \, then me" */ |
| 10977 | } |
| 10978 | } |
| 10979 | return c; |
| 10980 | } |
| 10981 | #endif |
| 10982 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10983 | /* |
| 10984 | * If eofmark is NULL, read a word or a redirection symbol. If eofmark |
| 10985 | * is not NULL, read a here document. In the latter case, eofmark is the |
| 10986 | * 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] | 10987 | * leading tabs should be stripped from the document. The argument c |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 10988 | * is the first character of the input token or document. |
| 10989 | * |
| 10990 | * Because C does not have internal subroutines, I have simulated them |
| 10991 | * using goto's to implement the subroutine linkage. The following macros |
| 10992 | * will run code that appears at the end of readtoken1. |
| 10993 | */ |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 10994 | #define CHECKEND() {goto checkend; checkend_return:;} |
| 10995 | #define PARSEREDIR() {goto parseredir; parseredir_return:;} |
| 10996 | #define PARSESUB() {goto parsesub; parsesub_return:;} |
| 10997 | #define PARSEBACKQOLD() {oldstyle = 1; goto parsebackq; parsebackq_oldreturn:;} |
| 10998 | #define PARSEBACKQNEW() {oldstyle = 0; goto parsebackq; parsebackq_newreturn:;} |
| 10999 | #define PARSEARITH() {goto parsearith; parsearith_return:;} |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11000 | static int |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11001 | readtoken1(int c, int syntax, char *eofmark, int striptabs) |
Manuel Novoa III | 16815d4 | 2001-08-10 19:36:07 +0000 | [diff] [blame] | 11002 | { |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11003 | /* NB: syntax parameter fits into smallint */ |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11004 | /* c parameter is an unsigned char or PEOF or PEOA */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11005 | char *out; |
| 11006 | int len; |
| 11007 | char line[EOFMARKLEN + 1]; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11008 | struct nodelist *bqlist; |
| 11009 | smallint quotef; |
| 11010 | smallint dblquote; |
| 11011 | smallint oldstyle; |
| 11012 | smallint prevsyntax; /* syntax before arithmetic */ |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11013 | #if ENABLE_ASH_EXPAND_PRMT |
| 11014 | smallint pssyntax; /* we are expanding a prompt string */ |
| 11015 | #endif |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11016 | int varnest; /* levels of variables expansion */ |
| 11017 | int arinest; /* levels of arithmetic expansion */ |
| 11018 | int parenlevel; /* levels of parens in arithmetic */ |
| 11019 | int dqvarnest; /* levels of variables expansion within double quotes */ |
| 11020 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11021 | IF_ASH_BASH_COMPAT(smallint bash_dollar_squote = 0;) |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11022 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11023 | #if __GNUC__ |
| 11024 | /* Avoid longjmp clobbering */ |
| 11025 | (void) &out; |
| 11026 | (void) "ef; |
| 11027 | (void) &dblquote; |
| 11028 | (void) &varnest; |
| 11029 | (void) &arinest; |
| 11030 | (void) &parenlevel; |
| 11031 | (void) &dqvarnest; |
| 11032 | (void) &oldstyle; |
| 11033 | (void) &prevsyntax; |
| 11034 | (void) &syntax; |
| 11035 | #endif |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11036 | startlinno = g_parsefile->linno; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11037 | bqlist = NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11038 | quotef = 0; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11039 | prevsyntax = 0; |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11040 | #if ENABLE_ASH_EXPAND_PRMT |
| 11041 | pssyntax = (syntax == PSSYNTAX); |
| 11042 | if (pssyntax) |
| 11043 | syntax = DQSYNTAX; |
| 11044 | #endif |
| 11045 | dblquote = (syntax == DQSYNTAX); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11046 | varnest = 0; |
| 11047 | arinest = 0; |
| 11048 | parenlevel = 0; |
| 11049 | dqvarnest = 0; |
| 11050 | |
| 11051 | STARTSTACKSTR(out); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11052 | loop: |
| 11053 | /* For each line, until end of word */ |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11054 | CHECKEND(); /* set c to PEOF if at end of here document */ |
| 11055 | for (;;) { /* until end of line or end of word */ |
| 11056 | CHECKSTRSPACE(4, out); /* permit 4 calls to USTPUTC */ |
| 11057 | switch (SIT(c, syntax)) { |
| 11058 | case CNL: /* '\n' */ |
| 11059 | if (syntax == BASESYNTAX) |
| 11060 | goto endword; /* exit outer loop */ |
| 11061 | USTPUTC(c, out); |
| 11062 | g_parsefile->linno++; |
| 11063 | setprompt_if(doprompt, 2); |
| 11064 | c = pgetc(); |
| 11065 | goto loop; /* continue outer loop */ |
| 11066 | case CWORD: |
| 11067 | USTPUTC(c, out); |
| 11068 | break; |
| 11069 | case CCTL: |
| 11070 | if (eofmark == NULL || dblquote) |
| 11071 | USTPUTC(CTLESC, out); |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11072 | #if ENABLE_ASH_BASH_COMPAT |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11073 | if (c == '\\' && bash_dollar_squote) { |
| 11074 | c = decode_dollar_squote(); |
| 11075 | if (c & 0x100) { |
| 11076 | USTPUTC('\\', out); |
| 11077 | c = (unsigned char)c; |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11078 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11079 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11080 | #endif |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11081 | USTPUTC(c, out); |
| 11082 | break; |
| 11083 | case CBACK: /* backslash */ |
| 11084 | c = pgetc_without_PEOA(); |
| 11085 | if (c == PEOF) { |
| 11086 | USTPUTC(CTLESC, out); |
| 11087 | USTPUTC('\\', out); |
| 11088 | pungetc(); |
| 11089 | } else if (c == '\n') { |
| 11090 | setprompt_if(doprompt, 2); |
| 11091 | } else { |
| 11092 | #if ENABLE_ASH_EXPAND_PRMT |
| 11093 | if (c == '$' && pssyntax) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11094 | USTPUTC(CTLESC, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11095 | USTPUTC('\\', out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11096 | } |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11097 | #endif |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11098 | /* Backslash is retained if we are in "str" and next char isn't special */ |
| 11099 | if (dblquote |
| 11100 | && c != '\\' |
| 11101 | && c != '`' |
| 11102 | && c != '$' |
| 11103 | && (c != '"' || eofmark != NULL) |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11104 | ) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11105 | USTPUTC(CTLESC, out); |
| 11106 | USTPUTC('\\', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11107 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11108 | if (SIT(c, SQSYNTAX) == CCTL) |
| 11109 | USTPUTC(CTLESC, out); |
Denys Vlasenko | 0ff78a0 | 2010-08-30 15:20:07 +0200 | [diff] [blame] | 11110 | USTPUTC(c, out); |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11111 | quotef = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11112 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11113 | break; |
| 11114 | case CSQUOTE: |
| 11115 | syntax = SQSYNTAX; |
| 11116 | quotemark: |
| 11117 | if (eofmark == NULL) { |
| 11118 | USTPUTC(CTLQUOTEMARK, out); |
| 11119 | } |
| 11120 | break; |
| 11121 | case CDQUOTE: |
| 11122 | syntax = DQSYNTAX; |
| 11123 | dblquote = 1; |
| 11124 | goto quotemark; |
| 11125 | case CENDQUOTE: |
| 11126 | IF_ASH_BASH_COMPAT(bash_dollar_squote = 0;) |
| 11127 | if (eofmark != NULL && arinest == 0 |
| 11128 | && varnest == 0 |
| 11129 | ) { |
| 11130 | USTPUTC(c, out); |
| 11131 | } else { |
| 11132 | if (dqvarnest == 0) { |
| 11133 | syntax = BASESYNTAX; |
| 11134 | dblquote = 0; |
| 11135 | } |
| 11136 | quotef = 1; |
| 11137 | goto quotemark; |
| 11138 | } |
| 11139 | break; |
| 11140 | case CVAR: /* '$' */ |
| 11141 | PARSESUB(); /* parse substitution */ |
| 11142 | break; |
| 11143 | case CENDVAR: /* '}' */ |
| 11144 | if (varnest > 0) { |
| 11145 | varnest--; |
| 11146 | if (dqvarnest > 0) { |
| 11147 | dqvarnest--; |
| 11148 | } |
| 11149 | c = CTLENDVAR; |
| 11150 | } |
| 11151 | USTPUTC(c, out); |
| 11152 | break; |
| 11153 | #if ENABLE_SH_MATH_SUPPORT |
| 11154 | case CLP: /* '(' in arithmetic */ |
| 11155 | parenlevel++; |
| 11156 | USTPUTC(c, out); |
| 11157 | break; |
| 11158 | case CRP: /* ')' in arithmetic */ |
| 11159 | if (parenlevel > 0) { |
| 11160 | parenlevel--; |
| 11161 | } else { |
| 11162 | if (pgetc() == ')') { |
| 11163 | if (--arinest == 0) { |
| 11164 | syntax = prevsyntax; |
| 11165 | dblquote = (syntax == DQSYNTAX); |
| 11166 | c = CTLENDARI; |
| 11167 | } |
| 11168 | } else { |
| 11169 | /* |
| 11170 | * unbalanced parens |
| 11171 | * (don't 2nd guess - no error) |
| 11172 | */ |
| 11173 | pungetc(); |
| 11174 | } |
| 11175 | } |
| 11176 | USTPUTC(c, out); |
| 11177 | break; |
| 11178 | #endif |
| 11179 | case CBQUOTE: /* '`' */ |
| 11180 | PARSEBACKQOLD(); |
| 11181 | break; |
| 11182 | case CENDFILE: |
| 11183 | goto endword; /* exit outer loop */ |
| 11184 | case CIGN: |
| 11185 | break; |
| 11186 | default: |
| 11187 | if (varnest == 0) { |
| 11188 | #if ENABLE_ASH_BASH_COMPAT |
| 11189 | if (c == '&') { |
| 11190 | if (pgetc() == '>') |
| 11191 | c = 0x100 + '>'; /* flag &> */ |
| 11192 | pungetc(); |
| 11193 | } |
| 11194 | #endif |
| 11195 | goto endword; /* exit outer loop */ |
| 11196 | } |
| 11197 | IF_ASH_ALIAS(if (c != PEOA)) |
| 11198 | USTPUTC(c, out); |
| 11199 | } |
| 11200 | c = pgetc_fast(); |
| 11201 | } /* for (;;) */ |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11202 | endword: |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11203 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11204 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11205 | if (syntax == ARISYNTAX) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11206 | raise_error_syntax("missing '))'"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11207 | #endif |
Denis Vlasenko | 99eb850 | 2007-02-23 21:09:49 +0000 | [diff] [blame] | 11208 | if (syntax != BASESYNTAX && !parsebackquote && eofmark == NULL) |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11209 | raise_error_syntax("unterminated quoted string"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11210 | if (varnest != 0) { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11211 | startlinno = g_parsefile->linno; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11212 | /* { */ |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11213 | raise_error_syntax("missing '}'"); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11214 | } |
| 11215 | USTPUTC('\0', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11216 | len = out - (char *)stackblock(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11217 | out = stackblock(); |
| 11218 | if (eofmark == NULL) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11219 | if ((c == '>' || c == '<' IF_ASH_BASH_COMPAT( || c == 0x100 + '>')) |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11220 | && quotef == 0 |
| 11221 | ) { |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11222 | if (isdigit_str9(out)) { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11223 | PARSEREDIR(); /* passed as params: out, c */ |
| 11224 | lasttoken = TREDIR; |
| 11225 | return lasttoken; |
| 11226 | } |
| 11227 | /* else: non-number X seen, interpret it |
| 11228 | * as "NNNX>file" = "NNNX >file" */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11229 | } |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11230 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11231 | } |
| 11232 | quoteflag = quotef; |
| 11233 | backquotelist = bqlist; |
| 11234 | grabstackblock(len); |
| 11235 | wordtext = out; |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11236 | lasttoken = TWORD; |
| 11237 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11238 | /* end of readtoken routine */ |
| 11239 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11240 | /* |
| 11241 | * Check to see whether we are at the end of the here document. When this |
| 11242 | * is called, c is set to the first character of the next input line. If |
| 11243 | * we are at the end of the here document, this routine sets the c to PEOF. |
| 11244 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11245 | checkend: { |
| 11246 | if (eofmark) { |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11247 | #if ENABLE_ASH_ALIAS |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11248 | if (c == PEOA) |
| 11249 | c = pgetc_without_PEOA(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11250 | #endif |
| 11251 | if (striptabs) { |
| 11252 | while (c == '\t') { |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11253 | c = pgetc_without_PEOA(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11254 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11255 | } |
| 11256 | if (c == *eofmark) { |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11257 | if (pfgets(line, sizeof(line)) != NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11258 | char *p, *q; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11259 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11260 | p = line; |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11261 | for (q = eofmark + 1; *q && *p == *q; p++, q++) |
| 11262 | continue; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11263 | if (*p == '\n' && *q == '\0') { |
| 11264 | c = PEOF; |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11265 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11266 | needprompt = doprompt; |
| 11267 | } else { |
| 11268 | pushstring(line, NULL); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11269 | } |
| 11270 | } |
| 11271 | } |
| 11272 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11273 | goto checkend_return; |
| 11274 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11275 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11276 | /* |
| 11277 | * Parse a redirection operator. The variable "out" points to a string |
| 11278 | * specifying the fd to be redirected. The variable "c" contains the |
| 11279 | * first character of the redirection operator. |
| 11280 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11281 | parseredir: { |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11282 | /* out is already checked to be a valid number or "" */ |
| 11283 | int fd = (*out == '\0' ? -1 : atoi(out)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11284 | union node *np; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11285 | |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11286 | np = stzalloc(sizeof(struct nfile)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11287 | if (c == '>') { |
| 11288 | np->nfile.fd = 1; |
| 11289 | c = pgetc(); |
| 11290 | if (c == '>') |
| 11291 | np->type = NAPPEND; |
| 11292 | else if (c == '|') |
| 11293 | np->type = NCLOBBER; |
| 11294 | else if (c == '&') |
| 11295 | np->type = NTOFD; |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11296 | /* 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] | 11297 | else { |
| 11298 | np->type = NTO; |
| 11299 | pungetc(); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11300 | } |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11301 | } |
| 11302 | #if ENABLE_ASH_BASH_COMPAT |
| 11303 | else if (c == 0x100 + '>') { /* this flags &> redirection */ |
| 11304 | np->nfile.fd = 1; |
| 11305 | pgetc(); /* this is '>', no need to check */ |
| 11306 | np->type = NTO2; |
| 11307 | } |
| 11308 | #endif |
| 11309 | else { /* c == '<' */ |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11310 | /*np->nfile.fd = 0; - stzalloc did it */ |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11311 | c = pgetc(); |
| 11312 | switch (c) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11313 | case '<': |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11314 | if (sizeof(struct nfile) != sizeof(struct nhere)) { |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11315 | np = stzalloc(sizeof(struct nhere)); |
| 11316 | /*np->nfile.fd = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11317 | } |
| 11318 | np->type = NHERE; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11319 | heredoc = stzalloc(sizeof(struct heredoc)); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11320 | heredoc->here = np; |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11321 | c = pgetc(); |
| 11322 | if (c == '-') { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11323 | heredoc->striptabs = 1; |
| 11324 | } else { |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11325 | /*heredoc->striptabs = 0; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11326 | pungetc(); |
| 11327 | } |
| 11328 | break; |
| 11329 | |
| 11330 | case '&': |
| 11331 | np->type = NFROMFD; |
| 11332 | break; |
| 11333 | |
| 11334 | case '>': |
| 11335 | np->type = NFROMTO; |
| 11336 | break; |
| 11337 | |
| 11338 | default: |
| 11339 | np->type = NFROM; |
| 11340 | pungetc(); |
| 11341 | break; |
| 11342 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11343 | } |
Denis Vlasenko | 6fbb43b | 2008-07-24 19:44:41 +0000 | [diff] [blame] | 11344 | if (fd >= 0) |
| 11345 | np->nfile.fd = fd; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11346 | redirnode = np; |
| 11347 | goto parseredir_return; |
| 11348 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11349 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11350 | /* |
| 11351 | * Parse a substitution. At this point, we have read the dollar sign |
| 11352 | * and nothing else. |
| 11353 | */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 11354 | |
| 11355 | /* is_special(c) evaluates to 1 for c in "!#$*-0123456789?@"; 0 otherwise |
| 11356 | * (assuming ascii char codes, as the original implementation did) */ |
| 11357 | #define is_special(c) \ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11358 | (((unsigned)(c) - 33 < 32) \ |
| 11359 | && ((0xc1ff920dU >> ((unsigned)(c) - 33)) & 1)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11360 | parsesub: { |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11361 | unsigned char subtype; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11362 | int typeloc; |
| 11363 | int flags; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11364 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11365 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11366 | if (c > 255 /* PEOA or PEOF */ |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11367 | || (c != '(' && c != '{' && !is_name(c) && !is_special(c)) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11368 | ) { |
Denis Vlasenko | ef527f5 | 2008-06-23 01:52:30 +0000 | [diff] [blame] | 11369 | #if ENABLE_ASH_BASH_COMPAT |
| 11370 | if (c == '\'') |
| 11371 | bash_dollar_squote = 1; |
| 11372 | else |
| 11373 | #endif |
| 11374 | USTPUTC('$', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11375 | pungetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11376 | } else if (c == '(') { |
| 11377 | /* $(command) or $((arith)) */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11378 | if (pgetc() == '(') { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11379 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11380 | PARSEARITH(); |
| 11381 | #else |
Mike Frysinger | 98a6f56 | 2008-06-09 09:38:45 +0000 | [diff] [blame] | 11382 | raise_error_syntax("you disabled math support for $((arith)) syntax"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11383 | #endif |
Glenn L McGrath | 9fef17d | 2002-08-22 18:41:20 +0000 | [diff] [blame] | 11384 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11385 | pungetc(); |
| 11386 | PARSEBACKQNEW(); |
| 11387 | } |
| 11388 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11389 | /* $VAR, $<specialchar>, ${...}, or PEOA/PEOF */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11390 | USTPUTC(CTLVAR, out); |
| 11391 | typeloc = out - (char *)stackblock(); |
| 11392 | USTPUTC(VSNORMAL, out); |
| 11393 | subtype = VSNORMAL; |
| 11394 | if (c == '{') { |
| 11395 | c = pgetc(); |
| 11396 | if (c == '#') { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11397 | c = pgetc(); |
| 11398 | if (c == '}') |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11399 | c = '#'; /* ${#} - same as $# */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11400 | else |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11401 | subtype = VSLENGTH; /* ${#VAR} */ |
| 11402 | } else { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11403 | subtype = 0; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11404 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11405 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11406 | if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11407 | /* $[{[#]]NAME[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11408 | do { |
| 11409 | STPUTC(c, out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11410 | c = pgetc(); |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11411 | } while (c <= 255 /* not PEOA or PEOF */ && is_in_name(c)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11412 | } else if (isdigit(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11413 | /* $[{[#]]NUM[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11414 | do { |
| 11415 | STPUTC(c, out); |
| 11416 | c = pgetc(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11417 | } while (isdigit(c)); |
Denis Vlasenko | a0f82e9 | 2007-02-18 12:35:30 +0000 | [diff] [blame] | 11418 | } else if (is_special(c)) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11419 | /* $[{[#]]<specialchar>[}] */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11420 | USTPUTC(c, out); |
| 11421 | c = pgetc(); |
Denis Vlasenko | 559691a | 2008-10-05 18:39:31 +0000 | [diff] [blame] | 11422 | } else { |
| 11423 | badsub: |
| 11424 | raise_error_syntax("bad substitution"); |
| 11425 | } |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11426 | if (c != '}' && subtype == VSLENGTH) { |
| 11427 | /* ${#VAR didn't end with } */ |
Cristian Ionescu-Idbohrn | 301f5ec | 2009-10-05 02:07:23 +0200 | [diff] [blame] | 11428 | goto badsub; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11429 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11430 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11431 | STPUTC('=', out); |
| 11432 | flags = 0; |
| 11433 | if (subtype == 0) { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11434 | /* ${VAR...} but not $VAR or ${#VAR} */ |
| 11435 | /* c == first char after VAR */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11436 | switch (c) { |
| 11437 | case ':': |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11438 | c = pgetc(); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11439 | #if ENABLE_ASH_BASH_COMPAT |
| 11440 | if (c == ':' || c == '$' || isdigit(c)) { |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 11441 | //TODO: support more general format ${v:EXPR:EXPR}, |
| 11442 | // where EXPR follows $(()) rules |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11443 | subtype = VSSUBSTR; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11444 | pungetc(); |
| 11445 | break; /* "goto do_pungetc" is bigger (!) */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11446 | } |
| 11447 | #endif |
| 11448 | flags = VSNUL; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11449 | /*FALLTHROUGH*/ |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11450 | default: { |
| 11451 | static const char types[] ALIGN1 = "}-+?="; |
| 11452 | const char *p = strchr(types, c); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11453 | if (p == NULL) |
| 11454 | goto badsub; |
| 11455 | subtype = p - types + VSNORMAL; |
| 11456 | break; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11457 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11458 | case '%': |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11459 | case '#': { |
| 11460 | int cc = c; |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11461 | subtype = (c == '#' ? VSTRIMLEFT : VSTRIMRIGHT); |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11462 | c = pgetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11463 | if (c != cc) |
| 11464 | goto do_pungetc; |
| 11465 | subtype++; |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11466 | break; |
| 11467 | } |
| 11468 | #if ENABLE_ASH_BASH_COMPAT |
| 11469 | case '/': |
Denys Vlasenko | 6040fe8 | 2010-09-12 15:03:16 +0200 | [diff] [blame] | 11470 | /* ${v/[/]pattern/repl} */ |
| 11471 | //TODO: encode pattern and repl separately. |
| 11472 | // Currently ${v/$var_with_slash/repl} is horribly broken |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11473 | subtype = VSREPLACE; |
| 11474 | c = pgetc(); |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11475 | if (c != '/') |
| 11476 | goto do_pungetc; |
| 11477 | subtype++; /* VSREPLACEALL */ |
Denis Vlasenko | 92e13c2 | 2008-03-25 01:17:40 +0000 | [diff] [blame] | 11478 | break; |
| 11479 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11480 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11481 | } else { |
Denys Vlasenko | b0fbe4b | 2010-08-05 17:19:27 +0200 | [diff] [blame] | 11482 | do_pungetc: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11483 | pungetc(); |
| 11484 | } |
| 11485 | if (dblquote || arinest) |
| 11486 | flags |= VSQUOTE; |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11487 | ((unsigned char *)stackblock())[typeloc] = subtype | flags; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11488 | if (subtype != VSNORMAL) { |
| 11489 | varnest++; |
| 11490 | if (dblquote || arinest) { |
| 11491 | dqvarnest++; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11492 | } |
| 11493 | } |
| 11494 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11495 | goto parsesub_return; |
| 11496 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11497 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11498 | /* |
| 11499 | * Called to parse command substitutions. Newstyle is set if the command |
| 11500 | * is enclosed inside $(...); nlpp is a pointer to the head of the linked |
| 11501 | * list of commands (passed by reference), and savelen is the number of |
| 11502 | * characters on the top of the stack which must be preserved. |
| 11503 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11504 | parsebackq: { |
| 11505 | struct nodelist **nlpp; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11506 | smallint savepbq; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11507 | union node *n; |
| 11508 | char *volatile str; |
| 11509 | struct jmploc jmploc; |
| 11510 | struct jmploc *volatile savehandler; |
| 11511 | size_t savelen; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11512 | smallint saveprompt = 0; |
| 11513 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11514 | #ifdef __GNUC__ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11515 | (void) &saveprompt; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11516 | #endif |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11517 | savepbq = parsebackquote; |
| 11518 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 11519 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11520 | parsebackquote = 0; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11521 | exception_handler = savehandler; |
| 11522 | longjmp(exception_handler->loc, 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11523 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11524 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11525 | str = NULL; |
| 11526 | savelen = out - (char *)stackblock(); |
| 11527 | if (savelen > 0) { |
| 11528 | str = ckmalloc(savelen); |
| 11529 | memcpy(str, stackblock(), savelen); |
| 11530 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11531 | savehandler = exception_handler; |
| 11532 | exception_handler = &jmploc; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11533 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11534 | if (oldstyle) { |
| 11535 | /* We must read until the closing backquote, giving special |
| 11536 | treatment to some slashes, and then push the string and |
| 11537 | reread it as input, interpreting it normally. */ |
| 11538 | char *pout; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11539 | size_t psavelen; |
| 11540 | char *pstr; |
| 11541 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11542 | STARTSTACKSTR(pout); |
| 11543 | for (;;) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11544 | int pc; |
| 11545 | |
| 11546 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11547 | pc = pgetc(); |
| 11548 | switch (pc) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11549 | case '`': |
| 11550 | goto done; |
| 11551 | |
| 11552 | case '\\': |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11553 | pc = pgetc(); |
| 11554 | if (pc == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11555 | g_parsefile->linno++; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11556 | setprompt_if(doprompt, 2); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11557 | /* |
| 11558 | * If eating a newline, avoid putting |
| 11559 | * the newline into the new character |
| 11560 | * stream (via the STPUTC after the |
| 11561 | * switch). |
| 11562 | */ |
| 11563 | continue; |
| 11564 | } |
| 11565 | if (pc != '\\' && pc != '`' && pc != '$' |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11566 | && (!dblquote || pc != '"') |
| 11567 | ) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11568 | STPUTC('\\', pout); |
Denys Vlasenko | 76bc2d6 | 2009-11-29 01:37:46 +0100 | [diff] [blame] | 11569 | } |
Denys Vlasenko | cd71683 | 2009-11-28 22:14:02 +0100 | [diff] [blame] | 11570 | if (pc <= 255 /* not PEOA or PEOF */) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11571 | break; |
| 11572 | } |
| 11573 | /* fall through */ |
| 11574 | |
| 11575 | case PEOF: |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11576 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11577 | startlinno = g_parsefile->linno; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11578 | raise_error_syntax("EOF in backquote substitution"); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11579 | |
| 11580 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11581 | g_parsefile->linno++; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11582 | needprompt = doprompt; |
| 11583 | break; |
| 11584 | |
| 11585 | default: |
| 11586 | break; |
| 11587 | } |
| 11588 | STPUTC(pc, pout); |
| 11589 | } |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 11590 | done: |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11591 | STPUTC('\0', pout); |
| 11592 | psavelen = pout - (char *)stackblock(); |
| 11593 | if (psavelen > 0) { |
| 11594 | pstr = grabstackstr(pout); |
| 11595 | setinputstring(pstr); |
| 11596 | } |
| 11597 | } |
| 11598 | nlpp = &bqlist; |
| 11599 | while (*nlpp) |
| 11600 | nlpp = &(*nlpp)->next; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11601 | *nlpp = stzalloc(sizeof(**nlpp)); |
| 11602 | /* (*nlpp)->next = NULL; - stzalloc did it */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11603 | parsebackquote = oldstyle; |
| 11604 | |
| 11605 | if (oldstyle) { |
| 11606 | saveprompt = doprompt; |
| 11607 | doprompt = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11608 | } |
| 11609 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11610 | n = list(2); |
| 11611 | |
| 11612 | if (oldstyle) |
| 11613 | doprompt = saveprompt; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11614 | else if (readtoken() != TRP) |
| 11615 | raise_error_unexpected_syntax(TRP); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11616 | |
| 11617 | (*nlpp)->n = n; |
| 11618 | if (oldstyle) { |
| 11619 | /* |
| 11620 | * Start reading from old file again, ignoring any pushed back |
| 11621 | * tokens left from the backquote parsing |
| 11622 | */ |
| 11623 | popfile(); |
| 11624 | tokpushback = 0; |
| 11625 | } |
| 11626 | while (stackblocksize() <= savelen) |
| 11627 | growstackblock(); |
| 11628 | STARTSTACKSTR(out); |
| 11629 | if (str) { |
| 11630 | memcpy(out, str, savelen); |
| 11631 | STADJUST(savelen, out); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11632 | INT_OFF; |
| 11633 | free(str); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11634 | str = NULL; |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 11635 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11636 | } |
| 11637 | parsebackquote = savepbq; |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 11638 | exception_handler = savehandler; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11639 | if (arinest || dblquote) |
| 11640 | USTPUTC(CTLBACKQ | CTLQUOTE, out); |
| 11641 | else |
| 11642 | USTPUTC(CTLBACKQ, out); |
| 11643 | if (oldstyle) |
| 11644 | goto parsebackq_oldreturn; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11645 | goto parsebackq_newreturn; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11646 | } |
| 11647 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 11648 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11649 | /* |
| 11650 | * Parse an arithmetic expansion (indicate start of one and set state) |
| 11651 | */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11652 | parsearith: { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11653 | if (++arinest == 1) { |
| 11654 | prevsyntax = syntax; |
| 11655 | syntax = ARISYNTAX; |
| 11656 | USTPUTC(CTLARI, out); |
| 11657 | if (dblquote) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11658 | USTPUTC('"', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11659 | else |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 11660 | USTPUTC(' ', out); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11661 | } else { |
| 11662 | /* |
| 11663 | * we collapse embedded arithmetic expansion to |
| 11664 | * parenthesis, which should be equivalent |
| 11665 | */ |
| 11666 | USTPUTC('(', out); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11667 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11668 | goto parsearith_return; |
| 11669 | } |
| 11670 | #endif |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11671 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11672 | } /* end of readtoken */ |
| 11673 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11674 | /* |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11675 | * Read the next input token. |
| 11676 | * If the token is a word, we set backquotelist to the list of cmds in |
| 11677 | * backquotes. We set quoteflag to true if any part of the word was |
| 11678 | * quoted. |
| 11679 | * If the token is TREDIR, then we set redirnode to a structure containing |
| 11680 | * the redirection. |
| 11681 | * In all cases, the variable startlinno is set to the number of the line |
| 11682 | * on which the token starts. |
| 11683 | * |
| 11684 | * [Change comment: here documents and internal procedures] |
| 11685 | * [Readtoken shouldn't have any arguments. Perhaps we should make the |
| 11686 | * word parsing code into a separate routine. In this case, readtoken |
| 11687 | * doesn't need to have any internal procedures, but parseword does. |
| 11688 | * We could also make parseoperator in essence the main routine, and |
| 11689 | * have parseword (readtoken1?) handle both words and redirection.] |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11690 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11691 | #define NEW_xxreadtoken |
| 11692 | #ifdef NEW_xxreadtoken |
| 11693 | /* singles must be first! */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11694 | static const char xxreadtoken_chars[7] ALIGN1 = { |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11695 | '\n', '(', ')', /* singles */ |
| 11696 | '&', '|', ';', /* doubles */ |
| 11697 | 0 |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11698 | }; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11699 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11700 | #define xxreadtoken_singles 3 |
| 11701 | #define xxreadtoken_doubles 3 |
| 11702 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11703 | static const char xxreadtoken_tokens[] ALIGN1 = { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11704 | TNL, TLP, TRP, /* only single occurrence allowed */ |
| 11705 | TBACKGND, TPIPE, TSEMI, /* if single occurrence */ |
| 11706 | TEOF, /* corresponds to trailing nul */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 11707 | TAND, TOR, TENDCASE /* if double occurrence */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11708 | }; |
| 11709 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11710 | static int |
| 11711 | xxreadtoken(void) |
| 11712 | { |
| 11713 | int c; |
| 11714 | |
| 11715 | if (tokpushback) { |
| 11716 | tokpushback = 0; |
| 11717 | return lasttoken; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11718 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11719 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11720 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11721 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11722 | c = pgetc_fast(); |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 11723 | if (c == ' ' || c == '\t' IF_ASH_ALIAS( || c == PEOA)) |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11724 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11725 | |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11726 | if (c == '#') { |
| 11727 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11728 | continue; |
| 11729 | pungetc(); |
| 11730 | } else if (c == '\\') { |
| 11731 | if (pgetc() != '\n') { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11732 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11733 | break; /* return readtoken1(...) */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11734 | } |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11735 | startlinno = ++g_parsefile->linno; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11736 | setprompt_if(doprompt, 2); |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11737 | } else { |
| 11738 | const char *p; |
| 11739 | |
| 11740 | p = xxreadtoken_chars + sizeof(xxreadtoken_chars) - 1; |
| 11741 | if (c != PEOF) { |
| 11742 | if (c == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11743 | g_parsefile->linno++; |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11744 | needprompt = doprompt; |
| 11745 | } |
| 11746 | |
| 11747 | p = strchr(xxreadtoken_chars, c); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11748 | if (p == NULL) |
| 11749 | break; /* return readtoken1(...) */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11750 | |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11751 | if ((int)(p - xxreadtoken_chars) >= xxreadtoken_singles) { |
| 11752 | int cc = pgetc(); |
| 11753 | if (cc == c) { /* double occurrence? */ |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11754 | p += xxreadtoken_doubles + 1; |
| 11755 | } else { |
| 11756 | pungetc(); |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11757 | #if ENABLE_ASH_BASH_COMPAT |
| 11758 | if (c == '&' && cc == '>') /* &> */ |
| 11759 | break; /* return readtoken1(...) */ |
| 11760 | #endif |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11761 | } |
| 11762 | } |
| 11763 | } |
| 11764 | lasttoken = xxreadtoken_tokens[p - xxreadtoken_chars]; |
| 11765 | return lasttoken; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11766 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11767 | } /* for (;;) */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11768 | |
| 11769 | return readtoken1(c, BASESYNTAX, (char *) NULL, 0); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11770 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11771 | #else /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11772 | #define RETURN(token) return lasttoken = token |
| 11773 | static int |
| 11774 | xxreadtoken(void) |
| 11775 | { |
| 11776 | int c; |
| 11777 | |
| 11778 | if (tokpushback) { |
| 11779 | tokpushback = 0; |
| 11780 | return lasttoken; |
| 11781 | } |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11782 | setprompt_if(needprompt, 2); |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11783 | startlinno = g_parsefile->linno; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11784 | for (;;) { /* until token or start of word found */ |
Denis Vlasenko | 834dee7 | 2008-10-07 09:18:30 +0000 | [diff] [blame] | 11785 | c = pgetc_fast(); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11786 | switch (c) { |
| 11787 | case ' ': case '\t': |
Denys Vlasenko | 2ce42e9 | 2009-11-29 02:18:13 +0100 | [diff] [blame] | 11788 | IF_ASH_ALIAS(case PEOA:) |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11789 | continue; |
| 11790 | case '#': |
Denis Vlasenko | f7d5665 | 2008-03-25 05:51:41 +0000 | [diff] [blame] | 11791 | while ((c = pgetc()) != '\n' && c != PEOF) |
| 11792 | continue; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11793 | pungetc(); |
| 11794 | continue; |
| 11795 | case '\\': |
| 11796 | if (pgetc() == '\n') { |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11797 | startlinno = ++g_parsefile->linno; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11798 | setprompt_if(doprompt, 2); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11799 | continue; |
| 11800 | } |
| 11801 | pungetc(); |
| 11802 | goto breakloop; |
| 11803 | case '\n': |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 11804 | g_parsefile->linno++; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11805 | needprompt = doprompt; |
| 11806 | RETURN(TNL); |
| 11807 | case PEOF: |
| 11808 | RETURN(TEOF); |
| 11809 | case '&': |
| 11810 | if (pgetc() == '&') |
| 11811 | RETURN(TAND); |
| 11812 | pungetc(); |
| 11813 | RETURN(TBACKGND); |
| 11814 | case '|': |
| 11815 | if (pgetc() == '|') |
| 11816 | RETURN(TOR); |
| 11817 | pungetc(); |
| 11818 | RETURN(TPIPE); |
| 11819 | case ';': |
| 11820 | if (pgetc() == ';') |
| 11821 | RETURN(TENDCASE); |
| 11822 | pungetc(); |
| 11823 | RETURN(TSEMI); |
| 11824 | case '(': |
| 11825 | RETURN(TLP); |
| 11826 | case ')': |
| 11827 | RETURN(TRP); |
| 11828 | default: |
| 11829 | goto breakloop; |
| 11830 | } |
| 11831 | } |
| 11832 | breakloop: |
| 11833 | return readtoken1(c, BASESYNTAX, (char *)NULL, 0); |
| 11834 | #undef RETURN |
| 11835 | } |
Denis Vlasenko | 176d49d | 2008-10-06 09:51:47 +0000 | [diff] [blame] | 11836 | #endif /* old xxreadtoken */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11837 | |
| 11838 | static int |
| 11839 | readtoken(void) |
| 11840 | { |
| 11841 | int t; |
| 11842 | #if DEBUG |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11843 | smallint alreadyseen = tokpushback; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11844 | #endif |
| 11845 | |
| 11846 | #if ENABLE_ASH_ALIAS |
| 11847 | top: |
| 11848 | #endif |
| 11849 | |
| 11850 | t = xxreadtoken(); |
| 11851 | |
| 11852 | /* |
| 11853 | * eat newlines |
| 11854 | */ |
| 11855 | if (checkkwd & CHKNL) { |
| 11856 | while (t == TNL) { |
| 11857 | parseheredoc(); |
| 11858 | t = xxreadtoken(); |
| 11859 | } |
| 11860 | } |
| 11861 | |
| 11862 | if (t != TWORD || quoteflag) { |
| 11863 | goto out; |
| 11864 | } |
| 11865 | |
| 11866 | /* |
| 11867 | * check for keywords |
| 11868 | */ |
| 11869 | if (checkkwd & CHKKWD) { |
| 11870 | const char *const *pp; |
| 11871 | |
| 11872 | pp = findkwd(wordtext); |
| 11873 | if (pp) { |
| 11874 | lasttoken = t = pp - tokname_array; |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11875 | TRACE(("keyword '%s' recognized\n", tokname_array[t] + 1)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11876 | goto out; |
| 11877 | } |
| 11878 | } |
| 11879 | |
| 11880 | if (checkkwd & CHKALIAS) { |
| 11881 | #if ENABLE_ASH_ALIAS |
| 11882 | struct alias *ap; |
| 11883 | ap = lookupalias(wordtext, 1); |
| 11884 | if (ap != NULL) { |
| 11885 | if (*ap->val) { |
| 11886 | pushstring(ap->val, ap); |
| 11887 | } |
| 11888 | goto top; |
| 11889 | } |
| 11890 | #endif |
| 11891 | } |
| 11892 | out: |
| 11893 | checkkwd = 0; |
| 11894 | #if DEBUG |
| 11895 | if (!alreadyseen) |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11896 | TRACE(("token '%s' %s\n", tokname_array[t] + 1, t == TWORD ? wordtext : "")); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11897 | else |
Denys Vlasenko | a0ec4f5 | 2010-05-20 12:50:42 +0200 | [diff] [blame] | 11898 | 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] | 11899 | #endif |
| 11900 | return t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11901 | } |
| 11902 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11903 | static char |
| 11904 | peektoken(void) |
| 11905 | { |
| 11906 | int t; |
| 11907 | |
| 11908 | t = readtoken(); |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11909 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11910 | return tokname_array[t][0]; |
| 11911 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11912 | |
| 11913 | /* |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11914 | * Read and parse a command. Returns NODE_EOF on end of file. |
| 11915 | * (NULL is a valid parse tree indicating a blank line.) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11916 | */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11917 | static union node * |
| 11918 | parsecmd(int interact) |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 11919 | { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11920 | int t; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11921 | |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11922 | tokpushback = 0; |
| 11923 | doprompt = interact; |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11924 | setprompt_if(doprompt, doprompt); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11925 | needprompt = 0; |
| 11926 | t = readtoken(); |
| 11927 | if (t == TEOF) |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 11928 | return NODE_EOF; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11929 | if (t == TNL) |
| 11930 | return NULL; |
Denis Vlasenko | bcceb0c | 2007-09-21 18:06:20 +0000 | [diff] [blame] | 11931 | tokpushback = 1; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11932 | return list(1); |
| 11933 | } |
| 11934 | |
| 11935 | /* |
| 11936 | * Input any here documents. |
| 11937 | */ |
| 11938 | static void |
| 11939 | parseheredoc(void) |
| 11940 | { |
| 11941 | struct heredoc *here; |
| 11942 | union node *n; |
| 11943 | |
| 11944 | here = heredoclist; |
Denis Vlasenko | 838ffd5 | 2008-02-21 04:32:08 +0000 | [diff] [blame] | 11945 | heredoclist = NULL; |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11946 | |
| 11947 | while (here) { |
Denys Vlasenko | 958581a | 2010-09-12 15:04:27 +0200 | [diff] [blame] | 11948 | setprompt_if(needprompt, 2); |
| 11949 | readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX, |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11950 | here->eofmark, here->striptabs); |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11951 | n = stzalloc(sizeof(struct narg)); |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11952 | n->narg.type = NARG; |
Denis Vlasenko | 597906c | 2008-02-20 16:38:54 +0000 | [diff] [blame] | 11953 | /*n->narg.next = NULL; - stzalloc did it */ |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 11954 | n->narg.text = wordtext; |
| 11955 | n->narg.backquote = backquotelist; |
| 11956 | here->here->nhere.doc = n; |
| 11957 | here = here->next; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11958 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11959 | } |
| 11960 | |
| 11961 | |
| 11962 | /* |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 11963 | * called by editline -- any expansions to the prompt should be added here. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 11964 | */ |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 11965 | #if ENABLE_ASH_EXPAND_PRMT |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11966 | static const char * |
| 11967 | expandstr(const char *ps) |
| 11968 | { |
| 11969 | union node n; |
| 11970 | |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 11971 | /* XXX Fix (char *) cast. It _is_ a bug. ps is variable's value, |
| 11972 | * and token processing _can_ alter it (delete NULs etc). */ |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11973 | setinputstring((char *)ps); |
Denis Vlasenko | 46a5306 | 2007-09-24 18:30:02 +0000 | [diff] [blame] | 11974 | readtoken1(pgetc(), PSSYNTAX, nullstr, 0); |
"Vladimir N. Oleynik" | bef14d7 | 2005-09-05 13:25:11 +0000 | [diff] [blame] | 11975 | popfile(); |
| 11976 | |
| 11977 | n.narg.type = NARG; |
| 11978 | n.narg.next = NULL; |
| 11979 | n.narg.text = wordtext; |
| 11980 | n.narg.backquote = backquotelist; |
| 11981 | |
| 11982 | expandarg(&n, NULL, 0); |
| 11983 | return stackblock(); |
| 11984 | } |
| 11985 | #endif |
| 11986 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11987 | /* |
| 11988 | * Execute a command or commands contained in a string. |
| 11989 | */ |
| 11990 | static int |
| 11991 | evalstring(char *s, int mask) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 11992 | { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 11993 | union node *n; |
| 11994 | struct stackmark smark; |
| 11995 | int skip; |
| 11996 | |
| 11997 | setinputstring(s); |
| 11998 | setstackmark(&smark); |
| 11999 | |
| 12000 | skip = 0; |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12001 | while ((n = parsecmd(0)) != NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12002 | evaltree(n, 0); |
| 12003 | popstackmark(&smark); |
| 12004 | skip = evalskip; |
| 12005 | if (skip) |
| 12006 | break; |
| 12007 | } |
| 12008 | popfile(); |
| 12009 | |
| 12010 | skip &= mask; |
| 12011 | evalskip = skip; |
| 12012 | return skip; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12013 | } |
| 12014 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12015 | /* |
| 12016 | * The eval command. |
| 12017 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12018 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12019 | evalcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12020 | { |
| 12021 | char *p; |
| 12022 | char *concat; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12023 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12024 | if (argv[1]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12025 | p = argv[1]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12026 | argv += 2; |
| 12027 | if (argv[0]) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12028 | STARTSTACKSTR(concat); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12029 | for (;;) { |
| 12030 | concat = stack_putstr(p, concat); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12031 | p = *argv++; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12032 | if (p == NULL) |
| 12033 | break; |
| 12034 | STPUTC(' ', concat); |
| 12035 | } |
| 12036 | STPUTC('\0', concat); |
| 12037 | p = grabstackstr(concat); |
| 12038 | } |
| 12039 | evalstring(p, ~SKIPEVAL); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12040 | } |
| 12041 | return exitstatus; |
| 12042 | } |
| 12043 | |
| 12044 | /* |
Denys Vlasenko | 285ad15 | 2009-12-04 23:02:27 +0100 | [diff] [blame] | 12045 | * Read and execute commands. |
| 12046 | * "Top" is nonzero for the top level command loop; |
| 12047 | * it turns on prompting if the shell is interactive. |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12048 | */ |
| 12049 | static int |
| 12050 | cmdloop(int top) |
| 12051 | { |
| 12052 | union node *n; |
| 12053 | struct stackmark smark; |
| 12054 | int inter; |
| 12055 | int numeof = 0; |
| 12056 | |
| 12057 | TRACE(("cmdloop(%d) called\n", top)); |
| 12058 | for (;;) { |
| 12059 | int skip; |
| 12060 | |
| 12061 | setstackmark(&smark); |
| 12062 | #if JOBS |
Denis Vlasenko | b07a496 | 2008-06-22 13:16:23 +0000 | [diff] [blame] | 12063 | if (doing_jobctl) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12064 | showjobs(stderr, SHOW_CHANGED); |
| 12065 | #endif |
| 12066 | inter = 0; |
| 12067 | if (iflag && top) { |
| 12068 | inter++; |
| 12069 | #if ENABLE_ASH_MAIL |
| 12070 | chkmail(); |
| 12071 | #endif |
| 12072 | } |
| 12073 | n = parsecmd(inter); |
Denys Vlasenko | 7cee00e | 2009-07-24 01:08:03 +0200 | [diff] [blame] | 12074 | #if DEBUG |
| 12075 | if (DEBUG > 2 && debug && (n != NODE_EOF)) |
Denys Vlasenko | 883cea4 | 2009-07-11 15:31:59 +0200 | [diff] [blame] | 12076 | showtree(n); |
Denis Vlasenko | 135cecb | 2009-04-12 00:00:57 +0000 | [diff] [blame] | 12077 | #endif |
Denys Vlasenko | 86e83ec | 2009-07-23 22:07:07 +0200 | [diff] [blame] | 12078 | if (n == NODE_EOF) { |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12079 | if (!top || numeof >= 50) |
| 12080 | break; |
| 12081 | if (!stoppedjobs()) { |
| 12082 | if (!Iflag) |
| 12083 | break; |
| 12084 | out2str("\nUse \"exit\" to leave shell.\n"); |
| 12085 | } |
| 12086 | numeof++; |
| 12087 | } else if (nflag == 0) { |
Denis Vlasenko | fcfaf2e | 2007-07-14 18:45:37 +0000 | [diff] [blame] | 12088 | /* job_warning can only be 2,1,0. Here 2->1, 1/0->0 */ |
| 12089 | job_warning >>= 1; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12090 | numeof = 0; |
| 12091 | evaltree(n, 0); |
| 12092 | } |
| 12093 | popstackmark(&smark); |
| 12094 | skip = evalskip; |
| 12095 | |
| 12096 | if (skip) { |
| 12097 | evalskip = 0; |
| 12098 | return skip & SKIPEVAL; |
| 12099 | } |
| 12100 | } |
| 12101 | return 0; |
| 12102 | } |
| 12103 | |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12104 | /* |
| 12105 | * Take commands from a file. To be compatible we should do a path |
| 12106 | * search for the file, which is necessary to find sub-commands. |
| 12107 | */ |
| 12108 | static char * |
| 12109 | find_dot_file(char *name) |
| 12110 | { |
| 12111 | char *fullname; |
| 12112 | const char *path = pathval(); |
| 12113 | struct stat statb; |
| 12114 | |
| 12115 | /* don't try this for absolute or relative paths */ |
| 12116 | if (strchr(name, '/')) |
| 12117 | return name; |
| 12118 | |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12119 | /* IIRC standards do not say whether . is to be searched. |
| 12120 | * And it is even smaller this way, making it unconditional for now: |
| 12121 | */ |
| 12122 | if (1) { /* ENABLE_ASH_BASH_COMPAT */ |
| 12123 | fullname = name; |
| 12124 | goto try_cur_dir; |
| 12125 | } |
| 12126 | |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12127 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | 8ad78e1 | 2009-02-15 12:40:30 +0000 | [diff] [blame] | 12128 | try_cur_dir: |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12129 | if ((stat(fullname, &statb) == 0) && S_ISREG(statb.st_mode)) { |
| 12130 | /* |
| 12131 | * Don't bother freeing here, since it will |
| 12132 | * be freed by the caller. |
| 12133 | */ |
| 12134 | return fullname; |
| 12135 | } |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12136 | if (fullname != name) |
| 12137 | stunalloc(fullname); |
Denis Vlasenko | 0dec6de | 2007-02-23 21:10:47 +0000 | [diff] [blame] | 12138 | } |
| 12139 | |
| 12140 | /* not found in the PATH */ |
| 12141 | ash_msg_and_raise_error("%s: not found", name); |
| 12142 | /* NOTREACHED */ |
| 12143 | } |
| 12144 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12145 | static int FAST_FUNC |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12146 | dotcmd(int argc, char **argv) |
| 12147 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12148 | char *fullname; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12149 | struct strlist *sp; |
| 12150 | volatile struct shparam saveparam; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12151 | |
| 12152 | for (sp = cmdenviron; sp; sp = sp->next) |
Denis Vlasenko | 4222ae4 | 2007-02-25 02:37:49 +0000 | [diff] [blame] | 12153 | setvareq(ckstrdup(sp->text), VSTRFIXED | VTEXTFIXED); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12154 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12155 | if (!argv[1]) { |
| 12156 | /* bash says: "bash: .: filename argument required" */ |
| 12157 | return 2; /* bash compat */ |
| 12158 | } |
| 12159 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12160 | /* "false; . empty_file; echo $?" should print 0, not 1: */ |
| 12161 | exitstatus = 0; |
| 12162 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12163 | fullname = find_dot_file(argv[1]); |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12164 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12165 | argv += 2; |
| 12166 | argc -= 2; |
| 12167 | if (argc) { /* argc > 0, argv[0] != NULL */ |
| 12168 | saveparam = shellparam; |
| 12169 | shellparam.malloced = 0; |
| 12170 | shellparam.nparam = argc; |
| 12171 | shellparam.p = argv; |
| 12172 | }; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12173 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12174 | setinputfile(fullname, INPUT_PUSH_FILE); |
| 12175 | commandname = fullname; |
| 12176 | cmdloop(0); |
| 12177 | popfile(); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12178 | |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 12179 | if (argc) { |
| 12180 | freeparam(&shellparam); |
| 12181 | shellparam = saveparam; |
| 12182 | }; |
| 12183 | |
Denys Vlasenko | cd10dc4 | 2010-05-17 17:10:46 +0200 | [diff] [blame] | 12184 | return exitstatus; |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12185 | } |
| 12186 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12187 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12188 | exitcmd(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12189 | { |
| 12190 | if (stoppedjobs()) |
| 12191 | return 0; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12192 | if (argv[1]) |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12193 | exitstatus = number(argv[1]); |
| 12194 | raise_exception(EXEXIT); |
| 12195 | /* NOTREACHED */ |
| 12196 | } |
| 12197 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12198 | /* |
| 12199 | * Read a file containing shell functions. |
| 12200 | */ |
| 12201 | static void |
| 12202 | readcmdfile(char *name) |
| 12203 | { |
| 12204 | setinputfile(name, INPUT_PUSH_FILE); |
| 12205 | cmdloop(0); |
| 12206 | popfile(); |
| 12207 | } |
| 12208 | |
| 12209 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12210 | /* ============ find_command inplementation */ |
| 12211 | |
| 12212 | /* |
| 12213 | * Resolve a command name. If you change this routine, you may have to |
| 12214 | * change the shellexec routine as well. |
| 12215 | */ |
| 12216 | static void |
| 12217 | find_command(char *name, struct cmdentry *entry, int act, const char *path) |
| 12218 | { |
| 12219 | struct tblentry *cmdp; |
| 12220 | int idx; |
| 12221 | int prev; |
| 12222 | char *fullname; |
| 12223 | struct stat statb; |
| 12224 | int e; |
| 12225 | int updatetbl; |
| 12226 | struct builtincmd *bcmd; |
| 12227 | |
| 12228 | /* If name contains a slash, don't use PATH or hash table */ |
| 12229 | if (strchr(name, '/') != NULL) { |
| 12230 | entry->u.index = -1; |
| 12231 | if (act & DO_ABS) { |
| 12232 | while (stat(name, &statb) < 0) { |
| 12233 | #ifdef SYSV |
| 12234 | if (errno == EINTR) |
| 12235 | continue; |
| 12236 | #endif |
| 12237 | entry->cmdtype = CMDUNKNOWN; |
| 12238 | return; |
| 12239 | } |
| 12240 | } |
| 12241 | entry->cmdtype = CMDNORMAL; |
| 12242 | return; |
| 12243 | } |
| 12244 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12245 | /* #if ENABLE_FEATURE_SH_STANDALONE... moved after builtin check */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12246 | |
| 12247 | updatetbl = (path == pathval()); |
| 12248 | if (!updatetbl) { |
| 12249 | act |= DO_ALTPATH; |
| 12250 | if (strstr(path, "%builtin") != NULL) |
| 12251 | act |= DO_ALTBLTIN; |
| 12252 | } |
| 12253 | |
| 12254 | /* If name is in the table, check answer will be ok */ |
| 12255 | cmdp = cmdlookup(name, 0); |
| 12256 | if (cmdp != NULL) { |
| 12257 | int bit; |
| 12258 | |
| 12259 | switch (cmdp->cmdtype) { |
| 12260 | default: |
| 12261 | #if DEBUG |
| 12262 | abort(); |
| 12263 | #endif |
| 12264 | case CMDNORMAL: |
| 12265 | bit = DO_ALTPATH; |
| 12266 | break; |
| 12267 | case CMDFUNCTION: |
| 12268 | bit = DO_NOFUNC; |
| 12269 | break; |
| 12270 | case CMDBUILTIN: |
| 12271 | bit = DO_ALTBLTIN; |
| 12272 | break; |
| 12273 | } |
| 12274 | if (act & bit) { |
| 12275 | updatetbl = 0; |
| 12276 | cmdp = NULL; |
| 12277 | } else if (cmdp->rehash == 0) |
| 12278 | /* if not invalidated by cd, we're done */ |
| 12279 | goto success; |
| 12280 | } |
| 12281 | |
| 12282 | /* If %builtin not in path, check for builtin next */ |
| 12283 | bcmd = find_builtin(name); |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12284 | if (bcmd) { |
| 12285 | if (IS_BUILTIN_REGULAR(bcmd)) |
| 12286 | goto builtin_success; |
| 12287 | if (act & DO_ALTPATH) { |
| 12288 | if (!(act & DO_ALTBLTIN)) |
| 12289 | goto builtin_success; |
| 12290 | } else if (builtinloc <= 0) { |
| 12291 | goto builtin_success; |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 12292 | } |
Denis Vlasenko | f98dc4d | 2007-02-23 21:11:02 +0000 | [diff] [blame] | 12293 | } |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12294 | |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12295 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 7465dbc | 2008-04-13 02:25:53 +0000 | [diff] [blame] | 12296 | { |
| 12297 | int applet_no = find_applet_by_name(name); |
| 12298 | if (applet_no >= 0) { |
| 12299 | entry->cmdtype = CMDNORMAL; |
| 12300 | entry->u.index = -2 - applet_no; |
| 12301 | return; |
| 12302 | } |
Denis Vlasenko | f20de5b | 2007-04-29 23:42:54 +0000 | [diff] [blame] | 12303 | } |
| 12304 | #endif |
| 12305 | |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12306 | /* We have to search path. */ |
| 12307 | prev = -1; /* where to start */ |
| 12308 | if (cmdp && cmdp->rehash) { /* doing a rehash */ |
| 12309 | if (cmdp->cmdtype == CMDBUILTIN) |
| 12310 | prev = builtinloc; |
| 12311 | else |
| 12312 | prev = cmdp->param.index; |
| 12313 | } |
| 12314 | |
| 12315 | e = ENOENT; |
| 12316 | idx = -1; |
| 12317 | loop: |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 12318 | while ((fullname = path_advance(&path, name)) != NULL) { |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12319 | stunalloc(fullname); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12320 | /* NB: code below will still use fullname |
| 12321 | * despite it being "unallocated" */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12322 | idx++; |
| 12323 | if (pathopt) { |
| 12324 | if (prefix(pathopt, "builtin")) { |
| 12325 | if (bcmd) |
| 12326 | goto builtin_success; |
| 12327 | continue; |
Denis Vlasenko | 4a9ca13 | 2008-04-12 20:07:08 +0000 | [diff] [blame] | 12328 | } |
| 12329 | if ((act & DO_NOFUNC) |
| 12330 | || !prefix(pathopt, "func") |
| 12331 | ) { /* ignore unimplemented options */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12332 | continue; |
| 12333 | } |
| 12334 | } |
| 12335 | /* if rehash, don't redo absolute path names */ |
| 12336 | if (fullname[0] == '/' && idx <= prev) { |
| 12337 | if (idx < prev) |
| 12338 | continue; |
| 12339 | TRACE(("searchexec \"%s\": no change\n", name)); |
| 12340 | goto success; |
| 12341 | } |
| 12342 | while (stat(fullname, &statb) < 0) { |
| 12343 | #ifdef SYSV |
| 12344 | if (errno == EINTR) |
| 12345 | continue; |
| 12346 | #endif |
| 12347 | if (errno != ENOENT && errno != ENOTDIR) |
| 12348 | e = errno; |
| 12349 | goto loop; |
| 12350 | } |
| 12351 | e = EACCES; /* if we fail, this will be the error */ |
| 12352 | if (!S_ISREG(statb.st_mode)) |
| 12353 | continue; |
| 12354 | if (pathopt) { /* this is a %func directory */ |
| 12355 | stalloc(strlen(fullname) + 1); |
Denis Vlasenko | dee82b6 | 2007-07-29 14:05:27 +0000 | [diff] [blame] | 12356 | /* NB: stalloc will return space pointed by fullname |
| 12357 | * (because we don't have any intervening allocations |
| 12358 | * between stunalloc above and this stalloc) */ |
Denis Vlasenko | cc57151 | 2007-02-23 21:10:35 +0000 | [diff] [blame] | 12359 | readcmdfile(fullname); |
| 12360 | cmdp = cmdlookup(name, 0); |
| 12361 | if (cmdp == NULL || cmdp->cmdtype != CMDFUNCTION) |
| 12362 | ash_msg_and_raise_error("%s not defined in %s", name, fullname); |
| 12363 | stunalloc(fullname); |
| 12364 | goto success; |
| 12365 | } |
| 12366 | TRACE(("searchexec \"%s\" returns \"%s\"\n", name, fullname)); |
| 12367 | if (!updatetbl) { |
| 12368 | entry->cmdtype = CMDNORMAL; |
| 12369 | entry->u.index = idx; |
| 12370 | return; |
| 12371 | } |
| 12372 | INT_OFF; |
| 12373 | cmdp = cmdlookup(name, 1); |
| 12374 | cmdp->cmdtype = CMDNORMAL; |
| 12375 | cmdp->param.index = idx; |
| 12376 | INT_ON; |
| 12377 | goto success; |
| 12378 | } |
| 12379 | |
| 12380 | /* We failed. If there was an entry for this command, delete it */ |
| 12381 | if (cmdp && updatetbl) |
| 12382 | delete_cmd_entry(); |
| 12383 | if (act & DO_ERR) |
| 12384 | ash_msg("%s: %s", name, errmsg(e, "not found")); |
| 12385 | entry->cmdtype = CMDUNKNOWN; |
| 12386 | return; |
| 12387 | |
| 12388 | builtin_success: |
| 12389 | if (!updatetbl) { |
| 12390 | entry->cmdtype = CMDBUILTIN; |
| 12391 | entry->u.cmd = bcmd; |
| 12392 | return; |
| 12393 | } |
| 12394 | INT_OFF; |
| 12395 | cmdp = cmdlookup(name, 1); |
| 12396 | cmdp->cmdtype = CMDBUILTIN; |
| 12397 | cmdp->param.cmd = bcmd; |
| 12398 | INT_ON; |
| 12399 | success: |
| 12400 | cmdp->rehash = 0; |
| 12401 | entry->cmdtype = cmdp->cmdtype; |
| 12402 | entry->u = cmdp->param; |
| 12403 | } |
| 12404 | |
| 12405 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12406 | /* ============ trap.c */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12407 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12408 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12409 | * The trap builtin. |
| 12410 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12411 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12412 | trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12413 | { |
| 12414 | char *action; |
| 12415 | char **ap; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12416 | int signo, exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12417 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12418 | nextopt(nullstr); |
| 12419 | ap = argptr; |
| 12420 | if (!*ap) { |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12421 | for (signo = 0; signo < NSIG; signo++) { |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12422 | char *tr = trap_ptr[signo]; |
| 12423 | if (tr) { |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12424 | /* note: bash adds "SIG", but only if invoked |
| 12425 | * as "bash". If called as "sh", or if set -o posix, |
| 12426 | * then it prints short signal names. |
| 12427 | * We are printing short names: */ |
| 12428 | out1fmt("trap -- %s %s\n", |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12429 | single_quote(tr), |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12430 | get_signame(signo)); |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12431 | /* trap_ptr != trap only if we are in special-cased `trap` code. |
| 12432 | * In this case, we will exit very soon, no need to free(). */ |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 12433 | /* if (trap_ptr != trap && tp[0]) */ |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12434 | /* free(tr); */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12435 | } |
| 12436 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12437 | /* |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12438 | if (trap_ptr != trap) { |
| 12439 | free(trap_ptr); |
| 12440 | trap_ptr = trap; |
| 12441 | } |
Denys Vlasenko | 726e1a0 | 2009-09-25 02:58:20 +0200 | [diff] [blame] | 12442 | */ |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12443 | return 0; |
| 12444 | } |
Denys Vlasenko | 21d87d4 | 2009-09-25 00:06:51 +0200 | [diff] [blame] | 12445 | |
Denis Vlasenko | 4e19a9c | 2008-07-26 13:45:57 +0000 | [diff] [blame] | 12446 | action = NULL; |
| 12447 | if (ap[1]) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12448 | action = *ap++; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12449 | exitcode = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12450 | while (*ap) { |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12451 | signo = get_signum(*ap); |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12452 | if (signo < 0) { |
| 12453 | /* Mimic bash message exactly */ |
| 12454 | ash_msg("%s: invalid signal specification", *ap); |
| 12455 | exitcode = 1; |
| 12456 | goto next; |
| 12457 | } |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12458 | INT_OFF; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12459 | if (action) { |
Denis Vlasenko | 9f73944 | 2006-12-16 23:49:13 +0000 | [diff] [blame] | 12460 | if (LONE_DASH(action)) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12461 | action = NULL; |
| 12462 | else |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12463 | action = ckstrdup(action); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12464 | } |
Denis Vlasenko | 6081868 | 2007-09-28 22:07:23 +0000 | [diff] [blame] | 12465 | free(trap[signo]); |
Denys Vlasenko | 238bf18 | 2010-05-18 15:49:07 +0200 | [diff] [blame] | 12466 | if (action) |
| 12467 | may_have_traps = 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12468 | trap[signo] = action; |
| 12469 | if (signo != 0) |
| 12470 | setsignal(signo); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12471 | INT_ON; |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12472 | next: |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12473 | ap++; |
| 12474 | } |
Denys Vlasenko | 496d5bf | 2010-03-26 15:52:24 +0100 | [diff] [blame] | 12475 | return exitcode; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12476 | } |
| 12477 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12478 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12479 | /* ============ Builtins */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12480 | |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 12481 | #if !ENABLE_FEATURE_SH_EXTRA_QUIET |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12482 | /* |
| 12483 | * Lists available builtins |
| 12484 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12485 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12486 | helpcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12487 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 12488 | unsigned col; |
| 12489 | unsigned i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12490 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 12491 | out1fmt( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 12492 | "Built-in commands:\n" |
| 12493 | "------------------\n"); |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 12494 | for (col = 0, i = 0; i < ARRAY_SIZE(builtintab); i++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12495 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), |
Denis Vlasenko | 5276402 | 2007-02-24 13:42:56 +0000 | [diff] [blame] | 12496 | builtintab[i].name + 1); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12497 | if (col > 60) { |
| 12498 | out1fmt("\n"); |
| 12499 | col = 0; |
| 12500 | } |
| 12501 | } |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 12502 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 12503 | { |
| 12504 | const char *a = applet_names; |
| 12505 | while (*a) { |
| 12506 | col += out1fmt("%c%s", ((col == 0) ? '\t' : ' '), a); |
| 12507 | if (col > 60) { |
| 12508 | out1fmt("\n"); |
| 12509 | col = 0; |
| 12510 | } |
| 12511 | a += strlen(a) + 1; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12512 | } |
| 12513 | } |
| 12514 | #endif |
| 12515 | out1fmt("\n\n"); |
| 12516 | return EXIT_SUCCESS; |
| 12517 | } |
Denis Vlasenko | 131ae17 | 2007-02-18 13:00:19 +0000 | [diff] [blame] | 12518 | #endif /* FEATURE_SH_EXTRA_QUIET */ |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12519 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12520 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12521 | * The export and readonly commands. |
| 12522 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12523 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12524 | exportcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12525 | { |
| 12526 | struct var *vp; |
| 12527 | char *name; |
| 12528 | const char *p; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12529 | char **aptr; |
Denis Vlasenko | b730474 | 2008-10-20 08:15:51 +0000 | [diff] [blame] | 12530 | int flag = argv[0][0] == 'r' ? VREADONLY : VEXPORT; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12531 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12532 | if (nextopt("p") != 'p') { |
| 12533 | aptr = argptr; |
| 12534 | name = *aptr; |
| 12535 | if (name) { |
| 12536 | do { |
| 12537 | p = strchr(name, '='); |
| 12538 | if (p != NULL) { |
| 12539 | p++; |
| 12540 | } else { |
| 12541 | vp = *findvar(hashvar(name), name); |
| 12542 | if (vp) { |
| 12543 | vp->flags |= flag; |
| 12544 | continue; |
| 12545 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12546 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12547 | setvar(name, p, flag); |
| 12548 | } while ((name = *++aptr) != NULL); |
| 12549 | return 0; |
| 12550 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12551 | } |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12552 | showvars(argv[0], flag, 0); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12553 | return 0; |
| 12554 | } |
| 12555 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12556 | /* |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12557 | * Delete a function if it exists. |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12558 | */ |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12559 | static void |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12560 | unsetfunc(const char *name) |
Aaron Lehmann | b6ecbdc | 2001-12-06 03:37:38 +0000 | [diff] [blame] | 12561 | { |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12562 | struct tblentry *cmdp; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12563 | |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12564 | cmdp = cmdlookup(name, 0); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12565 | if (cmdp != NULL && cmdp->cmdtype == CMDFUNCTION) |
Denis Vlasenko | 5651bfc | 2007-02-23 21:08:58 +0000 | [diff] [blame] | 12566 | delete_cmd_entry(); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12567 | } |
| 12568 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12569 | /* |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12570 | * The unset builtin command. We unset the function before we unset the |
| 12571 | * variable to allow a function to be unset when there is a readonly variable |
| 12572 | * with the same name. |
| 12573 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12574 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12575 | unsetcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12576 | { |
| 12577 | char **ap; |
| 12578 | int i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12579 | int flag = 0; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12580 | int ret = 0; |
| 12581 | |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12582 | while ((i = nextopt("vf")) != 0) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12583 | flag = i; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12584 | } |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12585 | |
Denis Vlasenko | 2da584f | 2007-02-19 22:44:05 +0000 | [diff] [blame] | 12586 | for (ap = argptr; *ap; ap++) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12587 | if (flag != 'f') { |
| 12588 | i = unsetvar(*ap); |
| 12589 | ret |= i; |
| 12590 | if (!(i & 2)) |
| 12591 | continue; |
| 12592 | } |
| 12593 | if (flag != 'v') |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12594 | unsetfunc(*ap); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12595 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12596 | return ret & 1; |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12597 | } |
| 12598 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12599 | static const unsigned char timescmd_str[] ALIGN1 = { |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12600 | ' ', offsetof(struct tms, tms_utime), |
| 12601 | '\n', offsetof(struct tms, tms_stime), |
| 12602 | ' ', offsetof(struct tms, tms_cutime), |
| 12603 | '\n', offsetof(struct tms, tms_cstime), |
| 12604 | 0 |
| 12605 | }; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12606 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12607 | timescmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12608 | { |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12609 | unsigned long clk_tck, s, t; |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12610 | const unsigned char *p; |
| 12611 | struct tms buf; |
| 12612 | |
| 12613 | clk_tck = sysconf(_SC_CLK_TCK); |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12614 | times(&buf); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12615 | |
| 12616 | p = timescmd_str; |
| 12617 | do { |
| 12618 | t = *(clock_t *)(((char *) &buf) + p[1]); |
| 12619 | s = t / clk_tck; |
Denys Vlasenko | 8cd9f34 | 2010-06-18 15:36:48 +0200 | [diff] [blame] | 12620 | t = t % clk_tck; |
| 12621 | out1fmt("%lum%lu.%03lus%c", |
| 12622 | s / 60, s % 60, |
| 12623 | (t * 1000) / clk_tck, |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12624 | p[0]); |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12625 | p += 2; |
| 12626 | } while (*p); |
Manuel Novoa III | 4456f25 | 2003-08-13 17:48:47 +0000 | [diff] [blame] | 12627 | |
Eric Andersen | cb57d55 | 2001-06-28 07:25:16 +0000 | [diff] [blame] | 12628 | return 0; |
| 12629 | } |
| 12630 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12631 | #if ENABLE_SH_MATH_SUPPORT |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12632 | /* |
Denys Vlasenko | 1ed2fb4 | 2010-06-18 14:09:48 +0200 | [diff] [blame] | 12633 | * The let builtin. Partially stolen from GNU Bash, the Bourne Again SHell. |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12634 | * Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. |
Eric Andersen | 9089844 | 2003-08-06 11:20:52 +0000 | [diff] [blame] | 12635 | * |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12636 | * Copyright (C) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12637 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12638 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12639 | letcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12640 | { |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12641 | arith_t i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12642 | |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12643 | argv++; |
| 12644 | if (!*argv) |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12645 | ash_msg_and_raise_error("expression expected"); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12646 | do { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 12647 | i = ash_arith(*argv); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12648 | } while (*++argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12649 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 12650 | return !i; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12651 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12652 | #endif |
Eric Andersen | 74bcd16 | 2001-07-30 21:41:37 +0000 | [diff] [blame] | 12653 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12654 | /* |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12655 | * The read builtin. Options: |
| 12656 | * -r Do not interpret '\' specially |
| 12657 | * -s Turn off echo (tty only) |
| 12658 | * -n NCHARS Read NCHARS max |
| 12659 | * -p PROMPT Display PROMPT on stderr (if input is from tty) |
| 12660 | * -t SECONDS Timeout after SECONDS (tty or pipe only) |
| 12661 | * -u FD Read from given FD instead of fd 0 |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12662 | * This uses unbuffered input, which may be avoidable in some cases. |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12663 | * TODO: bash also has: |
| 12664 | * -a ARRAY Read into array[0],[1],etc |
| 12665 | * -d DELIM End on DELIM char, not newline |
| 12666 | * -e Use line editing (tty only) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12667 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12668 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12669 | readcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12670 | { |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12671 | char *opt_n = NULL; |
| 12672 | char *opt_p = NULL; |
| 12673 | char *opt_t = NULL; |
| 12674 | char *opt_u = NULL; |
| 12675 | int read_flags = 0; |
| 12676 | const char *r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12677 | int i; |
| 12678 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12679 | while ((i = nextopt("p:u:rt:n:s")) != '\0') { |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 12680 | switch (i) { |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12681 | case 'p': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12682 | opt_p = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12683 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12684 | case 'n': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12685 | opt_n = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12686 | break; |
| 12687 | case 's': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12688 | read_flags |= BUILTIN_READ_SILENT; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12689 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12690 | case 't': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12691 | opt_t = optionarg; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12692 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12693 | case 'r': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12694 | read_flags |= BUILTIN_READ_RAW; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12695 | break; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12696 | case 'u': |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12697 | opt_u = optionarg; |
Denis Vlasenko | 59f351c | 2008-03-25 00:07:12 +0000 | [diff] [blame] | 12698 | break; |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12699 | default: |
| 12700 | break; |
| 12701 | } |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12702 | } |
Paul Fox | 02eb934 | 2005-09-07 16:56:02 +0000 | [diff] [blame] | 12703 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 12704 | r = shell_builtin_read(setvar2, |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12705 | argptr, |
| 12706 | bltinlookup("IFS"), /* can be NULL */ |
| 12707 | read_flags, |
| 12708 | opt_n, |
| 12709 | opt_p, |
| 12710 | opt_t, |
| 12711 | opt_u |
| 12712 | ); |
Denis Vlasenko | 46aeab9 | 2009-03-31 19:18:17 +0000 | [diff] [blame] | 12713 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12714 | if ((uintptr_t)r > 1) |
| 12715 | ash_msg_and_raise_error(r); |
Denis Vlasenko | 037576d | 2007-10-20 18:30:38 +0000 | [diff] [blame] | 12716 | |
Denys Vlasenko | 7306727 | 2010-01-12 22:11:24 +0100 | [diff] [blame] | 12717 | return (uintptr_t)r; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12718 | } |
| 12719 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12720 | static int FAST_FUNC |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12721 | umaskcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12722 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 12723 | static const char permuser[3] ALIGN1 = "ugo"; |
| 12724 | static const char permmode[3] ALIGN1 = "rwx"; |
| 12725 | static const short permmask[] ALIGN2 = { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12726 | S_IRUSR, S_IWUSR, S_IXUSR, |
| 12727 | S_IRGRP, S_IWGRP, S_IXGRP, |
| 12728 | S_IROTH, S_IWOTH, S_IXOTH |
| 12729 | }; |
| 12730 | |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 12731 | /* TODO: use bb_parse_mode() instead */ |
| 12732 | |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12733 | char *ap; |
| 12734 | mode_t mask; |
| 12735 | int i; |
| 12736 | int symbolic_mode = 0; |
| 12737 | |
| 12738 | while (nextopt("S") != '\0') { |
| 12739 | symbolic_mode = 1; |
| 12740 | } |
| 12741 | |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12742 | INT_OFF; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12743 | mask = umask(0); |
| 12744 | umask(mask); |
Denis Vlasenko | b012b10 | 2007-02-19 22:43:01 +0000 | [diff] [blame] | 12745 | INT_ON; |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12746 | |
Denis Vlasenko | 5cedb75 | 2007-02-18 19:56:41 +0000 | [diff] [blame] | 12747 | ap = *argptr; |
| 12748 | if (ap == NULL) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12749 | if (symbolic_mode) { |
| 12750 | char buf[18]; |
| 12751 | char *p = buf; |
| 12752 | |
| 12753 | for (i = 0; i < 3; i++) { |
| 12754 | int j; |
| 12755 | |
| 12756 | *p++ = permuser[i]; |
| 12757 | *p++ = '='; |
| 12758 | for (j = 0; j < 3; j++) { |
| 12759 | if ((mask & permmask[3 * i + j]) == 0) { |
| 12760 | *p++ = permmode[j]; |
| 12761 | } |
| 12762 | } |
| 12763 | *p++ = ','; |
| 12764 | } |
| 12765 | *--p = 0; |
| 12766 | puts(buf); |
| 12767 | } else { |
| 12768 | out1fmt("%.4o\n", mask); |
| 12769 | } |
| 12770 | } else { |
Denis Vlasenko | aa74445 | 2007-02-23 01:04:22 +0000 | [diff] [blame] | 12771 | if (isdigit((unsigned char) *ap)) { |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12772 | mask = 0; |
| 12773 | do { |
| 12774 | if (*ap >= '8' || *ap < '0') |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 12775 | ash_msg_and_raise_error(msg_illnum, argv[1]); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12776 | mask = (mask << 3) + (*ap - '0'); |
| 12777 | } while (*++ap != '\0'); |
| 12778 | umask(mask); |
| 12779 | } else { |
| 12780 | mask = ~mask & 0777; |
| 12781 | if (!bb_parse_mode(ap, &mask)) { |
Denis Vlasenko | 3af3e5b | 2007-03-05 00:24:52 +0000 | [diff] [blame] | 12782 | ash_msg_and_raise_error("illegal mode: %s", ap); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12783 | } |
| 12784 | umask(~mask & 0777); |
| 12785 | } |
| 12786 | } |
| 12787 | return 0; |
| 12788 | } |
| 12789 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 12790 | static int FAST_FUNC |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12791 | ulimitcmd(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12792 | { |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 12793 | return shell_builtin_ulimit(argv); |
Eric Andersen | c470f44 | 2003-07-28 09:56:35 +0000 | [diff] [blame] | 12794 | } |
| 12795 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12796 | /* ============ main() and helpers */ |
| 12797 | |
| 12798 | /* |
| 12799 | * Called to exit the shell. |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12800 | */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12801 | static void exitshell(void) NORETURN; |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12802 | static void |
| 12803 | exitshell(void) |
| 12804 | { |
| 12805 | struct jmploc loc; |
| 12806 | char *p; |
| 12807 | int status; |
| 12808 | |
| 12809 | status = exitstatus; |
| 12810 | TRACE(("pid %d, exitshell(%d)\n", getpid(), status)); |
| 12811 | if (setjmp(loc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 12812 | if (exception_type == EXEXIT) |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12813 | /* dash bug: it just does _exit(exitstatus) here |
| 12814 | * but we have to do setjobctl(0) first! |
| 12815 | * (bug is still not fixed in dash-0.5.3 - if you run dash |
| 12816 | * under Midnight Commander, on exit from dash MC is backgrounded) */ |
| 12817 | status = exitstatus; |
| 12818 | goto out; |
| 12819 | } |
| 12820 | exception_handler = &loc; |
| 12821 | p = trap[0]; |
| 12822 | if (p) { |
| 12823 | trap[0] = NULL; |
| 12824 | evalstring(p, 0); |
Denys Vlasenko | 0800e3a | 2009-09-24 03:09:26 +0200 | [diff] [blame] | 12825 | free(p); |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12826 | } |
| 12827 | flush_stdout_stderr(); |
| 12828 | out: |
| 12829 | setjobctl(0); |
| 12830 | _exit(status); |
| 12831 | /* NOTREACHED */ |
| 12832 | } |
| 12833 | |
Denis Vlasenko | 5c67e3e | 2007-02-23 01:05:03 +0000 | [diff] [blame] | 12834 | static void |
| 12835 | init(void) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12836 | { |
| 12837 | /* from input.c: */ |
Denys Vlasenko | 82dd14a | 2010-05-17 10:10:01 +0200 | [diff] [blame] | 12838 | /* we will never free this */ |
| 12839 | basepf.next_to_pgetc = basepf.buf = ckmalloc(IBUFSIZ); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12840 | |
| 12841 | /* from trap.c: */ |
| 12842 | signal(SIGCHLD, SIG_DFL); |
Denys Vlasenko | 7a7b034 | 2009-12-04 04:18:31 +0100 | [diff] [blame] | 12843 | /* bash re-enables SIGHUP which is SIG_IGNed on entry. |
| 12844 | * Try: "trap '' HUP; bash; echo RET" and type "kill -HUP $$" |
| 12845 | */ |
| 12846 | signal(SIGHUP, SIG_DFL); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12847 | |
| 12848 | /* from var.c: */ |
| 12849 | { |
| 12850 | char **envp; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12851 | const char *p; |
| 12852 | struct stat st1, st2; |
| 12853 | |
| 12854 | initvar(); |
| 12855 | for (envp = environ; envp && *envp; envp++) { |
| 12856 | if (strchr(*envp, '=')) { |
| 12857 | setvareq(*envp, VEXPORT|VTEXTFIXED); |
| 12858 | } |
| 12859 | } |
| 12860 | |
Denys Vlasenko | 7bb346f | 2009-10-06 22:09:50 +0200 | [diff] [blame] | 12861 | setvar("PPID", utoa(getppid()), 0); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12862 | |
| 12863 | p = lookupvar("PWD"); |
| 12864 | if (p) |
| 12865 | if (*p != '/' || stat(p, &st1) || stat(".", &st2) |
| 12866 | || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) |
| 12867 | p = '\0'; |
| 12868 | setpwd(p, 0); |
| 12869 | } |
| 12870 | } |
| 12871 | |
| 12872 | /* |
| 12873 | * Process the shell command line arguments. |
| 12874 | */ |
| 12875 | static void |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12876 | procargs(char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12877 | { |
| 12878 | int i; |
| 12879 | const char *xminusc; |
| 12880 | char **xargv; |
| 12881 | |
| 12882 | xargv = argv; |
| 12883 | arg0 = xargv[0]; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 12884 | /* if (xargv[0]) - mmm, this is always true! */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12885 | xargv++; |
| 12886 | for (i = 0; i < NOPTS; i++) |
| 12887 | optlist[i] = 2; |
| 12888 | argptr = xargv; |
Denis Vlasenko | 28bf671 | 2008-02-14 15:01:47 +0000 | [diff] [blame] | 12889 | if (options(1)) { |
| 12890 | /* it already printed err message */ |
| 12891 | raise_exception(EXERROR); |
| 12892 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12893 | xargv = argptr; |
| 12894 | xminusc = minusc; |
| 12895 | if (*xargv == NULL) { |
| 12896 | if (xminusc) |
| 12897 | ash_msg_and_raise_error(bb_msg_requires_arg, "-c"); |
| 12898 | sflag = 1; |
| 12899 | } |
| 12900 | if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) |
| 12901 | iflag = 1; |
| 12902 | if (mflag == 2) |
| 12903 | mflag = iflag; |
| 12904 | for (i = 0; i < NOPTS; i++) |
| 12905 | if (optlist[i] == 2) |
| 12906 | optlist[i] = 0; |
| 12907 | #if DEBUG == 2 |
| 12908 | debug = 1; |
| 12909 | #endif |
| 12910 | /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ |
| 12911 | if (xminusc) { |
| 12912 | minusc = *xargv++; |
| 12913 | if (*xargv) |
| 12914 | goto setarg0; |
| 12915 | } else if (!sflag) { |
| 12916 | setinputfile(*xargv, 0); |
| 12917 | setarg0: |
| 12918 | arg0 = *xargv++; |
| 12919 | commandname = arg0; |
| 12920 | } |
| 12921 | |
| 12922 | shellparam.p = xargv; |
| 12923 | #if ENABLE_ASH_GETOPTS |
| 12924 | shellparam.optind = 1; |
| 12925 | shellparam.optoff = -1; |
| 12926 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12927 | /* assert(shellparam.malloced == 0 && shellparam.nparam == 0); */ |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12928 | while (*xargv) { |
| 12929 | shellparam.nparam++; |
| 12930 | xargv++; |
| 12931 | } |
| 12932 | optschanged(); |
| 12933 | } |
| 12934 | |
| 12935 | /* |
| 12936 | * Read /etc/profile or .profile. |
| 12937 | */ |
| 12938 | static void |
| 12939 | read_profile(const char *name) |
| 12940 | { |
| 12941 | int skip; |
| 12942 | |
| 12943 | if (setinputfile(name, INPUT_PUSH_FILE | INPUT_NOFILE_OK) < 0) |
| 12944 | return; |
| 12945 | skip = cmdloop(0); |
| 12946 | popfile(); |
| 12947 | if (skip) |
| 12948 | exitshell(); |
| 12949 | } |
| 12950 | |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12951 | /* |
| 12952 | * This routine is called when an error or an interrupt occurs in an |
| 12953 | * interactive shell and control is returned to the main command loop. |
| 12954 | */ |
| 12955 | static void |
| 12956 | reset(void) |
| 12957 | { |
| 12958 | /* from eval.c: */ |
| 12959 | evalskip = 0; |
| 12960 | loopnest = 0; |
| 12961 | /* from input.c: */ |
Denis Vlasenko | 41eb300 | 2008-11-28 03:42:31 +0000 | [diff] [blame] | 12962 | g_parsefile->left_in_buffer = 0; |
| 12963 | g_parsefile->left_in_line = 0; /* clear input buffer */ |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12964 | popallfiles(); |
| 12965 | /* from parser.c: */ |
| 12966 | tokpushback = 0; |
| 12967 | checkkwd = 0; |
| 12968 | /* from redir.c: */ |
Denis Vlasenko | 34c73c4 | 2008-08-16 11:48:02 +0000 | [diff] [blame] | 12969 | clearredir(/*drop:*/ 0); |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 12970 | } |
| 12971 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12972 | #if PROFILE |
| 12973 | static short profile_buf[16384]; |
| 12974 | extern int etext(); |
| 12975 | #endif |
| 12976 | |
Denis Vlasenko | bc54cff | 2007-02-23 01:05:52 +0000 | [diff] [blame] | 12977 | /* |
| 12978 | * Main routine. We initialize things, parse the arguments, execute |
| 12979 | * profiles if we're a login shell, and then call cmdloop to execute |
| 12980 | * commands. The setjmp call sets up the location to jump to when an |
| 12981 | * exception occurs. When an exception occurs the variable "state" |
| 12982 | * is used to figure out how far we had gotten. |
| 12983 | */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 12984 | int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 12985 | int ash_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12986 | { |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 12987 | const char *shinit; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 12988 | volatile smallint state; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 12989 | struct jmploc jmploc; |
| 12990 | struct stackmark smark; |
| 12991 | |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12992 | /* Initialize global data */ |
| 12993 | INIT_G_misc(); |
| 12994 | INIT_G_memstack(); |
| 12995 | INIT_G_var(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12996 | #if ENABLE_ASH_ALIAS |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12997 | INIT_G_alias(); |
Denis Vlasenko | ee87ebf | 2007-12-21 22:18:16 +0000 | [diff] [blame] | 12998 | #endif |
Denis Vlasenko | 0163111 | 2007-12-16 17:20:38 +0000 | [diff] [blame] | 12999 | INIT_G_cmdtable(); |
| 13000 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13001 | #if PROFILE |
| 13002 | monitor(4, etext, profile_buf, sizeof(profile_buf), 50); |
| 13003 | #endif |
| 13004 | |
| 13005 | #if ENABLE_FEATURE_EDITING |
| 13006 | line_input_state = new_line_input_t(FOR_SHELL | WITH_PATH_LOOKUP); |
| 13007 | #endif |
| 13008 | state = 0; |
| 13009 | if (setjmp(jmploc.loc)) { |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13010 | smallint e; |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13011 | smallint s; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13012 | |
| 13013 | reset(); |
| 13014 | |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13015 | e = exception_type; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13016 | if (e == EXERROR) |
| 13017 | exitstatus = 2; |
| 13018 | s = state; |
| 13019 | if (e == EXEXIT || s == 0 || iflag == 0 || shlvl) |
| 13020 | exitshell(); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13021 | if (e == EXINT) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13022 | outcslow('\n', stderr); |
Denis Vlasenko | 7f88e34 | 2009-03-19 03:36:18 +0000 | [diff] [blame] | 13023 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13024 | popstackmark(&smark); |
| 13025 | FORCE_INT_ON; /* enable interrupts */ |
| 13026 | if (s == 1) |
| 13027 | goto state1; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13028 | if (s == 2) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13029 | goto state2; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13030 | if (s == 3) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13031 | goto state3; |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13032 | goto state4; |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13033 | } |
| 13034 | exception_handler = &jmploc; |
| 13035 | #if DEBUG |
| 13036 | opentrace(); |
Denis Vlasenko | 653d8e7 | 2009-03-19 21:59:35 +0000 | [diff] [blame] | 13037 | TRACE(("Shell args: ")); |
Denis Vlasenko | fe1f00a | 2007-02-23 01:04:50 +0000 | [diff] [blame] | 13038 | trace_puts_args(argv); |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13039 | #endif |
| 13040 | rootpid = getpid(); |
| 13041 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13042 | init(); |
| 13043 | setstackmark(&smark); |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 13044 | procargs(argv); |
| 13045 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13046 | #if ENABLE_FEATURE_EDITING_SAVEHISTORY |
| 13047 | if (iflag) { |
| 13048 | const char *hp = lookupvar("HISTFILE"); |
| 13049 | |
| 13050 | if (hp == NULL) { |
| 13051 | hp = lookupvar("HOME"); |
| 13052 | if (hp != NULL) { |
| 13053 | char *defhp = concat_path_file(hp, ".ash_history"); |
| 13054 | setvar("HISTFILE", defhp, 0); |
| 13055 | free(defhp); |
| 13056 | } |
| 13057 | } |
| 13058 | } |
| 13059 | #endif |
Denis Vlasenko | 4e12b1a | 2008-12-23 23:36:47 +0000 | [diff] [blame] | 13060 | if (/* argv[0] && */ argv[0][0] == '-') |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13061 | isloginsh = 1; |
| 13062 | if (isloginsh) { |
| 13063 | state = 1; |
| 13064 | read_profile("/etc/profile"); |
| 13065 | state1: |
| 13066 | state = 2; |
| 13067 | read_profile(".profile"); |
| 13068 | } |
| 13069 | state2: |
| 13070 | state = 3; |
| 13071 | if ( |
| 13072 | #ifndef linux |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13073 | getuid() == geteuid() && getgid() == getegid() && |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13074 | #endif |
Denis Vlasenko | 0c032a4 | 2007-02-23 01:03:40 +0000 | [diff] [blame] | 13075 | iflag |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13076 | ) { |
| 13077 | shinit = lookupvar("ENV"); |
| 13078 | if (shinit != NULL && *shinit != '\0') { |
| 13079 | read_profile(shinit); |
| 13080 | } |
| 13081 | } |
| 13082 | state3: |
| 13083 | state = 4; |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13084 | if (minusc) { |
| 13085 | /* evalstring pushes parsefile stack. |
| 13086 | * Ensure we don't falsely claim that 0 (stdin) |
Denis Vlasenko | 5368ad5 | 2009-03-20 10:20:08 +0000 | [diff] [blame] | 13087 | * is one of stacked source fds. |
| 13088 | * Testcase: ash -c 'exec 1>&0' must not complain. */ |
Denys Vlasenko | 79b3d42 | 2010-06-03 04:29:08 +0200 | [diff] [blame] | 13089 | // if (!sflag) g_parsefile->pf_fd = -1; |
Denys Vlasenko | 08d8b3c | 2010-06-03 04:28:28 +0200 | [diff] [blame] | 13090 | // ^^ not necessary since now we special-case fd 0 |
| 13091 | // in is_hidden_fd() to not be considered "hidden fd" |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13092 | evalstring(minusc, 0); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13093 | } |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13094 | |
| 13095 | if (sflag || minusc == NULL) { |
Denys Vlasenko | 0337e03 | 2009-11-29 00:12:30 +0100 | [diff] [blame] | 13096 | #if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_FEATURE_EDITING_SAVEHISTORY |
Denis Vlasenko | 2f5d0cd | 2008-06-23 13:24:19 +0000 | [diff] [blame] | 13097 | if (iflag) { |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13098 | const char *hp = lookupvar("HISTFILE"); |
Denis Vlasenko | 5c2b814 | 2009-03-19 01:59:59 +0000 | [diff] [blame] | 13099 | if (hp) |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13100 | line_input_state->hist_file = hp; |
| 13101 | } |
| 13102 | #endif |
| 13103 | state4: /* XXX ??? - why isn't this before the "if" statement */ |
| 13104 | cmdloop(1); |
| 13105 | } |
| 13106 | #if PROFILE |
| 13107 | monitor(0); |
| 13108 | #endif |
| 13109 | #ifdef GPROF |
| 13110 | { |
| 13111 | extern void _mcleanup(void); |
| 13112 | _mcleanup(); |
| 13113 | } |
| 13114 | #endif |
| 13115 | exitshell(); |
| 13116 | /* NOTREACHED */ |
| 13117 | } |
| 13118 | |
Denis Vlasenko | a624c11 | 2007-02-19 22:45:43 +0000 | [diff] [blame] | 13119 | |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13120 | /*- |
| 13121 | * Copyright (c) 1989, 1991, 1993, 1994 |
Eric Andersen | 2870d96 | 2001-07-02 17:27:21 +0000 | [diff] [blame] | 13122 | * The Regents of the University of California. All rights reserved. |
Eric Andersen | df82f61 | 2001-06-28 07:46:40 +0000 | [diff] [blame] | 13123 | * |
| 13124 | * This code is derived from software contributed to Berkeley by |
| 13125 | * Kenneth Almquist. |
| 13126 | * |
| 13127 | * Redistribution and use in source and binary forms, with or without |
| 13128 | * modification, are permitted provided that the following conditions |
| 13129 | * are met: |
| 13130 | * 1. Redistributions of source code must retain the above copyright |
| 13131 | * notice, this list of conditions and the following disclaimer. |
| 13132 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13133 | * notice, this list of conditions and the following disclaimer in the |
| 13134 | * documentation and/or other materials provided with the distribution. |
"Vladimir N. Oleynik" | ddc280e | 2005-12-15 12:01:49 +0000 | [diff] [blame] | 13135 | * 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] | 13136 | * may be used to endorse or promote products derived from this software |
| 13137 | * without specific prior written permission. |
| 13138 | * |
| 13139 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 13140 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 13141 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 13142 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 13143 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 13144 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 13145 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 13146 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 13147 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 13148 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 13149 | * SUCH DAMAGE. |
| 13150 | */ |