Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3 | * A prototype Bourne shell grammar parser. |
| 4 | * Intended to follow the original Thompson and Ritchie |
| 5 | * "small and simple is beautiful" philosophy, which |
| 6 | * incidentally is a good match to today's BusyBox. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7 | * |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 8 | * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org> |
Denis Vlasenko | c8d2733 | 2009-04-06 10:47:21 +0000 | [diff] [blame] | 9 | * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com> |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 10 | * |
| 11 | * Credits: |
| 12 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 13 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 14 | * execution engine, the builtins, and much of the underlying |
| 15 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 17 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 18 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 19 | * Troan, which they placed in the public domain. I don't know |
| 20 | * how much of the Johnson/Troan code has survived the repeated |
| 21 | * rewrites. |
| 22 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 23 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 24 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 25 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 26 | * and many builtins derived from contributions by Erik Andersen. |
| 27 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 28 | * |
| 29 | * There are two big (and related) architecture differences between |
| 30 | * this parser and the lash parser. One is that this version is |
| 31 | * actually designed from the ground up to understand nearly all |
| 32 | * of the Bourne grammar. The second, consequential change is that |
| 33 | * the parser and input reader have been turned inside out. Now, |
| 34 | * the parser is in control, and asks for input as needed. The old |
| 35 | * way had the input reader in control, and it asked for parsing to |
| 36 | * take place as needed. The new way makes it much easier to properly |
| 37 | * handle the recursion implicit in the various substitutions, especially |
| 38 | * across continuation lines. |
| 39 | * |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 40 | * TODOs: |
| 41 | * grep for "TODO" and fix (some of them are easy) |
| 42 | * special variables (done: PWD, PPID, RANDOM) |
| 43 | * tilde expansion |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 44 | * aliases |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 45 | * follow IFS rules more precisely, including update semantics |
| 46 | * builtins mandated by standards we don't support: |
| 47 | * [un]alias, command, fc, getopts, newgrp, readonly, times |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 48 | * make complex ${var%...} constructs support optional |
| 49 | * make here documents optional |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 50 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 51 | * Bash compat TODO: |
| 52 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 53 | * subst operator: ${var/[/]expr/expr} |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 54 | * brace expansion: one/{two,three,four} |
| 55 | * reserved words: function select |
| 56 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 57 | * process substitution: <(list) and >(list) |
| 58 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 59 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 60 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 61 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 62 | * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used) |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 63 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 64 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 65 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 66 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 67 | * export builtin should be special, its arguments are assignments |
| 68 | * and therefore expansion of them should be "one-word" expansion: |
| 69 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 70 | * compare with: |
| 71 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 72 | * ls: cannot access i=a: No such file or directory |
| 73 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 74 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 75 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 76 | * is not quoted: |
| 77 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 78 | * aaa bbb |
| 79 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 80 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 81 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 82 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 83 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 84 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 85 | #include <malloc.h> /* for malloc_trim */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 86 | #include <glob.h> |
| 87 | /* #include <dmalloc.h> */ |
| 88 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 89 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 90 | #endif |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 91 | |
| 92 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 93 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 94 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 95 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 96 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 97 | #else |
| 98 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 99 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 100 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 101 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 102 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 103 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 104 | //applet:IF_HUSH(APPLET(hush, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 105 | //applet:IF_MSH(APPLET(msh, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 106 | //applet:IF_LASH(APPLET(lash, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 107 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, _BB_DIR_BIN, _BB_SUID_DROP, sh)) |
| 108 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, _BB_DIR_BIN, _BB_SUID_DROP, bash)) |
| 109 | |
| 110 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 111 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 112 | |
| 113 | //config:config HUSH |
| 114 | //config: bool "hush" |
| 115 | //config: default y |
| 116 | //config: help |
| 117 | //config: hush is a small shell (22k). It handles the normal flow control |
| 118 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 119 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 120 | //config: and functions are supported. |
| 121 | //config: |
| 122 | //config: It will compile and work on no-mmu systems. |
| 123 | //config: |
| 124 | //config: It does not handle select, aliases, brace expansion, |
| 125 | //config: tilde expansion, &>file and >&file redirection of stdout+stderr. |
| 126 | //config: |
| 127 | //config:config HUSH_BASH_COMPAT |
| 128 | //config: bool "bash-compatible extensions" |
| 129 | //config: default y |
| 130 | //config: depends on HUSH |
| 131 | //config: help |
| 132 | //config: Enable bash-compatible extensions. |
| 133 | //config: |
| 134 | //config:config HUSH_HELP |
| 135 | //config: bool "help builtin" |
| 136 | //config: default y |
| 137 | //config: depends on HUSH |
| 138 | //config: help |
| 139 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 140 | //config: |
| 141 | //config:config HUSH_INTERACTIVE |
| 142 | //config: bool "Interactive mode" |
| 143 | //config: default y |
| 144 | //config: depends on HUSH |
| 145 | //config: help |
| 146 | //config: Enable interactive mode (prompt and command editing). |
| 147 | //config: Without this, hush simply reads and executes commands |
| 148 | //config: from stdin just like a shell script from a file. |
| 149 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 150 | //config: |
| 151 | //config:config HUSH_JOB |
| 152 | //config: bool "Job control" |
| 153 | //config: default y |
| 154 | //config: depends on HUSH_INTERACTIVE |
| 155 | //config: help |
| 156 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 157 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 158 | //config: "cmd &" still works by simply spawning a process and immediately |
| 159 | //config: prompting for next command (or executing next command in a script), |
| 160 | //config: but no separate process group is formed. |
| 161 | //config: |
| 162 | //config:config HUSH_TICK |
| 163 | //config: bool "Process substitution" |
| 164 | //config: default y |
| 165 | //config: depends on HUSH |
| 166 | //config: help |
| 167 | //config: Enable process substitution `command` and $(command) in hush. |
| 168 | //config: |
| 169 | //config:config HUSH_IF |
| 170 | //config: bool "Support if/then/elif/else/fi" |
| 171 | //config: default y |
| 172 | //config: depends on HUSH |
| 173 | //config: help |
| 174 | //config: Enable if/then/elif/else/fi in hush. |
| 175 | //config: |
| 176 | //config:config HUSH_LOOPS |
| 177 | //config: bool "Support for, while and until loops" |
| 178 | //config: default y |
| 179 | //config: depends on HUSH |
| 180 | //config: help |
| 181 | //config: Enable for, while and until loops in hush. |
| 182 | //config: |
| 183 | //config:config HUSH_CASE |
| 184 | //config: bool "Support case ... esac statement" |
| 185 | //config: default y |
| 186 | //config: depends on HUSH |
| 187 | //config: help |
| 188 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 189 | //config: |
| 190 | //config:config HUSH_FUNCTIONS |
| 191 | //config: bool "Support funcname() { commands; } syntax" |
| 192 | //config: default y |
| 193 | //config: depends on HUSH |
| 194 | //config: help |
| 195 | //config: Enable support for shell functions in hush. +800 bytes. |
| 196 | //config: |
| 197 | //config:config HUSH_LOCAL |
| 198 | //config: bool "Support local builtin" |
| 199 | //config: default y |
| 200 | //config: depends on HUSH_FUNCTIONS |
| 201 | //config: help |
| 202 | //config: Enable support for local variables in functions. |
| 203 | //config: |
| 204 | //config:config HUSH_RANDOM_SUPPORT |
| 205 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 206 | //config: default y |
| 207 | //config: depends on HUSH |
| 208 | //config: help |
| 209 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 210 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 211 | //config: |
| 212 | //config:config HUSH_EXPORT_N |
| 213 | //config: bool "Support 'export -n' option" |
| 214 | //config: default y |
| 215 | //config: depends on HUSH |
| 216 | //config: help |
| 217 | //config: export -n unexports variables. It is a bash extension. |
| 218 | //config: |
| 219 | //config:config HUSH_MODE_X |
| 220 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 221 | //config: default y |
| 222 | //config: depends on HUSH |
| 223 | //config: help |
| 224 | //config: This instructs hush to print commands before execution. Adds ~300 bytes. |
| 225 | //config: |
| 226 | |
| 227 | //usage:#define hush_trivial_usage NOUSAGE_STR |
| 228 | //usage:#define hush_full_usage "" |
| 229 | //usage:#define lash_trivial_usage NOUSAGE_STR |
| 230 | //usage:#define lash_full_usage "" |
| 231 | //usage:#define msh_trivial_usage NOUSAGE_STR |
| 232 | //usage:#define msh_full_usage "" |
| 233 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 234 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 235 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 236 | #define LEAK_HUNTING 0 |
| 237 | #define BUILD_AS_NOMMU 0 |
| 238 | /* Enable/disable sanity checks. Ok to enable in production, |
| 239 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 240 | * Keeping 1 for now even in released versions. |
| 241 | */ |
| 242 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 243 | /* Slightly bigger (+200 bytes), but faster hush. |
| 244 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 245 | * which allows us to do fewer waitpid's. |
| 246 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 247 | * and therefore waitpid will return the same result as last time) |
| 248 | */ |
| 249 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 250 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 251 | * So far ${var%...} ops are always enabled: |
| 252 | */ |
| 253 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 254 | |
| 255 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 256 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 257 | # undef BB_MMU |
| 258 | # undef USE_FOR_NOMMU |
| 259 | # undef USE_FOR_MMU |
| 260 | # define BB_MMU 0 |
| 261 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 262 | # define USE_FOR_MMU(...) |
| 263 | #endif |
| 264 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 265 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 266 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 267 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 268 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 269 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 270 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 271 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 272 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 273 | # define IF_FEATURE_SH_STANDALONE(...) |
| 274 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 275 | #endif |
| 276 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 277 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 278 | # undef ENABLE_FEATURE_EDITING |
| 279 | # define ENABLE_FEATURE_EDITING 0 |
| 280 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 281 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 282 | #endif |
| 283 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 284 | /* Do we support ANY keywords? */ |
| 285 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 286 | # define HAS_KEYWORDS 1 |
| 287 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 288 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 289 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 290 | # define HAS_KEYWORDS 0 |
| 291 | # define IF_HAS_KEYWORDS(...) |
| 292 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 293 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 294 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 295 | /* If you comment out one of these below, it will be #defined later |
| 296 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 297 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 298 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 299 | #define debug_printf_parse(...) do {} while (0) |
| 300 | #define debug_print_tree(a, b) do {} while (0) |
| 301 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 302 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 303 | #define debug_printf_jobs(...) do {} while (0) |
| 304 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 305 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 306 | #define debug_printf_glob(...) do {} while (0) |
| 307 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 308 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 309 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 310 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 311 | #define ERR_PTR ((void*)(long)1) |
| 312 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 313 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 314 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 315 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 316 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 317 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
| 318 | |
| 319 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 320 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 321 | struct variable; |
| 322 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 323 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 324 | |
| 325 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 326 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 327 | */ |
| 328 | #if !BB_MMU |
| 329 | typedef struct nommu_save_t { |
| 330 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 331 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 332 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 333 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 334 | } nommu_save_t; |
| 335 | #endif |
| 336 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 337 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 338 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 339 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 340 | RES_IF , |
| 341 | RES_THEN , |
| 342 | RES_ELIF , |
| 343 | RES_ELSE , |
| 344 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 345 | #endif |
| 346 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 347 | RES_FOR , |
| 348 | RES_WHILE , |
| 349 | RES_UNTIL , |
| 350 | RES_DO , |
| 351 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 352 | #endif |
| 353 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 354 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 355 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 356 | #if ENABLE_HUSH_CASE |
| 357 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 358 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 359 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 360 | RES_MATCH , /* "word)" */ |
| 361 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 362 | RES_ESAC , |
| 363 | #endif |
| 364 | RES_XXXX , |
| 365 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 366 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 367 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 368 | typedef struct o_string { |
| 369 | char *data; |
| 370 | int length; /* position where data is appended */ |
| 371 | int maxlen; |
| 372 | /* Protect newly added chars against globbing |
| 373 | * (by prepending \ to *, ?, [, \) */ |
| 374 | smallint o_escape; |
| 375 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 376 | /* At least some part of the string was inside '' or "", |
| 377 | * possibly empty one: word"", wo''rd etc. */ |
| 378 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 379 | smallint has_empty_slot; |
| 380 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 381 | } o_string; |
| 382 | enum { |
| 383 | MAYBE_ASSIGNMENT = 0, |
| 384 | DEFINITELY_ASSIGNMENT = 1, |
| 385 | NOT_ASSIGNMENT = 2, |
| 386 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 387 | }; |
| 388 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 389 | #define NULL_O_STRING { NULL } |
| 390 | |
| 391 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 392 | * available? Where is it documented? */ |
| 393 | typedef struct in_str { |
| 394 | const char *p; |
| 395 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 396 | char eof_flag; /* meaningless if ->p == NULL */ |
| 397 | char peek_buf[2]; |
| 398 | #if ENABLE_HUSH_INTERACTIVE |
| 399 | smallint promptme; |
| 400 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 401 | #endif |
| 402 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 403 | int (*get) (struct in_str *) FAST_FUNC; |
| 404 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 405 | } in_str; |
| 406 | #define i_getch(input) ((input)->get(input)) |
| 407 | #define i_peek(input) ((input)->peek(input)) |
| 408 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 409 | /* The descrip member of this structure is only used to make |
| 410 | * debugging output pretty */ |
| 411 | static const struct { |
| 412 | int mode; |
| 413 | signed char default_fd; |
| 414 | char descrip[3]; |
| 415 | } redir_table[] = { |
| 416 | { O_RDONLY, 0, "<" }, |
| 417 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 418 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 419 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 420 | { O_RDONLY, 0, "<<" }, |
| 421 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 422 | /* { O_RDONLY, 77, "<<" }, */ |
| 423 | }; |
| 424 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 425 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 426 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 427 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 428 | int rd_fd; /* fd to redirect */ |
| 429 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 430 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 431 | smallint rd_type; /* (enum redir_type) */ |
| 432 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 433 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 434 | * bit 0: do we need to trim leading tabs? |
| 435 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 436 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 437 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 438 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 439 | REDIRECT_INPUT = 0, |
| 440 | REDIRECT_OVERWRITE = 1, |
| 441 | REDIRECT_APPEND = 2, |
| 442 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 443 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 444 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 445 | |
| 446 | REDIRFD_CLOSE = -3, |
| 447 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 448 | REDIRFD_TO_FILE = -1, |
| 449 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 450 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 451 | HEREDOC_SKIPTABS = 1, |
| 452 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 453 | } redir_type; |
| 454 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 455 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 456 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 457 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 458 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 459 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 460 | smallint cmd_type; /* CMD_xxx */ |
| 461 | #define CMD_NORMAL 0 |
| 462 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 463 | |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 464 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 465 | #if ENABLE_HUSH_BASH_COMPAT |
| 466 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 467 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 468 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 469 | /* used for "export noglob=* glob* a=`echo a b`" */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 470 | //#define CMD_SINGLEWORD_NOGLOB_COND 3 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 471 | // It is hard to implement correctly, it adds significant amounts of tricky code, |
| 472 | // and all this is only useful for really obscure export statements |
| 473 | // almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 474 | // guards the code which implements it, but I have doubts it works |
| 475 | // in all cases (especially with mixed globbed/non-globbed arguments) |
| 476 | |
| 477 | #if ENABLE_HUSH_FUNCTIONS |
| 478 | # define CMD_FUNCDEF 3 |
| 479 | #endif |
| 480 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 481 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 482 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 483 | #if !BB_MMU |
| 484 | char *group_as_string; |
| 485 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 486 | #if ENABLE_HUSH_FUNCTIONS |
| 487 | struct function *child_func; |
| 488 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 489 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 490 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 491 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 492 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 493 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 494 | * we put those fields back into cmd->xxx |
| 495 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 496 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 497 | * Without this trick, loop would execute a;b;b;b;... |
| 498 | * instead of correct sequence a;b;a;b;... |
| 499 | * When command is freed, it severs the link |
| 500 | * (sets ->child_func->parent_cmd to NULL). |
| 501 | */ |
| 502 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 503 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 504 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 505 | * and on execution these are substituted with their values. |
| 506 | * Substitution can make _several_ words out of one argv[n]! |
| 507 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 508 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 509 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 510 | struct redir_struct *redirects; /* I/O redirections */ |
| 511 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 512 | /* Is there anything in this command at all? */ |
| 513 | #define IS_NULL_CMD(cmd) \ |
| 514 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 515 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 516 | |
| 517 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 518 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 519 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 520 | int alive_cmds; /* number of commands running (not exited) */ |
| 521 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 522 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 523 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 524 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 525 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 526 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 527 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 528 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 529 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 530 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 531 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 532 | typedef enum pipe_style { |
| 533 | PIPE_SEQ = 1, |
| 534 | PIPE_AND = 2, |
| 535 | PIPE_OR = 3, |
| 536 | PIPE_BG = 4, |
| 537 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 538 | /* Is there anything in this pipe at all? */ |
| 539 | #define IS_NULL_PIPE(pi) \ |
| 540 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 541 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 542 | /* This holds pointers to the various results of parsing */ |
| 543 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 544 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 545 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 546 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 547 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 548 | /* last command in pipe (being constructed right now) */ |
| 549 | struct command *command; |
| 550 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 551 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 552 | #if !BB_MMU |
| 553 | o_string as_string; |
| 554 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 555 | #if HAS_KEYWORDS |
| 556 | smallint ctx_res_w; |
| 557 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 558 | #if ENABLE_HUSH_CASE |
| 559 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 560 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 561 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 562 | int old_flag; |
| 563 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 564 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 565 | * when we see "if" or "then", we malloc and copy current context, |
| 566 | * and make ->stack point to it. then we parse pipeN. |
| 567 | * when closing "then" / fi" / whatever is found, |
| 568 | * we move list_head into ->stack->command->group, |
| 569 | * copy ->stack into current context, and delete ->stack. |
| 570 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 571 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 572 | struct parse_context *stack; |
| 573 | #endif |
| 574 | }; |
| 575 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 576 | /* On program start, environ points to initial environment. |
| 577 | * putenv adds new pointers into it, unsetenv removes them. |
| 578 | * Neither of these (de)allocates the strings. |
| 579 | * setenv allocates new strings in malloc space and does putenv, |
| 580 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 581 | #define setenv(...) setenv_is_leaky_dont_use() |
| 582 | struct variable { |
| 583 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 584 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 585 | #if ENABLE_HUSH_LOCAL |
| 586 | unsigned func_nest_level; |
| 587 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 588 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 589 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 590 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 591 | }; |
| 592 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 593 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 594 | BC_BREAK = 1, |
| 595 | BC_CONTINUE = 2, |
| 596 | }; |
| 597 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 598 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 599 | struct function { |
| 600 | struct function *next; |
| 601 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 602 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 603 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 604 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 605 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 606 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 607 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 608 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 609 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 611 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 612 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 613 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 614 | /* interactive_fd != 0 means we are an interactive shell. |
| 615 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 616 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 617 | * job control still works, but terminal signals |
| 618 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 619 | * process groups can only be created with "cmd &". |
| 620 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 621 | * to give tty to the foreground process group, |
| 622 | * and will take it back when the group is stopped (^Z) |
| 623 | * or killed (^C). |
| 624 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 625 | #if ENABLE_HUSH_INTERACTIVE |
| 626 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 627 | * _AND_ if we decided to act interactively */ |
| 628 | int interactive_fd; |
| 629 | const char *PS1; |
| 630 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 631 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 632 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 633 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 634 | #endif |
| 635 | #if ENABLE_FEATURE_EDITING |
| 636 | line_input_t *line_input_state; |
| 637 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 638 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 639 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 640 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 641 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 642 | random_t random_gen; |
| 643 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 644 | #if ENABLE_HUSH_JOB |
| 645 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 646 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 647 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 648 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 649 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 650 | #else |
| 651 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 652 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 653 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 654 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 655 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 656 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 657 | #if ENABLE_HUSH_FUNCTIONS |
| 658 | /* 0: outside of a function (or sourced file) |
| 659 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 660 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 661 | */ |
| 662 | smallint flag_return_in_progress; |
| 663 | #endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 664 | smallint n_mode; |
| 665 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 666 | smallint x_mode; |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 667 | # define G_x_mode G.x_mode |
| 668 | #else |
| 669 | # define G_x_mode 0 |
| 670 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 671 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 672 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 673 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 674 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 675 | smalluint global_args_malloced; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 676 | smalluint inherited_set_is_saved; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 677 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 678 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 679 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 680 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 681 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 682 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 683 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 684 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 685 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 686 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 687 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 688 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 689 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 690 | struct variable shell_ver; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 691 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 692 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 693 | # if ENABLE_HUSH_LOCAL |
| 694 | struct variable **shadowed_vars_pp; |
| 695 | unsigned func_nest_level; |
| 696 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 697 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 698 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 699 | #if ENABLE_HUSH_FAST |
| 700 | unsigned count_SIGCHLD; |
| 701 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 702 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 703 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 704 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 705 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 706 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 707 | sigset_t blocked_set; |
| 708 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 709 | #if HUSH_DEBUG |
| 710 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 711 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 712 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 713 | char user_input_buf[ENABLE_FEATURE_EDITING ? CONFIG_FEATURE_EDITING_MAX_LEN : 2]; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 714 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 715 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 716 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 717 | * (too many defines). Also, I actually prefer to see when a variable |
| 718 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 719 | #define INIT_G() do { \ |
| 720 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 721 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 722 | |
| 723 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 724 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 725 | static int builtin_cd(char **argv) FAST_FUNC; |
| 726 | static int builtin_echo(char **argv) FAST_FUNC; |
| 727 | static int builtin_eval(char **argv) FAST_FUNC; |
| 728 | static int builtin_exec(char **argv) FAST_FUNC; |
| 729 | static int builtin_exit(char **argv) FAST_FUNC; |
| 730 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 731 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 732 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 733 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 734 | #endif |
| 735 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 736 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 737 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 738 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 739 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 740 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 741 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 742 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 743 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 744 | #if ENABLE_PRINTF |
| 745 | static int builtin_printf(char **argv) FAST_FUNC; |
| 746 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 747 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 748 | static int builtin_read(char **argv) FAST_FUNC; |
| 749 | static int builtin_set(char **argv) FAST_FUNC; |
| 750 | static int builtin_shift(char **argv) FAST_FUNC; |
| 751 | static int builtin_source(char **argv) FAST_FUNC; |
| 752 | static int builtin_test(char **argv) FAST_FUNC; |
| 753 | static int builtin_trap(char **argv) FAST_FUNC; |
| 754 | static int builtin_type(char **argv) FAST_FUNC; |
| 755 | static int builtin_true(char **argv) FAST_FUNC; |
| 756 | static int builtin_umask(char **argv) FAST_FUNC; |
| 757 | static int builtin_unset(char **argv) FAST_FUNC; |
| 758 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 759 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 760 | static int builtin_break(char **argv) FAST_FUNC; |
| 761 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 762 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 763 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 764 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 765 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 766 | |
| 767 | /* Table of built-in functions. They can be forked or not, depending on |
| 768 | * context: within pipes, they fork. As simple commands, they do not. |
| 769 | * When used in non-forking context, they can change global variables |
| 770 | * in the parent shell process. If forked, of course they cannot. |
| 771 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 772 | * still be set at the end. */ |
| 773 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 774 | const char *b_cmd; |
| 775 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 776 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 777 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 778 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 779 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 780 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 781 | #endif |
| 782 | }; |
| 783 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 784 | static const struct built_in_command bltins1[] = { |
| 785 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 786 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 787 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 788 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 789 | #endif |
| 790 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 791 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 792 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 793 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 794 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 795 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 796 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 797 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 798 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 799 | BLTIN("exit" , builtin_exit , "Exit"), |
| 800 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 801 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 802 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 803 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 804 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 805 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 806 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 807 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 808 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 809 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 810 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 811 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 812 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 813 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 814 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 815 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 816 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 817 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 818 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 819 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 820 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 821 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 822 | #if ENABLE_HUSH_BASH_COMPAT |
| 823 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 824 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 825 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 826 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 827 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 828 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 829 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 830 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 831 | }; |
| 832 | /* For now, echo and test are unconditionally enabled. |
| 833 | * Maybe make it configurable? */ |
| 834 | static const struct built_in_command bltins2[] = { |
| 835 | BLTIN("[" , builtin_test , NULL), |
| 836 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 837 | #if ENABLE_PRINTF |
| 838 | BLTIN("printf" , builtin_printf , NULL), |
| 839 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 840 | BLTIN("pwd" , builtin_pwd , NULL), |
| 841 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 842 | }; |
| 843 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 844 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 845 | /* Debug printouts. |
| 846 | */ |
| 847 | #if HUSH_DEBUG |
| 848 | /* prevent disasters with G.debug_indent < 0 */ |
| 849 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 850 | # define debug_enter() (G.debug_indent++) |
| 851 | # define debug_leave() (G.debug_indent--) |
| 852 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 853 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 854 | # define debug_enter() ((void)0) |
| 855 | # define debug_leave() ((void)0) |
| 856 | #endif |
| 857 | |
| 858 | #ifndef debug_printf |
| 859 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 860 | #endif |
| 861 | |
| 862 | #ifndef debug_printf_parse |
| 863 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 864 | #endif |
| 865 | |
| 866 | #ifndef debug_printf_exec |
| 867 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 868 | #endif |
| 869 | |
| 870 | #ifndef debug_printf_env |
| 871 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 872 | #endif |
| 873 | |
| 874 | #ifndef debug_printf_jobs |
| 875 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 876 | # define DEBUG_JOBS 1 |
| 877 | #else |
| 878 | # define DEBUG_JOBS 0 |
| 879 | #endif |
| 880 | |
| 881 | #ifndef debug_printf_expand |
| 882 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 883 | # define DEBUG_EXPAND 1 |
| 884 | #else |
| 885 | # define DEBUG_EXPAND 0 |
| 886 | #endif |
| 887 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 888 | #ifndef debug_printf_varexp |
| 889 | # define debug_printf_varexp(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 890 | #endif |
| 891 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 892 | #ifndef debug_printf_glob |
| 893 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 894 | # define DEBUG_GLOB 1 |
| 895 | #else |
| 896 | # define DEBUG_GLOB 0 |
| 897 | #endif |
| 898 | |
| 899 | #ifndef debug_printf_list |
| 900 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 901 | #endif |
| 902 | |
| 903 | #ifndef debug_printf_subst |
| 904 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 905 | #endif |
| 906 | |
| 907 | #ifndef debug_printf_clean |
| 908 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 909 | # define DEBUG_CLEAN 1 |
| 910 | #else |
| 911 | # define DEBUG_CLEAN 0 |
| 912 | #endif |
| 913 | |
| 914 | #if DEBUG_EXPAND |
| 915 | static void debug_print_strings(const char *prefix, char **vv) |
| 916 | { |
| 917 | indent(); |
| 918 | fprintf(stderr, "%s:\n", prefix); |
| 919 | while (*vv) |
| 920 | fprintf(stderr, " '%s'\n", *vv++); |
| 921 | } |
| 922 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 923 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 924 | #endif |
| 925 | |
| 926 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 927 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 928 | */ |
| 929 | #if LEAK_HUNTING |
| 930 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 931 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 932 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 933 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 934 | return ptr; |
| 935 | } |
| 936 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 937 | { |
| 938 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 939 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 940 | return ptr; |
| 941 | } |
| 942 | static char *xxstrdup(int lineno, const char *str) |
| 943 | { |
| 944 | char *ptr = xstrdup(str); |
| 945 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 946 | return ptr; |
| 947 | } |
| 948 | static void xxfree(void *ptr) |
| 949 | { |
| 950 | fdprintf(2, "free %p\n", ptr); |
| 951 | free(ptr); |
| 952 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 953 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 954 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 955 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 956 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 957 | #endif |
| 958 | |
| 959 | |
| 960 | /* Syntax and runtime errors. They always abort scripts. |
| 961 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 962 | * and return to the prompt. |
| 963 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 964 | */ |
| 965 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 966 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 967 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 968 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 969 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 970 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 971 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 972 | #endif |
| 973 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 974 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 975 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 976 | va_list p; |
| 977 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 978 | #if HUSH_DEBUG >= 2 |
| 979 | bb_error_msg("hush.c:%u", lineno); |
| 980 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 981 | va_start(p, fmt); |
| 982 | bb_verror_msg(fmt, p, NULL); |
| 983 | va_end(p); |
| 984 | if (!G_interactive_fd) |
| 985 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 986 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 987 | |
| 988 | static void syntax_error(unsigned lineno, const char *msg) |
| 989 | { |
| 990 | if (msg) |
| 991 | die_if_script(lineno, "syntax error: %s", msg); |
| 992 | else |
| 993 | die_if_script(lineno, "syntax error", NULL); |
| 994 | } |
| 995 | |
| 996 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 997 | { |
| 998 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 999 | } |
| 1000 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1001 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 1002 | { |
| 1003 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 1004 | } |
| 1005 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1006 | /* It so happens that all such cases are totally fatal |
| 1007 | * even if shell is interactive: EOF while looking for closing |
| 1008 | * delimiter. There is nowhere to read stuff from after that, |
| 1009 | * it's EOF! The only choice is to terminate. |
| 1010 | */ |
| 1011 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1012 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1013 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1014 | char msg[2] = { ch, '\0' }; |
| 1015 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1016 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1019 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1020 | { |
| 1021 | char msg[2]; |
| 1022 | msg[0] = ch; |
| 1023 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1024 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1027 | #if HUSH_DEBUG < 2 |
| 1028 | # undef die_if_script |
| 1029 | # undef syntax_error |
| 1030 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1031 | # undef syntax_error_unterm_ch |
| 1032 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1033 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1034 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1035 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1036 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1037 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1038 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1039 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1040 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1041 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1042 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1043 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1044 | #if ENABLE_HUSH_INTERACTIVE |
| 1045 | static void cmdedit_update_prompt(void); |
| 1046 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1047 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1048 | #endif |
| 1049 | |
| 1050 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1051 | /* Utility functions |
| 1052 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1053 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1054 | static char *unbackslash(char *src) |
| 1055 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1056 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1057 | while (1) { |
| 1058 | if (*src == '\\') |
| 1059 | src++; |
| 1060 | if ((*dst++ = *src++) == '\0') |
| 1061 | break; |
| 1062 | } |
| 1063 | return dst; |
| 1064 | } |
| 1065 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1066 | static char **add_strings_to_strings(char **strings, char **add, int need_to_dup) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1067 | { |
| 1068 | int i; |
| 1069 | unsigned count1; |
| 1070 | unsigned count2; |
| 1071 | char **v; |
| 1072 | |
| 1073 | v = strings; |
| 1074 | count1 = 0; |
| 1075 | if (v) { |
| 1076 | while (*v) { |
| 1077 | count1++; |
| 1078 | v++; |
| 1079 | } |
| 1080 | } |
| 1081 | count2 = 0; |
| 1082 | v = add; |
| 1083 | while (*v) { |
| 1084 | count2++; |
| 1085 | v++; |
| 1086 | } |
| 1087 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1088 | v[count1 + count2] = NULL; |
| 1089 | i = count2; |
| 1090 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1091 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1092 | return v; |
| 1093 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1094 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1095 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1096 | { |
| 1097 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1098 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1099 | return ptr; |
| 1100 | } |
| 1101 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1102 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1103 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1104 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1105 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1106 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1107 | { |
| 1108 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1109 | v[0] = add; |
| 1110 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1111 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1112 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1113 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1114 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1115 | { |
| 1116 | char **ptr = add_string_to_strings(strings, add); |
| 1117 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1118 | return ptr; |
| 1119 | } |
| 1120 | #define add_string_to_strings(strings, add) \ |
| 1121 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1122 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1123 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1124 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1125 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1126 | char **v; |
| 1127 | |
| 1128 | if (!strings) |
| 1129 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1130 | v = strings; |
| 1131 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1132 | free(*v); |
| 1133 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1134 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1135 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1138 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1139 | /* Helpers for setting new $n and restoring them back |
| 1140 | */ |
| 1141 | typedef struct save_arg_t { |
| 1142 | char *sv_argv0; |
| 1143 | char **sv_g_argv; |
| 1144 | int sv_g_argc; |
| 1145 | smallint sv_g_malloced; |
| 1146 | } save_arg_t; |
| 1147 | |
| 1148 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1149 | { |
| 1150 | int n; |
| 1151 | |
| 1152 | sv->sv_argv0 = argv[0]; |
| 1153 | sv->sv_g_argv = G.global_argv; |
| 1154 | sv->sv_g_argc = G.global_argc; |
| 1155 | sv->sv_g_malloced = G.global_args_malloced; |
| 1156 | |
| 1157 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1158 | G.global_argv = argv; |
| 1159 | G.global_args_malloced = 0; |
| 1160 | |
| 1161 | n = 1; |
| 1162 | while (*++argv) |
| 1163 | n++; |
| 1164 | G.global_argc = n; |
| 1165 | } |
| 1166 | |
| 1167 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1168 | { |
| 1169 | char **pp; |
| 1170 | |
| 1171 | if (G.global_args_malloced) { |
| 1172 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1173 | pp = G.global_argv; |
| 1174 | while (*++pp) /* note: does not free $0 */ |
| 1175 | free(*pp); |
| 1176 | free(G.global_argv); |
| 1177 | } |
| 1178 | argv[0] = sv->sv_argv0; |
| 1179 | G.global_argv = sv->sv_g_argv; |
| 1180 | G.global_argc = sv->sv_g_argc; |
| 1181 | G.global_args_malloced = sv->sv_g_malloced; |
| 1182 | } |
| 1183 | |
| 1184 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1185 | /* Basic theory of signal handling in shell |
| 1186 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1187 | * This does not describe what hush does, rather, it is current understanding |
| 1188 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1189 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1190 | * |
| 1191 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1192 | * is finished or backgrounded. It is the same in interactive and |
| 1193 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1194 | * a user trap handler is installed or a shell special one is in effect. |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1195 | * ^C or ^Z from keyboard seems to execute "at once" because it usually |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1196 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1197 | * pipe. |
| 1198 | * |
| 1199 | * Wait builtin in interruptible by signals for which user trap is set |
| 1200 | * or by SIGINT in interactive shell. |
| 1201 | * |
| 1202 | * Trap handlers will execute even within trap handlers. (right?) |
| 1203 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1204 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1205 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1206 | * |
| 1207 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1208 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1209 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1210 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1211 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1212 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1213 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1214 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1215 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1216 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1217 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1218 | * |
| 1219 | * SIGQUIT: ignore |
| 1220 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1221 | * SIGHUP (interactive): |
| 1222 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1223 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1224 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1225 | * that all pipe members are stopped. Try this in bash: |
| 1226 | * while :; do :; done - ^Z does not background it |
| 1227 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1228 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1229 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1230 | * to interactive shell while shell is waiting for a pipe, |
| 1231 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1232 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1233 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1234 | * Example 2: this does not wait and does not execute ls: |
| 1235 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1236 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1237 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1238 | * |
| 1239 | * (What happens to signals which are IGN on shell start?) |
| 1240 | * (What happens with signal mask on shell start?) |
| 1241 | * |
| 1242 | * Implementation in hush |
| 1243 | * ====================== |
| 1244 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1245 | * We block all signals which we don't want to take action immediately, |
| 1246 | * i.e. we block all signals which need to have special handling as described |
| 1247 | * above, and all signals which have traps set. |
| 1248 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1249 | * and act on them. |
| 1250 | * |
| 1251 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1252 | * sigset_t blocked_set: current blocked signal set |
| 1253 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1254 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1255 | * clear bit in blocked_set unless it is also in non_DFL_mask |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1256 | * "trap 'cmd' SIGxxx": |
| 1257 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1258 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1259 | * unblock signals with special interactive handling |
| 1260 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1261 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1262 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1263 | * POSIX says fork clears pending signal mask in child - no need to clear it. |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1264 | * Restore blocked signal set to one inherited by shell just prior to exec. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1265 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1266 | * Note: as a result, we do not use signal handlers much. The only uses |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1267 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1268 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1269 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1270 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1271 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1272 | * are set to the default actions". bash interprets it so that traps which |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1273 | * are set to '' (ignore) are NOT reset to defaults. We do the same. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1274 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1275 | enum { |
| 1276 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1277 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1278 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1279 | | (1 << SIGHUP) |
| 1280 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1281 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1282 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1283 | | (1 << SIGTTIN) |
| 1284 | | (1 << SIGTTOU) |
| 1285 | | (1 << SIGTSTP) |
| 1286 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1287 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1288 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1289 | #if ENABLE_HUSH_FAST |
| 1290 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1291 | { |
| 1292 | G.count_SIGCHLD++; |
| 1293 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1294 | } |
| 1295 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1296 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1297 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1298 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1299 | /* After [v]fork, in child: do not restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1300 | # define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1301 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1302 | # define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1303 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1304 | /* Restores tty foreground process group, and exits. |
| 1305 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1306 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1307 | * or called directly with -EXITCODE. |
| 1308 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1309 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1310 | static void sigexit(int sig) |
| 1311 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1312 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1313 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1314 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1315 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1316 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1317 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1318 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1319 | |
| 1320 | /* Not a signal, just exit */ |
| 1321 | if (sig <= 0) |
| 1322 | _exit(- sig); |
| 1323 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1324 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1325 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1326 | #else |
| 1327 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1328 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1329 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1330 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1331 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1332 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1333 | /* Restores tty foreground process group, and exits. */ |
| 1334 | static void hush_exit(int exitcode) NORETURN; |
| 1335 | static void hush_exit(int exitcode) |
| 1336 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1337 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1338 | /* Prevent recursion: |
| 1339 | * trap "echo Hi; exit" EXIT; exit |
| 1340 | */ |
| 1341 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1342 | G.traps[0] = NULL; |
| 1343 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1344 | builtin_eval(argv); |
| 1345 | free(argv[1]); |
| 1346 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1347 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1348 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1349 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1350 | sigexit(- (exitcode & 0xff)); |
| 1351 | #else |
| 1352 | exit(exitcode); |
| 1353 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1356 | static int check_and_run_traps(int sig) |
| 1357 | { |
Dan Fandrich | fdd7b56 | 2010-06-18 22:37:42 -0700 | [diff] [blame] | 1358 | static const struct timespec zero_timespec; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1359 | smalluint save_rcode; |
| 1360 | int last_sig = 0; |
| 1361 | |
| 1362 | if (sig) |
| 1363 | goto jump_in; |
| 1364 | while (1) { |
| 1365 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1366 | if (sig <= 0) |
| 1367 | break; |
| 1368 | jump_in: |
| 1369 | last_sig = sig; |
| 1370 | if (G.traps && G.traps[sig]) { |
| 1371 | if (G.traps[sig][0]) { |
| 1372 | /* We have user-defined handler */ |
| 1373 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1374 | save_rcode = G.last_exitcode; |
| 1375 | builtin_eval(argv); |
| 1376 | free(argv[1]); |
| 1377 | G.last_exitcode = save_rcode; |
| 1378 | } /* else: "" trap, ignoring signal */ |
| 1379 | continue; |
| 1380 | } |
| 1381 | /* not a trap: special action */ |
| 1382 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1383 | #if ENABLE_HUSH_FAST |
| 1384 | case SIGCHLD: |
| 1385 | G.count_SIGCHLD++; |
| 1386 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1387 | break; |
| 1388 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1389 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1390 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1391 | bb_putchar('\n'); |
| 1392 | G.flag_SIGINT = 1; |
| 1393 | break; |
| 1394 | #if ENABLE_HUSH_JOB |
| 1395 | case SIGHUP: { |
| 1396 | struct pipe *job; |
| 1397 | /* bash is observed to signal whole process groups, |
| 1398 | * not individual processes */ |
| 1399 | for (job = G.job_list; job; job = job->next) { |
| 1400 | if (job->pgrp <= 0) |
| 1401 | continue; |
| 1402 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1403 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1404 | kill(- job->pgrp, SIGCONT); |
| 1405 | } |
| 1406 | sigexit(SIGHUP); |
| 1407 | } |
| 1408 | #endif |
| 1409 | default: /* ignored: */ |
| 1410 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1411 | break; |
| 1412 | } |
| 1413 | } |
| 1414 | return last_sig; |
| 1415 | } |
| 1416 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1417 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1418 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1419 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1420 | if (force || G.cwd == NULL) { |
| 1421 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1422 | * we must not try to free(bb_msg_unknown) */ |
| 1423 | if (G.cwd == bb_msg_unknown) |
| 1424 | G.cwd = NULL; |
| 1425 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1426 | if (!G.cwd) |
| 1427 | G.cwd = bb_msg_unknown; |
| 1428 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1429 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1432 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1433 | /* |
| 1434 | * Shell and environment variable support |
| 1435 | */ |
| 1436 | static struct variable **get_ptr_to_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1437 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1438 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1439 | struct variable *cur; |
| 1440 | int len; |
| 1441 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1442 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1443 | pp = &G.top_var; |
| 1444 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1445 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1446 | return pp; |
| 1447 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1448 | } |
| 1449 | return NULL; |
| 1450 | } |
| 1451 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1452 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1453 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1454 | struct variable **pp = get_ptr_to_local_var(name); |
| 1455 | if (pp) |
| 1456 | return *pp; |
| 1457 | return NULL; |
| 1458 | } |
| 1459 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1460 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1461 | { |
| 1462 | struct variable **pp = get_ptr_to_local_var(name); |
| 1463 | if (pp) |
| 1464 | return strchr((*pp)->varstr, '=') + 1; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1465 | if (strcmp(name, "PPID") == 0) |
| 1466 | return utoa(G.root_ppid); |
| 1467 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1468 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1469 | if (strcmp(name, "RANDOM") == 0) { |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1470 | return utoa(next_random(&G.random_gen)); |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1471 | } |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1472 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1473 | return NULL; |
| 1474 | } |
| 1475 | |
| 1476 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1477 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1478 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1479 | * 0: do not change export flag |
| 1480 | * (if creating new variable, flag will be 0) |
| 1481 | * 1: set export flag and putenv the variable |
| 1482 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1483 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1484 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1485 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1486 | /* all params are used */ |
| 1487 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1488 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1489 | set_local_var(str, flg_export, local_lvl) |
| 1490 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1491 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1492 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1493 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1494 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1495 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1496 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1497 | static int set_local_var(char *str, int flg_export, int local_lvl, int flg_read_only) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1498 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1499 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1500 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1501 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1502 | int name_len; |
| 1503 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1504 | eq_sign = strchr(str, '='); |
| 1505 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1506 | free(str); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1510 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1511 | var_pp = &G.top_var; |
| 1512 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1513 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1514 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1515 | continue; |
| 1516 | } |
| 1517 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1518 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1519 | #if !BB_MMU |
| 1520 | if (!flg_read_only) |
| 1521 | #endif |
| 1522 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1523 | free(str); |
| 1524 | return -1; |
| 1525 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1526 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1527 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1528 | *eq_sign = '\0'; |
| 1529 | unsetenv(str); |
| 1530 | *eq_sign = '='; |
| 1531 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1532 | #if ENABLE_HUSH_LOCAL |
| 1533 | if (cur->func_nest_level < local_lvl) { |
| 1534 | /* New variable is declared as local, |
| 1535 | * and existing one is global, or local |
| 1536 | * from enclosing function. |
| 1537 | * Remove and save old one: */ |
| 1538 | *var_pp = cur->next; |
| 1539 | cur->next = *G.shadowed_vars_pp; |
| 1540 | *G.shadowed_vars_pp = cur; |
| 1541 | /* bash 3.2.33(1) and exported vars: |
| 1542 | * # export z=z |
| 1543 | * # f() { local z=a; env | grep ^z; } |
| 1544 | * # f |
| 1545 | * z=a |
| 1546 | * # env | grep ^z |
| 1547 | * z=z |
| 1548 | */ |
| 1549 | if (cur->flg_export) |
| 1550 | flg_export = 1; |
| 1551 | break; |
| 1552 | } |
| 1553 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1554 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1555 | free_and_exp: |
| 1556 | free(str); |
| 1557 | goto exp; |
| 1558 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1559 | if (cur->max_len != 0) { |
| 1560 | if (cur->max_len >= strlen(str)) { |
| 1561 | /* This one is from startup env, reuse space */ |
| 1562 | strcpy(cur->varstr, str); |
| 1563 | goto free_and_exp; |
| 1564 | } |
| 1565 | } else { |
| 1566 | /* max_len == 0 signifies "malloced" var, which we can |
| 1567 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1568 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1569 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1570 | cur->max_len = 0; |
| 1571 | goto set_str_and_exp; |
| 1572 | } |
| 1573 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1574 | /* Not found - create new variable struct */ |
| 1575 | cur = xzalloc(sizeof(*cur)); |
| 1576 | #if ENABLE_HUSH_LOCAL |
| 1577 | cur->func_nest_level = local_lvl; |
| 1578 | #endif |
| 1579 | cur->next = *var_pp; |
| 1580 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1581 | |
| 1582 | set_str_and_exp: |
| 1583 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1584 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1585 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1586 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1587 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1588 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1589 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1590 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1591 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1592 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1593 | if (flg_export == -1) { |
| 1594 | cur->flg_export = 0; |
| 1595 | /* unsetenv was already done */ |
| 1596 | } else { |
| 1597 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1598 | return putenv(cur->varstr); |
| 1599 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1600 | } |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1604 | /* Used at startup and after each cd */ |
| 1605 | static void set_pwd_var(int exp) |
| 1606 | { |
| 1607 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1608 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1609 | } |
| 1610 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1611 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1612 | { |
| 1613 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1614 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1617 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1618 | var_pp = &G.top_var; |
| 1619 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1620 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1621 | if (cur->flg_read_only) { |
| 1622 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1623 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1624 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1625 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1626 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1627 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1628 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1629 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1630 | if (!cur->max_len) |
| 1631 | free(cur->varstr); |
| 1632 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1633 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1634 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1635 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1636 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1637 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1640 | static int unset_local_var(const char *name) |
| 1641 | { |
| 1642 | return unset_local_var_len(name, strlen(name)); |
| 1643 | } |
| 1644 | |
| 1645 | static void unset_vars(char **strings) |
| 1646 | { |
| 1647 | char **v; |
| 1648 | |
| 1649 | if (!strings) |
| 1650 | return; |
| 1651 | v = strings; |
| 1652 | while (*v) { |
| 1653 | const char *eq = strchrnul(*v, '='); |
| 1654 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1655 | v++; |
| 1656 | } |
| 1657 | free(strings); |
| 1658 | } |
| 1659 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1660 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1661 | # define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1662 | # define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1663 | static char* FAST_FUNC endofname(const char *name) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1664 | { |
| 1665 | char *p; |
| 1666 | |
| 1667 | p = (char *) name; |
| 1668 | if (!is_name(*p)) |
| 1669 | return p; |
| 1670 | while (*++p) { |
| 1671 | if (!is_in_name(*p)) |
| 1672 | break; |
| 1673 | } |
| 1674 | return p; |
| 1675 | } |
Denys Vlasenko | 6b01b71 | 2010-01-24 22:52:21 +0100 | [diff] [blame] | 1676 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1677 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1678 | static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1679 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1680 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1681 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1682 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1683 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1684 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1685 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1686 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1687 | */ |
| 1688 | static void add_vars(struct variable *var) |
| 1689 | { |
| 1690 | struct variable *next; |
| 1691 | |
| 1692 | while (var) { |
| 1693 | next = var->next; |
| 1694 | var->next = G.top_var; |
| 1695 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1696 | if (var->flg_export) { |
| 1697 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1698 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1699 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1700 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1701 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1702 | var = next; |
| 1703 | } |
| 1704 | } |
| 1705 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1706 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1707 | { |
| 1708 | char **s; |
| 1709 | struct variable *old = NULL; |
| 1710 | |
| 1711 | if (!strings) |
| 1712 | return old; |
| 1713 | s = strings; |
| 1714 | while (*s) { |
| 1715 | struct variable *var_p; |
| 1716 | struct variable **var_pp; |
| 1717 | char *eq; |
| 1718 | |
| 1719 | eq = strchr(*s, '='); |
| 1720 | if (eq) { |
| 1721 | *eq = '\0'; |
| 1722 | var_pp = get_ptr_to_local_var(*s); |
| 1723 | *eq = '='; |
| 1724 | if (var_pp) { |
| 1725 | /* Remove variable from global linked list */ |
| 1726 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1727 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1728 | *var_pp = var_p->next; |
| 1729 | /* Add it to returned list */ |
| 1730 | var_p->next = old; |
| 1731 | old = var_p; |
| 1732 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1733 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1734 | } |
| 1735 | s++; |
| 1736 | } |
| 1737 | return old; |
| 1738 | } |
| 1739 | |
| 1740 | |
| 1741 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1742 | * in_str support |
| 1743 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1744 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1745 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1746 | int ch = *i->p; |
| 1747 | if (ch != '\0') { |
| 1748 | i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1749 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1750 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1751 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1754 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1755 | { |
| 1756 | return *i->p; |
| 1757 | } |
| 1758 | |
| 1759 | #if ENABLE_HUSH_INTERACTIVE |
| 1760 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1761 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1762 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1763 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1764 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1765 | if (G.PS1 == NULL) |
| 1766 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1767 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1768 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1769 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1770 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1771 | if (G.PS2 == NULL) |
| 1772 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1773 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1774 | |
| 1775 | static const char* setup_prompt_string(int promptmode) |
| 1776 | { |
| 1777 | const char *prompt_str; |
| 1778 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1779 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1780 | /* Set up the prompt */ |
| 1781 | if (promptmode == 0) { /* PS1 */ |
| 1782 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1783 | /* bash uses $PWD value, even if it is set by user. |
| 1784 | * It uses current dir only if PWD is unset. |
| 1785 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1786 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1787 | prompt_str = G.PS1; |
| 1788 | } else |
| 1789 | prompt_str = G.PS2; |
| 1790 | } else |
| 1791 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1792 | debug_printf("result '%s'\n", prompt_str); |
| 1793 | return prompt_str; |
| 1794 | } |
| 1795 | |
| 1796 | static void get_user_input(struct in_str *i) |
| 1797 | { |
| 1798 | int r; |
| 1799 | const char *prompt_str; |
| 1800 | |
| 1801 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1802 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1803 | /* Enable command line editing only while a command line |
| 1804 | * is actually being read */ |
| 1805 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1806 | G.flag_SIGINT = 0; |
| 1807 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1808 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1809 | r = read_line_input(prompt_str, G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1, G.line_input_state); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1810 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1811 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1812 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1813 | i->eof_flag = (r < 0); |
| 1814 | if (i->eof_flag) { /* EOF/error detected */ |
| 1815 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1816 | G.user_input_buf[1] = '\0'; |
| 1817 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1818 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1819 | do { |
| 1820 | G.flag_SIGINT = 0; |
| 1821 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1822 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1823 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1824 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1825 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1826 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1827 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1828 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1829 | i->p = G.user_input_buf; |
| 1830 | } |
| 1831 | |
| 1832 | #endif /* INTERACTIVE */ |
| 1833 | |
| 1834 | /* This is the magic location that prints prompts |
| 1835 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1836 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1837 | { |
| 1838 | int ch; |
| 1839 | |
| 1840 | /* If there is data waiting, eat it up */ |
| 1841 | if (i->p && *i->p) { |
| 1842 | #if ENABLE_HUSH_INTERACTIVE |
| 1843 | take_cached: |
| 1844 | #endif |
| 1845 | ch = *i->p++; |
| 1846 | if (i->eof_flag && !*i->p) |
| 1847 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1848 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1849 | } else { |
| 1850 | /* need to double check i->file because we might be doing something |
| 1851 | * more complicated by now, like sourcing or substituting. */ |
| 1852 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1853 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1854 | do { |
| 1855 | get_user_input(i); |
| 1856 | } while (!*i->p); /* need non-empty line */ |
| 1857 | i->promptmode = 1; /* PS2 */ |
| 1858 | i->promptme = 0; |
| 1859 | goto take_cached; |
| 1860 | } |
| 1861 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1862 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1863 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1864 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1865 | #if ENABLE_HUSH_INTERACTIVE |
| 1866 | if (ch == '\n') |
| 1867 | i->promptme = 1; |
| 1868 | #endif |
| 1869 | return ch; |
| 1870 | } |
| 1871 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1872 | /* All callers guarantee this routine will never |
| 1873 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1874 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1875 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1876 | { |
| 1877 | int ch; |
| 1878 | if (i->p && *i->p) { |
| 1879 | if (i->eof_flag && !i->p[1]) |
| 1880 | return EOF; |
| 1881 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1882 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1883 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1884 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1885 | i->eof_flag = (ch == EOF); |
| 1886 | i->peek_buf[0] = ch; |
| 1887 | i->peek_buf[1] = '\0'; |
| 1888 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1889 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1890 | return ch; |
| 1891 | } |
| 1892 | |
| 1893 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1894 | { |
| 1895 | i->peek = file_peek; |
| 1896 | i->get = file_get; |
| 1897 | #if ENABLE_HUSH_INTERACTIVE |
| 1898 | i->promptme = 1; |
| 1899 | i->promptmode = 0; /* PS1 */ |
| 1900 | #endif |
| 1901 | i->file = f; |
| 1902 | i->p = NULL; |
| 1903 | } |
| 1904 | |
| 1905 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1906 | { |
| 1907 | i->peek = static_peek; |
| 1908 | i->get = static_get; |
| 1909 | #if ENABLE_HUSH_INTERACTIVE |
| 1910 | i->promptme = 1; |
| 1911 | i->promptmode = 0; /* PS1 */ |
| 1912 | #endif |
| 1913 | i->p = s; |
| 1914 | i->eof_flag = 0; |
| 1915 | } |
| 1916 | |
| 1917 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1918 | /* |
| 1919 | * o_string support |
| 1920 | */ |
| 1921 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1922 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1923 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1924 | { |
| 1925 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1926 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1927 | if (o->data) |
| 1928 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1931 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1932 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1933 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1934 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1937 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1938 | { |
| 1939 | free(o->data); |
| 1940 | } |
| 1941 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1942 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1943 | { |
| 1944 | if (o->length + len > o->maxlen) { |
| 1945 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1946 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1947 | } |
| 1948 | } |
| 1949 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1950 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1951 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1952 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1953 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1954 | o->data[o->length] = ch; |
| 1955 | o->length++; |
| 1956 | o->data[o->length] = '\0'; |
| 1957 | } |
| 1958 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1959 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1960 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1961 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1962 | memcpy(&o->data[o->length], str, len); |
| 1963 | o->length += len; |
| 1964 | o->data[o->length] = '\0'; |
| 1965 | } |
| 1966 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1967 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1968 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1969 | o_addblock(o, str, strlen(str)); |
| 1970 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 1971 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1972 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1973 | static void nommu_addchr(o_string *o, int ch) |
| 1974 | { |
| 1975 | if (o) |
| 1976 | o_addchr(o, ch); |
| 1977 | } |
| 1978 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1979 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1980 | #endif |
| 1981 | |
| 1982 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1983 | { |
| 1984 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1987 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1988 | { |
| 1989 | while (len) { |
| 1990 | o_addchr(o, *str); |
| 1991 | if (*str++ == '\\' |
| 1992 | && (*str != '*' && *str != '?' && *str != '[') |
| 1993 | ) { |
| 1994 | o_addchr(o, '\\'); |
| 1995 | } |
| 1996 | len--; |
| 1997 | } |
| 1998 | } |
| 1999 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2000 | #undef HUSH_BRACE_EXP |
| 2001 | /* |
| 2002 | * HUSH_BRACE_EXP code needs corresponding quoting on variable expansion side. |
| 2003 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2004 | * Apparently, on unquoted $v bash still does globbing |
| 2005 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2006 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2007 | * quoting mechanisms on $v expansion side: one protects |
| 2008 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2009 | * We have only second one. |
| 2010 | */ |
| 2011 | |
| 2012 | #ifdef HUSH_BRACE_EXP |
| 2013 | # define MAYBE_BRACES "{}" |
| 2014 | #else |
| 2015 | # define MAYBE_BRACES "" |
| 2016 | #endif |
| 2017 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2018 | /* My analysis of quoting semantics tells me that state information |
| 2019 | * is associated with a destination, not a source. |
| 2020 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2021 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2022 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2023 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2024 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2025 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2026 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2027 | o_grow_by(o, sz); |
| 2028 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2029 | o->data[o->length] = '\\'; |
| 2030 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2031 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2032 | o->data[o->length] = ch; |
| 2033 | o->length++; |
| 2034 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2037 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2038 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2039 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2040 | if (o->o_escape && strchr("*?[\\" MAYBE_BRACES, ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2041 | sz++; |
| 2042 | o->data[o->length] = '\\'; |
| 2043 | o->length++; |
| 2044 | } |
| 2045 | o_grow_by(o, sz); |
| 2046 | o->data[o->length] = ch; |
| 2047 | o->length++; |
| 2048 | o->data[o->length] = '\0'; |
| 2049 | } |
| 2050 | |
| 2051 | static void o_addQstr(o_string *o, const char *str, int len) |
| 2052 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2053 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2054 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2055 | return; |
| 2056 | } |
| 2057 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2058 | char ch; |
| 2059 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2060 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2061 | if (ordinary_cnt > len) /* paranoia */ |
| 2062 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2063 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2064 | if (ordinary_cnt == len) |
| 2065 | return; |
| 2066 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2067 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2068 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2069 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2070 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2071 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2072 | sz++; |
| 2073 | o->data[o->length] = '\\'; |
| 2074 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2075 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2076 | o_grow_by(o, sz); |
| 2077 | o->data[o->length] = ch; |
| 2078 | o->length++; |
| 2079 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2083 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2084 | * It contains char* list[] at the beginning, which is grown in 16 element |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2085 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2086 | * list[i] contains an INDEX (int!) into this string data. |
| 2087 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2088 | * but list[i]'s need not be modified. |
| 2089 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2090 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2091 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2092 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2093 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2094 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2095 | { |
| 2096 | char **list = (char**)o->data; |
| 2097 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2098 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2099 | |
| 2100 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2101 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 2102 | prefix, list, n, string_start, o->length, o->maxlen); |
| 2103 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2104 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2105 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 2106 | o->data + (int)list[i] + string_start, |
| 2107 | o->data + (int)list[i] + string_start); |
| 2108 | i++; |
| 2109 | } |
| 2110 | if (n) { |
| 2111 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2112 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2113 | fprintf(stderr, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2114 | } |
| 2115 | } |
| 2116 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2117 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2118 | #endif |
| 2119 | |
| 2120 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2121 | * in list[n] so that it points past last stored byte so far. |
| 2122 | * It returns n+1. */ |
| 2123 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2124 | { |
| 2125 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2126 | int string_start; |
| 2127 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2128 | |
| 2129 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2130 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2131 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2132 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2133 | debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2134 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2135 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2136 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2137 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2138 | memmove(list + n + 0x10, list + n, string_len); |
| 2139 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2140 | } else { |
| 2141 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2142 | n, string_len, string_start); |
| 2143 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2144 | } else { |
| 2145 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2146 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2147 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2148 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2149 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2150 | o->has_empty_slot = 0; |
| 2151 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2152 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2153 | return n + 1; |
| 2154 | } |
| 2155 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2156 | /* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2157 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2158 | { |
| 2159 | char **list = (char**)o->data; |
| 2160 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2161 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2162 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2165 | #ifdef HUSH_BRACE_EXP |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2166 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2167 | * first, it processes even {a} (no commas), second, |
| 2168 | * I didn't manage to make it return strings when they don't match |
Denys Vlasenko | 160746b | 2009-11-16 05:51:18 +0100 | [diff] [blame] | 2169 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2170 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2171 | |
| 2172 | /* Helper */ |
| 2173 | static int glob_needed(const char *s) |
| 2174 | { |
| 2175 | while (*s) { |
| 2176 | if (*s == '\\') { |
| 2177 | if (!s[1]) |
| 2178 | return 0; |
| 2179 | s += 2; |
| 2180 | continue; |
| 2181 | } |
| 2182 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2183 | return 1; |
| 2184 | s++; |
| 2185 | } |
| 2186 | return 0; |
| 2187 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2188 | /* Return pointer to next closing brace or to comma */ |
| 2189 | static const char *next_brace_sub(const char *cp) |
| 2190 | { |
| 2191 | unsigned depth = 0; |
| 2192 | cp++; |
| 2193 | while (*cp != '\0') { |
| 2194 | if (*cp == '\\') { |
| 2195 | if (*++cp == '\0') |
| 2196 | break; |
| 2197 | cp++; |
| 2198 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2199 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2200 | /*{*/ if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
| 2201 | break; |
| 2202 | if (*cp++ == '{') /*}*/ |
| 2203 | depth++; |
| 2204 | } |
| 2205 | |
| 2206 | return *cp != '\0' ? cp : NULL; |
| 2207 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2208 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2209 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2210 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2211 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2212 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2213 | const char *next; |
| 2214 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2215 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2216 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2217 | |
| 2218 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2219 | |
| 2220 | begin = pattern; |
| 2221 | while (1) { |
| 2222 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2223 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2224 | if (*begin == '{') /*}*/ { |
| 2225 | /* Find the first sub-pattern and at the same time |
| 2226 | * find the rest after the closing brace */ |
| 2227 | next = next_brace_sub(begin); |
| 2228 | if (next == NULL) { |
| 2229 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2230 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2231 | } |
| 2232 | /*{*/ if (*next == '}') { |
| 2233 | /* "{abc}" with no commas - illegal |
| 2234 | * brace expr, disregard and skip it */ |
| 2235 | begin = next + 1; |
| 2236 | continue; |
| 2237 | } |
| 2238 | break; |
| 2239 | } |
| 2240 | if (*begin == '\\' && begin[1] != '\0') |
| 2241 | begin++; |
| 2242 | begin++; |
| 2243 | } |
| 2244 | debug_printf_glob("begin:%s\n", begin); |
| 2245 | debug_printf_glob("next:%s\n", next); |
| 2246 | |
| 2247 | /* Now find the end of the whole brace expression */ |
| 2248 | rest = next; |
| 2249 | /*{*/ while (*rest != '}') { |
| 2250 | rest = next_brace_sub(rest); |
| 2251 | if (rest == NULL) { |
| 2252 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2253 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2254 | } |
| 2255 | debug_printf_glob("rest:%s\n", rest); |
| 2256 | } |
| 2257 | rest_len = strlen(++rest) + 1; |
| 2258 | |
| 2259 | /* We are sure the brace expression is well-formed */ |
| 2260 | |
| 2261 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2262 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2263 | |
| 2264 | /* We have a brace expression. BEGIN points to the opening {, |
| 2265 | * NEXT points past the terminator of the first element, and REST |
| 2266 | * points past the final }. We will accumulate result names from |
| 2267 | * recursive runs for each brace alternative in the buffer using |
| 2268 | * GLOB_APPEND. */ |
| 2269 | |
| 2270 | p = begin + 1; |
| 2271 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2272 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2273 | memcpy( |
| 2274 | mempcpy( |
| 2275 | mempcpy(new_pattern_buf, |
| 2276 | /* We know the prefix for all sub-patterns */ |
| 2277 | pattern, begin - pattern), |
| 2278 | p, next - p), |
| 2279 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2280 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2281 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2282 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2283 | */ |
| 2284 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2285 | /*{*/ if (*next == '}') { |
| 2286 | /* We saw the last entry */ |
| 2287 | break; |
| 2288 | } |
| 2289 | p = next + 1; |
| 2290 | next = next_brace_sub(next); |
| 2291 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2292 | free(new_pattern_buf); |
| 2293 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2294 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2295 | simple_glob: |
| 2296 | { |
| 2297 | int gr; |
| 2298 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2299 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2300 | memset(&globdata, 0, sizeof(globdata)); |
| 2301 | gr = glob(pattern, 0, NULL, &globdata); |
| 2302 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2303 | if (gr != 0) { |
| 2304 | if (gr == GLOB_NOMATCH) { |
| 2305 | globfree(&globdata); |
| 2306 | /* NB: garbles parameter */ |
| 2307 | unbackslash(pattern); |
| 2308 | o_addstr_with_NUL(o, pattern); |
| 2309 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2310 | return o_save_ptr_helper(o, n); |
| 2311 | } |
| 2312 | if (gr == GLOB_NOSPACE) |
| 2313 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2314 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2315 | * but we didn't specify it. Paranoia again. */ |
| 2316 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2317 | } |
| 2318 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2319 | char **argv = globdata.gl_pathv; |
| 2320 | while (1) { |
| 2321 | o_addstr_with_NUL(o, *argv); |
| 2322 | n = o_save_ptr_helper(o, n); |
| 2323 | argv++; |
| 2324 | if (!*argv) |
| 2325 | break; |
| 2326 | } |
| 2327 | } |
| 2328 | globfree(&globdata); |
| 2329 | } |
| 2330 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2331 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2332 | /* Performs globbing on last list[], |
| 2333 | * saving each result as a new list[]. |
| 2334 | */ |
| 2335 | static int o_glob(o_string *o, int n) |
| 2336 | { |
| 2337 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2338 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2339 | debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data); |
| 2340 | if (!o->data) |
| 2341 | return o_save_ptr_helper(o, n); |
| 2342 | pattern = o->data + o_get_last_ptr(o, n); |
| 2343 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2344 | if (!glob_needed(pattern)) { |
| 2345 | /* unbackslash last string in o in place, fix length */ |
| 2346 | o->length = unbackslash(pattern) - o->data; |
| 2347 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2348 | return o_save_ptr_helper(o, n); |
| 2349 | } |
| 2350 | |
| 2351 | copy = xstrdup(pattern); |
| 2352 | /* "forget" pattern in o */ |
| 2353 | o->length = pattern - o->data; |
| 2354 | n = glob_brace(copy, o, n); |
| 2355 | free(copy); |
| 2356 | if (DEBUG_GLOB) |
| 2357 | debug_print_list("o_glob returning", o, n); |
| 2358 | return n; |
| 2359 | } |
| 2360 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2361 | #else /* !HUSH_BRACE_EXP */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2362 | |
| 2363 | /* Helper */ |
| 2364 | static int glob_needed(const char *s) |
| 2365 | { |
| 2366 | while (*s) { |
| 2367 | if (*s == '\\') { |
| 2368 | if (!s[1]) |
| 2369 | return 0; |
| 2370 | s += 2; |
| 2371 | continue; |
| 2372 | } |
| 2373 | if (*s == '*' || *s == '[' || *s == '?') |
| 2374 | return 1; |
| 2375 | s++; |
| 2376 | } |
| 2377 | return 0; |
| 2378 | } |
| 2379 | /* Performs globbing on last list[], |
| 2380 | * saving each result as a new list[]. |
| 2381 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2382 | static int o_glob(o_string *o, int n) |
| 2383 | { |
| 2384 | glob_t globdata; |
| 2385 | int gr; |
| 2386 | char *pattern; |
| 2387 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2388 | debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2389 | if (!o->data) |
| 2390 | return o_save_ptr_helper(o, n); |
| 2391 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2392 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2393 | if (!glob_needed(pattern)) { |
| 2394 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2395 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2396 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2397 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2398 | return o_save_ptr_helper(o, n); |
| 2399 | } |
| 2400 | |
| 2401 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2402 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2403 | * If we glob "*.\*" and don't find anything, we need |
| 2404 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2405 | * will return "*.\*"! |
| 2406 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2407 | gr = glob(pattern, 0, NULL, &globdata); |
| 2408 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2409 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2410 | if (gr == GLOB_NOMATCH) { |
| 2411 | globfree(&globdata); |
| 2412 | goto literal; |
| 2413 | } |
| 2414 | if (gr == GLOB_NOSPACE) |
| 2415 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2416 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2417 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2418 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2419 | } |
| 2420 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2421 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2422 | /* "forget" pattern in o */ |
| 2423 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2424 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2425 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2426 | n = o_save_ptr_helper(o, n); |
| 2427 | argv++; |
| 2428 | if (!*argv) |
| 2429 | break; |
| 2430 | } |
| 2431 | } |
| 2432 | globfree(&globdata); |
| 2433 | if (DEBUG_GLOB) |
| 2434 | debug_print_list("o_glob returning", o, n); |
| 2435 | return n; |
| 2436 | } |
| 2437 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2438 | #endif /* !HUSH_BRACE_EXP */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2439 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2440 | /* If o->o_glob == 1, glob the string so far remembered. |
| 2441 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2442 | static int o_save_ptr(o_string *o, int n) |
| 2443 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2444 | if (o->o_glob) { /* if globbing is requested */ |
| 2445 | /* If o->has_empty_slot, list[n] was already globbed |
| 2446 | * (if it was requested back then when it was filled) |
| 2447 | * so don't do that again! */ |
| 2448 | if (!o->has_empty_slot) |
| 2449 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2450 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2451 | return o_save_ptr_helper(o, n); |
| 2452 | } |
| 2453 | |
| 2454 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2455 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2456 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2457 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2458 | int string_start; |
| 2459 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2460 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2461 | if (DEBUG_EXPAND) |
| 2462 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2463 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2464 | list = (char**)o->data; |
| 2465 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2466 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2467 | while (n) { |
| 2468 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2469 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2470 | } |
| 2471 | return list; |
| 2472 | } |
| 2473 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2474 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2475 | /* Expansion can recurse */ |
| 2476 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2477 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2478 | #endif |
| 2479 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2480 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2481 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2482 | parse_stream_dquoted(dest, input, dquote_end) |
| 2483 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2484 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2485 | o_string *dest, |
| 2486 | struct in_str *input, |
| 2487 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2488 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2489 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2490 | * all variable references within and returns a pointer to |
| 2491 | * a list of expanded strings, possibly with larger number |
| 2492 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2493 | * This new list is allocated as a single malloc block. |
| 2494 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2495 | * followed by strings themself. |
| 2496 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2497 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2498 | /* Store given string, finalizing the word and starting new one whenever |
| 2499 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2500 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2501 | static int expand_on_ifs(o_string *output, int n, const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2502 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2503 | while (1) { |
| 2504 | int word_len = strcspn(str, G.ifs); |
| 2505 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2506 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2507 | o_addQstr(output, str, word_len); |
| 2508 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2509 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2510 | str += word_len; |
| 2511 | } |
| 2512 | if (!*str) /* EOL - do not finalize word */ |
| 2513 | break; |
| 2514 | o_addchr(output, '\0'); |
| 2515 | debug_print_list("expand_on_ifs", output, n); |
| 2516 | n = o_save_ptr(output, n); |
| 2517 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2518 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2519 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2520 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2523 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2524 | * they are in double quotes, with the exception that they are not :). |
| 2525 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2526 | * |
| 2527 | * Returns malloced string. |
| 2528 | * As an optimization, we return NULL if expansion is not needed. |
| 2529 | */ |
| 2530 | static char *expand_pseudo_dquoted(const char *str) |
| 2531 | { |
| 2532 | char *exp_str; |
| 2533 | struct in_str input; |
| 2534 | o_string dest = NULL_O_STRING; |
| 2535 | |
| 2536 | if (strchr(str, '$') == NULL |
| 2537 | #if ENABLE_HUSH_TICK |
| 2538 | && strchr(str, '`') == NULL |
| 2539 | #endif |
| 2540 | ) { |
| 2541 | return NULL; |
| 2542 | } |
| 2543 | |
| 2544 | /* We need to expand. Example: |
| 2545 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2546 | */ |
| 2547 | setup_string_in_str(&input, str); |
| 2548 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2549 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2550 | exp_str = expand_string_to_string(dest.data); |
| 2551 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2552 | o_free_unsafe(&dest); |
| 2553 | return exp_str; |
| 2554 | } |
| 2555 | |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2556 | #if ENABLE_SH_MATH_SUPPORT |
| 2557 | static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) |
| 2558 | { |
| 2559 | arith_eval_hooks_t hooks; |
| 2560 | arith_t res; |
| 2561 | char *exp_str; |
| 2562 | |
| 2563 | hooks.lookupvar = get_local_var_value; |
| 2564 | hooks.setvar = set_local_var_from_halves; |
| 2565 | hooks.endofname = endofname; |
| 2566 | exp_str = expand_pseudo_dquoted(arg); |
| 2567 | res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); |
| 2568 | free(exp_str); |
| 2569 | return res; |
| 2570 | } |
| 2571 | #endif |
| 2572 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2573 | /* Expand all variable references in given string, adding words to list[] |
| 2574 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2575 | * to be filled). This routine is extremely tricky: has to deal with |
| 2576 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2577 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 2578 | static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg, char or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2579 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2580 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2581 | * expansion of right-hand side of assignment == 1-element expand. |
| 2582 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2583 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2584 | char ored_ch; |
| 2585 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2586 | |
| 2587 | ored_ch = 0; |
| 2588 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 2589 | debug_printf_expand("expand_vars_to_list: arg:'%s' or_mask:%x\n", arg, or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2590 | debug_print_list("expand_vars_to_list", output, n); |
| 2591 | n = o_save_ptr(output, n); |
| 2592 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2593 | |
| 2594 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2595 | char first_ch; |
| 2596 | int i; |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2597 | char *to_be_freed = NULL; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2598 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2599 | #if ENABLE_HUSH_TICK |
| 2600 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2601 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2602 | #if ENABLE_SH_MATH_SUPPORT |
| 2603 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2604 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2605 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2606 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2607 | arg = ++p; |
| 2608 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2609 | |
| 2610 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2611 | /* "$@" is special. Even if quoted, it can still |
| 2612 | * expand to nothing (not even an empty string) */ |
| 2613 | if ((first_ch & 0x7f) != '@') |
| 2614 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2615 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2616 | switch (first_ch & 0x7f) { |
| 2617 | /* Highest bit in first_ch indicates that var is double-quoted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2618 | case '*': |
| 2619 | case '@': |
| 2620 | i = 1; |
| 2621 | if (!G.global_argv[i]) |
| 2622 | break; |
| 2623 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2624 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2625 | smallint sv = output->o_escape; |
| 2626 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2627 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2628 | while (G.global_argv[i]) { |
| 2629 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2630 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2631 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2632 | /* this argv[] is not empty and not last: |
| 2633 | * put terminating NUL, start new word */ |
| 2634 | o_addchr(output, '\0'); |
| 2635 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2636 | n = o_save_ptr(output, n); |
| 2637 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2638 | } |
| 2639 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2640 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2641 | } else |
| 2642 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2643 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2644 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2645 | while (1) { |
| 2646 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2647 | if (++i >= G.global_argc) |
| 2648 | break; |
| 2649 | o_addchr(output, '\0'); |
| 2650 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2651 | n = o_save_ptr(output, n); |
| 2652 | } |
| 2653 | } else { /* quoted $*: add as one word */ |
| 2654 | while (1) { |
| 2655 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2656 | if (!G.global_argv[++i]) |
| 2657 | break; |
| 2658 | if (G.ifs[0]) |
| 2659 | o_addchr(output, G.ifs[0]); |
| 2660 | } |
| 2661 | } |
| 2662 | break; |
| 2663 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2664 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2665 | arg++; |
| 2666 | ored_ch = 0x80; |
| 2667 | break; |
| 2668 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2669 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2670 | *p = '\0'; |
| 2671 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2672 | /* Can't just stuff it into output o_string, |
| 2673 | * expanded result may need to be globbed |
| 2674 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2675 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 2676 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 2677 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2678 | val = subst_result.data; |
| 2679 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2680 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2681 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2682 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2683 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2684 | int errcode; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2685 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2686 | arg++; /* skip '+' */ |
| 2687 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2688 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2689 | res = expand_and_evaluate_arith(arg, &errcode); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2690 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2691 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2692 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2693 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2694 | case -3: |
| 2695 | msg = "exponent less than 0"; |
| 2696 | break; |
| 2697 | case -2: |
| 2698 | msg = "divide by 0"; |
| 2699 | break; |
| 2700 | case -5: |
| 2701 | msg = "expression recursion loop detected"; |
| 2702 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2703 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2704 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2705 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2706 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2707 | sprintf(arith_buf, arith_t_fmt, res); |
| 2708 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2709 | break; |
| 2710 | } |
| 2711 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2712 | default: { /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
| 2713 | char *var; |
| 2714 | char first_char; |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2715 | char exp_op; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2716 | char exp_save = exp_save; /* for compiler */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2717 | char *exp_saveptr; /* points to expansion operator */ |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2718 | char *exp_word = exp_word; /* for compiler */ |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2719 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2720 | var = arg; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2721 | *p = '\0'; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2722 | exp_saveptr = arg[1] ? strchr("%#:-=+?", arg[1]) : NULL; |
| 2723 | first_char = arg[0] = first_ch & 0x7f; |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2724 | exp_op = 0; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2725 | |
| 2726 | if (first_char == '#' && arg[1] && !exp_saveptr) { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2727 | /* handle length expansion ${#var} */ |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2728 | var++; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2729 | exp_op = 'L'; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2730 | } else { |
| 2731 | /* maybe handle parameter expansion */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2732 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 2733 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 2734 | ) { |
| 2735 | /* ${?:0}, ${#[:]%0} etc */ |
| 2736 | exp_saveptr = var + 1; |
| 2737 | } else { |
| 2738 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 2739 | exp_saveptr = var+1 + strcspn(var+1, "%#:-=+?"); |
| 2740 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2741 | exp_op = exp_save = *exp_saveptr; |
| 2742 | if (exp_op) { |
| 2743 | exp_word = exp_saveptr + 1; |
| 2744 | if (exp_op == ':') { |
| 2745 | exp_op = *exp_word++; |
| 2746 | if (ENABLE_HUSH_BASH_COMPAT |
| 2747 | && (exp_op == '\0' || !strchr("%#:-=+?"+3, exp_op)) |
| 2748 | ) { |
| 2749 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 2750 | exp_op = ':'; |
| 2751 | exp_word--; |
| 2752 | } |
| 2753 | } |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2754 | *exp_saveptr = '\0'; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2755 | } /* else: it's not an expansion op, but bare ${var} */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /* lookup the variable in question */ |
| 2759 | if (isdigit(var[0])) { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2760 | /* parse_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2761 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2762 | if (i < G.global_argc) |
| 2763 | val = G.global_argv[i]; |
| 2764 | /* else val remains NULL: $N with too big N */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2765 | } else { |
| 2766 | switch (var[0]) { |
| 2767 | case '$': /* pid */ |
| 2768 | val = utoa(G.root_pid); |
| 2769 | break; |
| 2770 | case '!': /* bg pid */ |
| 2771 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2772 | break; |
| 2773 | case '?': /* exitcode */ |
| 2774 | val = utoa(G.last_exitcode); |
| 2775 | break; |
| 2776 | case '#': /* argc */ |
| 2777 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2778 | break; |
| 2779 | default: |
| 2780 | val = get_local_var_value(var); |
| 2781 | } |
| 2782 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2783 | |
| 2784 | /* handle any expansions */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2785 | if (exp_op == 'L') { |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2786 | debug_printf_expand("expand: length(%s)=", val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2787 | val = utoa(val ? strlen(val) : 0); |
| 2788 | debug_printf_expand("%s\n", val); |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2789 | } else if (exp_op) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2790 | if (exp_op == '%' || exp_op == '#') { |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2791 | /* Standard-mandated substring removal ops: |
| 2792 | * ${parameter%word} - remove smallest suffix pattern |
| 2793 | * ${parameter%%word} - remove largest suffix pattern |
| 2794 | * ${parameter#word} - remove smallest prefix pattern |
| 2795 | * ${parameter##word} - remove largest prefix pattern |
| 2796 | * |
| 2797 | * Word is expanded to produce a glob pattern. |
| 2798 | * Then var's value is matched to it and matching part removed. |
| 2799 | */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2800 | if (val) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2801 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2802 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2803 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2804 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2805 | exp_word++; |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2806 | val = to_be_freed = xstrdup(val); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 2807 | { |
| 2808 | char *exp_exp_word = expand_pseudo_dquoted(exp_word); |
| 2809 | if (exp_exp_word) |
| 2810 | exp_word = exp_exp_word; |
| 2811 | loc = scan(to_be_freed, exp_word, match_at_left); |
| 2812 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
| 2813 | // exp_op, to_be_freed, exp_word, loc); |
| 2814 | free(exp_exp_word); |
| 2815 | } |
| 2816 | if (loc) { /* match was found */ |
| 2817 | if (match_at_left) /* # or ## */ |
| 2818 | val = loc; |
| 2819 | else /* % or %% */ |
| 2820 | *loc = '\0'; |
| 2821 | } |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2822 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2823 | } else if (exp_op == ':') { |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 2824 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2825 | /* It's ${var:N[:M]} bashism. |
| 2826 | * Note that in encoded form it has TWO parts: |
| 2827 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2828 | */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2829 | arith_t beg, len; |
| 2830 | int errcode = 0; |
| 2831 | |
| 2832 | beg = expand_and_evaluate_arith(exp_word, &errcode); |
| 2833 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 2834 | *p++ = SPECIAL_VAR_SYMBOL; |
| 2835 | exp_word = p; |
| 2836 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2837 | *p = '\0'; |
| 2838 | len = expand_and_evaluate_arith(exp_word, &errcode); |
| 2839 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
| 2840 | |
| 2841 | if (errcode >= 0 && len >= 0) { /* bash compat: len < 0 is illegal */ |
| 2842 | if (beg < 0) /* bash compat */ |
| 2843 | beg = 0; |
| 2844 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | 73e013f | 2010-05-21 15:24:12 +0200 | [diff] [blame] | 2845 | if (len == 0 || !val || beg >= strlen(val)) |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2846 | val = ""; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2847 | else { |
| 2848 | /* Paranoia. What if user entered 9999999999999 |
| 2849 | * which fits in arith_t but not int? */ |
| 2850 | if (len >= INT_MAX) |
| 2851 | len = INT_MAX; |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2852 | val = to_be_freed = xstrndup(val + beg, len); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2853 | } |
| 2854 | debug_printf_varexp("val:'%s'\n", val); |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2855 | } else |
| 2856 | #endif |
| 2857 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2858 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | 73e013f | 2010-05-21 15:24:12 +0200 | [diff] [blame] | 2859 | val = ""; |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2860 | } |
| 2861 | } else { /* one of "-=+?" */ |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2862 | /* Standard-mandated substitution ops: |
| 2863 | * ${var?word} - indicate error if unset |
| 2864 | * If var is unset, word (or a message indicating it is unset |
| 2865 | * if word is null) is written to standard error |
| 2866 | * and the shell exits with a non-zero exit status. |
| 2867 | * Otherwise, the value of var is substituted. |
| 2868 | * ${var-word} - use default value |
| 2869 | * If var is unset, word is substituted. |
| 2870 | * ${var=word} - assign and use default value |
| 2871 | * If var is unset, word is assigned to var. |
| 2872 | * In all cases, final value of var is substituted. |
| 2873 | * ${var+word} - use alternative value |
| 2874 | * If var is unset, null is substituted. |
| 2875 | * Otherwise, word is substituted. |
| 2876 | * |
| 2877 | * Word is subjected to tilde expansion, parameter expansion, |
| 2878 | * command substitution, and arithmetic expansion. |
| 2879 | * If word is not needed, it is not expanded. |
| 2880 | * |
| 2881 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 2882 | * but also treat null var as if it is unset. |
| 2883 | */ |
| 2884 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2885 | if (exp_op == '+') |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2886 | use_word = !use_word; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2887 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2888 | (exp_save == ':') ? "true" : "false", use_word); |
| 2889 | if (use_word) { |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2890 | to_be_freed = expand_pseudo_dquoted(exp_word); |
| 2891 | if (to_be_freed) |
| 2892 | exp_word = to_be_freed; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2893 | if (exp_op == '?') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2894 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2895 | die_if_script("%s: %s", |
| 2896 | var, |
| 2897 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2898 | ); |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2899 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2900 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2901 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2902 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2903 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2904 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2905 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2906 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2907 | /* mimic bash message */ |
| 2908 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2909 | val = NULL; |
| 2910 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2911 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2912 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2913 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2914 | } |
| 2915 | } |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2916 | } /* one of "-=+?" */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2917 | |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2918 | *exp_saveptr = exp_save; |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2919 | } /* if (exp_op) */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2920 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2921 | arg[0] = first_ch; |
| 2922 | #if ENABLE_HUSH_TICK |
| 2923 | store_val: |
| 2924 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2925 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2926 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, output->o_escape); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2927 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2928 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2929 | smallint sv = output->o_escape; |
| 2930 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2931 | n = expand_on_ifs(output, n, val); |
| 2932 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2933 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2934 | } |
| 2935 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2936 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, output->o_escape); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2937 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2938 | } /* default: */ |
| 2939 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2940 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2941 | if (val) { |
| 2942 | o_addQstr(output, val, strlen(val)); |
| 2943 | } |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2944 | free(to_be_freed); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2945 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2946 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2947 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2948 | |
| 2949 | #if ENABLE_HUSH_TICK |
| 2950 | o_free(&subst_result); |
| 2951 | #endif |
| 2952 | arg = ++p; |
| 2953 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2954 | |
| 2955 | if (arg[0]) { |
| 2956 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2957 | /* this part is literal, and it was already pre-quoted |
| 2958 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2959 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2960 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2961 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2962 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2963 | ) { |
| 2964 | n--; |
| 2965 | /* allow to reuse list[n] later without re-growth */ |
| 2966 | output->has_empty_slot = 1; |
| 2967 | } else { |
| 2968 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2969 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2970 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2973 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2974 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2975 | int n; |
| 2976 | char **list; |
| 2977 | char **v; |
| 2978 | o_string output = NULL_O_STRING; |
| 2979 | |
| 2980 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2981 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2982 | /* (unquoted $var will temporarily switch it off) */ |
| 2983 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2984 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2985 | |
| 2986 | n = 0; |
| 2987 | v = argv; |
| 2988 | while (*v) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2989 | n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2990 | v++; |
| 2991 | } |
| 2992 | debug_print_list("expand_variables", &output, n); |
| 2993 | |
| 2994 | /* output.data (malloced in one block) gets returned in "list" */ |
| 2995 | list = o_finalize_list(&output, n); |
| 2996 | debug_print_strings("expand_variables[1]", list); |
| 2997 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3000 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3001 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3002 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3005 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3006 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 3007 | { |
| 3008 | return expand_variables(argv, 0x80); |
| 3009 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3010 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3011 | |
| 3012 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 3013 | static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv) |
| 3014 | { |
| 3015 | int n; |
| 3016 | char **list; |
| 3017 | char **v; |
| 3018 | o_string output = NULL_O_STRING; |
| 3019 | |
| 3020 | n = 0; |
| 3021 | v = argv; |
| 3022 | while (*v) { |
| 3023 | int is_var = is_well_formed_var_name(*v, '='); |
| 3024 | /* is_var * 0x80: singleword expansion for vars */ |
| 3025 | n = expand_vars_to_list(&output, n, *v, is_var * 0x80); |
| 3026 | |
| 3027 | /* Subtle! expand_vars_to_list did not glob last word yet. |
| 3028 | * It does this only when fed with further data. |
| 3029 | * Therefore we set globbing flags AFTER it, not before: |
| 3030 | */ |
| 3031 | |
| 3032 | /* if it is not recognizably abc=...; then: */ |
| 3033 | output.o_escape = !is_var; /* protect against globbing for "$var" */ |
| 3034 | /* (unquoted $var will temporarily switch it off) */ |
| 3035 | output.o_glob = !is_var; /* and indeed do globbing */ |
| 3036 | v++; |
| 3037 | } |
| 3038 | debug_print_list("expand_cond", &output, n); |
| 3039 | |
| 3040 | /* output.data (malloced in one block) gets returned in "list" */ |
| 3041 | list = o_finalize_list(&output, n); |
| 3042 | debug_print_strings("expand_cond[1]", list); |
| 3043 | return list; |
| 3044 | } |
| 3045 | #endif |
| 3046 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3047 | /* Used for expansion of right hand of assignments */ |
| 3048 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 3049 | * "v=/bin/c*" */ |
| 3050 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3051 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3052 | char *argv[2], **list; |
| 3053 | |
| 3054 | argv[0] = (char*)str; |
| 3055 | argv[1] = NULL; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3056 | list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3057 | if (HUSH_DEBUG) |
| 3058 | if (!list[0] || list[1]) |
| 3059 | bb_error_msg_and_die("BUG in varexp2"); |
| 3060 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 3061 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3062 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3063 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 3064 | return (char*)list; |
| 3065 | } |
| 3066 | |
| 3067 | /* Used for "eval" builtin */ |
| 3068 | static char* expand_strvec_to_string(char **argv) |
| 3069 | { |
| 3070 | char **list; |
| 3071 | |
| 3072 | list = expand_variables(argv, 0x80); |
| 3073 | /* Convert all NULs to spaces */ |
| 3074 | if (list[0]) { |
| 3075 | int n = 1; |
| 3076 | while (list[n]) { |
| 3077 | if (HUSH_DEBUG) |
| 3078 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 3079 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 3080 | /* bash uses ' ' regardless of $IFS contents */ |
| 3081 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3082 | n++; |
| 3083 | } |
| 3084 | } |
| 3085 | overlapping_strcpy((char*)list, list[0]); |
| 3086 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 3087 | return (char*)list; |
| 3088 | } |
| 3089 | |
| 3090 | static char **expand_assignments(char **argv, int count) |
| 3091 | { |
| 3092 | int i; |
| 3093 | char **p = NULL; |
| 3094 | /* Expand assignments into one string each */ |
| 3095 | for (i = 0; i < count; i++) { |
| 3096 | p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 3097 | } |
| 3098 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3101 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3102 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3103 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3104 | void re_execute_shell(char ***to_free, const char *s, |
| 3105 | char *g_argv0, char **g_argv, |
| 3106 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3107 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3108 | static void reset_traps_to_defaults(void) |
| 3109 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3110 | /* This function is always called in a child shell |
| 3111 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 3112 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3113 | unsigned sig; |
| 3114 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 3115 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3116 | /* Child shells are not interactive. |
| 3117 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 3118 | * Testcase: (while :; do :; done) + ^Z should background. |
| 3119 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 3120 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3121 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3122 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3123 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3124 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 3125 | * Stupid. It can be done with *single* &= op, but we can't use |
| 3126 | * the fact that G.blocked_set is implemented as a bitmask |
| 3127 | * in libc... */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3128 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 3129 | sig = 1; |
| 3130 | while (1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3131 | if (mask & 1) { |
| 3132 | /* Careful. Only if no trap or trap is not "" */ |
| 3133 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 3134 | sigdelset(&G.blocked_set, sig); |
| 3135 | } |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3136 | mask >>= 1; |
| 3137 | if (!mask) |
| 3138 | break; |
| 3139 | sig++; |
| 3140 | } |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3141 | /* Our homegrown sig mask is saner to work with :) */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3142 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3143 | |
| 3144 | /* Resetting all traps to default except empty ones */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3145 | mask = G.non_DFL_mask; |
| 3146 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3147 | if (!G.traps[sig] || !G.traps[sig][0]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3148 | continue; |
| 3149 | free(G.traps[sig]); |
| 3150 | G.traps[sig] = NULL; |
| 3151 | /* There is no signal for 0 (EXIT) */ |
| 3152 | if (sig == 0) |
| 3153 | continue; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3154 | /* There was a trap handler, we just removed it. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3155 | * But if sig still has non-DFL handling, |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3156 | * we should not unblock the sig. */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3157 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3158 | continue; |
| 3159 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3160 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 3161 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | #else /* !BB_MMU */ |
| 3165 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3166 | static void re_execute_shell(char ***to_free, const char *s, |
| 3167 | char *g_argv0, char **g_argv, |
| 3168 | char **builtin_argv) NORETURN; |
| 3169 | static void re_execute_shell(char ***to_free, const char *s, |
| 3170 | char *g_argv0, char **g_argv, |
| 3171 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3172 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 3173 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3174 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 3175 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3176 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3177 | struct variable *cur; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3178 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3179 | struct function *funcp; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3180 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3181 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3182 | unsigned cnt; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3183 | unsigned long long empty_trap_mask; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3184 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3185 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3186 | argv = heredoc_argv; |
| 3187 | argv[0] = (char *) G.argv0_for_re_execing; |
| 3188 | argv[1] = (char *) "-<"; |
| 3189 | argv[2] = (char *) s; |
| 3190 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 3191 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3192 | goto do_exec; |
| 3193 | } |
| 3194 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3195 | cnt = 0; |
| 3196 | pp = builtin_argv; |
| 3197 | if (pp) while (*pp++) |
| 3198 | cnt++; |
| 3199 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3200 | empty_trap_mask = 0; |
| 3201 | if (G.traps) { |
| 3202 | int sig; |
| 3203 | for (sig = 1; sig < NSIG; sig++) { |
| 3204 | if (G.traps[sig] && !G.traps[sig][0]) |
| 3205 | empty_trap_mask |= 1LL << sig; |
| 3206 | } |
| 3207 | } |
| 3208 | |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3209 | sprintf(param_buf, NOMMU_HACK_FMT |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3210 | , (unsigned) G.root_pid |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 3211 | , (unsigned) G.root_ppid |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3212 | , (unsigned) G.last_bg_pid |
| 3213 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3214 | , cnt |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3215 | , empty_trap_mask |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3216 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3217 | ); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 3218 | # undef NOMMU_HACK_FMT |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3219 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3220 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3221 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3222 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3223 | for (cur = G.top_var; cur; cur = cur->next) { |
| 3224 | if (!cur->flg_export || cur->flg_read_only) |
| 3225 | cnt += 2; |
| 3226 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3227 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3228 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 3229 | cnt += 3; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3230 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3231 | pp = g_argv; |
| 3232 | while (*pp++) |
| 3233 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3234 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3235 | *pp++ = (char *) G.argv0_for_re_execing; |
| 3236 | *pp++ = param_buf; |
| 3237 | for (cur = G.top_var; cur; cur = cur->next) { |
| 3238 | if (cur->varstr == hush_version_str) |
| 3239 | continue; |
| 3240 | if (cur->flg_read_only) { |
| 3241 | *pp++ = (char *) "-R"; |
| 3242 | *pp++ = cur->varstr; |
| 3243 | } else if (!cur->flg_export) { |
| 3244 | *pp++ = (char *) "-V"; |
| 3245 | *pp++ = cur->varstr; |
| 3246 | } |
| 3247 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3248 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3249 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 3250 | *pp++ = (char *) "-F"; |
| 3251 | *pp++ = funcp->name; |
| 3252 | *pp++ = funcp->body_as_string; |
| 3253 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3254 | # endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3255 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 3256 | * |
| 3257 | * However, POSIX says that subshells reset signals with traps |
| 3258 | * to SIG_DFL. |
| 3259 | * I tested bash-3.2 and it not only does that with true subshells |
| 3260 | * of the form ( list ), but with any forked children shells. |
| 3261 | * I set trap "echo W" WINCH; and then tried: |
| 3262 | * |
| 3263 | * { echo 1; sleep 20; echo 2; } & |
| 3264 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 3265 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 3266 | * |
| 3267 | * In all these cases sending SIGWINCH to the child shell |
| 3268 | * did not run the trap. If I add trap "echo V" WINCH; |
| 3269 | * _inside_ group (just before echo 1), it works. |
| 3270 | * |
| 3271 | * I conclude it means we don't need to pass active traps here. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3272 | * Even if we would use signal handlers instead of signal masking |
| 3273 | * in order to implement trap handling, |
| 3274 | * exec syscall below resets signals to SIG_DFL for us. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3275 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3276 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3277 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3278 | if (builtin_argv) { |
| 3279 | while (*++builtin_argv) |
| 3280 | *pp++ = *builtin_argv; |
| 3281 | *pp++ = (char *) ""; |
| 3282 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3283 | *pp++ = g_argv0; |
| 3284 | while (*g_argv) |
| 3285 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3286 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3287 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3288 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3289 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3290 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 3291 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3292 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3293 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3294 | if (argv[0][0] == '/') |
| 3295 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3296 | xfunc_error_retval = 127; |
| 3297 | bb_error_msg_and_die("can't re-execute the shell"); |
| 3298 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3299 | #endif /* !BB_MMU */ |
| 3300 | |
| 3301 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3302 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3303 | { |
| 3304 | struct fd_pair pair; |
| 3305 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3306 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3307 | /* the _body_ of heredoc (misleading field name) */ |
| 3308 | const char *heredoc = redir->rd_filename; |
| 3309 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3310 | #if !BB_MMU |
| 3311 | char **to_free; |
| 3312 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3313 | |
| 3314 | expanded = NULL; |
| 3315 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 3316 | expanded = expand_pseudo_dquoted(heredoc); |
| 3317 | if (expanded) |
| 3318 | heredoc = expanded; |
| 3319 | } |
| 3320 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3321 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3322 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3323 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3324 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3325 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3326 | /* Try writing without forking. Newer kernels have |
| 3327 | * dynamically growing pipes. Must use non-blocking write! */ |
| 3328 | ndelay_on(pair.wr); |
| 3329 | while (1) { |
| 3330 | written = write(pair.wr, heredoc, len); |
| 3331 | if (written <= 0) |
| 3332 | break; |
| 3333 | len -= written; |
| 3334 | if (len == 0) { |
| 3335 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 3336 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3337 | return; |
| 3338 | } |
| 3339 | heredoc += written; |
| 3340 | } |
| 3341 | ndelay_off(pair.wr); |
| 3342 | |
| 3343 | /* Okay, pipe buffer was not big enough */ |
| 3344 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3345 | * for the unsuspecting parent process. Child creates a grandchild |
| 3346 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3347 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 3348 | #if !BB_MMU |
| 3349 | to_free = NULL; |
| 3350 | #endif |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 3351 | pid = xvfork(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3352 | if (pid == 0) { |
| 3353 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 3354 | disable_restore_tty_pgrp_on_exit(); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 3355 | pid = BB_MMU ? xfork() : xvfork(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3356 | if (pid != 0) |
| 3357 | _exit(0); |
| 3358 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3359 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3360 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3361 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3362 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3363 | #else |
| 3364 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3365 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3366 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3367 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3368 | } |
| 3369 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3370 | #if ENABLE_HUSH_FAST |
| 3371 | G.count_SIGCHLD++; |
| 3372 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3373 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3374 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3375 | #if !BB_MMU |
| 3376 | free(to_free); |
| 3377 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3378 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3379 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3380 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3383 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 3384 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3385 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3386 | { |
| 3387 | int openfd, mode; |
| 3388 | struct redir_struct *redir; |
| 3389 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3390 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3391 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3392 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3393 | if (squirrel && redir->rd_fd < 3 |
| 3394 | && squirrel[redir->rd_fd] < 0 |
| 3395 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3396 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3397 | } |
| 3398 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 3399 | * of the heredoc */ |
| 3400 | debug_printf_parse("set heredoc '%s'\n", |
| 3401 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3402 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 3403 | continue; |
| 3404 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3405 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3406 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 3407 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3408 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3409 | if (redir->rd_filename == NULL) { |
| 3410 | /* Something went wrong in the parse. |
| 3411 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3412 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3413 | continue; |
| 3414 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3415 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3416 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3417 | openfd = open_or_warn(p, mode); |
| 3418 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3419 | if (openfd < 0) { |
| 3420 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3421 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 3422 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3423 | return 1; |
| 3424 | } |
| 3425 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3426 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3427 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3430 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3431 | if (squirrel && redir->rd_fd < 3 |
| 3432 | && squirrel[redir->rd_fd] < 0 |
| 3433 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3434 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3435 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3436 | if (openfd == REDIRFD_CLOSE) { |
| 3437 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3438 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3439 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3440 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3441 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3442 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3443 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | return 0; |
| 3447 | } |
| 3448 | |
| 3449 | static void restore_redirects(int squirrel[]) |
| 3450 | { |
| 3451 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3452 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3453 | fd = squirrel[i]; |
| 3454 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3455 | /* We simply die on error */ |
| 3456 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3457 | } |
| 3458 | } |
| 3459 | } |
| 3460 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3461 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3462 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3463 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3464 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3465 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3466 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3467 | char **p; |
| 3468 | struct command *command; |
| 3469 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3470 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3471 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3472 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3473 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3474 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3475 | for (i = 0; i < pi->num_cmds; i++) { |
| 3476 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3477 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3478 | if (command->argv) { |
| 3479 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3480 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3481 | } |
| 3482 | free_strings(command->argv); |
| 3483 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3484 | } |
| 3485 | /* not "else if": on syntax error, we may have both! */ |
| 3486 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3487 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3488 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3489 | free_pipe_list(command->group); |
| 3490 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3491 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3492 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3493 | /* else is crucial here. |
| 3494 | * If group != NULL, child_func is meaningless */ |
| 3495 | #if ENABLE_HUSH_FUNCTIONS |
| 3496 | else if (command->child_func) { |
| 3497 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3498 | command->child_func->parent_cmd = NULL; |
| 3499 | } |
| 3500 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3501 | #if !BB_MMU |
| 3502 | free(command->group_as_string); |
| 3503 | command->group_as_string = NULL; |
| 3504 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3505 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3506 | debug_printf_clean(" redirect %d%s", |
| 3507 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3508 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3509 | if (r->rd_filename) { |
| 3510 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3511 | free(r->rd_filename); |
| 3512 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3513 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3514 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3515 | rnext = r->next; |
| 3516 | free(r); |
| 3517 | } |
| 3518 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3519 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3520 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 3521 | pi->cmds = NULL; |
| 3522 | #if ENABLE_HUSH_JOB |
| 3523 | free(pi->cmdtext); |
| 3524 | pi->cmdtext = NULL; |
| 3525 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3526 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3527 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3528 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3529 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3530 | struct pipe *pi, *next; |
| 3531 | |
| 3532 | for (pi = head; pi; pi = next) { |
| 3533 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3534 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3535 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3536 | free_pipe(pi); |
| 3537 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3538 | next = pi->next; |
| 3539 | /*pi->next = NULL;*/ |
| 3540 | free(pi); |
| 3541 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3545 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3546 | #if BB_MMU |
| 3547 | #define parse_stream(pstring, input, end_trigger) \ |
| 3548 | parse_stream(input, end_trigger) |
| 3549 | #endif |
| 3550 | static struct pipe *parse_stream(char **pstring, |
| 3551 | struct in_str *input, |
| 3552 | int end_trigger); |
| 3553 | static void parse_and_run_string(const char *s); |
| 3554 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3555 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 3556 | static char *find_in_path(const char *arg) |
| 3557 | { |
| 3558 | char *ret = NULL; |
| 3559 | const char *PATH = get_local_var_value("PATH"); |
| 3560 | |
| 3561 | if (!PATH) |
| 3562 | return NULL; |
| 3563 | |
| 3564 | while (1) { |
| 3565 | const char *end = strchrnul(PATH, ':'); |
| 3566 | int sz = end - PATH; /* must be int! */ |
| 3567 | |
| 3568 | free(ret); |
| 3569 | if (sz != 0) { |
| 3570 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 3571 | } else { |
| 3572 | /* We have xxx::yyyy in $PATH, |
| 3573 | * it means "use current dir" */ |
| 3574 | ret = xstrdup(arg); |
| 3575 | } |
| 3576 | if (access(ret, F_OK) == 0) |
| 3577 | break; |
| 3578 | |
| 3579 | if (*end == '\0') { |
| 3580 | free(ret); |
| 3581 | return NULL; |
| 3582 | } |
| 3583 | PATH = end + 1; |
| 3584 | } |
| 3585 | |
| 3586 | return ret; |
| 3587 | } |
| 3588 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3589 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 3590 | const struct built_in_command *x, |
| 3591 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3592 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3593 | while (x != end) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 3594 | if (strcmp(name, x->b_cmd) != 0) { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3595 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3596 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3597 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3598 | debug_printf_exec("found builtin '%s'\n", name); |
| 3599 | return x; |
| 3600 | } |
| 3601 | return NULL; |
| 3602 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3603 | static const struct built_in_command* find_builtin1(const char *name) |
| 3604 | { |
| 3605 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 3606 | } |
| 3607 | static const struct built_in_command* find_builtin(const char *name) |
| 3608 | { |
| 3609 | const struct built_in_command *x = find_builtin1(name); |
| 3610 | if (x) |
| 3611 | return x; |
| 3612 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3613 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3614 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3615 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3616 | static struct function **find_function_slot(const char *name) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3617 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3618 | struct function **funcpp = &G.top_func; |
| 3619 | while (*funcpp) { |
| 3620 | if (strcmp(name, (*funcpp)->name) == 0) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3621 | break; |
| 3622 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3623 | funcpp = &(*funcpp)->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3624 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3625 | return funcpp; |
| 3626 | } |
| 3627 | |
| 3628 | static const struct function *find_function(const char *name) |
| 3629 | { |
| 3630 | const struct function *funcp = *find_function_slot(name); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3631 | if (funcp) |
| 3632 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3633 | return funcp; |
| 3634 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3635 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3636 | /* Note: takes ownership on name ptr */ |
| 3637 | static struct function *new_function(char *name) |
| 3638 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3639 | struct function **funcpp = find_function_slot(name); |
| 3640 | struct function *funcp = *funcpp; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3641 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3642 | if (funcp != NULL) { |
| 3643 | struct command *cmd = funcp->parent_cmd; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3644 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3645 | if (!cmd) { |
| 3646 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3647 | free(funcp->name); |
| 3648 | /* Note: if !funcp->body, do not free body_as_string! |
| 3649 | * This is a special case of "-F name body" function: |
| 3650 | * body_as_string was not malloced! */ |
| 3651 | if (funcp->body) { |
| 3652 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3653 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3654 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3655 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3656 | } |
| 3657 | } else { |
| 3658 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3659 | cmd->argv[0] = funcp->name; |
| 3660 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3661 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3662 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3663 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3664 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3665 | } else { |
| 3666 | debug_printf_exec("remembering new function '%s'\n", name); |
| 3667 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3668 | /*funcp->next = NULL;*/ |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3669 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3670 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3671 | funcp->name = name; |
| 3672 | return funcp; |
| 3673 | } |
| 3674 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3675 | static void unset_func(const char *name) |
| 3676 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3677 | struct function **funcpp = find_function_slot(name); |
| 3678 | struct function *funcp = *funcpp; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3679 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3680 | if (funcp != NULL) { |
| 3681 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 3682 | *funcpp = funcp->next; |
| 3683 | /* funcp is unlinked now, deleting it. |
| 3684 | * Note: if !funcp->body, the function was created by |
| 3685 | * "-F name body", do not free ->body_as_string |
| 3686 | * and ->name as they were not malloced. */ |
| 3687 | if (funcp->body) { |
| 3688 | free_pipe_list(funcp->body); |
| 3689 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3690 | # if !BB_MMU |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3691 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3692 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3693 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3694 | free(funcp); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3695 | } |
| 3696 | } |
| 3697 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3698 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3699 | #define exec_function(to_free, funcp, argv) \ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3700 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3701 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3702 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3703 | const struct function *funcp, |
| 3704 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3705 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3706 | const struct function *funcp, |
| 3707 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3708 | { |
| 3709 | # if BB_MMU |
| 3710 | int n = 1; |
| 3711 | |
| 3712 | argv[0] = G.global_argv[0]; |
| 3713 | G.global_argv = argv; |
| 3714 | while (*++argv) |
| 3715 | n++; |
| 3716 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3717 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3718 | n = run_list(funcp->body); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3719 | fflush_all(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3720 | _exit(n); |
| 3721 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3722 | re_execute_shell(to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3723 | funcp->body_as_string, |
| 3724 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3725 | argv + 1, |
| 3726 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3727 | # endif |
| 3728 | } |
| 3729 | |
| 3730 | static int run_function(const struct function *funcp, char **argv) |
| 3731 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3732 | int rc; |
| 3733 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3734 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3735 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3736 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3737 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3738 | /* "we are in function, ok to use return" */ |
| 3739 | sv_flg = G.flag_return_in_progress; |
| 3740 | G.flag_return_in_progress = -1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3741 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3742 | G.func_nest_level++; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3743 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3744 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3745 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3746 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3747 | if (!funcp->body) { |
| 3748 | /* Function defined by -F */ |
| 3749 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3750 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3751 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3752 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3753 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3754 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3755 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3756 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3757 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3758 | { |
| 3759 | struct variable *var; |
| 3760 | struct variable **var_pp; |
| 3761 | |
| 3762 | var_pp = &G.top_var; |
| 3763 | while ((var = *var_pp) != NULL) { |
| 3764 | if (var->func_nest_level < G.func_nest_level) { |
| 3765 | var_pp = &var->next; |
| 3766 | continue; |
| 3767 | } |
| 3768 | /* Unexport */ |
| 3769 | if (var->flg_export) |
| 3770 | bb_unsetenv(var->varstr); |
| 3771 | /* Remove from global list */ |
| 3772 | *var_pp = var->next; |
| 3773 | /* Free */ |
| 3774 | if (!var->max_len) |
| 3775 | free(var->varstr); |
| 3776 | free(var); |
| 3777 | } |
| 3778 | G.func_nest_level--; |
| 3779 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3780 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3781 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3782 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3783 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3784 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3785 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3786 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3787 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3788 | |
| 3789 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3790 | #if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3791 | #define exec_builtin(to_free, x, argv) \ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3792 | exec_builtin(x, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3793 | #else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3794 | #define exec_builtin(to_free, x, argv) \ |
| 3795 | exec_builtin(to_free, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3796 | #endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3797 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3798 | const struct built_in_command *x, |
| 3799 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3800 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3801 | const struct built_in_command *x, |
| 3802 | char **argv) |
| 3803 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3804 | #if BB_MMU |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 3805 | int rcode = x->b_function(argv); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3806 | fflush_all(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3807 | _exit(rcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3808 | #else |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3809 | /* On NOMMU, we must never block! |
| 3810 | * Example: { sleep 99 | read line; } & echo Ok |
| 3811 | */ |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3812 | re_execute_shell(to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3813 | argv[0], |
| 3814 | G.global_argv[0], |
| 3815 | G.global_argv + 1, |
| 3816 | argv); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3817 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3821 | static void execvp_or_die(char **argv) NORETURN; |
| 3822 | static void execvp_or_die(char **argv) |
| 3823 | { |
| 3824 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 3825 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3826 | execvp(argv[0], argv); |
| 3827 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 3828 | _exit(127); /* bash compat */ |
| 3829 | } |
| 3830 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 3831 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3832 | static void dump_cmd_in_x_mode(char **argv) |
| 3833 | { |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 3834 | if (G_x_mode && argv) { |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3835 | /* We want to output the line in one write op */ |
| 3836 | char *buf, *p; |
| 3837 | int len; |
| 3838 | int n; |
| 3839 | |
| 3840 | len = 3; |
| 3841 | n = 0; |
| 3842 | while (argv[n]) |
| 3843 | len += strlen(argv[n++]) + 1; |
| 3844 | buf = xmalloc(len); |
| 3845 | buf[0] = '+'; |
| 3846 | p = buf + 1; |
| 3847 | n = 0; |
| 3848 | while (argv[n]) |
| 3849 | p += sprintf(p, " %s", argv[n++]); |
| 3850 | *p++ = '\n'; |
| 3851 | *p = '\0'; |
| 3852 | fputs(buf, stderr); |
| 3853 | free(buf); |
| 3854 | } |
| 3855 | } |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 3856 | #else |
| 3857 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 3858 | #endif |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3859 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3860 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3861 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3862 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3863 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3864 | pseudo_exec(command, argv_expanded) |
| 3865 | #endif |
| 3866 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3867 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3868 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3869 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3870 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3871 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3872 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3873 | char **argv, int assignment_cnt, |
| 3874 | char **argv_expanded) NORETURN; |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3875 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3876 | char **argv, int assignment_cnt, |
| 3877 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3878 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3879 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3880 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3881 | new_env = expand_assignments(argv, assignment_cnt); |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3882 | dump_cmd_in_x_mode(new_env); |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 3883 | |
| 3884 | if (!argv[assignment_cnt]) { |
| 3885 | /* Case when we are here: ... | var=val | ... |
| 3886 | * (note that we do not exit early, i.e., do not optimize out |
| 3887 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 3888 | */ |
| 3889 | free_strings(new_env); |
| 3890 | _exit(EXIT_SUCCESS); |
| 3891 | } |
| 3892 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3893 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3894 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3895 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3896 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3897 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3898 | #else |
| 3899 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3900 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3901 | #endif |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 3902 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3903 | if (argv_expanded) { |
| 3904 | argv = argv_expanded; |
| 3905 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3906 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3907 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3908 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3909 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3910 | } |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3911 | dump_cmd_in_x_mode(argv); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3912 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3913 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3914 | if (strchr(argv[0], '/') != NULL) |
| 3915 | goto skip; |
| 3916 | #endif |
| 3917 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3918 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3919 | * Depending on context, this might be redundant. But it's |
| 3920 | * easier to waste a few CPU cycles than it is to figure out |
| 3921 | * if this is one of those cases. |
| 3922 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3923 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3924 | /* On NOMMU, it is more expensive to re-execute shell |
| 3925 | * just in order to run echo or test builtin. |
| 3926 | * It's better to skip it here and run corresponding |
| 3927 | * non-builtin later. */ |
| 3928 | const struct built_in_command *x; |
| 3929 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3930 | if (x) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3931 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3932 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3933 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3934 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3935 | /* Check if the command matches any functions */ |
| 3936 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3937 | const struct function *funcp = find_function(argv[0]); |
| 3938 | if (funcp) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3939 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3940 | } |
| 3941 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3942 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3943 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3944 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3945 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3946 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3947 | int a = find_applet_by_name(argv[0]); |
| 3948 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3949 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3950 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3951 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3952 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3953 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3954 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3955 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3956 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3957 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3958 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3959 | /* If they called chroot or otherwise made the binary no longer |
| 3960 | * executable, fall through */ |
| 3961 | } |
| 3962 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3963 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3964 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3965 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3966 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3967 | #endif |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3968 | execvp_or_die(argv); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3971 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3972 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3973 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3974 | struct command *command, |
| 3975 | char **argv_expanded) NORETURN; |
| 3976 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 3977 | struct command *command, |
| 3978 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3979 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3980 | if (command->argv) { |
| 3981 | pseudo_exec_argv(nommu_save, command->argv, |
| 3982 | command->assignment_cnt, argv_expanded); |
| 3983 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3984 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3985 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 3986 | /* Cases when we are here: |
| 3987 | * ( list ) |
| 3988 | * { list } & |
| 3989 | * ... | ( list ) | ... |
| 3990 | * ... | { list } | ... |
| 3991 | */ |
| 3992 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 3993 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 3994 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3995 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3996 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 3997 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3998 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3999 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4000 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4001 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 4002 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4003 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 4004 | G.global_argv + 1, |
| 4005 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 4006 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4007 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4008 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 4009 | /* Case when we are here: ... | >file */ |
| 4010 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4011 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4014 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4015 | static const char *get_cmdtext(struct pipe *pi) |
| 4016 | { |
| 4017 | char **argv; |
| 4018 | char *p; |
| 4019 | int len; |
| 4020 | |
| 4021 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 4022 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4023 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4024 | if (pi->cmdtext) |
| 4025 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4026 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4027 | if (!argv || !argv[0]) { |
| 4028 | pi->cmdtext = xzalloc(1); |
| 4029 | return pi->cmdtext; |
| 4030 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4031 | |
| 4032 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4033 | do { |
| 4034 | len += strlen(*argv) + 1; |
| 4035 | } while (*++argv); |
| 4036 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4037 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4038 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4039 | do { |
| 4040 | len = strlen(*argv); |
| 4041 | memcpy(p, *argv, len); |
| 4042 | p += len; |
| 4043 | *p++ = ' '; |
| 4044 | } while (*++argv); |
| 4045 | p[-1] = '\0'; |
| 4046 | return pi->cmdtext; |
| 4047 | } |
| 4048 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4049 | static void insert_bg_job(struct pipe *pi) |
| 4050 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4051 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4052 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4053 | |
| 4054 | /* Linear search for the ID of the job to use */ |
| 4055 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4056 | for (job = G.job_list; job; job = job->next) |
| 4057 | if (job->jobid >= pi->jobid) |
| 4058 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4059 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4060 | /* Add job to the list of running jobs */ |
| 4061 | jobp = &G.job_list; |
| 4062 | while ((job = *jobp) != NULL) |
| 4063 | jobp = &job->next; |
| 4064 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4065 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4066 | *job = *pi; /* physical copy */ |
| 4067 | job->next = NULL; |
| 4068 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 4069 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4070 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4071 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4072 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4073 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4074 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4075 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4076 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4077 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 4078 | /* Last command's pid goes to $! */ |
| 4079 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 4080 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4083 | static void remove_bg_job(struct pipe *pi) |
| 4084 | { |
| 4085 | struct pipe *prev_pipe; |
| 4086 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4087 | if (pi == G.job_list) { |
| 4088 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4089 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4090 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4091 | while (prev_pipe->next != pi) |
| 4092 | prev_pipe = prev_pipe->next; |
| 4093 | prev_pipe->next = pi->next; |
| 4094 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4095 | if (G.job_list) |
| 4096 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4097 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4098 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4099 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4100 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4101 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4102 | static void delete_finished_bg_job(struct pipe *pi) |
| 4103 | { |
| 4104 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4105 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4106 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4107 | free(pi); |
| 4108 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4109 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4110 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4111 | /* Check to see if any processes have exited -- if they |
| 4112 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4113 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4114 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4115 | int attributes; |
| 4116 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4117 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4118 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4119 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4120 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4121 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4122 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4123 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 4124 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4125 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4126 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4127 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4128 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 4129 | errno = 0; |
| 4130 | #if ENABLE_HUSH_FAST |
| 4131 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 4132 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 4133 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
Denys Vlasenko | 68759ed | 2009-05-26 14:39:41 +0200 | [diff] [blame] | 4134 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 4135 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 4136 | if (!G.we_have_children) { |
| 4137 | errno = ECHILD; |
| 4138 | return -1; |
| 4139 | } |
| 4140 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 4141 | /* We have children, but they did not exit |
| 4142 | * or stop yet (we saw no SIGCHLD) */ |
| 4143 | return 0; |
| 4144 | } |
| 4145 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 4146 | } |
| 4147 | #endif |
| 4148 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4149 | /* Do we do this right? |
| 4150 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4151 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4152 | * [3]+ Stopped sleep 20 | false |
| 4153 | * bash-3.00# echo $? |
| 4154 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4155 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4156 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4157 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4158 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4159 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4160 | int dead; |
| 4161 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4162 | #if ENABLE_HUSH_FAST |
| 4163 | i = G.count_SIGCHLD; |
| 4164 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4165 | childpid = waitpid(-1, &status, attributes); |
| 4166 | if (childpid <= 0) { |
| 4167 | if (childpid && errno != ECHILD) |
| 4168 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4169 | #if ENABLE_HUSH_FAST |
| 4170 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 4171 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4172 | G.handled_SIGCHLD = i; |
| 4173 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4174 | } |
| 4175 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4176 | break; |
| 4177 | } |
| 4178 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 4179 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 4180 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4181 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 4182 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4183 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 4184 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 4185 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4186 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 4187 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 4188 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4189 | childpid, WEXITSTATUS(status)); |
| 4190 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4191 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4192 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4193 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 4194 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 4195 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4196 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4197 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4198 | fg_pipe->cmds[i].pid = 0; |
| 4199 | fg_pipe->alive_cmds--; |
| 4200 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4201 | /* last process gives overall exitstatus */ |
| 4202 | rcode = WEXITSTATUS(status); |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 4203 | /* bash prints killer signal's name for *last* |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 4204 | * process in pipe (prints just newline for SIGINT). |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 4205 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 4206 | */ |
| 4207 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 4208 | int sig = WTERMSIG(status); |
| 4209 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denys Vlasenko | a4899ef | 2010-01-04 11:37:09 +0100 | [diff] [blame] | 4210 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 4211 | * Maybe we need to use sig | 128? */ |
| 4212 | rcode = sig + 128; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 4213 | } |
Denys Vlasenko | a4899ef | 2010-01-04 11:37:09 +0100 | [diff] [blame] | 4214 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4215 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4216 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4217 | fg_pipe->cmds[i].is_stopped = 1; |
| 4218 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4219 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4220 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 4221 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 4222 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4223 | /* All processes in fg pipe have exited or stopped */ |
| 4224 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 4225 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 4226 | * and "killall -STOP cat" */ |
| 4227 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4228 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4229 | if (fg_pipe->alive_cmds) |
| 4230 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4231 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4232 | return rcode; |
| 4233 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 4234 | if (!fg_pipe->alive_cmds) |
| 4235 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4236 | } |
| 4237 | /* There are still running processes in the fg pipe */ |
| 4238 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4239 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4240 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4241 | } |
| 4242 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4243 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4244 | /* We asked to wait for bg or orphaned children */ |
| 4245 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4246 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4247 | for (i = 0; i < pi->num_cmds; i++) { |
| 4248 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4249 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4250 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4251 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4252 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 4253 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4254 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4255 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4256 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4257 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4258 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4259 | pi->cmds[i].pid = 0; |
| 4260 | pi->alive_cmds--; |
| 4261 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4262 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 4263 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 4264 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4265 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4266 | } |
| 4267 | } else { |
| 4268 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4269 | pi->cmds[i].is_stopped = 1; |
| 4270 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4271 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4272 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4273 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4274 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4275 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4276 | } |
| 4277 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4278 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4279 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 4280 | { |
| 4281 | pid_t p; |
| 4282 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4283 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4284 | /* Job finished, move the shell to the foreground */ |
| 4285 | p = getpgrp(); /* our process group id */ |
| 4286 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 4287 | tcsetpgrp(G_interactive_fd, p); |
| 4288 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4289 | return rcode; |
| 4290 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4291 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4292 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 4293 | /* Start all the jobs, but don't wait for anything to finish. |
| 4294 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4295 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4296 | * Return code is normally -1, when the caller has to wait for children |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4297 | * to finish to determine the exit status of the pipe. If the pipe |
| 4298 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4299 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4300 | * return value. |
| 4301 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4302 | * Returns -1 only if started some children. IOW: we have to |
| 4303 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4304 | * |
| 4305 | * The only case when we do not need to [v]fork is when the pipe |
| 4306 | * is single, non-backgrounded, non-subshell command. Examples: |
| 4307 | * cmd ; ... { list } ; ... |
| 4308 | * cmd && ... { list } && ... |
| 4309 | * cmd || ... { list } || ... |
| 4310 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 4311 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4312 | * with run_list. If it isn't one of these, we fork and exec cmd. |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4313 | * |
| 4314 | * Cases when we must fork: |
| 4315 | * non-single: cmd | cmd |
| 4316 | * backgrounded: cmd & { list } & |
| 4317 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4318 | */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 4319 | static NOINLINE int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4320 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4321 | static const char *const null_ptr = NULL; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4322 | |
| 4323 | int cmd_no; |
| 4324 | int next_infd; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4325 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4326 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4327 | char **argv; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4328 | char *p; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4329 | /* it is not always needed, but we aim to smaller code */ |
| 4330 | int squirrel[] = { -1, -1, -1 }; |
| 4331 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4332 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4333 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4334 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4335 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4336 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4337 | pi->stopped_cmds = 0; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4338 | command = &pi->cmds[0]; |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4339 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4340 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4341 | if (pi->num_cmds != 1 |
| 4342 | || pi->followup == PIPE_BG |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4343 | || command->cmd_type == CMD_SUBSHELL |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4344 | ) { |
| 4345 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4346 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4347 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4348 | pi->alive_cmds = 1; |
| 4349 | |
| 4350 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 4351 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 4352 | |
| 4353 | if (command->group) { |
| 4354 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4355 | if (command->cmd_type == CMD_FUNCDEF) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4356 | /* "executing" func () { list } */ |
| 4357 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4358 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 4359 | funcp = new_function(command->argv[0]); |
| 4360 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4361 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4362 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4363 | funcp->body_as_string = command->group_as_string; |
| 4364 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4365 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4366 | command->group = NULL; |
| 4367 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 4368 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 4369 | funcp->parent_cmd = command; |
| 4370 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4371 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4372 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 4373 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4374 | return EXIT_SUCCESS; |
| 4375 | } |
| 4376 | #endif |
| 4377 | /* { list } */ |
| 4378 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4379 | rcode = 1; /* exitcode if redir failed */ |
| 4380 | if (setup_redirects(command, squirrel) == 0) { |
| 4381 | debug_printf_exec(": run_list\n"); |
| 4382 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4383 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 4384 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4385 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4386 | debug_leave(); |
| 4387 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4388 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4391 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 4392 | { |
| 4393 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4394 | #if ENABLE_HUSH_FUNCTIONS |
| 4395 | const struct function *funcp; |
| 4396 | #else |
| 4397 | enum { funcp = 0 }; |
| 4398 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4399 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4400 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4401 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4402 | if (argv[command->assignment_cnt] == NULL) { |
| 4403 | /* Assignments, but no command */ |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4404 | /* Ensure redirects take effect (that is, create files). |
| 4405 | * Try "a=t >file": */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4406 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4407 | restore_redirects(squirrel); |
| 4408 | /* Set shell variables */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 4409 | if (G_x_mode) |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4410 | bb_putchar_stderr('+'); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4411 | while (*argv) { |
| 4412 | p = expand_string_to_string(*argv); |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 4413 | if (G_x_mode) |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4414 | fprintf(stderr, " %s", p); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4415 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 4416 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4417 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4418 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4419 | } |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 4420 | if (G_x_mode) |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4421 | bb_putchar_stderr('\n'); |
Denys Vlasenko | b3389de | 2010-07-15 12:33:37 +0200 | [diff] [blame] | 4422 | /* Redirect error sets $? to 1. Otherwise, |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4423 | * if evaluating assignment value set $?, retain it. |
| 4424 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 4425 | if (rcode == 0) |
| 4426 | rcode = G.last_exitcode; |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4427 | /* Do we need to flag set_local_var() errors? |
| 4428 | * "assignment to readonly var" and "putenv error" |
| 4429 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4430 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4431 | debug_leave(); |
| 4432 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4433 | return rcode; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4434 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4435 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4436 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4437 | if (0) {} |
| 4438 | #if ENABLE_HUSH_BASH_COMPAT |
| 4439 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4440 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 4441 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4442 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4443 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 4444 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) { |
| 4445 | argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt); |
| 4446 | |
| 4447 | } |
| 4448 | #endif |
| 4449 | else { |
| 4450 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 4451 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4452 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4453 | /* if someone gives us an empty string: `cmd with empty output` */ |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4454 | if (!argv_expanded[0]) { |
Denys Vlasenko | 385cc59 | 2010-01-12 06:47:39 +0100 | [diff] [blame] | 4455 | free(argv_expanded); |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4456 | debug_leave(); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 4457 | return G.last_exitcode; |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4458 | } |
| 4459 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4460 | x = find_builtin(argv_expanded[0]); |
| 4461 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4462 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4463 | if (!x) |
| 4464 | funcp = find_function(argv_expanded[0]); |
| 4465 | #endif |
| 4466 | if (x || funcp) { |
| 4467 | if (!funcp) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 4468 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4469 | debug_printf("exec with redirects only\n"); |
| 4470 | rcode = setup_redirects(command, NULL); |
| 4471 | goto clean_up_and_ret1; |
| 4472 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4473 | } |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4474 | /* setup_redirects acts on file descriptors, not FILEs. |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4475 | * This is perfect for work that comes after exec(). |
| 4476 | * Is it really safe for inline use? Experimentally, |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4477 | * things seem to work. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4478 | rcode = setup_redirects(command, squirrel); |
| 4479 | if (rcode == 0) { |
| 4480 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4481 | dump_cmd_in_x_mode(new_env); |
| 4482 | dump_cmd_in_x_mode(argv_expanded); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4483 | old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4484 | if (!funcp) { |
| 4485 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4486 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 4487 | rcode = x->b_function(argv_expanded) & 0xff; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 4488 | fflush_all(); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4489 | } |
| 4490 | #if ENABLE_HUSH_FUNCTIONS |
| 4491 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4492 | # if ENABLE_HUSH_LOCAL |
| 4493 | struct variable **sv; |
| 4494 | sv = G.shadowed_vars_pp; |
| 4495 | G.shadowed_vars_pp = &old_vars; |
| 4496 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4497 | debug_printf_exec(": function '%s' '%s'...\n", |
| 4498 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 4499 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4500 | # if ENABLE_HUSH_LOCAL |
| 4501 | G.shadowed_vars_pp = sv; |
| 4502 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4503 | } |
| 4504 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4505 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4506 | clean_up_and_ret: |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4507 | restore_redirects(squirrel); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4508 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4509 | add_vars(old_vars); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4510 | clean_up_and_ret1: |
| 4511 | free(argv_expanded); |
| 4512 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4513 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4514 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 4515 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4516 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4517 | |
Denys Vlasenko | 8fa1f5d | 2010-07-15 08:18:46 +0200 | [diff] [blame] | 4518 | if (ENABLE_FEATURE_SH_STANDALONE) { |
| 4519 | int n = find_applet_by_name(argv_expanded[0]); |
| 4520 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 4521 | rcode = setup_redirects(command, squirrel); |
| 4522 | if (rcode == 0) { |
| 4523 | new_env = expand_assignments(argv, command->assignment_cnt); |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4524 | dump_cmd_in_x_mode(new_env); |
| 4525 | dump_cmd_in_x_mode(argv_expanded); |
Denys Vlasenko | 8fa1f5d | 2010-07-15 08:18:46 +0200 | [diff] [blame] | 4526 | old_vars = set_vars_and_save_old(new_env); |
| 4527 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 4528 | argv_expanded[0], argv_expanded[1]); |
| 4529 | rcode = run_nofork_applet(n, argv_expanded); |
| 4530 | } |
| 4531 | goto clean_up_and_ret; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4532 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4533 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4534 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4537 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4538 | /* NB: argv_expanded may already be created, and that |
| 4539 | * might include `cmd` runs! Do not rerun it! We *must* |
| 4540 | * use argv_expanded if it's non-NULL */ |
| 4541 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4542 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4543 | pi->alive_cmds = 0; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4544 | next_infd = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4545 | |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4546 | cmd_no = 0; |
| 4547 | while (cmd_no < pi->num_cmds) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4548 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4549 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4550 | volatile nommu_save_t nommu_save; |
| 4551 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4552 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4553 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4554 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4555 | #endif |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4556 | command = &pi->cmds[cmd_no]; |
| 4557 | cmd_no++; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4558 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4559 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 4560 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4561 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4562 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4563 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4564 | |
| 4565 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4566 | pipefds.rd = 0; |
| 4567 | pipefds.wr = 1; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4568 | if (cmd_no < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4569 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4570 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4571 | command->pid = BB_MMU ? fork() : vfork(); |
| 4572 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4573 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4574 | disable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4575 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4576 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4577 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4578 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4579 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4580 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4581 | pgrp = pi->pgrp; |
| 4582 | if (pgrp < 0) /* true for 1st process only */ |
| 4583 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4584 | if (setpgid(0, pgrp) == 0 |
| 4585 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4586 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4587 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4588 | /* We do it in *every* child, not just first, |
| 4589 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4590 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4591 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4592 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4593 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4594 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 4595 | /* 1st cmd in backgrounded pipe |
| 4596 | * should have its stdin /dev/null'ed */ |
| 4597 | close(0); |
| 4598 | if (open(bb_dev_null, O_RDONLY)) |
| 4599 | xopen("/", O_RDONLY); |
| 4600 | } else { |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4601 | xmove_fd(next_infd, 0); |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4602 | } |
| 4603 | xmove_fd(pipefds.wr, 1); |
| 4604 | if (pipefds.rd > 1) |
| 4605 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4606 | /* Like bash, explicit redirects override pipes, |
| 4607 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4608 | if (setup_redirects(command, NULL)) |
| 4609 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4610 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4611 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4612 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 4613 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4614 | /* Stores to nommu_save list of env vars putenv'ed |
| 4615 | * (NOMMU, on MMU we don't need that) */ |
| 4616 | /* cast away volatility... */ |
| 4617 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4618 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4619 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4620 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4621 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4622 | #if ENABLE_HUSH_FAST |
| 4623 | G.count_SIGCHLD++; |
| 4624 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4625 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4626 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4627 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4628 | /* Clean up after vforked child */ |
| 4629 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4630 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4631 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4632 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4633 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4634 | free(argv_expanded); |
| 4635 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4636 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4637 | /* Clearly indicate, was it fork or vfork */ |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 4638 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4639 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4640 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4641 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4642 | /* Second and next children need to know pid of first one */ |
| 4643 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4644 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4645 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4646 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4647 | |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4648 | if (cmd_no > 1) |
| 4649 | close(next_infd); |
| 4650 | if (cmd_no < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4651 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4652 | /* Pass read (output) pipe end to next iteration */ |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4653 | next_infd = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4654 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4655 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4656 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4657 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4658 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 4659 | return 1; |
| 4660 | } |
| 4661 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4662 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4663 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4664 | return -1; |
| 4665 | } |
| 4666 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4667 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4668 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 4669 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4670 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4671 | [PIPE_SEQ] = "SEQ", |
| 4672 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4673 | [PIPE_OR ] = "OR" , |
| 4674 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4675 | }; |
| 4676 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4677 | [RES_NONE ] = "NONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4678 | # if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4679 | [RES_IF ] = "IF" , |
| 4680 | [RES_THEN ] = "THEN" , |
| 4681 | [RES_ELIF ] = "ELIF" , |
| 4682 | [RES_ELSE ] = "ELSE" , |
| 4683 | [RES_FI ] = "FI" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4684 | # endif |
| 4685 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4686 | [RES_FOR ] = "FOR" , |
| 4687 | [RES_WHILE] = "WHILE", |
| 4688 | [RES_UNTIL] = "UNTIL", |
| 4689 | [RES_DO ] = "DO" , |
| 4690 | [RES_DONE ] = "DONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4691 | # endif |
| 4692 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4693 | [RES_IN ] = "IN" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4694 | # endif |
| 4695 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4696 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4697 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4698 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4699 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4700 | [RES_ESAC ] = "ESAC" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4701 | # endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4702 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4703 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4704 | }; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4705 | static const char *const CMDTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4706 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4707 | "()", |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4708 | "[noglob]", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4709 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4710 | "func()", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4711 | # endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4712 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4713 | |
| 4714 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4715 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4716 | pin = 0; |
| 4717 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4718 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4719 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4720 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4721 | while (prn < pi->num_cmds) { |
| 4722 | struct command *command = &pi->cmds[prn]; |
| 4723 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4724 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4725 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4726 | lvl*2, "", prn, |
| 4727 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4728 | if (command->group) { |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4729 | fprintf(stderr, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4730 | CMDTYPE[command->cmd_type], |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4731 | argv |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4732 | # if !BB_MMU |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4733 | , " group_as_string:", command->group_as_string |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4734 | # else |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4735 | , "", "" |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4736 | # endif |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4737 | ); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4738 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4739 | prn++; |
| 4740 | continue; |
| 4741 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4742 | if (argv) while (*argv) { |
| 4743 | fprintf(stderr, " '%s'", *argv); |
| 4744 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4745 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4746 | fprintf(stderr, "\n"); |
| 4747 | prn++; |
| 4748 | } |
| 4749 | pi = pi->next; |
| 4750 | pin++; |
| 4751 | } |
| 4752 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4753 | #endif /* debug_print_tree */ |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4754 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4755 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4756 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4757 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4758 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4759 | #if ENABLE_HUSH_CASE |
| 4760 | char *case_word = NULL; |
| 4761 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4762 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4763 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4764 | char **for_lcur = NULL; |
| 4765 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4766 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4767 | smallint last_followup; |
| 4768 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4769 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4770 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4771 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4772 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4773 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4774 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4775 | smallint rword; /* enum reserved_style */ |
| 4776 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4777 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4778 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4779 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4780 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4781 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4782 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4783 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4784 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4785 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4786 | continue; |
| 4787 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4788 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4789 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4790 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4791 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4792 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4793 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4794 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4795 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4796 | continue; |
| 4797 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4798 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4799 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4800 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4801 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4802 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4803 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4804 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4805 | } |
| 4806 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4807 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4808 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4809 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4810 | * in order to return, no direct "return" statements please. |
| 4811 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4812 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4813 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4814 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4815 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4816 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4817 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4818 | rword = RES_NONE; |
| 4819 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4820 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4821 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4822 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4823 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4824 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4825 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 4826 | if (G.flag_SIGINT) |
| 4827 | break; |
| 4828 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4829 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4830 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4831 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4832 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4833 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4834 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4835 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4836 | /* start of a loop: remember where loop starts */ |
| 4837 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4838 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4839 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4840 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4841 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4842 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4843 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4844 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4845 | ) { |
| 4846 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4847 | * and we should not execute CMD */ |
| 4848 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4849 | last_followup = pi->followup; |
| 4850 | continue; |
| 4851 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4852 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4853 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4854 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4855 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4856 | if (cond_code) { |
| 4857 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4858 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4859 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4860 | /* "if <false> THEN cmd": skip cmd */ |
| 4861 | continue; |
| 4862 | } |
| 4863 | } else { |
| 4864 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4865 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4866 | * skip cmd and all following ones */ |
| 4867 | break; |
| 4868 | } |
| 4869 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4870 | #endif |
| 4871 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4872 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4873 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4874 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4875 | |
| 4876 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4877 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4878 | }; /* encoded representation of "$@" */ |
| 4879 | static const char *const encoded_dollar_at_argv[] = { |
| 4880 | encoded_dollar_at, NULL |
| 4881 | }; /* argv list with one element: "$@" */ |
| 4882 | char **vals; |
| 4883 | |
| 4884 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4885 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4886 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4887 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4888 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4889 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4890 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4891 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4892 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4893 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4894 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4895 | debug_print_strings("for_list made from", vals); |
| 4896 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4897 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4898 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4899 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4900 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4901 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4902 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4903 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4904 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4905 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4906 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4907 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4908 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4909 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4910 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4911 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4912 | if (rword == RES_IN) { |
| 4913 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4914 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4915 | if (rword == RES_DONE) { |
| 4916 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4917 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4918 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4919 | #if ENABLE_HUSH_CASE |
| 4920 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4921 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4922 | continue; |
| 4923 | } |
| 4924 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4925 | char **argv; |
| 4926 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4927 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4928 | break; |
| 4929 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4930 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4931 | while (*argv) { |
| 4932 | char *pattern = expand_string_to_string(*argv); |
| 4933 | /* TODO: which FNM_xxx flags to use? */ |
| 4934 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 4935 | free(pattern); |
| 4936 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 4937 | free(case_word); /* make future "word)" stop */ |
| 4938 | case_word = NULL; |
| 4939 | break; |
| 4940 | } |
| 4941 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4942 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4943 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4944 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4945 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4946 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4947 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4948 | } |
| 4949 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4950 | /* Just pressing <enter> in shell should check for jobs. |
| 4951 | * OTOH, in non-interactive shell this is useless |
| 4952 | * and only leads to extra job checks */ |
| 4953 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4954 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4955 | goto check_jobs_and_continue; |
| 4956 | continue; |
| 4957 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4958 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4959 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4960 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4961 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4962 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4963 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4964 | { |
| 4965 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4966 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4967 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4968 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4969 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 4970 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4971 | /* We ran a builtin, function, or group. |
| 4972 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4973 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4974 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4975 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4976 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4977 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4978 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4979 | if (G.flag_break_continue) { |
| 4980 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4981 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4982 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4983 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4984 | G.depth_break_continue--; |
| 4985 | if (G.depth_break_continue == 0) |
| 4986 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4987 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 4988 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4989 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 4990 | goto check_jobs_and_break; |
| 4991 | /* "continue": simulate end of loop */ |
| 4992 | rword = RES_DONE; |
| 4993 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4994 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 4995 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 4996 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 4997 | if (G.flag_return_in_progress == 1) { |
| 4998 | /* same as "goto check_jobs_and_break" */ |
| 4999 | checkjobs(NULL); |
| 5000 | break; |
| 5001 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 5002 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5003 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5004 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5005 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5006 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 5007 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5008 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5009 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5010 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5011 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 5012 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5013 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5014 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5015 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5016 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5017 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5018 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5019 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5020 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5021 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5022 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5023 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5024 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5025 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5026 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5027 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5028 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5029 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5030 | } |
| 5031 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5032 | |
| 5033 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5034 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 5035 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5036 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5037 | #endif |
| 5038 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5039 | /* Beware of "while false; true; do ..."! */ |
| 5040 | if (pi->next && pi->next->res_word == RES_DO) { |
| 5041 | if (rword == RES_WHILE) { |
| 5042 | if (rcode) { |
| 5043 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5044 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5045 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 5046 | goto check_jobs_and_break; |
| 5047 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 5048 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5049 | if (rword == RES_UNTIL) { |
| 5050 | if (!rcode) { |
| 5051 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5052 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5053 | checkjobs(NULL); |
| 5054 | break; |
| 5055 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5056 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5057 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5058 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 5059 | |
| 5060 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 5061 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5062 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 5063 | |
| 5064 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5065 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 5066 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 5067 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5068 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5069 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 5070 | free(for_list); |
| 5071 | #endif |
| 5072 | #if ENABLE_HUSH_CASE |
| 5073 | free(case_word); |
| 5074 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5075 | debug_leave(); |
| 5076 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5077 | return rcode; |
| 5078 | } |
| 5079 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5080 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5081 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5082 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5083 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5084 | debug_printf_exec("run_and_free_list entered\n"); |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 5085 | if (!G.n_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5086 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5087 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5088 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5089 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5090 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5091 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5092 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 5093 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5094 | return rcode; |
| 5095 | } |
| 5096 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5097 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 5098 | static struct pipe *new_pipe(void) |
| 5099 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5100 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 5101 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5102 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5103 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5104 | return pi; |
| 5105 | } |
| 5106 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5107 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 5108 | * if ctx->command is NULL. |
| 5109 | * No errors possible here. |
| 5110 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5111 | static int done_command(struct parse_context *ctx) |
| 5112 | { |
| 5113 | /* The command is really already in the pipe structure, so |
| 5114 | * advance the pipe counter and make a new, null command. */ |
| 5115 | struct pipe *pi = ctx->pipe; |
| 5116 | struct command *command = ctx->command; |
| 5117 | |
| 5118 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5119 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5120 | debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5121 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5122 | } |
| 5123 | pi->num_cmds++; |
| 5124 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5125 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5126 | } else { |
| 5127 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 5128 | } |
| 5129 | |
| 5130 | /* Only real trickiness here is that the uncommitted |
| 5131 | * command structure is not counted in pi->num_cmds. */ |
| 5132 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5133 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 5134 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5135 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5136 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 5137 | } |
| 5138 | |
| 5139 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 5140 | { |
| 5141 | int not_null; |
| 5142 | |
| 5143 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 5144 | /* Close previous command */ |
| 5145 | not_null = done_command(ctx); |
| 5146 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5147 | #if HAS_KEYWORDS |
| 5148 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 5149 | ctx->ctx_inverted = 0; |
| 5150 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 5151 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5152 | |
| 5153 | /* Without this check, even just <enter> on command line generates |
| 5154 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5155 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5156 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 5157 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5158 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 5159 | #endif |
| 5160 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5161 | || ctx->ctx_res_w == RES_DONE |
| 5162 | || ctx->ctx_res_w == RES_FOR |
| 5163 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 5164 | #endif |
| 5165 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5166 | || ctx->ctx_res_w == RES_ESAC |
| 5167 | #endif |
| 5168 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5169 | struct pipe *new_p; |
| 5170 | debug_printf_parse("done_pipe: adding new pipe: " |
| 5171 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 5172 | not_null, ctx->ctx_res_w); |
| 5173 | new_p = new_pipe(); |
| 5174 | ctx->pipe->next = new_p; |
| 5175 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5176 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5177 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5178 | * This is used to control execution. |
| 5179 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 5180 | * cases where variable or value happens to match a keyword): |
| 5181 | */ |
| 5182 | #if ENABLE_HUSH_LOOPS |
| 5183 | if (ctx->ctx_res_w == RES_FOR |
| 5184 | || ctx->ctx_res_w == RES_IN) |
| 5185 | ctx->ctx_res_w = RES_NONE; |
| 5186 | #endif |
| 5187 | #if ENABLE_HUSH_CASE |
| 5188 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5189 | ctx->ctx_res_w = RES_CASE_BODY; |
| 5190 | if (ctx->ctx_res_w == RES_CASE) |
| 5191 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5192 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5193 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5194 | /* Create the memory for command, roughly: |
| 5195 | * ctx->pipe->cmds = new struct command; |
| 5196 | * ctx->command = &ctx->pipe->cmds[0]; |
| 5197 | */ |
| 5198 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5199 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5200 | } |
| 5201 | debug_printf_parse("done_pipe return\n"); |
| 5202 | } |
| 5203 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5204 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5205 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5206 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5207 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5208 | /* Create the memory for command, roughly: |
| 5209 | * ctx->pipe->cmds = new struct command; |
| 5210 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5211 | */ |
| 5212 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5213 | } |
| 5214 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5215 | /* If a reserved word is found and processed, parse context is modified |
| 5216 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5217 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5218 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5219 | struct reserved_combo { |
| 5220 | char literal[6]; |
| 5221 | unsigned char res; |
| 5222 | unsigned char assignment_flag; |
| 5223 | int flag; |
| 5224 | }; |
| 5225 | enum { |
| 5226 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5227 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5228 | FLAG_IF = (1 << RES_IF ), |
| 5229 | FLAG_THEN = (1 << RES_THEN ), |
| 5230 | FLAG_ELIF = (1 << RES_ELIF ), |
| 5231 | FLAG_ELSE = (1 << RES_ELSE ), |
| 5232 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5233 | # endif |
| 5234 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5235 | FLAG_FOR = (1 << RES_FOR ), |
| 5236 | FLAG_WHILE = (1 << RES_WHILE), |
| 5237 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 5238 | FLAG_DO = (1 << RES_DO ), |
| 5239 | FLAG_DONE = (1 << RES_DONE ), |
| 5240 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5241 | # endif |
| 5242 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5243 | FLAG_MATCH = (1 << RES_MATCH), |
| 5244 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5245 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5246 | FLAG_START = (1 << RES_XXXX ), |
| 5247 | }; |
| 5248 | |
| 5249 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 5250 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5251 | /* Mostly a list of accepted follow-up reserved words. |
| 5252 | * FLAG_END means we are done with the sequence, and are ready |
| 5253 | * to turn the compound list into a command. |
| 5254 | * FLAG_START means the word must start a new compound list. |
| 5255 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5256 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5257 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5258 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 5259 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 5260 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 5261 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 5262 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 5263 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5264 | # endif |
| 5265 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5266 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 5267 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 5268 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 5269 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 5270 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 5271 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5272 | # endif |
| 5273 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5274 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 5275 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5276 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5277 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5278 | const struct reserved_combo *r; |
| 5279 | |
| 5280 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 5281 | if (strcmp(word->data, r->literal) == 0) |
| 5282 | return r; |
| 5283 | } |
| 5284 | return NULL; |
| 5285 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5286 | /* Return 0: not a keyword, 1: keyword |
| 5287 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5288 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 5289 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5290 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5291 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5292 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5293 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5294 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5295 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5296 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5297 | if (word->o_quoted) |
| 5298 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5299 | r = match_reserved_word(word); |
| 5300 | if (!r) |
| 5301 | return 0; |
| 5302 | |
| 5303 | debug_printf("found reserved word %s, res %d\n", r->literal, r->res); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5304 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5305 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 5306 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5307 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5308 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5309 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5310 | if (r->flag == 0) { /* '!' */ |
| 5311 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5312 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5313 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5314 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5315 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5316 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5317 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5318 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5319 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5320 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5321 | old = xmalloc(sizeof(*old)); |
| 5322 | debug_printf_parse("push stack %p\n", old); |
| 5323 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5324 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5325 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5326 | } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5327 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5328 | ctx->ctx_res_w = RES_SNTX; |
| 5329 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5330 | } else { |
| 5331 | /* "{...} fi" is ok. "{...} if" is not |
| 5332 | * Example: |
| 5333 | * if { echo foo; } then { echo bar; } fi */ |
| 5334 | if (ctx->command->group) |
| 5335 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5336 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5337 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5338 | ctx->ctx_res_w = r->res; |
| 5339 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5340 | word->o_assignment = r->assignment_flag; |
| 5341 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5342 | if (ctx->old_flag & FLAG_END) { |
| 5343 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5344 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5345 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5346 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5347 | old = ctx->stack; |
| 5348 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5349 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5350 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5351 | o_addstr(&old->as_string, ctx->as_string.data); |
| 5352 | o_free_unsafe(&ctx->as_string); |
| 5353 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 5354 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 5355 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5356 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5357 | *ctx = *old; /* physical copy */ |
| 5358 | free(old); |
| 5359 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5360 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5361 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5362 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5363 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5364 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5365 | * Normal return is 0. Syntax errors return 1. |
| 5366 | * Note: on return, word is reset, but not o_free'd! |
| 5367 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5368 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5369 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5370 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5371 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5372 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5373 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5374 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 5375 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5376 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5377 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5378 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5379 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 5380 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5381 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5382 | * "2.7 Redirection |
| 5383 | * ...the word that follows the redirection operator |
| 5384 | * shall be subjected to tilde expansion, parameter expansion, |
| 5385 | * command substitution, arithmetic expansion, and quote |
| 5386 | * removal. Pathname expansion shall not be performed |
| 5387 | * on the word by a non-interactive shell; an interactive |
| 5388 | * shell may perform it, but shall do so only when |
| 5389 | * the expansion would result in one word." |
| 5390 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5391 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5392 | /* Cater for >\file case: |
| 5393 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 5394 | * Same with heredocs: |
| 5395 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 5396 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5397 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 5398 | unbackslash(ctx->pending_redirect->rd_filename); |
| 5399 | /* Is it <<"HEREDOC"? */ |
| 5400 | if (word->o_quoted) { |
| 5401 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 5402 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5403 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5404 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5405 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5406 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5407 | /* If this word wasn't an assignment, next ones definitely |
| 5408 | * can't be assignments. Even if they look like ones. */ |
| 5409 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 5410 | && word->o_assignment != WORD_IS_KEYWORD |
| 5411 | ) { |
| 5412 | word->o_assignment = NOT_ASSIGNMENT; |
| 5413 | } else { |
| 5414 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 5415 | command->assignment_cnt++; |
| 5416 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 5417 | } |
| 5418 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5419 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5420 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 5421 | if (ctx->ctx_dsemicolon |
| 5422 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 5423 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 5424 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5425 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 5426 | ctx->ctx_dsemicolon = 0; |
| 5427 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5428 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5429 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5430 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5431 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 5432 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5433 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5434 | # if ENABLE_HUSH_CASE |
| 5435 | && ctx->ctx_res_w != RES_CASE |
| 5436 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5437 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5438 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5439 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5440 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5441 | debug_printf_parse("done_word return %d\n", |
| 5442 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5443 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5444 | } |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5445 | # ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 5446 | if (strcmp(word->data, "export") == 0 |
| 5447 | # if ENABLE_HUSH_LOCAL |
| 5448 | || strcmp(word->data, "local") == 0 |
| 5449 | # endif |
| 5450 | ) { |
| 5451 | command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND; |
| 5452 | } else |
| 5453 | # endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5454 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5455 | if (strcmp(word->data, "[[") == 0) { |
| 5456 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 5457 | } |
| 5458 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5459 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5460 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5461 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5462 | if (command->group) { |
| 5463 | /* "{ echo foo; } echo bar" - bad */ |
| 5464 | syntax_error_at(word->data); |
| 5465 | debug_printf_parse("done_word return 1: syntax error, " |
| 5466 | "groups and arglists don't mix\n"); |
| 5467 | return 1; |
| 5468 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5469 | if (word->o_quoted /* word had "xx" or 'xx' at least as part of it. */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5470 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 5471 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5472 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5473 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5474 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5475 | char *p = word->data; |
| 5476 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 5477 | && (p[1] & 0x7f) == '@' |
| 5478 | && p[2] == SPECIAL_VAR_SYMBOL |
| 5479 | ) { |
| 5480 | p += 3; |
| 5481 | } |
| 5482 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5483 | /* saw no "$@", or not only "$@" but some |
| 5484 | * real text is there too */ |
| 5485 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5486 | * e.g. "", $empty"" etc to not disappear */ |
| 5487 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5488 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5489 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 5490 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 5491 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5492 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5493 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5494 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5495 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5496 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5497 | if (word->o_quoted |
| 5498 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 5499 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5500 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5501 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5502 | return 1; |
| 5503 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5504 | /* Force FOR to have just one word (variable name) */ |
| 5505 | /* NB: basically, this makes hush see "for v in ..." |
| 5506 | * syntax as if it is "for v; in ...". FOR and IN become |
| 5507 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5508 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5509 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5510 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5511 | #if ENABLE_HUSH_CASE |
| 5512 | /* Force CASE to have just one word */ |
| 5513 | if (ctx->ctx_res_w == RES_CASE) { |
| 5514 | done_pipe(ctx, PIPE_SEQ); |
| 5515 | } |
| 5516 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5517 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5518 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5519 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5520 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5521 | return 0; |
| 5522 | } |
| 5523 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5524 | |
| 5525 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 5526 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5527 | * Return: |
| 5528 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 5529 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 5530 | * REDIRFD_TO_FILE if no & was seen, |
| 5531 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5532 | */ |
| 5533 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5534 | #define parse_redir_right_fd(as_string, input) \ |
| 5535 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5536 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5537 | static int parse_redir_right_fd(o_string *as_string, struct in_str *input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5538 | { |
| 5539 | int ch, d, ok; |
| 5540 | |
| 5541 | ch = i_peek(input); |
| 5542 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5543 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5544 | |
| 5545 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5546 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5547 | ch = i_peek(input); |
| 5548 | if (ch == '-') { |
| 5549 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5550 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5551 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5552 | } |
| 5553 | d = 0; |
| 5554 | ok = 0; |
| 5555 | while (ch != EOF && isdigit(ch)) { |
| 5556 | d = d*10 + (ch-'0'); |
| 5557 | ok = 1; |
| 5558 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5559 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5560 | ch = i_peek(input); |
| 5561 | } |
| 5562 | if (ok) return d; |
| 5563 | |
| 5564 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 5565 | |
| 5566 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5567 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5570 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5571 | */ |
| 5572 | static int parse_redirect(struct parse_context *ctx, |
| 5573 | int fd, |
| 5574 | redir_type style, |
| 5575 | struct in_str *input) |
| 5576 | { |
| 5577 | struct command *command = ctx->command; |
| 5578 | struct redir_struct *redir; |
| 5579 | struct redir_struct **redirp; |
| 5580 | int dup_num; |
| 5581 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5582 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5583 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5584 | /* Check for a '>&1' type redirect */ |
| 5585 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 5586 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 5587 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5588 | } else { |
| 5589 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5590 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5591 | if (dup_num) { /* <<-... */ |
| 5592 | ch = i_getch(input); |
| 5593 | nommu_addchr(&ctx->as_string, ch); |
| 5594 | ch = i_peek(input); |
| 5595 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5596 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5597 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5598 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5599 | int ch = i_peek(input); |
| 5600 | if (ch == '|') { |
| 5601 | /* >|FILE redirect ("clobbering" >). |
| 5602 | * Since we do not support "set -o noclobber" yet, |
| 5603 | * >| and > are the same for now. Just eat |. |
| 5604 | */ |
| 5605 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5606 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5607 | } |
| 5608 | } |
| 5609 | |
| 5610 | /* Create a new redir_struct and append it to the linked list */ |
| 5611 | redirp = &command->redirects; |
| 5612 | while ((redir = *redirp) != NULL) { |
| 5613 | redirp = &(redir->next); |
| 5614 | } |
| 5615 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 5616 | /* redir->next = NULL; */ |
| 5617 | /* redir->rd_filename = NULL; */ |
| 5618 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5619 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5620 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5621 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 5622 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5623 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5624 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5625 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5626 | /* Erik had a check here that the file descriptor in question |
| 5627 | * is legit; I postpone that to "run time" |
| 5628 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5629 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 5630 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5631 | } else { |
| 5632 | /* Set ctx->pending_redirect, so we know what to do at the |
| 5633 | * end of the next parsed word. */ |
| 5634 | ctx->pending_redirect = redir; |
| 5635 | } |
| 5636 | return 0; |
| 5637 | } |
| 5638 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5639 | /* If a redirect is immediately preceded by a number, that number is |
| 5640 | * supposed to tell which file descriptor to redirect. This routine |
| 5641 | * looks for such preceding numbers. In an ideal world this routine |
| 5642 | * needs to handle all the following classes of redirects... |
| 5643 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 5644 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 5645 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 5646 | * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5647 | * |
| 5648 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5649 | * "2.7 Redirection |
| 5650 | * ... If n is quoted, the number shall not be recognized as part of |
| 5651 | * the redirection expression. For example: |
| 5652 | * echo \2>a |
| 5653 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5654 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5655 | * |
| 5656 | * A -1 return means no valid number was found, |
| 5657 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5658 | */ |
| 5659 | static int redirect_opt_num(o_string *o) |
| 5660 | { |
| 5661 | int num; |
| 5662 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5663 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5664 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5665 | num = bb_strtou(o->data, NULL, 10); |
| 5666 | if (errno || num < 0) |
| 5667 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5668 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5669 | return num; |
| 5670 | } |
| 5671 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5672 | #if BB_MMU |
| 5673 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 5674 | fetch_till_str(input, word, skip_tabs) |
| 5675 | #endif |
| 5676 | static char *fetch_till_str(o_string *as_string, |
| 5677 | struct in_str *input, |
| 5678 | const char *word, |
| 5679 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5680 | { |
| 5681 | o_string heredoc = NULL_O_STRING; |
| 5682 | int past_EOL = 0; |
| 5683 | int ch; |
| 5684 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5685 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5686 | while (1) { |
| 5687 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5688 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5689 | if (ch == '\n') { |
| 5690 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 5691 | heredoc.data[past_EOL] = '\0'; |
| 5692 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 5693 | return heredoc.data; |
| 5694 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5695 | do { |
| 5696 | o_addchr(&heredoc, ch); |
| 5697 | past_EOL = heredoc.length; |
| 5698 | jump_in: |
| 5699 | do { |
| 5700 | ch = i_getch(input); |
| 5701 | nommu_addchr(as_string, ch); |
| 5702 | } while (skip_tabs && ch == '\t'); |
| 5703 | } while (ch == '\n'); |
| 5704 | } |
| 5705 | if (ch == EOF) { |
| 5706 | o_free_unsafe(&heredoc); |
| 5707 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5708 | } |
| 5709 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5710 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5711 | } |
| 5712 | } |
| 5713 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5714 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5715 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5716 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5717 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5718 | { |
| 5719 | struct pipe *pi = ctx->list_head; |
| 5720 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5721 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5722 | int i; |
| 5723 | struct command *cmd = pi->cmds; |
| 5724 | |
| 5725 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5726 | pi->num_cmds, |
| 5727 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5728 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5729 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5730 | |
| 5731 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5732 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5733 | while (redir) { |
| 5734 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5735 | char *p; |
| 5736 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5737 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5738 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5739 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5740 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5741 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5742 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5743 | return 1; |
| 5744 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5745 | free(redir->rd_filename); |
| 5746 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5747 | heredoc_cnt--; |
| 5748 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5749 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5750 | } |
| 5751 | cmd++; |
| 5752 | } |
| 5753 | pi = pi->next; |
| 5754 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5755 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5756 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5757 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5758 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5759 | #endif |
| 5760 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
| 5763 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5764 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5765 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5766 | { |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5767 | pid_t pid; |
| 5768 | int channel[2]; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5769 | # if !BB_MMU |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 5770 | char **to_free = NULL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5771 | # endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5772 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5773 | xpipe(channel); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 5774 | pid = BB_MMU ? xfork() : xvfork(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5775 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5776 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5777 | /* Process substitution is not considered to be usual |
| 5778 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5779 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5780 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5781 | bb_signals(0 |
| 5782 | + (1 << SIGTSTP) |
| 5783 | + (1 << SIGTTIN) |
| 5784 | + (1 << SIGTTOU) |
| 5785 | , SIG_IGN); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5786 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5787 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5788 | xmove_fd(channel[1], 1); |
| 5789 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5790 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 91836ba | 2009-09-23 01:46:19 +0200 | [diff] [blame] | 5791 | /* Awful hack for `trap` or $(trap). |
| 5792 | * |
| 5793 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5794 | * contains an example where "trap" is executed in a subshell: |
| 5795 | * |
| 5796 | * save_traps=$(trap) |
| 5797 | * ... |
| 5798 | * eval "$save_traps" |
| 5799 | * |
| 5800 | * Standard does not say that "trap" in subshell shall print |
| 5801 | * parent shell's traps. It only says that its output |
| 5802 | * must have suitable form, but then, in the above example |
| 5803 | * (which is not supposed to be normative), it implies that. |
| 5804 | * |
| 5805 | * bash (and probably other shell) does implement it |
| 5806 | * (traps are reset to defaults, but "trap" still shows them), |
| 5807 | * but as a result, "trap" logic is hopelessly messed up: |
| 5808 | * |
| 5809 | * # trap |
| 5810 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5811 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5812 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5813 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5814 | * trap -- 'echo Ho' SIGWINCH |
| 5815 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5816 | * trap -- 'echo Ho' SIGWINCH |
| 5817 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5818 | * trap -- 'echo Ho' SIGWINCH |
| 5819 | * |
| 5820 | * The rules when to forget and when to not forget traps |
| 5821 | * get really complex and nonsensical. |
| 5822 | * |
| 5823 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5824 | */ |
| 5825 | s = skip_whitespace(s); |
| 5826 | if (strncmp(s, "trap", 4) == 0 && (*skip_whitespace(s + 4) == '\0')) |
| 5827 | { |
| 5828 | static const char *const argv[] = { NULL, NULL }; |
| 5829 | builtin_trap((char**)argv); |
| 5830 | exit(0); /* not _exit() - we need to fflush */ |
| 5831 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5832 | # if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5833 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5834 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5835 | _exit(G.last_exitcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5836 | # else |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5837 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5838 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5839 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5840 | * echo OK |
| 5841 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5842 | re_execute_shell(&to_free, |
| 5843 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5844 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5845 | G.global_argv + 1, |
| 5846 | NULL); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5847 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5848 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5849 | |
| 5850 | /* parent */ |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5851 | *pid_p = pid; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5852 | # if ENABLE_HUSH_FAST |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5853 | G.count_SIGCHLD++; |
| 5854 | //bb_error_msg("[%d] fork in generate_stream_from_string: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5855 | # endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5856 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5857 | # if !BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5858 | free(to_free); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5859 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5860 | close(channel[1]); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5861 | close_on_exec_on(channel[0]); |
| 5862 | return xfdopen_for_read(channel[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5865 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5866 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5867 | { |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5868 | FILE *fp; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5869 | struct in_str pipe_str; |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5870 | pid_t pid; |
| 5871 | int status, ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5872 | |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5873 | fp = generate_stream_from_string(s, &pid); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5874 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5875 | /* Now send results of command back into original context */ |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5876 | setup_file_in_str(&pipe_str, fp); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5877 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5878 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5879 | if (ch == '\n') { |
| 5880 | eol_cnt++; |
| 5881 | continue; |
| 5882 | } |
| 5883 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5884 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5885 | eol_cnt--; |
| 5886 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5887 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5888 | } |
| 5889 | |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5890 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5891 | fclose(fp); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5892 | /* We need to extract exitcode. Test case |
| 5893 | * "true; echo `sleep 1; false` $?" |
| 5894 | * should print 1 */ |
| 5895 | safe_waitpid(pid, &status, 0); |
| 5896 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5897 | return WEXITSTATUS(status); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5898 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5899 | #endif /* ENABLE_HUSH_TICK */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5900 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 5901 | #if !ENABLE_HUSH_FUNCTIONS |
| 5902 | #define parse_group(dest, ctx, input, ch) \ |
| 5903 | parse_group(ctx, input, ch) |
| 5904 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5905 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5906 | struct in_str *input, int ch) |
| 5907 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5908 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5909 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5910 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5911 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5912 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5913 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5914 | |
| 5915 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5916 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5917 | if (ch == '(' && !dest->o_quoted) { |
| 5918 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5919 | if (done_word(dest, ctx)) |
| 5920 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5921 | if (!command->argv) |
| 5922 | goto skip; /* (... */ |
| 5923 | if (command->argv[1]) { /* word word ... (... */ |
| 5924 | syntax_error_unexpected_ch('('); |
| 5925 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5926 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5927 | /* it is "word(..." or "word (..." */ |
| 5928 | do |
| 5929 | ch = i_getch(input); |
| 5930 | while (ch == ' ' || ch == '\t'); |
| 5931 | if (ch != ')') { |
| 5932 | syntax_error_unexpected_ch(ch); |
| 5933 | return 1; |
| 5934 | } |
| 5935 | nommu_addchr(&ctx->as_string, ch); |
| 5936 | do |
| 5937 | ch = i_getch(input); |
| 5938 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 5939 | if (ch != '{') { |
| 5940 | syntax_error_unexpected_ch(ch); |
| 5941 | return 1; |
| 5942 | } |
| 5943 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5944 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5945 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5946 | } |
| 5947 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5948 | |
| 5949 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5950 | if (command->argv /* word [word]{... */ |
| 5951 | || dest->length /* word{... */ |
| 5952 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5953 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5954 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5955 | debug_printf_parse("parse_group return 1: " |
| 5956 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5957 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5958 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 5959 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5960 | |
| 5961 | #if ENABLE_HUSH_FUNCTIONS |
| 5962 | skip: |
| 5963 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5964 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 5965 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5966 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5967 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5968 | } else { |
| 5969 | /* bash does not allow "{echo...", requires whitespace */ |
| 5970 | ch = i_getch(input); |
| 5971 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 5972 | syntax_error_unexpected_ch(ch); |
| 5973 | return 1; |
| 5974 | } |
| 5975 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5976 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5977 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5978 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5979 | #if BB_MMU |
| 5980 | # define as_string NULL |
| 5981 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5982 | char *as_string = NULL; |
| 5983 | #endif |
| 5984 | pipe_list = parse_stream(&as_string, input, endch); |
| 5985 | #if !BB_MMU |
| 5986 | if (as_string) |
| 5987 | o_addstr(&ctx->as_string, as_string); |
| 5988 | #endif |
| 5989 | /* empty ()/{} or parse error? */ |
| 5990 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5991 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 5992 | if (!BB_MMU) |
| 5993 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5994 | debug_printf_parse("parse_group return 1: " |
| 5995 | "parse_stream returned %p\n", pipe_list); |
| 5996 | return 1; |
| 5997 | } |
| 5998 | command->group = pipe_list; |
| 5999 | #if !BB_MMU |
| 6000 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 6001 | command->group_as_string = as_string; |
| 6002 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 6003 | command->group_as_string); |
| 6004 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6005 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6006 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6007 | debug_printf_parse("parse_group return 0\n"); |
| 6008 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6009 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6010 | } |
| 6011 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6012 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6013 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6014 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6015 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6016 | static void add_till_single_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6017 | { |
| 6018 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6019 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6020 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6021 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6022 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6023 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6024 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6025 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6026 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6027 | } |
| 6028 | } |
| 6029 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6030 | static void add_till_double_quote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6031 | { |
| 6032 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6033 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6034 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6035 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6036 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6037 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6038 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6039 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6040 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6041 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6042 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6043 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6044 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6045 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6046 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6047 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6048 | continue; |
| 6049 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 6050 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } |
| 6053 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 6054 | * \` quoting. |
| 6055 | * "Within the backquoted style of command substitution, backslash |
| 6056 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 6057 | * The search for the matching backquote shall be satisfied by the first |
| 6058 | * backquote found without a preceding backslash; during this search, |
| 6059 | * if a non-escaped backquote is encountered within a shell comment, |
| 6060 | * a here-document, an embedded command substitution of the $(command) |
| 6061 | * form, or a quoted string, undefined results occur. A single-quoted |
| 6062 | * or double-quoted string that begins, but does not end, within the |
| 6063 | * "`...`" sequence produces undefined results." |
| 6064 | * Example Output |
| 6065 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 6066 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6067 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6068 | { |
| 6069 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6070 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6071 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6072 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6073 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6074 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6075 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6076 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6077 | if (ch == '\\') { |
| 6078 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6079 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6080 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6081 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6082 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6083 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6084 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6085 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6086 | ch = ch2; |
| 6087 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6088 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6089 | } |
| 6090 | } |
| 6091 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 6092 | * quoting and nested ()s. |
| 6093 | * "With the $(command) style of command substitution, all characters |
| 6094 | * following the open parenthesis to the matching closing parenthesis |
| 6095 | * constitute the command. Any valid shell script can be used for command, |
| 6096 | * except a script consisting solely of redirections which produces |
| 6097 | * unspecified results." |
| 6098 | * Example Output |
| 6099 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 6100 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 6101 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6102 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6103 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6104 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6105 | * In bash compat mode, it needs to also be able to stop on '}' or ':' |
| 6106 | * for ${var:N[:M]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6107 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6108 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6109 | static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsigned end_ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6110 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6111 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6112 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6113 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6114 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6115 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6116 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 6117 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6118 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6119 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6120 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6121 | syntax_error_unterm_ch(end_ch); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6122 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6123 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6124 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6125 | if (!dbl) |
| 6126 | break; |
| 6127 | /* we look for closing )) of $((EXPR)) */ |
| 6128 | if (i_peek(input) == end_ch) { |
| 6129 | i_getch(input); /* eat second ')' */ |
| 6130 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6131 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6132 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6133 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6134 | if (ch == '(' || ch == '{') { |
| 6135 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6136 | add_till_closing_bracket(dest, input, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6137 | o_addchr(dest, ch); |
| 6138 | continue; |
| 6139 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6140 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6141 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6142 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6143 | continue; |
| 6144 | } |
| 6145 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6146 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6147 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6148 | continue; |
| 6149 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6150 | if (ch == '`') { |
| 6151 | add_till_backquote(dest, input); |
| 6152 | o_addchr(dest, ch); |
| 6153 | continue; |
| 6154 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6155 | if (ch == '\\') { |
| 6156 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6157 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6158 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6159 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6160 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6161 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6162 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6163 | continue; |
| 6164 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6165 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6166 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6167 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6168 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6169 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6170 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6171 | #if BB_MMU |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6172 | #define parse_dollar(as_string, dest, input) \ |
| 6173 | parse_dollar(dest, input) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6174 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6175 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6176 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6177 | o_string *dest, |
| 6178 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6179 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6180 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6181 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6182 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6183 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 6184 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6185 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6186 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 6187 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6188 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6189 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6190 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6191 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 6192 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6193 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 6194 | if (!isalnum(ch) && ch != '_') |
| 6195 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6196 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6197 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6198 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6199 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6200 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 6201 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6202 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6203 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6204 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 6205 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6206 | o_addchr(dest, ch | quote_mask); |
| 6207 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6208 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6209 | case '$': /* pid */ |
| 6210 | case '!': /* last bg pid */ |
| 6211 | case '?': /* last exit code */ |
| 6212 | case '#': /* number of args */ |
| 6213 | case '*': /* args */ |
| 6214 | case '@': /* args */ |
| 6215 | goto make_one_char_var; |
| 6216 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 6217 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6218 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6219 | ch = i_getch(input); /* eat '{' */ |
| 6220 | nommu_addchr(as_string, ch); |
| 6221 | |
| 6222 | ch = i_getch(input); /* first char after '{' */ |
| 6223 | nommu_addchr(as_string, ch); |
| 6224 | /* It should be ${?}, or ${#var}, |
| 6225 | * or even ${?+subst} - operator acting on a special variable, |
| 6226 | * or the beginning of variable name. |
| 6227 | */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 6228 | if (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) { /* not one of those */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6229 | bad_dollar_syntax: |
| 6230 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6231 | debug_printf_parse("parse_dollar return 1: unterminated ${name}\n"); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6232 | return 1; |
| 6233 | } |
| 6234 | ch |= quote_mask; |
| 6235 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6236 | /* It's possible to just call add_till_closing_bracket() at this point. |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6237 | * However, this regresses some of our testsuite cases |
| 6238 | * which check invalid constructs like ${%}. |
| 6239 | * Oh well... let's check that the var name part is fine... */ |
| 6240 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6241 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6242 | unsigned pos; |
| 6243 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6244 | o_addchr(dest, ch); |
| 6245 | debug_printf_parse(": '%c'\n", ch); |
| 6246 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6247 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6248 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6249 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6250 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6251 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6252 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6253 | unsigned end_ch; |
| 6254 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6255 | /* handle parameter expansions |
| 6256 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 6257 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6258 | if (!strchr("%#:-=+?", ch)) /* ${var<bad_char>... */ |
| 6259 | goto bad_dollar_syntax; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6260 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6261 | |
| 6262 | /* Eat everything until closing '}' (or ':') */ |
| 6263 | end_ch = '}'; |
| 6264 | if (ENABLE_HUSH_BASH_COMPAT |
| 6265 | && ch == ':' |
| 6266 | && !strchr("%#:-=+?"+3, i_peek(input)) |
| 6267 | ) { |
| 6268 | /* It's ${var:N[:M]} thing */ |
| 6269 | end_ch = '}' * 0x100 + ':'; |
| 6270 | } |
| 6271 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6272 | if (!BB_MMU) |
| 6273 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6274 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6275 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6276 | #else |
| 6277 | #error Simple code to only allow ${var} is not implemented |
| 6278 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6279 | if (as_string) { |
| 6280 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6281 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6282 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6283 | |
| 6284 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 6285 | /* close the first block: */ |
| 6286 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6287 | /* while parsing N from ${var:N[:M]}... */ |
| 6288 | if ((end_ch & 0xff) == last_ch) { |
| 6289 | /* ...got ':' - parse the rest */ |
| 6290 | end_ch = '}'; |
| 6291 | goto again; |
| 6292 | } |
| 6293 | /* ...got '}', not ':' - it's ${var:N}! emulate :999999999 */ |
| 6294 | o_addstr(dest, "999999999"); |
| 6295 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6296 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6297 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6298 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6299 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6300 | break; |
| 6301 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6302 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6303 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6304 | unsigned pos; |
| 6305 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6306 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6307 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6308 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6309 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6310 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6311 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6312 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6313 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6314 | if (!BB_MMU) |
| 6315 | pos = dest->length; |
| 6316 | add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6317 | if (as_string) { |
| 6318 | o_addstr(as_string, dest->data + pos); |
| 6319 | o_addchr(as_string, ')'); |
| 6320 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6321 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6322 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6323 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6324 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6325 | # endif |
| 6326 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6327 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6328 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6329 | if (!BB_MMU) |
| 6330 | pos = dest->length; |
| 6331 | add_till_closing_bracket(dest, input, ')'); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6332 | if (as_string) { |
| 6333 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 6334 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6335 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6336 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6337 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6338 | break; |
| 6339 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6340 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6341 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6342 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6343 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6344 | ch = i_peek(input); |
| 6345 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 6346 | ch = '_'; |
| 6347 | goto make_var; |
| 6348 | } |
| 6349 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 6350 | /* TODO: $_ and $-: */ |
| 6351 | /* $_ Shell or shell script name; or last argument of last command |
| 6352 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 6353 | * but in command's env, set to full pathname used to invoke it */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6354 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 6355 | default: |
| 6356 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6357 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6358 | debug_printf_parse("parse_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6359 | return 0; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6360 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6363 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6364 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6365 | parse_stream_dquoted(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6366 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6367 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6368 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6369 | o_string *dest, |
| 6370 | struct in_str *input, |
| 6371 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6372 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6373 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6374 | int next; |
| 6375 | |
| 6376 | again: |
| 6377 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6378 | if (ch != EOF) |
| 6379 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6380 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6381 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6382 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6383 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 6384 | return 0; |
| 6385 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6386 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6387 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6388 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6389 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6390 | } |
| 6391 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6392 | if (ch != '\n') { |
| 6393 | next = i_peek(input); |
| 6394 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6395 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6396 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6397 | if (ch == '\\') { |
| 6398 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6399 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6400 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6401 | } |
| 6402 | /* bash: |
| 6403 | * "The backslash retains its special meaning [in "..."] |
| 6404 | * only when followed by one of the following characters: |
| 6405 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6406 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6407 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6408 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 6409 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6410 | if (ch != '\n') { |
| 6411 | o_addqchr(dest, ch); |
| 6412 | nommu_addchr(as_string, ch); |
| 6413 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6414 | } else { |
| 6415 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 6416 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6417 | } |
| 6418 | goto again; |
| 6419 | } |
| 6420 | if (ch == '$') { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6421 | if (parse_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6422 | debug_printf_parse("parse_stream_dquoted return 1: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6423 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6424 | return 1; |
| 6425 | } |
| 6426 | goto again; |
| 6427 | } |
| 6428 | #if ENABLE_HUSH_TICK |
| 6429 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6430 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6431 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6432 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6433 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6434 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6435 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6436 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6437 | } |
| 6438 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6439 | o_addQchr(dest, ch); |
| 6440 | if (ch == '=' |
| 6441 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 6442 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6443 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6444 | ) { |
| 6445 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 6446 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6447 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6448 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6449 | } |
| 6450 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6451 | /* |
| 6452 | * Scan input until EOF or end_trigger char. |
| 6453 | * Return a list of pipes to execute, or NULL on EOF |
| 6454 | * or if end_trigger character is met. |
| 6455 | * On syntax error, exit is shell is not interactive, |
| 6456 | * reset parsing machinery and start parsing anew, |
| 6457 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6458 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6459 | static struct pipe *parse_stream(char **pstring, |
| 6460 | struct in_str *input, |
| 6461 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6462 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6463 | struct parse_context ctx; |
| 6464 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6465 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6466 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6467 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6468 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6469 | * A single-quote triggers a bypass of the main loop until its mate is |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6470 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 6471 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6472 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 6473 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6474 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6475 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6476 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 6477 | * Preventing this: */ |
| 6478 | o_addchr(&dest, '\0'); |
| 6479 | dest.length = 0; |
| 6480 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6481 | G.ifs = get_local_var_value("IFS"); |
| 6482 | if (G.ifs == NULL) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 6483 | G.ifs = defifs; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6484 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6485 | reset: |
| 6486 | #if ENABLE_HUSH_INTERACTIVE |
| 6487 | input->promptmode = 0; /* PS1 */ |
| 6488 | #endif |
| 6489 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 6490 | initialize_context(&ctx); |
| 6491 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6492 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6493 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6494 | const char *is_ifs; |
| 6495 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6496 | int ch; |
| 6497 | int next; |
| 6498 | int redir_fd; |
| 6499 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6500 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6501 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6502 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6503 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6504 | goto parse_error; |
| 6505 | } |
| 6506 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6507 | is_in_dquote = 0; |
| 6508 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6509 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6510 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 6511 | ch, ch, dest.o_escape); |
| 6512 | if (ch == EOF) { |
| 6513 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6514 | |
| 6515 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6516 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6517 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6518 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6519 | /* end_trigger == '}' case errors out earlier, |
| 6520 | * checking only ')' */ |
| 6521 | if (end_trigger == ')') { |
| 6522 | syntax_error_unterm_ch('('); /* exits */ |
| 6523 | /* goto parse_error; */ |
| 6524 | } |
| 6525 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6526 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6527 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6528 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6529 | o_free(&dest); |
| 6530 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6531 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6532 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6533 | /* (this makes bare "&" cmd a no-op. |
| 6534 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6535 | if (pi->num_cmds == 0 |
| 6536 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 6537 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6538 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6539 | pi = NULL; |
| 6540 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6541 | #if !BB_MMU |
| 6542 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6543 | if (pstring) |
| 6544 | *pstring = ctx.as_string.data; |
| 6545 | else |
| 6546 | o_free_unsafe(&ctx.as_string); |
| 6547 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6548 | debug_leave(); |
| 6549 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6550 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6551 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6552 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6553 | |
| 6554 | next = '\0'; |
| 6555 | if (ch != '\n') |
| 6556 | next = i_peek(input); |
| 6557 | |
| 6558 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 6559 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 6560 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 6561 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 6562 | || dest.length /* word{... - non-special */ |
| 6563 | || dest.o_quoted /* ""{... - non-special */ |
| 6564 | || (next != ';' /* }; - special */ |
| 6565 | && next != ')' /* }) - special */ |
| 6566 | && next != '&' /* }& and }&& ... - special */ |
| 6567 | && next != '|' /* }|| ... - special */ |
| 6568 | && !strchr(G.ifs, next) /* {word - non-special */ |
| 6569 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6570 | ) { |
| 6571 | /* They are not special, skip "{}" */ |
| 6572 | is_special += 2; |
| 6573 | } |
| 6574 | is_special = strchr(is_special, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6575 | is_ifs = strchr(G.ifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6576 | |
| 6577 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 6578 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6579 | o_addQchr(&dest, ch); |
| 6580 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 6581 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6582 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6583 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6584 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6585 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6586 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6587 | continue; |
| 6588 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6589 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6590 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6591 | if (done_word(&dest, &ctx)) { |
| 6592 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 6593 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6594 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6595 | #if ENABLE_HUSH_CASE |
| 6596 | /* "case ... in <newline> word) ..." - |
| 6597 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6598 | if (ctx.command->argv == NULL |
| 6599 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6600 | ) { |
| 6601 | continue; |
| 6602 | } |
| 6603 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6604 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6605 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6606 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 6607 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6608 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6609 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6610 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6611 | heredoc_cnt = 0; |
| 6612 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6613 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6614 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 6615 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6616 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6617 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6618 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6619 | |
| 6620 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 6621 | * } is an ordinary char in this case, even inside { cmd; } |
| 6622 | * Pathological example: { ""}; } should exec "}" cmd |
| 6623 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6624 | if (ch == '}') { |
| 6625 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 6626 | || dest.length != 0 /* word} */ |
| 6627 | || dest.o_quoted /* ""} */ |
| 6628 | ) { |
| 6629 | goto ordinary_char; |
| 6630 | } |
| 6631 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 6632 | goto skip_end_trigger; |
| 6633 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6634 | } |
| 6635 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6636 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6637 | && (ch != ';' || heredoc_cnt == 0) |
| 6638 | #if ENABLE_HUSH_CASE |
| 6639 | && (ch != ')' |
| 6640 | || ctx.ctx_res_w != RES_MATCH |
| 6641 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 6642 | ) |
| 6643 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6644 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6645 | if (heredoc_cnt) { |
| 6646 | /* This is technically valid: |
| 6647 | * { cat <<HERE; }; echo Ok |
| 6648 | * heredoc |
| 6649 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6650 | * HERE |
| 6651 | * but we don't support this. |
| 6652 | * We require heredoc to be in enclosing {}/(), |
| 6653 | * if any. |
| 6654 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6655 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6656 | goto parse_error; |
| 6657 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6658 | if (done_word(&dest, &ctx)) { |
| 6659 | goto parse_error; |
| 6660 | } |
| 6661 | done_pipe(&ctx, PIPE_SEQ); |
| 6662 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6663 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6664 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6665 | IF_HAS_KEYWORDS(|| (ctx.ctx_res_w == RES_NONE && ctx.old_flag == 0)) |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6666 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6667 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6668 | #if !BB_MMU |
| 6669 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6670 | if (pstring) |
| 6671 | *pstring = ctx.as_string.data; |
| 6672 | else |
| 6673 | o_free_unsafe(&ctx.as_string); |
| 6674 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6675 | debug_leave(); |
| 6676 | debug_printf_parse("parse_stream return %p: " |
| 6677 | "end_trigger char found\n", |
| 6678 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6679 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6680 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6681 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6682 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6683 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6684 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6685 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 6686 | /* Catch <, > before deciding whether this word is |
| 6687 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 6688 | switch (ch) { |
| 6689 | case '>': |
| 6690 | redir_fd = redirect_opt_num(&dest); |
| 6691 | if (done_word(&dest, &ctx)) { |
| 6692 | goto parse_error; |
| 6693 | } |
| 6694 | redir_style = REDIRECT_OVERWRITE; |
| 6695 | if (next == '>') { |
| 6696 | redir_style = REDIRECT_APPEND; |
| 6697 | ch = i_getch(input); |
| 6698 | nommu_addchr(&ctx.as_string, ch); |
| 6699 | } |
| 6700 | #if 0 |
| 6701 | else if (next == '(') { |
| 6702 | syntax_error(">(process) not supported"); |
| 6703 | goto parse_error; |
| 6704 | } |
| 6705 | #endif |
| 6706 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6707 | goto parse_error; |
| 6708 | continue; /* back to top of while (1) */ |
| 6709 | case '<': |
| 6710 | redir_fd = redirect_opt_num(&dest); |
| 6711 | if (done_word(&dest, &ctx)) { |
| 6712 | goto parse_error; |
| 6713 | } |
| 6714 | redir_style = REDIRECT_INPUT; |
| 6715 | if (next == '<') { |
| 6716 | redir_style = REDIRECT_HEREDOC; |
| 6717 | heredoc_cnt++; |
| 6718 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 6719 | ch = i_getch(input); |
| 6720 | nommu_addchr(&ctx.as_string, ch); |
| 6721 | } else if (next == '>') { |
| 6722 | redir_style = REDIRECT_IO; |
| 6723 | ch = i_getch(input); |
| 6724 | nommu_addchr(&ctx.as_string, ch); |
| 6725 | } |
| 6726 | #if 0 |
| 6727 | else if (next == '(') { |
| 6728 | syntax_error("<(process) not supported"); |
| 6729 | goto parse_error; |
| 6730 | } |
| 6731 | #endif |
| 6732 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6733 | goto parse_error; |
| 6734 | continue; /* back to top of while (1) */ |
| 6735 | } |
| 6736 | |
| 6737 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 6738 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 6739 | && !ctx.pending_redirect |
| 6740 | ) { |
| 6741 | /* ch is a special char and thus this word |
| 6742 | * cannot be an assignment */ |
| 6743 | dest.o_assignment = NOT_ASSIGNMENT; |
| 6744 | } |
| 6745 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6746 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 6747 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6748 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6749 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6750 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6751 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6752 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6753 | if (ch == EOF || ch == '\n') |
| 6754 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6755 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6756 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6757 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6758 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6759 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6760 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6761 | } |
| 6762 | break; |
| 6763 | case '\\': |
| 6764 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6765 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6766 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6767 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6768 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6769 | if (ch != '\n') { |
| 6770 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6771 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6772 | o_addchr(&dest, ch); |
| 6773 | nommu_addchr(&ctx.as_string, ch); |
| 6774 | /* Example: echo Hello \2>file |
| 6775 | * we need to know that word 2 is quoted */ |
| 6776 | dest.o_quoted = 1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6777 | } |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6778 | #if !BB_MMU |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6779 | else { |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6780 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 6781 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6782 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 6783 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6784 | break; |
| 6785 | case '$': |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6786 | if (parse_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6787 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6788 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6789 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6790 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6791 | break; |
| 6792 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6793 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6794 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6795 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6796 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6797 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6798 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6799 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6800 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6801 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6802 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6803 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6804 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6805 | break; |
| 6806 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6807 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6808 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6809 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6810 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6811 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6812 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6813 | case '`': { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6814 | unsigned pos; |
| 6815 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6816 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6817 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6818 | pos = dest.length; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6819 | add_till_backquote(&dest, input); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6820 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6821 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6822 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6823 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6824 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6825 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6826 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6827 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6828 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6829 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6830 | #if ENABLE_HUSH_CASE |
| 6831 | case_semi: |
| 6832 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6833 | if (done_word(&dest, &ctx)) { |
| 6834 | goto parse_error; |
| 6835 | } |
| 6836 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6837 | #if ENABLE_HUSH_CASE |
| 6838 | /* Eat multiple semicolons, detect |
| 6839 | * whether it means something special */ |
| 6840 | while (1) { |
| 6841 | ch = i_peek(input); |
| 6842 | if (ch != ';') |
| 6843 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6844 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6845 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6846 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6847 | ctx.ctx_dsemicolon = 1; |
| 6848 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6849 | break; |
| 6850 | } |
| 6851 | } |
| 6852 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6853 | new_cmd: |
| 6854 | /* We just finished a cmd. New one may start |
| 6855 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6856 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6857 | break; |
| 6858 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6859 | if (done_word(&dest, &ctx)) { |
| 6860 | goto parse_error; |
| 6861 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6862 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6863 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6864 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6865 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6866 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6867 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6868 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6869 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6870 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6871 | if (done_word(&dest, &ctx)) { |
| 6872 | goto parse_error; |
| 6873 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6874 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6875 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6876 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6877 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6878 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6879 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6880 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6881 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6882 | } else { |
| 6883 | /* we could pick up a file descriptor choice here |
| 6884 | * with redirect_opt_num(), but bash doesn't do it. |
| 6885 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6886 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 6887 | #if !BB_MMU |
| 6888 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 6889 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6890 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6891 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6892 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6893 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6894 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6895 | if (ctx.ctx_res_w == RES_MATCH |
| 6896 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6897 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6898 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6899 | ) { |
| 6900 | continue; |
| 6901 | } |
| 6902 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6903 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6904 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6905 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6906 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6907 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6908 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6909 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6910 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6911 | goto case_semi; |
| 6912 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6913 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6914 | /* proper use of this character is caught by end_trigger: |
| 6915 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6916 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6917 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6918 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6919 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6920 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6921 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6922 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6923 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6924 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6925 | parse_error: |
| 6926 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6927 | struct parse_context *pctx; |
| 6928 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6929 | |
| 6930 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6931 | * Sample for finding leaks on syntax error recovery path. |
| 6932 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6933 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 6934 | * Samples to catch leaks at execution: |
| 6935 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 6936 | * while if (true | {true;}); then echo ok; fi; do (if echo ok; break; then :; fi) | cat; break; done |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6937 | */ |
| 6938 | pctx = &ctx; |
| 6939 | do { |
| 6940 | /* Update pipe/command counts, |
| 6941 | * otherwise freeing may miss some */ |
| 6942 | done_pipe(pctx, PIPE_SEQ); |
| 6943 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 6944 | pctx->list_head, pctx); |
| 6945 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6946 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6947 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6948 | #if !BB_MMU |
| 6949 | o_free_unsafe(&pctx->as_string); |
| 6950 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6951 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6952 | if (pctx != &ctx) { |
| 6953 | free(pctx); |
| 6954 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6955 | IF_HAS_KEYWORDS(pctx = p2;) |
| 6956 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6957 | /* Free text, clear all dest fields */ |
| 6958 | o_free(&dest); |
| 6959 | /* If we are not in top-level parse, we return, |
| 6960 | * our caller will propagate error. |
| 6961 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6962 | if (end_trigger != ';') { |
| 6963 | #if !BB_MMU |
| 6964 | if (pstring) |
| 6965 | *pstring = NULL; |
| 6966 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6967 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6968 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6969 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6970 | /* Discard cached input, force prompt */ |
| 6971 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 6972 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6973 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6974 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6975 | } |
| 6976 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6977 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6978 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 6979 | * end_trigger controls how often we stop parsing |
| 6980 | * NUL: parse all, execute, return |
| 6981 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 6982 | */ |
| 6983 | static void parse_and_run_stream(struct in_str *inp, int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6984 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6985 | /* Why we need empty flag? |
| 6986 | * An obscure corner case "false; ``; echo $?": |
| 6987 | * empty command in `` should still set $? to 0. |
| 6988 | * But we can't just set $? to 0 at the start, |
| 6989 | * this breaks "false; echo `echo $?`" case. |
| 6990 | */ |
| 6991 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6992 | while (1) { |
| 6993 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6994 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6995 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 6996 | if (!pipe_list) { /* EOF */ |
| 6997 | if (empty) |
| 6998 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6999 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7000 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7001 | debug_print_tree(pipe_list, 0); |
| 7002 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 7003 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7004 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7005 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7006 | } |
| 7007 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7008 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7009 | { |
| 7010 | struct in_str input; |
| 7011 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7012 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7013 | } |
| 7014 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7015 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7016 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7017 | struct in_str input; |
| 7018 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7019 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7020 | } |
| 7021 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7022 | /* Called a few times only (or even once if "sh -c") */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7023 | static void init_sigmasks(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7024 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7025 | unsigned sig; |
| 7026 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7027 | sigset_t old_blocked_set; |
| 7028 | |
| 7029 | if (!G.inherited_set_is_saved) { |
| 7030 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 7031 | G.inherited_set = G.blocked_set; |
| 7032 | } |
| 7033 | old_blocked_set = G.blocked_set; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7034 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7035 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7036 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 7037 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7038 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7039 | mask |= SPECIAL_JOB_SIGS; |
| 7040 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7041 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7042 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7043 | sig = 0; |
| 7044 | while (mask) { |
| 7045 | if (mask & 1) |
| 7046 | sigaddset(&G.blocked_set, sig); |
| 7047 | mask >>= 1; |
| 7048 | sig++; |
| 7049 | } |
| 7050 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7051 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7052 | if (memcmp(&old_blocked_set, &G.blocked_set, sizeof(old_blocked_set)) != 0) |
| 7053 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7054 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7055 | /* POSIX allows shell to re-enable SIGCHLD |
| 7056 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7057 | #if ENABLE_HUSH_FAST |
| 7058 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7059 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7060 | signal(SIGCHLD, SIGCHLD_handler); |
| 7061 | #else |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7062 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7063 | signal(SIGCHLD, SIG_DFL); |
| 7064 | #endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7065 | |
| 7066 | G.inherited_set_is_saved = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7067 | } |
| 7068 | |
| 7069 | #if ENABLE_HUSH_JOB |
| 7070 | /* helper */ |
| 7071 | static void maybe_set_to_sigexit(int sig) |
| 7072 | { |
| 7073 | void (*handler)(int); |
| 7074 | /* non_DFL_mask'ed signals are, well, masked, |
| 7075 | * no need to set handler for them. |
| 7076 | */ |
| 7077 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 7078 | handler = signal(sig, sigexit); |
| 7079 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 7080 | signal(sig, handler); |
| 7081 | } |
| 7082 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7083 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7084 | static void set_fatal_handlers(void) |
| 7085 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 7086 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7087 | if (HUSH_DEBUG) { |
| 7088 | maybe_set_to_sigexit(SIGILL ); |
| 7089 | maybe_set_to_sigexit(SIGFPE ); |
| 7090 | maybe_set_to_sigexit(SIGBUS ); |
| 7091 | maybe_set_to_sigexit(SIGSEGV); |
| 7092 | maybe_set_to_sigexit(SIGTRAP); |
| 7093 | } /* else: hush is perfect. what SEGV? */ |
| 7094 | maybe_set_to_sigexit(SIGABRT); |
| 7095 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7096 | maybe_set_to_sigexit(SIGPIPE); |
| 7097 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7098 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7099 | * if we aren't interactive... but in this case |
| 7100 | * we never want to restore pgrp on exit, and this fn is not called */ |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7101 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7102 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 7103 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 7104 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7105 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7106 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7107 | static int set_mode(const char cstate, const char mode) |
| 7108 | { |
| 7109 | int state = (cstate == '-' ? 1 : 0); |
| 7110 | switch (mode) { |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame^] | 7111 | case 'n': G.n_mode = state; break; |
| 7112 | case 'x': IF_HUSH_MODE_X(G_x_mode = state;) break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7113 | default: return EXIT_FAILURE; |
| 7114 | } |
| 7115 | return EXIT_SUCCESS; |
| 7116 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7117 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7118 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7119 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7120 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7121 | static const struct variable const_shell_ver = { |
| 7122 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 7123 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7124 | .max_len = 1, /* 0 can provoke free(name) */ |
| 7125 | .flg_export = 1, |
| 7126 | .flg_read_only = 1, |
| 7127 | }; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7128 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7129 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7130 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7131 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7132 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7133 | INIT_G(); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 7134 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7135 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7136 | #if !BB_MMU |
| 7137 | G.argv0_for_re_execing = argv[0]; |
| 7138 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7139 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7140 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 7141 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7142 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7143 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 7144 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7145 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7146 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7147 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7148 | if (e) while (*e) { |
| 7149 | char *value = strchr(*e, '='); |
| 7150 | if (value) { /* paranoia */ |
| 7151 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7152 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7153 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7154 | cur_var->max_len = strlen(*e); |
| 7155 | cur_var->flg_export = 1; |
| 7156 | } |
| 7157 | e++; |
| 7158 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7159 | /* reinstate HUSH_VERSION */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7160 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7161 | putenv((char *)hush_version_str); |
| 7162 | |
| 7163 | /* Export PWD */ |
| 7164 | set_pwd_var(/*exp:*/ 1); |
| 7165 | /* bash also exports SHLVL and _, |
| 7166 | * and sets (but doesn't export) the following variables: |
| 7167 | * BASH=/bin/bash |
| 7168 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7169 | * BASH_VERSION='3.2.0(1)-release' |
| 7170 | * HOSTTYPE=i386 |
| 7171 | * MACHTYPE=i386-pc-linux-gnu |
| 7172 | * OSTYPE=linux-gnu |
| 7173 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7174 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7175 | * EUID=<NNNNN> |
| 7176 | * UID=<NNNNN> |
| 7177 | * GROUPS=() |
| 7178 | * LINES=<NNN> |
| 7179 | * COLUMNS=<NNN> |
| 7180 | * BASH_ARGC=() |
| 7181 | * BASH_ARGV=() |
| 7182 | * BASH_LINENO=() |
| 7183 | * BASH_SOURCE=() |
| 7184 | * DIRSTACK=() |
| 7185 | * PIPESTATUS=([0]="0") |
| 7186 | * HISTFILE=/<xxx>/.bash_history |
| 7187 | * HISTFILESIZE=500 |
| 7188 | * HISTSIZE=500 |
| 7189 | * MAILCHECK=60 |
| 7190 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7191 | * SHELL=/bin/bash |
| 7192 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7193 | * TERM=dumb |
| 7194 | * OPTERR=1 |
| 7195 | * OPTIND=1 |
| 7196 | * IFS=$' \t\n' |
| 7197 | * PS1='\s-\v\$ ' |
| 7198 | * PS2='> ' |
| 7199 | * PS4='+ ' |
| 7200 | */ |
| 7201 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7202 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7203 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7204 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7205 | G.global_argc = argc; |
| 7206 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7207 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7208 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7209 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7210 | if (setjmp(die_jmp)) { |
| 7211 | /* xfunc has failed! die die die */ |
| 7212 | /* no EXIT traps, this is an escape hatch! */ |
| 7213 | G.exiting = 1; |
| 7214 | hush_exit(xfunc_error_retval); |
| 7215 | } |
| 7216 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7217 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7218 | * init_sigmasks() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7219 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7220 | * If we later decide that we are interactive, we run init_sigmasks() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7221 | * in order to intercept (more) signals. |
| 7222 | */ |
| 7223 | |
| 7224 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7225 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7226 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7227 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 7228 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7229 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7230 | "<:$:R:V:" |
| 7231 | # if ENABLE_HUSH_FUNCTIONS |
| 7232 | "F:" |
| 7233 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7234 | #endif |
| 7235 | ); |
| 7236 | if (opt <= 0) |
| 7237 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7238 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7239 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7240 | /* Possibilities: |
| 7241 | * sh ... -c 'script' |
| 7242 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7243 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7244 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7245 | * "" needs to be replaced with NULL |
| 7246 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7247 | * Note: the form without ARG0 never happens: |
| 7248 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7249 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7250 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7251 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7252 | G.root_ppid = getppid(); |
| 7253 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7254 | G.global_argv = argv + optind; |
| 7255 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7256 | if (builtin_argc) { |
| 7257 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7258 | const struct built_in_command *x; |
| 7259 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7260 | init_sigmasks(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7261 | x = find_builtin(optarg); |
| 7262 | if (x) { /* paranoia */ |
| 7263 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7264 | G.global_argv += builtin_argc; |
| 7265 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7266 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7267 | } |
| 7268 | goto final_return; |
| 7269 | } |
| 7270 | if (!G.global_argv[0]) { |
| 7271 | /* -c 'script' (no params): prevent empty $0 */ |
| 7272 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7273 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7274 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7275 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7276 | init_sigmasks(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7277 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7278 | goto final_return; |
| 7279 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7280 | /* Well, we cannot just declare interactiveness, |
| 7281 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7282 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7283 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7284 | case 's': |
| 7285 | /* "-s" means "read from stdin", but this is how we always |
| 7286 | * operate, so simply do nothing here. */ |
| 7287 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7288 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7289 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7290 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7291 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7292 | case '$': { |
| 7293 | unsigned long long empty_trap_mask; |
| 7294 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7295 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7296 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7297 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7298 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7299 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7300 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7301 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7302 | optarg++; |
| 7303 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7304 | optarg++; |
| 7305 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7306 | if (empty_trap_mask != 0) { |
| 7307 | int sig; |
| 7308 | init_sigmasks(); |
| 7309 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7310 | for (sig = 1; sig < NSIG; sig++) { |
| 7311 | if (empty_trap_mask & (1LL << sig)) { |
| 7312 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7313 | sigaddset(&G.blocked_set, sig); |
| 7314 | } |
| 7315 | } |
| 7316 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7317 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7318 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7319 | optarg++; |
| 7320 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7321 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7322 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7323 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7324 | case 'R': |
| 7325 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7326 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7327 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7328 | # if ENABLE_HUSH_FUNCTIONS |
| 7329 | case 'F': { |
| 7330 | struct function *funcp = new_function(optarg); |
| 7331 | /* funcp->name is already set to optarg */ |
| 7332 | /* funcp->body is set to NULL. It's a special case. */ |
| 7333 | funcp->body_as_string = argv[optind]; |
| 7334 | optind++; |
| 7335 | break; |
| 7336 | } |
| 7337 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7338 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7339 | case 'n': |
| 7340 | case 'x': |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 7341 | if (set_mode('-', opt) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7342 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7343 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7344 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7345 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7346 | " or: sh -c command [args]...\n\n"); |
| 7347 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7348 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7349 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7350 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7351 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7352 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7353 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7354 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7355 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7356 | G.root_ppid = getppid(); |
| 7357 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7358 | |
| 7359 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7360 | if (argv[0] && argv[0][0] == '-') { |
| 7361 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7362 | debug_printf("sourcing /etc/profile\n"); |
| 7363 | input = fopen_for_read("/etc/profile"); |
| 7364 | if (input != NULL) { |
| 7365 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7366 | init_sigmasks(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7367 | parse_and_run_file(input); |
| 7368 | fclose(input); |
| 7369 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7370 | /* bash: after sourcing /etc/profile, |
| 7371 | * tries to source (in the given order): |
| 7372 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7373 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7374 | * bash also sources ~/.bash_logout on exit. |
| 7375 | * If called as sh, skips .bash_XXX files. |
| 7376 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7379 | if (argv[optind]) { |
| 7380 | FILE *input; |
| 7381 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7382 | * "bash <script>" (which is never interactive (unless -i?)) |
| 7383 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7384 | * If called as sh, does the same but with $ENV. |
| 7385 | */ |
| 7386 | debug_printf("running script '%s'\n", argv[optind]); |
| 7387 | G.global_argv = argv + optind; |
| 7388 | G.global_argc = argc - optind; |
| 7389 | input = xfopen_for_read(argv[optind]); |
| 7390 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7391 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7392 | parse_and_run_file(input); |
| 7393 | #if ENABLE_FEATURE_CLEAN_UP |
| 7394 | fclose(input); |
| 7395 | #endif |
| 7396 | goto final_return; |
| 7397 | } |
| 7398 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7399 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7400 | * NB: don't forget to (re)run init_sigmasks() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7401 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7402 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7403 | /* A shell is interactive if the '-i' flag was given, |
| 7404 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 7405 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7406 | * no arguments remaining or the -s flag given |
| 7407 | * standard input is a terminal |
| 7408 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7409 | * Refer to Posix.2, the description of the 'sh' utility. |
| 7410 | */ |
| 7411 | #if ENABLE_HUSH_JOB |
| 7412 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7413 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 7414 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 7415 | if (G_saved_tty_pgrp < 0) |
| 7416 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7417 | |
| 7418 | /* try to dup stdin to high fd#, >= 255 */ |
| 7419 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7420 | if (G_interactive_fd < 0) { |
| 7421 | /* try to dup to any fd */ |
| 7422 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7423 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7424 | /* give up */ |
| 7425 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7426 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7427 | } |
| 7428 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7429 | // TODO: track & disallow any attempts of user |
| 7430 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7431 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7432 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7433 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7434 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7435 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7436 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7437 | /* If we were run as 'hush &', sleep until we are |
| 7438 | * in the foreground (tty pgrp == our pgrp). |
| 7439 | * If we get started under a job aware app (like bash), |
| 7440 | * make sure we are now in charge so we don't fight over |
| 7441 | * who gets the foreground */ |
| 7442 | while (1) { |
| 7443 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7444 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 7445 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7446 | break; |
| 7447 | /* send TTIN to ourself (should stop us) */ |
| 7448 | kill(- shell_pgrp, SIGTTIN); |
| 7449 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7450 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7451 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7452 | /* Block some signals */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7453 | init_sigmasks(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7454 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7455 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7456 | /* Set other signals to restore saved_tty_pgrp */ |
| 7457 | set_fatal_handlers(); |
| 7458 | /* Put ourselves in our own process group |
| 7459 | * (bash, too, does this only if ctty is available) */ |
| 7460 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 7461 | /* Grab control of the terminal */ |
| 7462 | tcsetpgrp(G_interactive_fd, getpid()); |
| 7463 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 7464 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 7465 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 7466 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7467 | } else { |
| 7468 | init_sigmasks(); |
| 7469 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7470 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7471 | /* No job control compiled in, only prompt/line editing */ |
| 7472 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7473 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7474 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7475 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7476 | G_interactive_fd = dup(STDIN_FILENO); |
| 7477 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7478 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7479 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7480 | } |
| 7481 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7482 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7483 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7484 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7485 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7486 | #else |
| 7487 | /* We have interactiveness code disabled */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7488 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7489 | #endif |
| 7490 | /* bash: |
| 7491 | * if interactive but not a login shell, sources ~/.bashrc |
| 7492 | * (--norc turns this off, --rcfile <file> overrides) |
| 7493 | */ |
| 7494 | |
| 7495 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7496 | /* note: ash and hush share this string */ |
| 7497 | printf("\n\n%s %s\n" |
| 7498 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7499 | "\n", |
| 7500 | bb_banner, |
| 7501 | "hush - the humble shell" |
| 7502 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7503 | } |
| 7504 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7505 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7506 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7507 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7508 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7509 | if (G.cwd != bb_msg_unknown) |
| 7510 | free((char*)G.cwd); |
| 7511 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7512 | while (cur_var) { |
| 7513 | struct variable *tmp = cur_var; |
| 7514 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7515 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7516 | cur_var = cur_var->next; |
| 7517 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 7518 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7519 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7520 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7521 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7522 | |
| 7523 | |
| 7524 | #if ENABLE_LASH |
| 7525 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7526 | int lash_main(int argc, char **argv) |
| 7527 | { |
Denys Vlasenko | 0793491 | 2009-08-20 03:40:04 +0200 | [diff] [blame] | 7528 | bb_error_msg("lash is deprecated, please use hush instead"); |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7529 | return hush_main(argc, argv); |
| 7530 | } |
| 7531 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7532 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7533 | #if ENABLE_MSH |
| 7534 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7535 | int msh_main(int argc, char **argv) |
| 7536 | { |
| 7537 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7538 | return hush_main(argc, argv); |
| 7539 | } |
| 7540 | #endif |
| 7541 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7542 | |
| 7543 | /* |
| 7544 | * Built-ins |
| 7545 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7546 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7547 | { |
| 7548 | return 0; |
| 7549 | } |
| 7550 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7551 | static int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv)) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7552 | { |
| 7553 | int argc = 0; |
| 7554 | while (*argv) { |
| 7555 | argc++; |
| 7556 | argv++; |
| 7557 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7558 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7559 | } |
| 7560 | |
| 7561 | static int FAST_FUNC builtin_test(char **argv) |
| 7562 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7563 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7564 | } |
| 7565 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7566 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7567 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7568 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7569 | } |
| 7570 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7571 | #if ENABLE_PRINTF |
| 7572 | static int FAST_FUNC builtin_printf(char **argv) |
| 7573 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7574 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7575 | } |
| 7576 | #endif |
| 7577 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7578 | static char **skip_dash_dash(char **argv) |
| 7579 | { |
| 7580 | argv++; |
| 7581 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 7582 | argv++; |
| 7583 | return argv; |
| 7584 | } |
| 7585 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7586 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7587 | { |
| 7588 | int rcode = EXIT_SUCCESS; |
| 7589 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7590 | argv = skip_dash_dash(argv); |
| 7591 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7592 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7593 | /* bash: |
| 7594 | * eval "echo Hi; done" ("done" is syntax error): |
| 7595 | * "echo Hi" will not execute too. |
| 7596 | */ |
| 7597 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7598 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7599 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7600 | } |
| 7601 | return rcode; |
| 7602 | } |
| 7603 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7604 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7605 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7606 | const char *newdir; |
| 7607 | |
| 7608 | argv = skip_dash_dash(argv); |
| 7609 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7610 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7611 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7612 | * bash says "bash: cd: HOME not set" and does nothing |
| 7613 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7614 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7615 | const char *home = get_local_var_value("HOME"); |
| 7616 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7617 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7618 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7619 | /* Mimic bash message exactly */ |
| 7620 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7621 | return EXIT_FAILURE; |
| 7622 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7623 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7624 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7625 | * set it again, but do not export. bash does the same. |
| 7626 | */ |
| 7627 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7628 | return EXIT_SUCCESS; |
| 7629 | } |
| 7630 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7631 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7632 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7633 | argv = skip_dash_dash(argv); |
| 7634 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7635 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7636 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7637 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7638 | * tty pgrp then, only top-level shell process does that */ |
| 7639 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7640 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7641 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7642 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 7643 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 7644 | * and tcsetpgrp, and this is inherently racy. |
| 7645 | */ |
| 7646 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7647 | } |
| 7648 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7649 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7650 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 7651 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7652 | |
| 7653 | /* interactive bash: |
| 7654 | * # trap "echo EEE" EXIT |
| 7655 | * # exit |
| 7656 | * exit |
| 7657 | * There are stopped jobs. |
| 7658 | * (if there are _stopped_ jobs, running ones don't count) |
| 7659 | * # exit |
| 7660 | * exit |
| 7661 | # EEE (then bash exits) |
| 7662 | * |
| 7663 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 7664 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 7665 | |
| 7666 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7667 | argv = skip_dash_dash(argv); |
| 7668 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7669 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7670 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 7671 | xfunc_error_retval = 255; |
| 7672 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7673 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7674 | } |
| 7675 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7676 | static void print_escaped(const char *s) |
| 7677 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7678 | if (*s == '\'') |
| 7679 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7680 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7681 | const char *p = strchrnul(s, '\''); |
| 7682 | /* print 'xxxx', possibly just '' */ |
| 7683 | printf("'%.*s'", (int)(p - s), s); |
| 7684 | if (*p == '\0') |
| 7685 | break; |
| 7686 | s = p; |
| 7687 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7688 | /* s points to '; print "'''...'''" */ |
| 7689 | putchar('"'); |
| 7690 | do putchar('\''); while (*++s == '\''); |
| 7691 | putchar('"'); |
| 7692 | } while (*s); |
| 7693 | } |
| 7694 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7695 | #if !ENABLE_HUSH_LOCAL |
| 7696 | #define helper_export_local(argv, exp, lvl) \ |
| 7697 | helper_export_local(argv, exp) |
| 7698 | #endif |
| 7699 | static void helper_export_local(char **argv, int exp, int lvl) |
| 7700 | { |
| 7701 | do { |
| 7702 | char *name = *argv; |
| 7703 | |
| 7704 | /* So far we do not check that name is valid (TODO?) */ |
| 7705 | |
| 7706 | if (strchr(name, '=') == NULL) { |
| 7707 | struct variable *var; |
| 7708 | |
| 7709 | var = get_local_var(name); |
| 7710 | if (exp == -1) { /* unexporting? */ |
| 7711 | /* export -n NAME (without =VALUE) */ |
| 7712 | if (var) { |
| 7713 | var->flg_export = 0; |
| 7714 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 7715 | unsetenv(name); |
| 7716 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 7717 | continue; |
| 7718 | } |
| 7719 | if (exp == 1) { /* exporting? */ |
| 7720 | /* export NAME (without =VALUE) */ |
| 7721 | if (var) { |
| 7722 | var->flg_export = 1; |
| 7723 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 7724 | putenv(var->varstr); |
| 7725 | continue; |
| 7726 | } |
| 7727 | } |
| 7728 | /* Exporting non-existing variable. |
| 7729 | * bash does not put it in environment, |
| 7730 | * but remembers that it is exported, |
| 7731 | * and does put it in env when it is set later. |
| 7732 | * We just set it to "" and export. */ |
| 7733 | /* Or, it's "local NAME" (without =VALUE). |
| 7734 | * bash sets the value to "". */ |
| 7735 | name = xasprintf("%s=", name); |
| 7736 | } else { |
| 7737 | /* (Un)exporting/making local NAME=VALUE */ |
| 7738 | name = xstrdup(name); |
| 7739 | } |
| 7740 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 7741 | } while (*++argv); |
| 7742 | } |
| 7743 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7744 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7745 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 7746 | unsigned opt_unexport; |
| 7747 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 7748 | #if ENABLE_HUSH_EXPORT_N |
| 7749 | /* "!": do not abort on errors */ |
| 7750 | opt_unexport = getopt32(argv, "!n"); |
| 7751 | if (opt_unexport == (uint32_t)-1) |
| 7752 | return EXIT_FAILURE; |
| 7753 | argv += optind; |
| 7754 | #else |
| 7755 | opt_unexport = 0; |
| 7756 | argv++; |
| 7757 | #endif |
| 7758 | |
| 7759 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7760 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7761 | if (e) { |
| 7762 | while (*e) { |
| 7763 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7764 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7765 | #else |
| 7766 | /* ash emits: export VAR='VAL' |
| 7767 | * bash: declare -x VAR="VAL" |
| 7768 | * we follow ash example */ |
| 7769 | const char *s = *e++; |
| 7770 | const char *p = strchr(s, '='); |
| 7771 | |
| 7772 | if (!p) /* wtf? take next variable */ |
| 7773 | continue; |
| 7774 | /* export var= */ |
| 7775 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7776 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7777 | putchar('\n'); |
| 7778 | #endif |
| 7779 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7780 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7781 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7782 | return EXIT_SUCCESS; |
| 7783 | } |
| 7784 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7785 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7786 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7787 | return EXIT_SUCCESS; |
| 7788 | } |
| 7789 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7790 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7791 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7792 | { |
| 7793 | if (G.func_nest_level == 0) { |
| 7794 | bb_error_msg("%s: not in a function", argv[0]); |
| 7795 | return EXIT_FAILURE; /* bash compat */ |
| 7796 | } |
| 7797 | helper_export_local(argv, 0, G.func_nest_level); |
| 7798 | return EXIT_SUCCESS; |
| 7799 | } |
| 7800 | #endif |
| 7801 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7802 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7803 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7804 | int sig; |
| 7805 | char *new_cmd; |
| 7806 | |
| 7807 | if (!G.traps) |
| 7808 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7809 | |
| 7810 | argv++; |
| 7811 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7812 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7813 | /* No args: print all trapped */ |
| 7814 | for (i = 0; i < NSIG; ++i) { |
| 7815 | if (G.traps[i]) { |
| 7816 | printf("trap -- "); |
| 7817 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 7818 | /* note: bash adds "SIG", but only if invoked |
| 7819 | * as "bash". If called as "sh", or if set -o posix, |
| 7820 | * then it prints short signal names. |
| 7821 | * We are printing short names: */ |
| 7822 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7823 | } |
| 7824 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7825 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7826 | return EXIT_SUCCESS; |
| 7827 | } |
| 7828 | |
| 7829 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7830 | /* If first arg is a number: reset all specified signals */ |
| 7831 | sig = bb_strtou(*argv, NULL, 10); |
| 7832 | if (errno == 0) { |
| 7833 | int ret; |
| 7834 | process_sig_list: |
| 7835 | ret = EXIT_SUCCESS; |
| 7836 | while (*argv) { |
| 7837 | sig = get_signum(*argv++); |
| 7838 | if (sig < 0 || sig >= NSIG) { |
| 7839 | ret = EXIT_FAILURE; |
| 7840 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7841 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7842 | continue; |
| 7843 | } |
| 7844 | |
| 7845 | free(G.traps[sig]); |
| 7846 | G.traps[sig] = xstrdup(new_cmd); |
| 7847 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7848 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7849 | get_signame(sig), sig, G.traps[sig]); |
| 7850 | |
| 7851 | /* There is no signal for 0 (EXIT) */ |
| 7852 | if (sig == 0) |
| 7853 | continue; |
| 7854 | |
| 7855 | if (new_cmd) { |
| 7856 | sigaddset(&G.blocked_set, sig); |
| 7857 | } else { |
| 7858 | /* There was a trap handler, we are removing it |
| 7859 | * (if sig has non-DFL handling, |
| 7860 | * we don't need to do anything) */ |
| 7861 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 7862 | continue; |
| 7863 | sigdelset(&G.blocked_set, sig); |
| 7864 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7865 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7866 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7867 | return ret; |
| 7868 | } |
| 7869 | |
| 7870 | if (!argv[1]) { /* no second arg */ |
| 7871 | bb_error_msg("trap: invalid arguments"); |
| 7872 | return EXIT_FAILURE; |
| 7873 | } |
| 7874 | |
| 7875 | /* First arg is "-": reset all specified to default */ |
| 7876 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 7877 | /* Everything else: set arg as signal handler |
| 7878 | * (includes "" case, which ignores signal) */ |
| 7879 | if (argv[0][0] == '-') { |
| 7880 | if (argv[0][1] == '\0') { /* "-" */ |
| 7881 | /* new_cmd remains NULL: "reset these sigs" */ |
| 7882 | goto reset_traps; |
| 7883 | } |
| 7884 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 7885 | argv++; |
| 7886 | } |
| 7887 | /* else: "-something", no special meaning */ |
| 7888 | } |
| 7889 | new_cmd = *argv; |
| 7890 | reset_traps: |
| 7891 | argv++; |
| 7892 | goto process_sig_list; |
| 7893 | } |
| 7894 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7895 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7896 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7897 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7898 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7899 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7900 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7901 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 7902 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7903 | |
| 7904 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7905 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7906 | type = "an alias";*/ |
| 7907 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7908 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7909 | type = "a function"; |
| 7910 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7911 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7912 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7913 | else if ((path = find_in_path(*argv)) != NULL) |
| 7914 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7915 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7916 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7917 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7918 | continue; |
| 7919 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7920 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7921 | printf("%s is %s\n", *argv, type); |
| 7922 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7923 | } |
| 7924 | |
| 7925 | return ret; |
| 7926 | } |
| 7927 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7928 | #if ENABLE_HUSH_JOB |
| 7929 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7930 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7931 | { |
| 7932 | int i, jobnum; |
| 7933 | struct pipe *pi; |
| 7934 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7935 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7936 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7937 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7938 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 7939 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7940 | for (pi = G.job_list; pi; pi = pi->next) { |
| 7941 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7942 | goto found; |
| 7943 | } |
| 7944 | } |
| 7945 | bb_error_msg("%s: no current job", argv[0]); |
| 7946 | return EXIT_FAILURE; |
| 7947 | } |
| 7948 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 7949 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 7950 | return EXIT_FAILURE; |
| 7951 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7952 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7953 | if (pi->jobid == jobnum) { |
| 7954 | goto found; |
| 7955 | } |
| 7956 | } |
| 7957 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 7958 | return EXIT_FAILURE; |
| 7959 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7960 | /* TODO: bash prints a string representation |
| 7961 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7962 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7963 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7964 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
| 7967 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7968 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 7969 | for (i = 0; i < pi->num_cmds; i++) { |
| 7970 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 7971 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7972 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 7973 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7974 | |
| 7975 | i = kill(- pi->pgrp, SIGCONT); |
| 7976 | if (i < 0) { |
| 7977 | if (errno == ESRCH) { |
| 7978 | delete_finished_bg_job(pi); |
| 7979 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7980 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7981 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7982 | } |
| 7983 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7984 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7985 | remove_bg_job(pi); |
| 7986 | return checkjobs_and_fg_shell(pi); |
| 7987 | } |
| 7988 | return EXIT_SUCCESS; |
| 7989 | } |
| 7990 | #endif |
| 7991 | |
| 7992 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7993 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7994 | { |
| 7995 | const struct built_in_command *x; |
| 7996 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7997 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 7998 | "Built-in commands:\n" |
| 7999 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8000 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8001 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8002 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8003 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8004 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8005 | return EXIT_SUCCESS; |
| 8006 | } |
| 8007 | #endif |
| 8008 | |
| 8009 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8010 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8011 | { |
| 8012 | struct pipe *job; |
| 8013 | const char *status_string; |
| 8014 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8015 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8016 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8017 | status_string = "Stopped"; |
| 8018 | else |
| 8019 | status_string = "Running"; |
| 8020 | |
| 8021 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8022 | } |
| 8023 | return EXIT_SUCCESS; |
| 8024 | } |
| 8025 | #endif |
| 8026 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8027 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8028 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8029 | { |
| 8030 | void *p; |
| 8031 | unsigned long l; |
| 8032 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8033 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8034 | /* Optional. Reduces probability of false positives */ |
| 8035 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8036 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8037 | /* Crude attempt to find where "free memory" starts, |
| 8038 | * sans fragmentation. */ |
| 8039 | p = malloc(240); |
| 8040 | l = (unsigned long)p; |
| 8041 | free(p); |
| 8042 | p = malloc(3400); |
| 8043 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8044 | free(p); |
| 8045 | |
| 8046 | if (!G.memleak_value) |
| 8047 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8048 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8049 | l -= G.memleak_value; |
| 8050 | if ((long)l < 0) |
| 8051 | l = 0; |
| 8052 | l /= 1024; |
| 8053 | if (l > 127) |
| 8054 | l = 127; |
| 8055 | |
| 8056 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8057 | return l; |
| 8058 | } |
| 8059 | #endif |
| 8060 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8061 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8062 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8063 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8064 | return EXIT_SUCCESS; |
| 8065 | } |
| 8066 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8067 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8068 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8069 | const char *r; |
| 8070 | char *opt_n = NULL; |
| 8071 | char *opt_p = NULL; |
| 8072 | char *opt_t = NULL; |
| 8073 | char *opt_u = NULL; |
| 8074 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8075 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8076 | /* "!": do not abort on errors. |
| 8077 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8078 | */ |
| 8079 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8080 | if (read_flags == (uint32_t)-1) |
| 8081 | return EXIT_FAILURE; |
| 8082 | argv += optind; |
| 8083 | |
| 8084 | r = shell_builtin_read(set_local_var_from_halves, |
| 8085 | argv, |
| 8086 | get_local_var_value("IFS"), /* can be NULL */ |
| 8087 | read_flags, |
| 8088 | opt_n, |
| 8089 | opt_p, |
| 8090 | opt_t, |
| 8091 | opt_u |
| 8092 | ); |
| 8093 | |
| 8094 | if ((uintptr_t)r > 1) { |
| 8095 | bb_error_msg("%s", r); |
| 8096 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8097 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8098 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8099 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8100 | } |
| 8101 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8102 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8103 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8104 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8105 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8106 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8107 | * set -- [argument...] |
| 8108 | * set -o |
| 8109 | * set +o |
| 8110 | * Implementations shall support the options in both their hyphen and |
| 8111 | * plus-sign forms. These options can also be specified as options to sh. |
| 8112 | * Examples: |
| 8113 | * Write out all variables and their values: set |
| 8114 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8115 | * Turn on the -x and -v options: set -xv |
| 8116 | * Unset all positional parameters: set -- |
| 8117 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8118 | * Set the positional parameters to the expansion of x, even if x expands |
| 8119 | * with a leading '-' or '+': set -- $x |
| 8120 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8121 | * So far, we only support "set -- [argument...]" and some of the short names. |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8122 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8123 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8124 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8125 | int n; |
| 8126 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8127 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8128 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8129 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8130 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8131 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8132 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8133 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8134 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8135 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8136 | do { |
| 8137 | if (!strcmp(arg, "--")) { |
| 8138 | ++argv; |
| 8139 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8140 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8141 | if (arg[0] != '+' && arg[0] != '-') |
| 8142 | break; |
| 8143 | for (n = 1; arg[n]; ++n) |
| 8144 | if (set_mode(arg[0], arg[n])) |
| 8145 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8146 | } while ((arg = *++argv) != NULL); |
| 8147 | /* Now argv[0] is 1st argument */ |
| 8148 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8149 | if (arg == NULL) |
| 8150 | return EXIT_SUCCESS; |
| 8151 | set_argv: |
| 8152 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8153 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8154 | g_argv = G.global_argv; |
| 8155 | if (G.global_args_malloced) { |
| 8156 | pp = g_argv; |
| 8157 | while (*++pp) |
| 8158 | free(*pp); |
| 8159 | g_argv[1] = NULL; |
| 8160 | } else { |
| 8161 | G.global_args_malloced = 1; |
| 8162 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8163 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8164 | g_argv = pp; |
| 8165 | } |
| 8166 | /* This realloc's G.global_argv */ |
| 8167 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8168 | |
| 8169 | n = 1; |
| 8170 | while (*++pp) |
| 8171 | n++; |
| 8172 | G.global_argc = n; |
| 8173 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8174 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8175 | |
| 8176 | /* Nothing known, so abort */ |
| 8177 | error: |
| 8178 | bb_error_msg("set: %s: invalid option", arg); |
| 8179 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8180 | } |
| 8181 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8182 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8183 | { |
| 8184 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8185 | argv = skip_dash_dash(argv); |
| 8186 | if (argv[0]) { |
| 8187 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8188 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8189 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8190 | if (G.global_args_malloced) { |
| 8191 | int m = 1; |
| 8192 | while (m <= n) |
| 8193 | free(G.global_argv[m++]); |
| 8194 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8195 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8196 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8197 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8198 | return EXIT_SUCCESS; |
| 8199 | } |
| 8200 | return EXIT_FAILURE; |
| 8201 | } |
| 8202 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8203 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8204 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8205 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8206 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8207 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8208 | #if ENABLE_HUSH_FUNCTIONS |
| 8209 | smallint sv_flg; |
| 8210 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8211 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8212 | argv = skip_dash_dash(argv); |
| 8213 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8214 | if (!filename) { |
| 8215 | /* bash says: "bash: .: filename argument required" */ |
| 8216 | return 2; /* bash compat */ |
| 8217 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8218 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8219 | if (!strchr(filename, '/')) { |
| 8220 | arg_path = find_in_path(filename); |
| 8221 | if (arg_path) |
| 8222 | filename = arg_path; |
| 8223 | } |
| 8224 | input = fopen_or_warn(filename, "r"); |
| 8225 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8226 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8227 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8228 | return EXIT_FAILURE; |
| 8229 | } |
| 8230 | close_on_exec_on(fileno(input)); |
| 8231 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8232 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8233 | sv_flg = G.flag_return_in_progress; |
| 8234 | /* "we are inside sourced file, ok to use return" */ |
| 8235 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8236 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8237 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8238 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8239 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8240 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8241 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8242 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8243 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8244 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8245 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8246 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8247 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8248 | } |
| 8249 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8250 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8251 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8252 | int rc; |
| 8253 | mode_t mask; |
| 8254 | |
| 8255 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8256 | argv = skip_dash_dash(argv); |
| 8257 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8258 | mode_t old_mask = mask; |
| 8259 | |
| 8260 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8261 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8262 | mask ^= 0777; |
| 8263 | if (rc == 0) { |
| 8264 | mask = old_mask; |
| 8265 | /* bash messages: |
| 8266 | * bash: umask: 'q': invalid symbolic mode operator |
| 8267 | * bash: umask: 999: octal number out of range |
| 8268 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8269 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8270 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8271 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8272 | rc = 1; |
| 8273 | /* Mimic bash */ |
| 8274 | printf("%04o\n", (unsigned) mask); |
| 8275 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8276 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8277 | umask(mask); |
| 8278 | |
| 8279 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8280 | } |
| 8281 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8282 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8283 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8284 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8285 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8286 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8287 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8288 | /* "!": do not abort on errors */ |
| 8289 | /* "+": stop at 1st non-option */ |
| 8290 | opts = getopt32(argv, "!+vf"); |
| 8291 | if (opts == (unsigned)-1) |
| 8292 | return EXIT_FAILURE; |
| 8293 | if (opts == 3) { |
| 8294 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8295 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8296 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8297 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8298 | |
| 8299 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8300 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8301 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8302 | if (unset_local_var(*argv)) { |
| 8303 | /* unset <nonexistent_var> doesn't fail. |
| 8304 | * Error is when one tries to unset RO var. |
| 8305 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8306 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8307 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8308 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8309 | #if ENABLE_HUSH_FUNCTIONS |
| 8310 | else { |
| 8311 | unset_func(*argv); |
| 8312 | } |
| 8313 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8314 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8315 | } |
| 8316 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8317 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8318 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8319 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8320 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8321 | { |
| 8322 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8323 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8324 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8325 | argv = skip_dash_dash(argv); |
| 8326 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8327 | /* Don't care about wait results */ |
| 8328 | /* Note 1: must wait until there are no more children */ |
| 8329 | /* Note 2: must be interruptible */ |
| 8330 | /* Examples: |
| 8331 | * $ sleep 3 & sleep 6 & wait |
| 8332 | * [1] 30934 sleep 3 |
| 8333 | * [2] 30935 sleep 6 |
| 8334 | * [1] Done sleep 3 |
| 8335 | * [2] Done sleep 6 |
| 8336 | * $ sleep 3 & sleep 6 & wait |
| 8337 | * [1] 30936 sleep 3 |
| 8338 | * [2] 30937 sleep 6 |
| 8339 | * [1] Done sleep 3 |
| 8340 | * ^C <-- after ~4 sec from keyboard |
| 8341 | * $ |
| 8342 | */ |
| 8343 | sigaddset(&G.blocked_set, SIGCHLD); |
| 8344 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8345 | while (1) { |
| 8346 | checkjobs(NULL); |
| 8347 | if (errno == ECHILD) |
| 8348 | break; |
| 8349 | /* Wait for SIGCHLD or any other signal of interest */ |
| 8350 | /* sigtimedwait with infinite timeout: */ |
| 8351 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 8352 | if (sig > 0) { |
| 8353 | sig = check_and_run_traps(sig); |
| 8354 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 8355 | ret = 128 + sig; |
| 8356 | break; |
| 8357 | } |
| 8358 | } |
| 8359 | } |
| 8360 | sigdelset(&G.blocked_set, SIGCHLD); |
| 8361 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8362 | return ret; |
| 8363 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8364 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8365 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8366 | while (*argv) { |
| 8367 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 8368 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8369 | /* mimic bash message */ |
| 8370 | bb_error_msg("wait: '%s': not a pid or valid job spec", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8371 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8372 | } |
| 8373 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8374 | if (WIFSIGNALED(status)) |
| 8375 | ret = 128 + WTERMSIG(status); |
| 8376 | else if (WIFEXITED(status)) |
| 8377 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8378 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8379 | ret = EXIT_FAILURE; |
| 8380 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8381 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8382 | ret = 127; |
| 8383 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8384 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8385 | } |
| 8386 | |
| 8387 | return ret; |
| 8388 | } |
| 8389 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8390 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 8391 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 8392 | { |
| 8393 | if (argv[1]) { |
| 8394 | def = bb_strtou(argv[1], NULL, 10); |
| 8395 | if (errno || def < def_min || argv[2]) { |
| 8396 | bb_error_msg("%s: bad arguments", argv[0]); |
| 8397 | def = UINT_MAX; |
| 8398 | } |
| 8399 | } |
| 8400 | return def; |
| 8401 | } |
| 8402 | #endif |
| 8403 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8404 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8405 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8406 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8407 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8408 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8409 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 8410 | return EXIT_SUCCESS; /* bash compat */ |
| 8411 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8412 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8413 | |
| 8414 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 8415 | if (depth == UINT_MAX) |
| 8416 | G.flag_break_continue = BC_BREAK; |
| 8417 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8418 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8419 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8420 | return EXIT_SUCCESS; |
| 8421 | } |
| 8422 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8423 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8424 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8425 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 8426 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8427 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8428 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8429 | |
| 8430 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8431 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8432 | { |
| 8433 | int rc; |
| 8434 | |
| 8435 | if (G.flag_return_in_progress != -1) { |
| 8436 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 8437 | return EXIT_FAILURE; /* bash compat */ |
| 8438 | } |
| 8439 | |
| 8440 | G.flag_return_in_progress = 1; |
| 8441 | |
| 8442 | /* bash: |
| 8443 | * out of range: wraps around at 256, does not error out |
| 8444 | * non-numeric param: |
| 8445 | * f() { false; return qwe; }; f; echo $? |
| 8446 | * bash: return: qwe: numeric argument required <== we do this |
| 8447 | * 255 <== we also do this |
| 8448 | */ |
| 8449 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 8450 | return rc; |
| 8451 | } |
| 8452 | #endif |