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 |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 117 | //config: hush is a small shell (25k). It handles the normal flow control |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 224 | //config: This instructs hush to print commands before execution. |
| 225 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 226 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame^] | 227 | //config:config LASH |
| 228 | //config: bool "lash (deprecated: aliased to hush)" |
| 229 | //config: default n |
| 230 | //config: select HUSH |
| 231 | //config: help |
| 232 | //config: lash is deprecated and will be removed, please migrate to hush. |
| 233 | //config: |
| 234 | //config:config MSH |
| 235 | //config: bool "msh (deprecated: aliased to hush)" |
| 236 | //config: default n |
| 237 | //config: select HUSH |
| 238 | //config: help |
| 239 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 240 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 241 | |
| 242 | //usage:#define hush_trivial_usage NOUSAGE_STR |
| 243 | //usage:#define hush_full_usage "" |
| 244 | //usage:#define lash_trivial_usage NOUSAGE_STR |
| 245 | //usage:#define lash_full_usage "" |
| 246 | //usage:#define msh_trivial_usage NOUSAGE_STR |
| 247 | //usage:#define msh_full_usage "" |
| 248 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 249 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 250 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 251 | #define LEAK_HUNTING 0 |
| 252 | #define BUILD_AS_NOMMU 0 |
| 253 | /* Enable/disable sanity checks. Ok to enable in production, |
| 254 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 255 | * Keeping 1 for now even in released versions. |
| 256 | */ |
| 257 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 258 | /* Slightly bigger (+200 bytes), but faster hush. |
| 259 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 260 | * which allows us to do fewer waitpid's. |
| 261 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 262 | * and therefore waitpid will return the same result as last time) |
| 263 | */ |
| 264 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 265 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 266 | * So far ${var%...} ops are always enabled: |
| 267 | */ |
| 268 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 269 | |
| 270 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 271 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 272 | # undef BB_MMU |
| 273 | # undef USE_FOR_NOMMU |
| 274 | # undef USE_FOR_MMU |
| 275 | # define BB_MMU 0 |
| 276 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 277 | # define USE_FOR_MMU(...) |
| 278 | #endif |
| 279 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 280 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 281 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 282 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 283 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 284 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 285 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 286 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 287 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 288 | # define IF_FEATURE_SH_STANDALONE(...) |
| 289 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 290 | #endif |
| 291 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 292 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 293 | # undef ENABLE_FEATURE_EDITING |
| 294 | # define ENABLE_FEATURE_EDITING 0 |
| 295 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 296 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 297 | #endif |
| 298 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 299 | /* Do we support ANY keywords? */ |
| 300 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 301 | # define HAS_KEYWORDS 1 |
| 302 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 303 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 304 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 305 | # define HAS_KEYWORDS 0 |
| 306 | # define IF_HAS_KEYWORDS(...) |
| 307 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 308 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 309 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 310 | /* If you comment out one of these below, it will be #defined later |
| 311 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 312 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 313 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 314 | #define debug_printf_parse(...) do {} while (0) |
| 315 | #define debug_print_tree(a, b) do {} while (0) |
| 316 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 317 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 318 | #define debug_printf_jobs(...) do {} while (0) |
| 319 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 320 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 321 | #define debug_printf_glob(...) do {} while (0) |
| 322 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 323 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 324 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 325 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 326 | #define ERR_PTR ((void*)(long)1) |
| 327 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 328 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 329 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 330 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 331 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 332 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
| 333 | |
| 334 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 335 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 336 | struct variable; |
| 337 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 338 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 339 | |
| 340 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 341 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 342 | */ |
| 343 | #if !BB_MMU |
| 344 | typedef struct nommu_save_t { |
| 345 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 346 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 347 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 348 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 349 | } nommu_save_t; |
| 350 | #endif |
| 351 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 352 | typedef enum reserved_style { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 353 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 354 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 355 | RES_IF , |
| 356 | RES_THEN , |
| 357 | RES_ELIF , |
| 358 | RES_ELSE , |
| 359 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 360 | #endif |
| 361 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 362 | RES_FOR , |
| 363 | RES_WHILE , |
| 364 | RES_UNTIL , |
| 365 | RES_DO , |
| 366 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 367 | #endif |
| 368 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 369 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 370 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 371 | #if ENABLE_HUSH_CASE |
| 372 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 373 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 374 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 375 | RES_MATCH , /* "word)" */ |
| 376 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 377 | RES_ESAC , |
| 378 | #endif |
| 379 | RES_XXXX , |
| 380 | RES_SNTX |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 381 | } reserved_style; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 382 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 383 | typedef struct o_string { |
| 384 | char *data; |
| 385 | int length; /* position where data is appended */ |
| 386 | int maxlen; |
| 387 | /* Protect newly added chars against globbing |
| 388 | * (by prepending \ to *, ?, [, \) */ |
| 389 | smallint o_escape; |
| 390 | smallint o_glob; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 391 | /* At least some part of the string was inside '' or "", |
| 392 | * possibly empty one: word"", wo''rd etc. */ |
| 393 | smallint o_quoted; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 394 | smallint has_empty_slot; |
| 395 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 396 | } o_string; |
| 397 | enum { |
| 398 | MAYBE_ASSIGNMENT = 0, |
| 399 | DEFINITELY_ASSIGNMENT = 1, |
| 400 | NOT_ASSIGNMENT = 2, |
| 401 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 402 | }; |
| 403 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 404 | #define NULL_O_STRING { NULL } |
| 405 | |
| 406 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 407 | * available? Where is it documented? */ |
| 408 | typedef struct in_str { |
| 409 | const char *p; |
| 410 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 411 | char eof_flag; /* meaningless if ->p == NULL */ |
| 412 | char peek_buf[2]; |
| 413 | #if ENABLE_HUSH_INTERACTIVE |
| 414 | smallint promptme; |
| 415 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 416 | #endif |
| 417 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 418 | int (*get) (struct in_str *) FAST_FUNC; |
| 419 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 420 | } in_str; |
| 421 | #define i_getch(input) ((input)->get(input)) |
| 422 | #define i_peek(input) ((input)->peek(input)) |
| 423 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 424 | /* The descrip member of this structure is only used to make |
| 425 | * debugging output pretty */ |
| 426 | static const struct { |
| 427 | int mode; |
| 428 | signed char default_fd; |
| 429 | char descrip[3]; |
| 430 | } redir_table[] = { |
| 431 | { O_RDONLY, 0, "<" }, |
| 432 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 433 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 434 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 435 | { O_RDONLY, 0, "<<" }, |
| 436 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 437 | /* { O_RDONLY, 77, "<<" }, */ |
| 438 | }; |
| 439 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 440 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 441 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 442 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 443 | int rd_fd; /* fd to redirect */ |
| 444 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 445 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 446 | smallint rd_type; /* (enum redir_type) */ |
| 447 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 448 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 449 | * bit 0: do we need to trim leading tabs? |
| 450 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 451 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 452 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 453 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 454 | REDIRECT_INPUT = 0, |
| 455 | REDIRECT_OVERWRITE = 1, |
| 456 | REDIRECT_APPEND = 2, |
| 457 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 458 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 459 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 460 | |
| 461 | REDIRFD_CLOSE = -3, |
| 462 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 463 | REDIRFD_TO_FILE = -1, |
| 464 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 465 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 466 | HEREDOC_SKIPTABS = 1, |
| 467 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 468 | } redir_type; |
| 469 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 470 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 471 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 472 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 473 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 474 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 475 | smallint cmd_type; /* CMD_xxx */ |
| 476 | #define CMD_NORMAL 0 |
| 477 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 478 | |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 479 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 480 | #if ENABLE_HUSH_BASH_COMPAT |
| 481 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 482 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 483 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 484 | /* used for "export noglob=* glob* a=`echo a b`" */ |
Denys Vlasenko | 82a6fb3 | 2009-06-14 19:42:12 +0200 | [diff] [blame] | 485 | //#define CMD_SINGLEWORD_NOGLOB_COND 3 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 486 | // It is hard to implement correctly, it adds significant amounts of tricky code, |
| 487 | // and all this is only useful for really obscure export statements |
| 488 | // almost nobody would use anyway. #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 489 | // guards the code which implements it, but I have doubts it works |
| 490 | // in all cases (especially with mixed globbed/non-globbed arguments) |
| 491 | |
| 492 | #if ENABLE_HUSH_FUNCTIONS |
| 493 | # define CMD_FUNCDEF 3 |
| 494 | #endif |
| 495 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 496 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 497 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 498 | #if !BB_MMU |
| 499 | char *group_as_string; |
| 500 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 501 | #if ENABLE_HUSH_FUNCTIONS |
| 502 | struct function *child_func; |
| 503 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 504 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 505 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 506 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 507 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 508 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 509 | * we put those fields back into cmd->xxx |
| 510 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 511 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 512 | * Without this trick, loop would execute a;b;b;b;... |
| 513 | * instead of correct sequence a;b;a;b;... |
| 514 | * When command is freed, it severs the link |
| 515 | * (sets ->child_func->parent_cmd to NULL). |
| 516 | */ |
| 517 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 518 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 519 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 520 | * and on execution these are substituted with their values. |
| 521 | * Substitution can make _several_ words out of one argv[n]! |
| 522 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 523 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 524 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 525 | struct redir_struct *redirects; /* I/O redirections */ |
| 526 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 527 | /* Is there anything in this command at all? */ |
| 528 | #define IS_NULL_CMD(cmd) \ |
| 529 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 530 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 531 | |
| 532 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 533 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 534 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 535 | int alive_cmds; /* number of commands running (not exited) */ |
| 536 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 537 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 538 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 539 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 540 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 541 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 542 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 543 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 544 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 545 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 546 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 547 | typedef enum pipe_style { |
| 548 | PIPE_SEQ = 1, |
| 549 | PIPE_AND = 2, |
| 550 | PIPE_OR = 3, |
| 551 | PIPE_BG = 4, |
| 552 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 553 | /* Is there anything in this pipe at all? */ |
| 554 | #define IS_NULL_PIPE(pi) \ |
| 555 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 556 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 557 | /* This holds pointers to the various results of parsing */ |
| 558 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 559 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 560 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 561 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 562 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 563 | /* last command in pipe (being constructed right now) */ |
| 564 | struct command *command; |
| 565 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 566 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 567 | #if !BB_MMU |
| 568 | o_string as_string; |
| 569 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 570 | #if HAS_KEYWORDS |
| 571 | smallint ctx_res_w; |
| 572 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 573 | #if ENABLE_HUSH_CASE |
| 574 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 575 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 576 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 577 | int old_flag; |
| 578 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 579 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 580 | * when we see "if" or "then", we malloc and copy current context, |
| 581 | * and make ->stack point to it. then we parse pipeN. |
| 582 | * when closing "then" / fi" / whatever is found, |
| 583 | * we move list_head into ->stack->command->group, |
| 584 | * copy ->stack into current context, and delete ->stack. |
| 585 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 586 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 587 | struct parse_context *stack; |
| 588 | #endif |
| 589 | }; |
| 590 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 591 | /* On program start, environ points to initial environment. |
| 592 | * putenv adds new pointers into it, unsetenv removes them. |
| 593 | * Neither of these (de)allocates the strings. |
| 594 | * setenv allocates new strings in malloc space and does putenv, |
| 595 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 596 | #define setenv(...) setenv_is_leaky_dont_use() |
| 597 | struct variable { |
| 598 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 599 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 600 | #if ENABLE_HUSH_LOCAL |
| 601 | unsigned func_nest_level; |
| 602 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 603 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 604 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 605 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 606 | }; |
| 607 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 608 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 609 | BC_BREAK = 1, |
| 610 | BC_CONTINUE = 2, |
| 611 | }; |
| 612 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 613 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 614 | struct function { |
| 615 | struct function *next; |
| 616 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 617 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 618 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 619 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 620 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 621 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 622 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 623 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 624 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 625 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 626 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 627 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 628 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 629 | /* interactive_fd != 0 means we are an interactive shell. |
| 630 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 631 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 632 | * job control still works, but terminal signals |
| 633 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 634 | * process groups can only be created with "cmd &". |
| 635 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 636 | * to give tty to the foreground process group, |
| 637 | * and will take it back when the group is stopped (^Z) |
| 638 | * or killed (^C). |
| 639 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 640 | #if ENABLE_HUSH_INTERACTIVE |
| 641 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 642 | * _AND_ if we decided to act interactively */ |
| 643 | int interactive_fd; |
| 644 | const char *PS1; |
| 645 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 646 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 647 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 648 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 649 | #endif |
| 650 | #if ENABLE_FEATURE_EDITING |
| 651 | line_input_t *line_input_state; |
| 652 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 653 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 654 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 655 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 656 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 657 | random_t random_gen; |
| 658 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 659 | #if ENABLE_HUSH_JOB |
| 660 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 661 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 662 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 663 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 664 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 665 | #else |
| 666 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 667 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 668 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 669 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 670 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 671 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 672 | #if ENABLE_HUSH_FUNCTIONS |
| 673 | /* 0: outside of a function (or sourced file) |
| 674 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 675 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 676 | */ |
| 677 | smallint flag_return_in_progress; |
| 678 | #endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 679 | smallint n_mode; |
| 680 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 681 | smallint x_mode; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 682 | # define G_x_mode (G.x_mode) |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 683 | #else |
| 684 | # define G_x_mode 0 |
| 685 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 686 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 687 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 688 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 689 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 690 | smalluint global_args_malloced; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 691 | smalluint inherited_set_is_saved; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 692 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 693 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 694 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 695 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 696 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 697 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 698 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 699 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 700 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 701 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 702 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 703 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 704 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 705 | struct variable shell_ver; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 706 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 707 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 708 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 709 | # if ENABLE_HUSH_LOCAL |
| 710 | struct variable **shadowed_vars_pp; |
| 711 | unsigned func_nest_level; |
| 712 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 713 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 714 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 715 | #if ENABLE_HUSH_FAST |
| 716 | unsigned count_SIGCHLD; |
| 717 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 718 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 719 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 720 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 721 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 722 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 723 | sigset_t blocked_set; |
| 724 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 725 | #if HUSH_DEBUG |
| 726 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 727 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 728 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 729 | 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] | 730 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 731 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 732 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 733 | * (too many defines). Also, I actually prefer to see when a variable |
| 734 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 735 | #define INIT_G() do { \ |
| 736 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 737 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 738 | |
| 739 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 740 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 741 | static int builtin_cd(char **argv) FAST_FUNC; |
| 742 | static int builtin_echo(char **argv) FAST_FUNC; |
| 743 | static int builtin_eval(char **argv) FAST_FUNC; |
| 744 | static int builtin_exec(char **argv) FAST_FUNC; |
| 745 | static int builtin_exit(char **argv) FAST_FUNC; |
| 746 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 747 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 748 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 749 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 750 | #endif |
| 751 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 752 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 753 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 754 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 755 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 756 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 757 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 758 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 759 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 760 | #if ENABLE_PRINTF |
| 761 | static int builtin_printf(char **argv) FAST_FUNC; |
| 762 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 763 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 764 | static int builtin_read(char **argv) FAST_FUNC; |
| 765 | static int builtin_set(char **argv) FAST_FUNC; |
| 766 | static int builtin_shift(char **argv) FAST_FUNC; |
| 767 | static int builtin_source(char **argv) FAST_FUNC; |
| 768 | static int builtin_test(char **argv) FAST_FUNC; |
| 769 | static int builtin_trap(char **argv) FAST_FUNC; |
| 770 | static int builtin_type(char **argv) FAST_FUNC; |
| 771 | static int builtin_true(char **argv) FAST_FUNC; |
| 772 | static int builtin_umask(char **argv) FAST_FUNC; |
| 773 | static int builtin_unset(char **argv) FAST_FUNC; |
| 774 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 775 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 776 | static int builtin_break(char **argv) FAST_FUNC; |
| 777 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 778 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 779 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 780 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 781 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 782 | |
| 783 | /* Table of built-in functions. They can be forked or not, depending on |
| 784 | * context: within pipes, they fork. As simple commands, they do not. |
| 785 | * When used in non-forking context, they can change global variables |
| 786 | * in the parent shell process. If forked, of course they cannot. |
| 787 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 788 | * still be set at the end. */ |
| 789 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 790 | const char *b_cmd; |
| 791 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 792 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 793 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 794 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 795 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 796 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 797 | #endif |
| 798 | }; |
| 799 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 800 | static const struct built_in_command bltins1[] = { |
| 801 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 802 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 803 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 804 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 805 | #endif |
| 806 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 807 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 808 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 809 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 810 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 811 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 812 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 813 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 814 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 815 | BLTIN("exit" , builtin_exit , "Exit"), |
| 816 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 817 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 818 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 819 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 820 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 821 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 822 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 823 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 824 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 825 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 826 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 827 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 828 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 829 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 830 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 831 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 832 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 833 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 834 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 835 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 836 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 837 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 838 | #if ENABLE_HUSH_BASH_COMPAT |
| 839 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 840 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 841 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 842 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 843 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 844 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 845 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 846 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 847 | }; |
| 848 | /* For now, echo and test are unconditionally enabled. |
| 849 | * Maybe make it configurable? */ |
| 850 | static const struct built_in_command bltins2[] = { |
| 851 | BLTIN("[" , builtin_test , NULL), |
| 852 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 853 | #if ENABLE_PRINTF |
| 854 | BLTIN("printf" , builtin_printf , NULL), |
| 855 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 856 | BLTIN("pwd" , builtin_pwd , NULL), |
| 857 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 858 | }; |
| 859 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 860 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 861 | /* Debug printouts. |
| 862 | */ |
| 863 | #if HUSH_DEBUG |
| 864 | /* prevent disasters with G.debug_indent < 0 */ |
| 865 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 866 | # define debug_enter() (G.debug_indent++) |
| 867 | # define debug_leave() (G.debug_indent--) |
| 868 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 869 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 870 | # define debug_enter() ((void)0) |
| 871 | # define debug_leave() ((void)0) |
| 872 | #endif |
| 873 | |
| 874 | #ifndef debug_printf |
| 875 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 876 | #endif |
| 877 | |
| 878 | #ifndef debug_printf_parse |
| 879 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 880 | #endif |
| 881 | |
| 882 | #ifndef debug_printf_exec |
| 883 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 884 | #endif |
| 885 | |
| 886 | #ifndef debug_printf_env |
| 887 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 888 | #endif |
| 889 | |
| 890 | #ifndef debug_printf_jobs |
| 891 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 892 | # define DEBUG_JOBS 1 |
| 893 | #else |
| 894 | # define DEBUG_JOBS 0 |
| 895 | #endif |
| 896 | |
| 897 | #ifndef debug_printf_expand |
| 898 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 899 | # define DEBUG_EXPAND 1 |
| 900 | #else |
| 901 | # define DEBUG_EXPAND 0 |
| 902 | #endif |
| 903 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 904 | #ifndef debug_printf_varexp |
| 905 | # define debug_printf_varexp(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 906 | #endif |
| 907 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 908 | #ifndef debug_printf_glob |
| 909 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 910 | # define DEBUG_GLOB 1 |
| 911 | #else |
| 912 | # define DEBUG_GLOB 0 |
| 913 | #endif |
| 914 | |
| 915 | #ifndef debug_printf_list |
| 916 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 917 | #endif |
| 918 | |
| 919 | #ifndef debug_printf_subst |
| 920 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 921 | #endif |
| 922 | |
| 923 | #ifndef debug_printf_clean |
| 924 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 925 | # define DEBUG_CLEAN 1 |
| 926 | #else |
| 927 | # define DEBUG_CLEAN 0 |
| 928 | #endif |
| 929 | |
| 930 | #if DEBUG_EXPAND |
| 931 | static void debug_print_strings(const char *prefix, char **vv) |
| 932 | { |
| 933 | indent(); |
| 934 | fprintf(stderr, "%s:\n", prefix); |
| 935 | while (*vv) |
| 936 | fprintf(stderr, " '%s'\n", *vv++); |
| 937 | } |
| 938 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 939 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 940 | #endif |
| 941 | |
| 942 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 943 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 944 | */ |
| 945 | #if LEAK_HUNTING |
| 946 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 947 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 948 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 949 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 950 | return ptr; |
| 951 | } |
| 952 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 953 | { |
| 954 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 955 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 956 | return ptr; |
| 957 | } |
| 958 | static char *xxstrdup(int lineno, const char *str) |
| 959 | { |
| 960 | char *ptr = xstrdup(str); |
| 961 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 962 | return ptr; |
| 963 | } |
| 964 | static void xxfree(void *ptr) |
| 965 | { |
| 966 | fdprintf(2, "free %p\n", ptr); |
| 967 | free(ptr); |
| 968 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 969 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 970 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 971 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 972 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 973 | #endif |
| 974 | |
| 975 | |
| 976 | /* Syntax and runtime errors. They always abort scripts. |
| 977 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 978 | * and return to the prompt. |
| 979 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 980 | */ |
| 981 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 982 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 983 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 984 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 985 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 986 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 987 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 988 | #endif |
| 989 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 990 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 991 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 992 | va_list p; |
| 993 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 994 | #if HUSH_DEBUG >= 2 |
| 995 | bb_error_msg("hush.c:%u", lineno); |
| 996 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 997 | va_start(p, fmt); |
| 998 | bb_verror_msg(fmt, p, NULL); |
| 999 | va_end(p); |
| 1000 | if (!G_interactive_fd) |
| 1001 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1002 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1003 | |
| 1004 | static void syntax_error(unsigned lineno, const char *msg) |
| 1005 | { |
| 1006 | if (msg) |
| 1007 | die_if_script(lineno, "syntax error: %s", msg); |
| 1008 | else |
| 1009 | die_if_script(lineno, "syntax error", NULL); |
| 1010 | } |
| 1011 | |
| 1012 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 1013 | { |
| 1014 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 1015 | } |
| 1016 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1017 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 1018 | { |
| 1019 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 1020 | } |
| 1021 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1022 | /* It so happens that all such cases are totally fatal |
| 1023 | * even if shell is interactive: EOF while looking for closing |
| 1024 | * delimiter. There is nowhere to read stuff from after that, |
| 1025 | * it's EOF! The only choice is to terminate. |
| 1026 | */ |
| 1027 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1028 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1029 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1030 | char msg[2] = { ch, '\0' }; |
| 1031 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1032 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1035 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1036 | { |
| 1037 | char msg[2]; |
| 1038 | msg[0] = ch; |
| 1039 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1040 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1043 | #if HUSH_DEBUG < 2 |
| 1044 | # undef die_if_script |
| 1045 | # undef syntax_error |
| 1046 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1047 | # undef syntax_error_unterm_ch |
| 1048 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1049 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1050 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1051 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1052 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1053 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1054 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1055 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1056 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1057 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1058 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1059 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1060 | #if ENABLE_HUSH_INTERACTIVE |
| 1061 | static void cmdedit_update_prompt(void); |
| 1062 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1063 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1064 | #endif |
| 1065 | |
| 1066 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1067 | /* Utility functions |
| 1068 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1069 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1070 | static char *unbackslash(char *src) |
| 1071 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1072 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1073 | while (1) { |
| 1074 | if (*src == '\\') |
| 1075 | src++; |
| 1076 | if ((*dst++ = *src++) == '\0') |
| 1077 | break; |
| 1078 | } |
| 1079 | return dst; |
| 1080 | } |
| 1081 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1082 | 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] | 1083 | { |
| 1084 | int i; |
| 1085 | unsigned count1; |
| 1086 | unsigned count2; |
| 1087 | char **v; |
| 1088 | |
| 1089 | v = strings; |
| 1090 | count1 = 0; |
| 1091 | if (v) { |
| 1092 | while (*v) { |
| 1093 | count1++; |
| 1094 | v++; |
| 1095 | } |
| 1096 | } |
| 1097 | count2 = 0; |
| 1098 | v = add; |
| 1099 | while (*v) { |
| 1100 | count2++; |
| 1101 | v++; |
| 1102 | } |
| 1103 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1104 | v[count1 + count2] = NULL; |
| 1105 | i = count2; |
| 1106 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1107 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1108 | return v; |
| 1109 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1110 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1111 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1112 | { |
| 1113 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1114 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1115 | return ptr; |
| 1116 | } |
| 1117 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1118 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1119 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1120 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1121 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1122 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1123 | { |
| 1124 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1125 | v[0] = add; |
| 1126 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1127 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1128 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1129 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1130 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1131 | { |
| 1132 | char **ptr = add_string_to_strings(strings, add); |
| 1133 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1134 | return ptr; |
| 1135 | } |
| 1136 | #define add_string_to_strings(strings, add) \ |
| 1137 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1138 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1139 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1140 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1141 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1142 | char **v; |
| 1143 | |
| 1144 | if (!strings) |
| 1145 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1146 | v = strings; |
| 1147 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1148 | free(*v); |
| 1149 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1150 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1151 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1154 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1155 | /* Helpers for setting new $n and restoring them back |
| 1156 | */ |
| 1157 | typedef struct save_arg_t { |
| 1158 | char *sv_argv0; |
| 1159 | char **sv_g_argv; |
| 1160 | int sv_g_argc; |
| 1161 | smallint sv_g_malloced; |
| 1162 | } save_arg_t; |
| 1163 | |
| 1164 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1165 | { |
| 1166 | int n; |
| 1167 | |
| 1168 | sv->sv_argv0 = argv[0]; |
| 1169 | sv->sv_g_argv = G.global_argv; |
| 1170 | sv->sv_g_argc = G.global_argc; |
| 1171 | sv->sv_g_malloced = G.global_args_malloced; |
| 1172 | |
| 1173 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1174 | G.global_argv = argv; |
| 1175 | G.global_args_malloced = 0; |
| 1176 | |
| 1177 | n = 1; |
| 1178 | while (*++argv) |
| 1179 | n++; |
| 1180 | G.global_argc = n; |
| 1181 | } |
| 1182 | |
| 1183 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1184 | { |
| 1185 | char **pp; |
| 1186 | |
| 1187 | if (G.global_args_malloced) { |
| 1188 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1189 | pp = G.global_argv; |
| 1190 | while (*++pp) /* note: does not free $0 */ |
| 1191 | free(*pp); |
| 1192 | free(G.global_argv); |
| 1193 | } |
| 1194 | argv[0] = sv->sv_argv0; |
| 1195 | G.global_argv = sv->sv_g_argv; |
| 1196 | G.global_argc = sv->sv_g_argc; |
| 1197 | G.global_args_malloced = sv->sv_g_malloced; |
| 1198 | } |
| 1199 | |
| 1200 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1201 | /* Basic theory of signal handling in shell |
| 1202 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1203 | * This does not describe what hush does, rather, it is current understanding |
| 1204 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1205 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1206 | * |
| 1207 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1208 | * is finished or backgrounded. It is the same in interactive and |
| 1209 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1210 | * 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] | 1211 | * ^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] | 1212 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1213 | * pipe. |
| 1214 | * |
| 1215 | * Wait builtin in interruptible by signals for which user trap is set |
| 1216 | * or by SIGINT in interactive shell. |
| 1217 | * |
| 1218 | * Trap handlers will execute even within trap handlers. (right?) |
| 1219 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1220 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1221 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1222 | * |
| 1223 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1224 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1225 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1226 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1227 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1228 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1229 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1230 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1231 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1232 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1233 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1234 | * |
| 1235 | * SIGQUIT: ignore |
| 1236 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1237 | * SIGHUP (interactive): |
| 1238 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1239 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1240 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1241 | * that all pipe members are stopped. Try this in bash: |
| 1242 | * while :; do :; done - ^Z does not background it |
| 1243 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1244 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1245 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1246 | * to interactive shell while shell is waiting for a pipe, |
| 1247 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1248 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1249 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1250 | * Example 2: this does not wait and does not execute ls: |
| 1251 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1252 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1253 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1254 | * |
| 1255 | * (What happens to signals which are IGN on shell start?) |
| 1256 | * (What happens with signal mask on shell start?) |
| 1257 | * |
| 1258 | * Implementation in hush |
| 1259 | * ====================== |
| 1260 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1261 | * We block all signals which we don't want to take action immediately, |
| 1262 | * i.e. we block all signals which need to have special handling as described |
| 1263 | * above, and all signals which have traps set. |
| 1264 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1265 | * and act on them. |
| 1266 | * |
| 1267 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1268 | * sigset_t blocked_set: current blocked signal set |
| 1269 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1270 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1271 | * 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] | 1272 | * "trap 'cmd' SIGxxx": |
| 1273 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1274 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1275 | * unblock signals with special interactive handling |
| 1276 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1277 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1278 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1279 | * 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] | 1280 | * 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] | 1281 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1282 | * 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] | 1283 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1284 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1285 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1286 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1287 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1288 | * 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] | 1289 | * 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] | 1290 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1291 | enum { |
| 1292 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1293 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1294 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1295 | | (1 << SIGHUP) |
| 1296 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1297 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1298 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1299 | | (1 << SIGTTIN) |
| 1300 | | (1 << SIGTTOU) |
| 1301 | | (1 << SIGTSTP) |
| 1302 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1303 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1304 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1305 | #if ENABLE_HUSH_FAST |
| 1306 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1307 | { |
| 1308 | G.count_SIGCHLD++; |
| 1309 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1310 | } |
| 1311 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1312 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1313 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1314 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1315 | /* 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] | 1316 | # define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1317 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1318 | # define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1319 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1320 | /* Restores tty foreground process group, and exits. |
| 1321 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1322 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1323 | * or called directly with -EXITCODE. |
| 1324 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1325 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1326 | static void sigexit(int sig) |
| 1327 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1328 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1329 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1330 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1331 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1332 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1333 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1334 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1335 | |
| 1336 | /* Not a signal, just exit */ |
| 1337 | if (sig <= 0) |
| 1338 | _exit(- sig); |
| 1339 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1340 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1341 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1342 | #else |
| 1343 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1344 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1345 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1346 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1347 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1348 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1349 | /* Restores tty foreground process group, and exits. */ |
| 1350 | static void hush_exit(int exitcode) NORETURN; |
| 1351 | static void hush_exit(int exitcode) |
| 1352 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1353 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1354 | /* Prevent recursion: |
| 1355 | * trap "echo Hi; exit" EXIT; exit |
| 1356 | */ |
| 1357 | char *argv[] = { NULL, G.traps[0], NULL }; |
| 1358 | G.traps[0] = NULL; |
| 1359 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1360 | builtin_eval(argv); |
| 1361 | free(argv[1]); |
| 1362 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1363 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1364 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1365 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1366 | sigexit(- (exitcode & 0xff)); |
| 1367 | #else |
| 1368 | exit(exitcode); |
| 1369 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1372 | static int check_and_run_traps(int sig) |
| 1373 | { |
Dan Fandrich | fdd7b56 | 2010-06-18 22:37:42 -0700 | [diff] [blame] | 1374 | static const struct timespec zero_timespec; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1375 | smalluint save_rcode; |
| 1376 | int last_sig = 0; |
| 1377 | |
| 1378 | if (sig) |
| 1379 | goto jump_in; |
| 1380 | while (1) { |
| 1381 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1382 | if (sig <= 0) |
| 1383 | break; |
| 1384 | jump_in: |
| 1385 | last_sig = sig; |
| 1386 | if (G.traps && G.traps[sig]) { |
| 1387 | if (G.traps[sig][0]) { |
| 1388 | /* We have user-defined handler */ |
| 1389 | char *argv[] = { NULL, xstrdup(G.traps[sig]), NULL }; |
| 1390 | save_rcode = G.last_exitcode; |
| 1391 | builtin_eval(argv); |
| 1392 | free(argv[1]); |
| 1393 | G.last_exitcode = save_rcode; |
| 1394 | } /* else: "" trap, ignoring signal */ |
| 1395 | continue; |
| 1396 | } |
| 1397 | /* not a trap: special action */ |
| 1398 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1399 | #if ENABLE_HUSH_FAST |
| 1400 | case SIGCHLD: |
| 1401 | G.count_SIGCHLD++; |
| 1402 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1403 | break; |
| 1404 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1405 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1406 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1407 | bb_putchar('\n'); |
| 1408 | G.flag_SIGINT = 1; |
| 1409 | break; |
| 1410 | #if ENABLE_HUSH_JOB |
| 1411 | case SIGHUP: { |
| 1412 | struct pipe *job; |
| 1413 | /* bash is observed to signal whole process groups, |
| 1414 | * not individual processes */ |
| 1415 | for (job = G.job_list; job; job = job->next) { |
| 1416 | if (job->pgrp <= 0) |
| 1417 | continue; |
| 1418 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1419 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1420 | kill(- job->pgrp, SIGCONT); |
| 1421 | } |
| 1422 | sigexit(SIGHUP); |
| 1423 | } |
| 1424 | #endif |
| 1425 | default: /* ignored: */ |
| 1426 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
| 1430 | return last_sig; |
| 1431 | } |
| 1432 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1433 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1434 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1435 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1436 | if (force || G.cwd == NULL) { |
| 1437 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1438 | * we must not try to free(bb_msg_unknown) */ |
| 1439 | if (G.cwd == bb_msg_unknown) |
| 1440 | G.cwd = NULL; |
| 1441 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1442 | if (!G.cwd) |
| 1443 | G.cwd = bb_msg_unknown; |
| 1444 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1445 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1448 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1449 | /* |
| 1450 | * Shell and environment variable support |
| 1451 | */ |
| 1452 | static struct variable **get_ptr_to_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; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1455 | struct variable *cur; |
| 1456 | int len; |
| 1457 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1458 | len = strlen(name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1459 | pp = &G.top_var; |
| 1460 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1461 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1462 | return pp; |
| 1463 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1464 | } |
| 1465 | return NULL; |
| 1466 | } |
| 1467 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1468 | static struct variable *get_local_var(const char *name) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1469 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1470 | struct variable **pp = get_ptr_to_local_var(name); |
| 1471 | if (pp) |
| 1472 | return *pp; |
| 1473 | return NULL; |
| 1474 | } |
| 1475 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1476 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1477 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1478 | struct variable **vpp; |
| 1479 | |
| 1480 | if (G.expanded_assignments) { |
| 1481 | char **cpp = G.expanded_assignments; |
| 1482 | int len = strlen(name); |
| 1483 | while (*cpp) { |
| 1484 | char *cp = *cpp; |
| 1485 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1486 | return cp + len + 1; |
| 1487 | cpp++; |
| 1488 | } |
| 1489 | } |
| 1490 | |
| 1491 | vpp = get_ptr_to_local_var(name); |
| 1492 | if (vpp) |
| 1493 | return strchr((*vpp)->varstr, '=') + 1; |
| 1494 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1495 | if (strcmp(name, "PPID") == 0) |
| 1496 | return utoa(G.root_ppid); |
| 1497 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1498 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1499 | if (strcmp(name, "RANDOM") == 0) { |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1500 | return utoa(next_random(&G.random_gen)); |
Denys Vlasenko | 8c66a9d | 2009-10-11 02:15:49 +0200 | [diff] [blame] | 1501 | } |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1502 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1503 | return NULL; |
| 1504 | } |
| 1505 | |
| 1506 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1507 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1508 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1509 | * 0: do not change export flag |
| 1510 | * (if creating new variable, flag will be 0) |
| 1511 | * 1: set export flag and putenv the variable |
| 1512 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1513 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1514 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1515 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1516 | /* all params are used */ |
| 1517 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1518 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1519 | set_local_var(str, flg_export, local_lvl) |
| 1520 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1521 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1522 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1523 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1524 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1525 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1526 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1527 | 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] | 1528 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1529 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1530 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1531 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1532 | int name_len; |
| 1533 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1534 | eq_sign = strchr(str, '='); |
| 1535 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1536 | free(str); |
| 1537 | return -1; |
| 1538 | } |
| 1539 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1540 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1541 | var_pp = &G.top_var; |
| 1542 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1543 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1544 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1545 | continue; |
| 1546 | } |
| 1547 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1548 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1549 | #if !BB_MMU |
| 1550 | if (!flg_read_only) |
| 1551 | #endif |
| 1552 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1553 | free(str); |
| 1554 | return -1; |
| 1555 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1556 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1557 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1558 | *eq_sign = '\0'; |
| 1559 | unsetenv(str); |
| 1560 | *eq_sign = '='; |
| 1561 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1562 | #if ENABLE_HUSH_LOCAL |
| 1563 | if (cur->func_nest_level < local_lvl) { |
| 1564 | /* New variable is declared as local, |
| 1565 | * and existing one is global, or local |
| 1566 | * from enclosing function. |
| 1567 | * Remove and save old one: */ |
| 1568 | *var_pp = cur->next; |
| 1569 | cur->next = *G.shadowed_vars_pp; |
| 1570 | *G.shadowed_vars_pp = cur; |
| 1571 | /* bash 3.2.33(1) and exported vars: |
| 1572 | * # export z=z |
| 1573 | * # f() { local z=a; env | grep ^z; } |
| 1574 | * # f |
| 1575 | * z=a |
| 1576 | * # env | grep ^z |
| 1577 | * z=z |
| 1578 | */ |
| 1579 | if (cur->flg_export) |
| 1580 | flg_export = 1; |
| 1581 | break; |
| 1582 | } |
| 1583 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1584 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1585 | free_and_exp: |
| 1586 | free(str); |
| 1587 | goto exp; |
| 1588 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1589 | if (cur->max_len != 0) { |
| 1590 | if (cur->max_len >= strlen(str)) { |
| 1591 | /* This one is from startup env, reuse space */ |
| 1592 | strcpy(cur->varstr, str); |
| 1593 | goto free_and_exp; |
| 1594 | } |
| 1595 | } else { |
| 1596 | /* max_len == 0 signifies "malloced" var, which we can |
| 1597 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1598 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1599 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1600 | cur->max_len = 0; |
| 1601 | goto set_str_and_exp; |
| 1602 | } |
| 1603 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1604 | /* Not found - create new variable struct */ |
| 1605 | cur = xzalloc(sizeof(*cur)); |
| 1606 | #if ENABLE_HUSH_LOCAL |
| 1607 | cur->func_nest_level = local_lvl; |
| 1608 | #endif |
| 1609 | cur->next = *var_pp; |
| 1610 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1611 | |
| 1612 | set_str_and_exp: |
| 1613 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1614 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1615 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1616 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1617 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1618 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1619 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1620 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1621 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1622 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1623 | if (flg_export == -1) { |
| 1624 | cur->flg_export = 0; |
| 1625 | /* unsetenv was already done */ |
| 1626 | } else { |
| 1627 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1628 | return putenv(cur->varstr); |
| 1629 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1630 | } |
| 1631 | return 0; |
| 1632 | } |
| 1633 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1634 | /* Used at startup and after each cd */ |
| 1635 | static void set_pwd_var(int exp) |
| 1636 | { |
| 1637 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1638 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1639 | } |
| 1640 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1641 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1642 | { |
| 1643 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1644 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1645 | |
| 1646 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1647 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1648 | var_pp = &G.top_var; |
| 1649 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1650 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1651 | if (cur->flg_read_only) { |
| 1652 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1653 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1654 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1655 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1656 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1657 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1658 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1659 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1660 | if (!cur->max_len) |
| 1661 | free(cur->varstr); |
| 1662 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1663 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1664 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1665 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1666 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1667 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1670 | static int unset_local_var(const char *name) |
| 1671 | { |
| 1672 | return unset_local_var_len(name, strlen(name)); |
| 1673 | } |
| 1674 | |
| 1675 | static void unset_vars(char **strings) |
| 1676 | { |
| 1677 | char **v; |
| 1678 | |
| 1679 | if (!strings) |
| 1680 | return; |
| 1681 | v = strings; |
| 1682 | while (*v) { |
| 1683 | const char *eq = strchrnul(*v, '='); |
| 1684 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1685 | v++; |
| 1686 | } |
| 1687 | free(strings); |
| 1688 | } |
| 1689 | |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1690 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1691 | # define is_name(c) ((c) == '_' || isalpha((unsigned char)(c))) |
| 1692 | # define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c))) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1693 | static char* FAST_FUNC endofname(const char *name) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1694 | { |
| 1695 | char *p; |
| 1696 | |
| 1697 | p = (char *) name; |
| 1698 | if (!is_name(*p)) |
| 1699 | return p; |
| 1700 | while (*++p) { |
| 1701 | if (!is_in_name(*p)) |
| 1702 | break; |
| 1703 | } |
| 1704 | return p; |
| 1705 | } |
Denys Vlasenko | 6b01b71 | 2010-01-24 22:52:21 +0100 | [diff] [blame] | 1706 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1707 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1708 | 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] | 1709 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1710 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1711 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1712 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1713 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1714 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1715 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1716 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1717 | */ |
| 1718 | static void add_vars(struct variable *var) |
| 1719 | { |
| 1720 | struct variable *next; |
| 1721 | |
| 1722 | while (var) { |
| 1723 | next = var->next; |
| 1724 | var->next = G.top_var; |
| 1725 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1726 | if (var->flg_export) { |
| 1727 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1728 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1729 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1730 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1731 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1732 | var = next; |
| 1733 | } |
| 1734 | } |
| 1735 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1736 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1737 | { |
| 1738 | char **s; |
| 1739 | struct variable *old = NULL; |
| 1740 | |
| 1741 | if (!strings) |
| 1742 | return old; |
| 1743 | s = strings; |
| 1744 | while (*s) { |
| 1745 | struct variable *var_p; |
| 1746 | struct variable **var_pp; |
| 1747 | char *eq; |
| 1748 | |
| 1749 | eq = strchr(*s, '='); |
| 1750 | if (eq) { |
| 1751 | *eq = '\0'; |
| 1752 | var_pp = get_ptr_to_local_var(*s); |
| 1753 | *eq = '='; |
| 1754 | if (var_pp) { |
| 1755 | /* Remove variable from global linked list */ |
| 1756 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1757 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1758 | *var_pp = var_p->next; |
| 1759 | /* Add it to returned list */ |
| 1760 | var_p->next = old; |
| 1761 | old = var_p; |
| 1762 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1763 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1764 | } |
| 1765 | s++; |
| 1766 | } |
| 1767 | return old; |
| 1768 | } |
| 1769 | |
| 1770 | |
| 1771 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1772 | * in_str support |
| 1773 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1774 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1775 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1776 | int ch = *i->p; |
| 1777 | if (ch != '\0') { |
| 1778 | i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1779 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1780 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1781 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1784 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1785 | { |
| 1786 | return *i->p; |
| 1787 | } |
| 1788 | |
| 1789 | #if ENABLE_HUSH_INTERACTIVE |
| 1790 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1791 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1792 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1793 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1794 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1795 | if (G.PS1 == NULL) |
| 1796 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1797 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1798 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1799 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1800 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1801 | if (G.PS2 == NULL) |
| 1802 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1803 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1804 | |
| 1805 | static const char* setup_prompt_string(int promptmode) |
| 1806 | { |
| 1807 | const char *prompt_str; |
| 1808 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1809 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1810 | /* Set up the prompt */ |
| 1811 | if (promptmode == 0) { /* PS1 */ |
| 1812 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1813 | /* bash uses $PWD value, even if it is set by user. |
| 1814 | * It uses current dir only if PWD is unset. |
| 1815 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1816 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1817 | prompt_str = G.PS1; |
| 1818 | } else |
| 1819 | prompt_str = G.PS2; |
| 1820 | } else |
| 1821 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1822 | debug_printf("result '%s'\n", prompt_str); |
| 1823 | return prompt_str; |
| 1824 | } |
| 1825 | |
| 1826 | static void get_user_input(struct in_str *i) |
| 1827 | { |
| 1828 | int r; |
| 1829 | const char *prompt_str; |
| 1830 | |
| 1831 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1832 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1833 | /* Enable command line editing only while a command line |
| 1834 | * is actually being read */ |
| 1835 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1836 | G.flag_SIGINT = 0; |
| 1837 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1838 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1839 | 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] | 1840 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1841 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1842 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1843 | i->eof_flag = (r < 0); |
| 1844 | if (i->eof_flag) { /* EOF/error detected */ |
| 1845 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1846 | G.user_input_buf[1] = '\0'; |
| 1847 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1848 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1849 | do { |
| 1850 | G.flag_SIGINT = 0; |
| 1851 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1852 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1853 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1854 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1855 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1856 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1857 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1858 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1859 | i->p = G.user_input_buf; |
| 1860 | } |
| 1861 | |
| 1862 | #endif /* INTERACTIVE */ |
| 1863 | |
| 1864 | /* This is the magic location that prints prompts |
| 1865 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1866 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1867 | { |
| 1868 | int ch; |
| 1869 | |
| 1870 | /* If there is data waiting, eat it up */ |
| 1871 | if (i->p && *i->p) { |
| 1872 | #if ENABLE_HUSH_INTERACTIVE |
| 1873 | take_cached: |
| 1874 | #endif |
| 1875 | ch = *i->p++; |
| 1876 | if (i->eof_flag && !*i->p) |
| 1877 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1878 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1879 | } else { |
| 1880 | /* need to double check i->file because we might be doing something |
| 1881 | * more complicated by now, like sourcing or substituting. */ |
| 1882 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1883 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1884 | do { |
| 1885 | get_user_input(i); |
| 1886 | } while (!*i->p); /* need non-empty line */ |
| 1887 | i->promptmode = 1; /* PS2 */ |
| 1888 | i->promptme = 0; |
| 1889 | goto take_cached; |
| 1890 | } |
| 1891 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1892 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1893 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1894 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1895 | #if ENABLE_HUSH_INTERACTIVE |
| 1896 | if (ch == '\n') |
| 1897 | i->promptme = 1; |
| 1898 | #endif |
| 1899 | return ch; |
| 1900 | } |
| 1901 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1902 | /* All callers guarantee this routine will never |
| 1903 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1904 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1905 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1906 | { |
| 1907 | int ch; |
| 1908 | if (i->p && *i->p) { |
| 1909 | if (i->eof_flag && !i->p[1]) |
| 1910 | return EOF; |
| 1911 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1912 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1913 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1914 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1915 | i->eof_flag = (ch == EOF); |
| 1916 | i->peek_buf[0] = ch; |
| 1917 | i->peek_buf[1] = '\0'; |
| 1918 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1919 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1920 | return ch; |
| 1921 | } |
| 1922 | |
| 1923 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1924 | { |
| 1925 | i->peek = file_peek; |
| 1926 | i->get = file_get; |
| 1927 | #if ENABLE_HUSH_INTERACTIVE |
| 1928 | i->promptme = 1; |
| 1929 | i->promptmode = 0; /* PS1 */ |
| 1930 | #endif |
| 1931 | i->file = f; |
| 1932 | i->p = NULL; |
| 1933 | } |
| 1934 | |
| 1935 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1936 | { |
| 1937 | i->peek = static_peek; |
| 1938 | i->get = static_get; |
| 1939 | #if ENABLE_HUSH_INTERACTIVE |
| 1940 | i->promptme = 1; |
| 1941 | i->promptmode = 0; /* PS1 */ |
| 1942 | #endif |
| 1943 | i->p = s; |
| 1944 | i->eof_flag = 0; |
| 1945 | } |
| 1946 | |
| 1947 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1948 | /* |
| 1949 | * o_string support |
| 1950 | */ |
| 1951 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1952 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1953 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1954 | { |
| 1955 | o->length = 0; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 1956 | o->o_quoted = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1957 | if (o->data) |
| 1958 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1961 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1962 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1963 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1964 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1967 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1968 | { |
| 1969 | free(o->data); |
| 1970 | } |
| 1971 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1972 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1973 | { |
| 1974 | if (o->length + len > o->maxlen) { |
| 1975 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1976 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1977 | } |
| 1978 | } |
| 1979 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1980 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1981 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1982 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1983 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1984 | o->data[o->length] = ch; |
| 1985 | o->length++; |
| 1986 | o->data[o->length] = '\0'; |
| 1987 | } |
| 1988 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1989 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1990 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1991 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1992 | memcpy(&o->data[o->length], str, len); |
| 1993 | o->length += len; |
| 1994 | o->data[o->length] = '\0'; |
| 1995 | } |
| 1996 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1997 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1998 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1999 | o_addblock(o, str, strlen(str)); |
| 2000 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2001 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2002 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2003 | static void nommu_addchr(o_string *o, int ch) |
| 2004 | { |
| 2005 | if (o) |
| 2006 | o_addchr(o, ch); |
| 2007 | } |
| 2008 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2009 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2010 | #endif |
| 2011 | |
| 2012 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2013 | { |
| 2014 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2017 | 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] | 2018 | { |
| 2019 | while (len) { |
| 2020 | o_addchr(o, *str); |
| 2021 | if (*str++ == '\\' |
| 2022 | && (*str != '*' && *str != '?' && *str != '[') |
| 2023 | ) { |
| 2024 | o_addchr(o, '\\'); |
| 2025 | } |
| 2026 | len--; |
| 2027 | } |
| 2028 | } |
| 2029 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2030 | #undef HUSH_BRACE_EXP |
| 2031 | /* |
| 2032 | * HUSH_BRACE_EXP code needs corresponding quoting on variable expansion side. |
| 2033 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2034 | * Apparently, on unquoted $v bash still does globbing |
| 2035 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2036 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2037 | * quoting mechanisms on $v expansion side: one protects |
| 2038 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2039 | * We have only second one. |
| 2040 | */ |
| 2041 | |
| 2042 | #ifdef HUSH_BRACE_EXP |
| 2043 | # define MAYBE_BRACES "{}" |
| 2044 | #else |
| 2045 | # define MAYBE_BRACES "" |
| 2046 | #endif |
| 2047 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2048 | /* My analysis of quoting semantics tells me that state information |
| 2049 | * is associated with a destination, not a source. |
| 2050 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2051 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2052 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2053 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2054 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2055 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2056 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2057 | o_grow_by(o, sz); |
| 2058 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2059 | o->data[o->length] = '\\'; |
| 2060 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2061 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2062 | o->data[o->length] = ch; |
| 2063 | o->length++; |
| 2064 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2067 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2068 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2069 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2070 | if (o->o_escape && strchr("*?[\\" MAYBE_BRACES, ch)) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2071 | sz++; |
| 2072 | o->data[o->length] = '\\'; |
| 2073 | o->length++; |
| 2074 | } |
| 2075 | o_grow_by(o, sz); |
| 2076 | o->data[o->length] = ch; |
| 2077 | o->length++; |
| 2078 | o->data[o->length] = '\0'; |
| 2079 | } |
| 2080 | |
| 2081 | static void o_addQstr(o_string *o, const char *str, int len) |
| 2082 | { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2083 | if (!o->o_escape) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2084 | o_addblock(o, str, len); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2085 | return; |
| 2086 | } |
| 2087 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2088 | char ch; |
| 2089 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2090 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2091 | if (ordinary_cnt > len) /* paranoia */ |
| 2092 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2093 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2094 | if (ordinary_cnt == len) |
| 2095 | return; |
| 2096 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2097 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2098 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2099 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2100 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2101 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2102 | sz++; |
| 2103 | o->data[o->length] = '\\'; |
| 2104 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2105 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2106 | o_grow_by(o, sz); |
| 2107 | o->data[o->length] = ch; |
| 2108 | o->length++; |
| 2109 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2110 | } |
| 2111 | } |
| 2112 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2113 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2114 | * 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] | 2115 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2116 | * list[i] contains an INDEX (int!) into this string data. |
| 2117 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2118 | * but list[i]'s need not be modified. |
| 2119 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2120 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2121 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2122 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2123 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2124 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2125 | { |
| 2126 | char **list = (char**)o->data; |
| 2127 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2128 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2129 | |
| 2130 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2131 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d\n", |
| 2132 | prefix, list, n, string_start, o->length, o->maxlen); |
| 2133 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2134 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2135 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 2136 | o->data + (int)list[i] + string_start, |
| 2137 | o->data + (int)list[i] + string_start); |
| 2138 | i++; |
| 2139 | } |
| 2140 | if (n) { |
| 2141 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2142 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2143 | 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] | 2144 | } |
| 2145 | } |
| 2146 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2147 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2148 | #endif |
| 2149 | |
| 2150 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2151 | * in list[n] so that it points past last stored byte so far. |
| 2152 | * It returns n+1. */ |
| 2153 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2154 | { |
| 2155 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2156 | int string_start; |
| 2157 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2158 | |
| 2159 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2160 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2161 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2162 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2163 | 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] | 2164 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2165 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2166 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2167 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2168 | memmove(list + n + 0x10, list + n, string_len); |
| 2169 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2170 | } else { |
| 2171 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2172 | n, string_len, string_start); |
| 2173 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2174 | } else { |
| 2175 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2176 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2177 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2178 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2179 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2180 | o->has_empty_slot = 0; |
| 2181 | } |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2182 | list[n] = (char*)(ptrdiff_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2183 | return n + 1; |
| 2184 | } |
| 2185 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2186 | /* "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] | 2187 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2188 | { |
| 2189 | char **list = (char**)o->data; |
| 2190 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2191 | |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2192 | return ((int)(ptrdiff_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2195 | #ifdef HUSH_BRACE_EXP |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2196 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2197 | * first, it processes even {a} (no commas), second, |
| 2198 | * 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] | 2199 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2200 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2201 | |
| 2202 | /* Helper */ |
| 2203 | static int glob_needed(const char *s) |
| 2204 | { |
| 2205 | while (*s) { |
| 2206 | if (*s == '\\') { |
| 2207 | if (!s[1]) |
| 2208 | return 0; |
| 2209 | s += 2; |
| 2210 | continue; |
| 2211 | } |
| 2212 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2213 | return 1; |
| 2214 | s++; |
| 2215 | } |
| 2216 | return 0; |
| 2217 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2218 | /* Return pointer to next closing brace or to comma */ |
| 2219 | static const char *next_brace_sub(const char *cp) |
| 2220 | { |
| 2221 | unsigned depth = 0; |
| 2222 | cp++; |
| 2223 | while (*cp != '\0') { |
| 2224 | if (*cp == '\\') { |
| 2225 | if (*++cp == '\0') |
| 2226 | break; |
| 2227 | cp++; |
| 2228 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2229 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2230 | /*{*/ if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
| 2231 | break; |
| 2232 | if (*cp++ == '{') /*}*/ |
| 2233 | depth++; |
| 2234 | } |
| 2235 | |
| 2236 | return *cp != '\0' ? cp : NULL; |
| 2237 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2238 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2239 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2240 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2241 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2242 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2243 | const char *next; |
| 2244 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2245 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2246 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2247 | |
| 2248 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2249 | |
| 2250 | begin = pattern; |
| 2251 | while (1) { |
| 2252 | if (*begin == '\0') |
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 | if (*begin == '{') /*}*/ { |
| 2255 | /* Find the first sub-pattern and at the same time |
| 2256 | * find the rest after the closing brace */ |
| 2257 | next = next_brace_sub(begin); |
| 2258 | if (next == NULL) { |
| 2259 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2260 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2261 | } |
| 2262 | /*{*/ if (*next == '}') { |
| 2263 | /* "{abc}" with no commas - illegal |
| 2264 | * brace expr, disregard and skip it */ |
| 2265 | begin = next + 1; |
| 2266 | continue; |
| 2267 | } |
| 2268 | break; |
| 2269 | } |
| 2270 | if (*begin == '\\' && begin[1] != '\0') |
| 2271 | begin++; |
| 2272 | begin++; |
| 2273 | } |
| 2274 | debug_printf_glob("begin:%s\n", begin); |
| 2275 | debug_printf_glob("next:%s\n", next); |
| 2276 | |
| 2277 | /* Now find the end of the whole brace expression */ |
| 2278 | rest = next; |
| 2279 | /*{*/ while (*rest != '}') { |
| 2280 | rest = next_brace_sub(rest); |
| 2281 | if (rest == NULL) { |
| 2282 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2283 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2284 | } |
| 2285 | debug_printf_glob("rest:%s\n", rest); |
| 2286 | } |
| 2287 | rest_len = strlen(++rest) + 1; |
| 2288 | |
| 2289 | /* We are sure the brace expression is well-formed */ |
| 2290 | |
| 2291 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2292 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2293 | |
| 2294 | /* We have a brace expression. BEGIN points to the opening {, |
| 2295 | * NEXT points past the terminator of the first element, and REST |
| 2296 | * points past the final }. We will accumulate result names from |
| 2297 | * recursive runs for each brace alternative in the buffer using |
| 2298 | * GLOB_APPEND. */ |
| 2299 | |
| 2300 | p = begin + 1; |
| 2301 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2302 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2303 | memcpy( |
| 2304 | mempcpy( |
| 2305 | mempcpy(new_pattern_buf, |
| 2306 | /* We know the prefix for all sub-patterns */ |
| 2307 | pattern, begin - pattern), |
| 2308 | p, next - p), |
| 2309 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2310 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2311 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2312 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2313 | */ |
| 2314 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2315 | /*{*/ if (*next == '}') { |
| 2316 | /* We saw the last entry */ |
| 2317 | break; |
| 2318 | } |
| 2319 | p = next + 1; |
| 2320 | next = next_brace_sub(next); |
| 2321 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2322 | free(new_pattern_buf); |
| 2323 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2324 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2325 | simple_glob: |
| 2326 | { |
| 2327 | int gr; |
| 2328 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2329 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2330 | memset(&globdata, 0, sizeof(globdata)); |
| 2331 | gr = glob(pattern, 0, NULL, &globdata); |
| 2332 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2333 | if (gr != 0) { |
| 2334 | if (gr == GLOB_NOMATCH) { |
| 2335 | globfree(&globdata); |
| 2336 | /* NB: garbles parameter */ |
| 2337 | unbackslash(pattern); |
| 2338 | o_addstr_with_NUL(o, pattern); |
| 2339 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2340 | return o_save_ptr_helper(o, n); |
| 2341 | } |
| 2342 | if (gr == GLOB_NOSPACE) |
| 2343 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2344 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2345 | * but we didn't specify it. Paranoia again. */ |
| 2346 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2347 | } |
| 2348 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2349 | char **argv = globdata.gl_pathv; |
| 2350 | while (1) { |
| 2351 | o_addstr_with_NUL(o, *argv); |
| 2352 | n = o_save_ptr_helper(o, n); |
| 2353 | argv++; |
| 2354 | if (!*argv) |
| 2355 | break; |
| 2356 | } |
| 2357 | } |
| 2358 | globfree(&globdata); |
| 2359 | } |
| 2360 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2361 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2362 | /* Performs globbing on last list[], |
| 2363 | * saving each result as a new list[]. |
| 2364 | */ |
| 2365 | static int o_glob(o_string *o, int n) |
| 2366 | { |
| 2367 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2368 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2369 | debug_printf_glob("start o_glob: n:%d o->data:%p\n", n, o->data); |
| 2370 | if (!o->data) |
| 2371 | return o_save_ptr_helper(o, n); |
| 2372 | pattern = o->data + o_get_last_ptr(o, n); |
| 2373 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2374 | if (!glob_needed(pattern)) { |
| 2375 | /* unbackslash last string in o in place, fix length */ |
| 2376 | o->length = unbackslash(pattern) - o->data; |
| 2377 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2378 | return o_save_ptr_helper(o, n); |
| 2379 | } |
| 2380 | |
| 2381 | copy = xstrdup(pattern); |
| 2382 | /* "forget" pattern in o */ |
| 2383 | o->length = pattern - o->data; |
| 2384 | n = glob_brace(copy, o, n); |
| 2385 | free(copy); |
| 2386 | if (DEBUG_GLOB) |
| 2387 | debug_print_list("o_glob returning", o, n); |
| 2388 | return n; |
| 2389 | } |
| 2390 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2391 | #else /* !HUSH_BRACE_EXP */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2392 | |
| 2393 | /* Helper */ |
| 2394 | static int glob_needed(const char *s) |
| 2395 | { |
| 2396 | while (*s) { |
| 2397 | if (*s == '\\') { |
| 2398 | if (!s[1]) |
| 2399 | return 0; |
| 2400 | s += 2; |
| 2401 | continue; |
| 2402 | } |
| 2403 | if (*s == '*' || *s == '[' || *s == '?') |
| 2404 | return 1; |
| 2405 | s++; |
| 2406 | } |
| 2407 | return 0; |
| 2408 | } |
| 2409 | /* Performs globbing on last list[], |
| 2410 | * saving each result as a new list[]. |
| 2411 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2412 | static int o_glob(o_string *o, int n) |
| 2413 | { |
| 2414 | glob_t globdata; |
| 2415 | int gr; |
| 2416 | char *pattern; |
| 2417 | |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2418 | 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] | 2419 | if (!o->data) |
| 2420 | return o_save_ptr_helper(o, n); |
| 2421 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2422 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2423 | if (!glob_needed(pattern)) { |
| 2424 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2425 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2426 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2427 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2428 | return o_save_ptr_helper(o, n); |
| 2429 | } |
| 2430 | |
| 2431 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2432 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2433 | * If we glob "*.\*" and don't find anything, we need |
| 2434 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2435 | * will return "*.\*"! |
| 2436 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2437 | gr = glob(pattern, 0, NULL, &globdata); |
| 2438 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2439 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2440 | if (gr == GLOB_NOMATCH) { |
| 2441 | globfree(&globdata); |
| 2442 | goto literal; |
| 2443 | } |
| 2444 | if (gr == GLOB_NOSPACE) |
| 2445 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2446 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2447 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2448 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2449 | } |
| 2450 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2451 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2452 | /* "forget" pattern in o */ |
| 2453 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2454 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2455 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2456 | n = o_save_ptr_helper(o, n); |
| 2457 | argv++; |
| 2458 | if (!*argv) |
| 2459 | break; |
| 2460 | } |
| 2461 | } |
| 2462 | globfree(&globdata); |
| 2463 | if (DEBUG_GLOB) |
| 2464 | debug_print_list("o_glob returning", o, n); |
| 2465 | return n; |
| 2466 | } |
| 2467 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2468 | #endif /* !HUSH_BRACE_EXP */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2469 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2470 | /* If o->o_glob == 1, glob the string so far remembered. |
| 2471 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2472 | static int o_save_ptr(o_string *o, int n) |
| 2473 | { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2474 | if (o->o_glob) { /* if globbing is requested */ |
| 2475 | /* If o->has_empty_slot, list[n] was already globbed |
| 2476 | * (if it was requested back then when it was filled) |
| 2477 | * so don't do that again! */ |
| 2478 | if (!o->has_empty_slot) |
| 2479 | return o_glob(o, n); /* o_save_ptr_helper is inside */ |
| 2480 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2481 | return o_save_ptr_helper(o, n); |
| 2482 | } |
| 2483 | |
| 2484 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2485 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2486 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2487 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2488 | int string_start; |
| 2489 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2490 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2491 | if (DEBUG_EXPAND) |
| 2492 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2493 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2494 | list = (char**)o->data; |
| 2495 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2496 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2497 | while (n) { |
| 2498 | n--; |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 2499 | list[n] = o->data + (int)(ptrdiff_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2500 | } |
| 2501 | return list; |
| 2502 | } |
| 2503 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2504 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2505 | /* Expansion can recurse */ |
| 2506 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2507 | static int process_command_subs(o_string *dest, const char *s); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2508 | #endif |
| 2509 | static char *expand_string_to_string(const char *str); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2510 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2511 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2512 | parse_stream_dquoted(dest, input, dquote_end) |
| 2513 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 2514 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2515 | o_string *dest, |
| 2516 | struct in_str *input, |
| 2517 | int dquote_end); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2518 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2519 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 2520 | * all variable references within and returns a pointer to |
| 2521 | * a list of expanded strings, possibly with larger number |
| 2522 | * of strings. (Think VAR="a b"; echo $VAR). |
| 2523 | * This new list is allocated as a single malloc block. |
| 2524 | * NULL-terminated list of char* pointers is at the beginning of it, |
| 2525 | * followed by strings themself. |
| 2526 | * Caller can deallocate entire list by single free(list). */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2527 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2528 | /* Store given string, finalizing the word and starting new one whenever |
| 2529 | * we encounter IFS char(s). This is used for expanding variable values. |
| 2530 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 2531 | 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] | 2532 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2533 | while (1) { |
| 2534 | int word_len = strcspn(str, G.ifs); |
| 2535 | if (word_len) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2536 | if (output->o_escape || !output->o_glob) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2537 | o_addQstr(output, str, word_len); |
| 2538 | else /* protect backslashes against globbing up :) */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2539 | o_addblock_duplicate_backslash(output, str, word_len); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2540 | str += word_len; |
| 2541 | } |
| 2542 | if (!*str) /* EOL - do not finalize word */ |
| 2543 | break; |
| 2544 | o_addchr(output, '\0'); |
| 2545 | debug_print_list("expand_on_ifs", output, n); |
| 2546 | n = o_save_ptr(output, n); |
| 2547 | str += strspn(str, G.ifs); /* skip ifs chars */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2548 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2549 | debug_print_list("expand_on_ifs[1]", output, n); |
| 2550 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2553 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 2554 | * they are in double quotes, with the exception that they are not :). |
| 2555 | * Just the rules are similar: "expand only $var and `cmd`" |
| 2556 | * |
| 2557 | * Returns malloced string. |
| 2558 | * As an optimization, we return NULL if expansion is not needed. |
| 2559 | */ |
| 2560 | static char *expand_pseudo_dquoted(const char *str) |
| 2561 | { |
| 2562 | char *exp_str; |
| 2563 | struct in_str input; |
| 2564 | o_string dest = NULL_O_STRING; |
| 2565 | |
| 2566 | if (strchr(str, '$') == NULL |
| 2567 | #if ENABLE_HUSH_TICK |
| 2568 | && strchr(str, '`') == NULL |
| 2569 | #endif |
| 2570 | ) { |
| 2571 | return NULL; |
| 2572 | } |
| 2573 | |
| 2574 | /* We need to expand. Example: |
| 2575 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 2576 | */ |
| 2577 | setup_string_in_str(&input, str); |
| 2578 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 2579 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 2580 | exp_str = expand_string_to_string(dest.data); |
| 2581 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 2582 | o_free_unsafe(&dest); |
| 2583 | return exp_str; |
| 2584 | } |
| 2585 | |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2586 | #if ENABLE_SH_MATH_SUPPORT |
| 2587 | static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) |
| 2588 | { |
| 2589 | arith_eval_hooks_t hooks; |
| 2590 | arith_t res; |
| 2591 | char *exp_str; |
| 2592 | |
| 2593 | hooks.lookupvar = get_local_var_value; |
| 2594 | hooks.setvar = set_local_var_from_halves; |
| 2595 | hooks.endofname = endofname; |
| 2596 | exp_str = expand_pseudo_dquoted(arg); |
| 2597 | res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); |
| 2598 | free(exp_str); |
| 2599 | return res; |
| 2600 | } |
| 2601 | #endif |
| 2602 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2603 | /* Expand all variable references in given string, adding words to list[] |
| 2604 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 2605 | * to be filled). This routine is extremely tricky: has to deal with |
| 2606 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 2607 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 2608 | 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] | 2609 | { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2610 | /* or_mask is either 0 (normal case) or 0x80 - |
| 2611 | * expansion of right-hand side of assignment == 1-element expand. |
| 2612 | * It will also do no globbing, and thus we must not backslash-quote! |
| 2613 | */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2614 | char ored_ch; |
| 2615 | char *p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2616 | |
| 2617 | ored_ch = 0; |
| 2618 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 2619 | 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] | 2620 | debug_print_list("expand_vars_to_list", output, n); |
| 2621 | n = o_save_ptr(output, n); |
| 2622 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 2623 | |
| 2624 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2625 | char first_ch; |
| 2626 | int i; |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2627 | char *to_be_freed = NULL; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2628 | const char *val = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2629 | #if ENABLE_HUSH_TICK |
| 2630 | o_string subst_result = NULL_O_STRING; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2631 | #endif |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2632 | #if ENABLE_SH_MATH_SUPPORT |
| 2633 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 2634 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2635 | o_addblock(output, arg, p - arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2636 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 2637 | arg = ++p; |
| 2638 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2639 | |
| 2640 | first_ch = arg[0] | or_mask; /* forced to "quoted" if or_mask = 0x80 */ |
| 2641 | /* "$@" is special. Even if quoted, it can still |
| 2642 | * expand to nothing (not even an empty string) */ |
| 2643 | if ((first_ch & 0x7f) != '@') |
| 2644 | ored_ch |= first_ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2645 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2646 | switch (first_ch & 0x7f) { |
| 2647 | /* Highest bit in first_ch indicates that var is double-quoted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2648 | case '*': |
| 2649 | case '@': |
| 2650 | i = 1; |
| 2651 | if (!G.global_argv[i]) |
| 2652 | break; |
| 2653 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 2654 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2655 | smallint sv = output->o_escape; |
| 2656 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2657 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2658 | while (G.global_argv[i]) { |
| 2659 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 2660 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 2661 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 2662 | /* this argv[] is not empty and not last: |
| 2663 | * put terminating NUL, start new word */ |
| 2664 | o_addchr(output, '\0'); |
| 2665 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 2666 | n = o_save_ptr(output, n); |
| 2667 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 2668 | } |
| 2669 | } |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2670 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2671 | } else |
| 2672 | /* If or_mask is nonzero, we handle assignment 'a=....$@.....' |
| 2673 | * and in this case should treat it like '$*' - see 'else...' below */ |
| 2674 | if (first_ch == ('@'|0x80) && !or_mask) { /* quoted $@ */ |
| 2675 | while (1) { |
| 2676 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2677 | if (++i >= G.global_argc) |
| 2678 | break; |
| 2679 | o_addchr(output, '\0'); |
| 2680 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 2681 | n = o_save_ptr(output, n); |
| 2682 | } |
| 2683 | } else { /* quoted $*: add as one word */ |
| 2684 | while (1) { |
| 2685 | o_addQstr(output, G.global_argv[i], strlen(G.global_argv[i])); |
| 2686 | if (!G.global_argv[++i]) |
| 2687 | break; |
| 2688 | if (G.ifs[0]) |
| 2689 | o_addchr(output, G.ifs[0]); |
| 2690 | } |
| 2691 | } |
| 2692 | break; |
| 2693 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 2694 | /* "Empty variable", used to make "" etc to not disappear */ |
| 2695 | arg++; |
| 2696 | ored_ch = 0x80; |
| 2697 | break; |
| 2698 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 2699 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2700 | *p = '\0'; |
| 2701 | arg++; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 2702 | /* Can't just stuff it into output o_string, |
| 2703 | * expanded result may need to be globbed |
| 2704 | * and $IFS-splitted */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2705 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 2706 | G.last_exitcode = process_command_subs(&subst_result, arg); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 2707 | 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] | 2708 | val = subst_result.data; |
| 2709 | goto store_val; |
Denis Vlasenko | 96f67dc | 2007-05-17 13:02:41 +0000 | [diff] [blame] | 2710 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2711 | #if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2712 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2713 | arith_t res; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2714 | int errcode; |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2715 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2716 | arg++; /* skip '+' */ |
| 2717 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2718 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2719 | res = expand_and_evaluate_arith(arg, &errcode); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2720 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2721 | if (errcode < 0) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2722 | const char *msg = "error in arithmetic"; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2723 | switch (errcode) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2724 | case -3: |
| 2725 | msg = "exponent less than 0"; |
| 2726 | break; |
| 2727 | case -2: |
| 2728 | msg = "divide by 0"; |
| 2729 | break; |
| 2730 | case -5: |
| 2731 | msg = "expression recursion loop detected"; |
| 2732 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2733 | } |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2734 | die_if_script(msg); |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 2735 | } |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2736 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2737 | sprintf(arith_buf, arith_t_fmt, res); |
| 2738 | val = arith_buf; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2739 | break; |
| 2740 | } |
| 2741 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2742 | default: { /* <SPECIAL_VAR_SYMBOL>varname<SPECIAL_VAR_SYMBOL> */ |
| 2743 | char *var; |
| 2744 | char first_char; |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2745 | char exp_op; |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2746 | char exp_save = exp_save; /* for compiler */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2747 | char *exp_saveptr; /* points to expansion operator */ |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2748 | char *exp_word = exp_word; /* for compiler */ |
Denis Vlasenko | 232be3e | 2009-04-05 09:16:00 +0000 | [diff] [blame] | 2749 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2750 | var = arg; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2751 | *p = '\0'; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2752 | exp_saveptr = arg[1] ? strchr("%#:-=+?", arg[1]) : NULL; |
| 2753 | first_char = arg[0] = first_ch & 0x7f; |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2754 | exp_op = 0; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2755 | |
| 2756 | if (first_char == '#' && arg[1] && !exp_saveptr) { |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2757 | /* handle length expansion ${#var} */ |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2758 | var++; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2759 | exp_op = 'L'; |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2760 | } else { |
| 2761 | /* maybe handle parameter expansion */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2762 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 2763 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 2764 | ) { |
| 2765 | /* ${?:0}, ${#[:]%0} etc */ |
| 2766 | exp_saveptr = var + 1; |
| 2767 | } else { |
| 2768 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 2769 | exp_saveptr = var+1 + strcspn(var+1, "%#:-=+?"); |
| 2770 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2771 | exp_op = exp_save = *exp_saveptr; |
| 2772 | if (exp_op) { |
| 2773 | exp_word = exp_saveptr + 1; |
| 2774 | if (exp_op == ':') { |
| 2775 | exp_op = *exp_word++; |
| 2776 | if (ENABLE_HUSH_BASH_COMPAT |
| 2777 | && (exp_op == '\0' || !strchr("%#:-=+?"+3, exp_op)) |
| 2778 | ) { |
| 2779 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 2780 | exp_op = ':'; |
| 2781 | exp_word--; |
| 2782 | } |
| 2783 | } |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2784 | *exp_saveptr = '\0'; |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2785 | } /* else: it's not an expansion op, but bare ${var} */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | /* lookup the variable in question */ |
| 2789 | if (isdigit(var[0])) { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2790 | /* parse_dollar() should have vetted var for us */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2791 | i = xatoi_u(var); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2792 | if (i < G.global_argc) |
| 2793 | val = G.global_argv[i]; |
| 2794 | /* else val remains NULL: $N with too big N */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2795 | } else { |
| 2796 | switch (var[0]) { |
| 2797 | case '$': /* pid */ |
| 2798 | val = utoa(G.root_pid); |
| 2799 | break; |
| 2800 | case '!': /* bg pid */ |
| 2801 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : (char*)""; |
| 2802 | break; |
| 2803 | case '?': /* exitcode */ |
| 2804 | val = utoa(G.last_exitcode); |
| 2805 | break; |
| 2806 | case '#': /* argc */ |
| 2807 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 2808 | break; |
| 2809 | default: |
| 2810 | val = get_local_var_value(var); |
| 2811 | } |
| 2812 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2813 | |
| 2814 | /* handle any expansions */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2815 | if (exp_op == 'L') { |
Denys Vlasenko | ee0775d | 2010-05-20 16:37:53 +0200 | [diff] [blame] | 2816 | debug_printf_expand("expand: length(%s)=", val); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2817 | val = utoa(val ? strlen(val) : 0); |
| 2818 | debug_printf_expand("%s\n", val); |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2819 | } else if (exp_op) { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2820 | if (exp_op == '%' || exp_op == '#') { |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2821 | /* Standard-mandated substring removal ops: |
| 2822 | * ${parameter%word} - remove smallest suffix pattern |
| 2823 | * ${parameter%%word} - remove largest suffix pattern |
| 2824 | * ${parameter#word} - remove smallest prefix pattern |
| 2825 | * ${parameter##word} - remove largest prefix pattern |
| 2826 | * |
| 2827 | * Word is expanded to produce a glob pattern. |
| 2828 | * Then var's value is matched to it and matching part removed. |
| 2829 | */ |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2830 | if (val) { |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2831 | bool match_at_left; |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2832 | char *loc; |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2833 | scan_t scan = pick_scan(exp_op, *exp_word, &match_at_left); |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2834 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2835 | exp_word++; |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2836 | val = to_be_freed = xstrdup(val); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 2837 | { |
| 2838 | char *exp_exp_word = expand_pseudo_dquoted(exp_word); |
| 2839 | if (exp_exp_word) |
| 2840 | exp_word = exp_exp_word; |
| 2841 | loc = scan(to_be_freed, exp_word, match_at_left); |
| 2842 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
| 2843 | // exp_op, to_be_freed, exp_word, loc); |
| 2844 | free(exp_exp_word); |
| 2845 | } |
| 2846 | if (loc) { /* match was found */ |
| 2847 | if (match_at_left) /* # or ## */ |
| 2848 | val = loc; |
| 2849 | else /* % or %% */ |
| 2850 | *loc = '\0'; |
| 2851 | } |
Mike Frysinger | 57e7467 | 2009-04-09 23:00:33 +0000 | [diff] [blame] | 2852 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2853 | } else if (exp_op == ':') { |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 2854 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2855 | /* It's ${var:N[:M]} bashism. |
| 2856 | * Note that in encoded form it has TWO parts: |
| 2857 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2858 | */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2859 | arith_t beg, len; |
| 2860 | int errcode = 0; |
| 2861 | |
| 2862 | beg = expand_and_evaluate_arith(exp_word, &errcode); |
| 2863 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 2864 | *p++ = SPECIAL_VAR_SYMBOL; |
| 2865 | exp_word = p; |
| 2866 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 2867 | *p = '\0'; |
| 2868 | len = expand_and_evaluate_arith(exp_word, &errcode); |
| 2869 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
| 2870 | |
| 2871 | if (errcode >= 0 && len >= 0) { /* bash compat: len < 0 is illegal */ |
| 2872 | if (beg < 0) /* bash compat */ |
| 2873 | beg = 0; |
| 2874 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | 73e013f | 2010-05-21 15:24:12 +0200 | [diff] [blame] | 2875 | if (len == 0 || !val || beg >= strlen(val)) |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2876 | val = ""; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2877 | else { |
| 2878 | /* Paranoia. What if user entered 9999999999999 |
| 2879 | * which fits in arith_t but not int? */ |
| 2880 | if (len >= INT_MAX) |
| 2881 | len = INT_MAX; |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2882 | val = to_be_freed = xstrndup(val + beg, len); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2883 | } |
| 2884 | debug_printf_varexp("val:'%s'\n", val); |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2885 | } else |
| 2886 | #endif |
| 2887 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2888 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | 73e013f | 2010-05-21 15:24:12 +0200 | [diff] [blame] | 2889 | val = ""; |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2890 | } |
| 2891 | } else { /* one of "-=+?" */ |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2892 | /* Standard-mandated substitution ops: |
| 2893 | * ${var?word} - indicate error if unset |
| 2894 | * If var is unset, word (or a message indicating it is unset |
| 2895 | * if word is null) is written to standard error |
| 2896 | * and the shell exits with a non-zero exit status. |
| 2897 | * Otherwise, the value of var is substituted. |
| 2898 | * ${var-word} - use default value |
| 2899 | * If var is unset, word is substituted. |
| 2900 | * ${var=word} - assign and use default value |
| 2901 | * If var is unset, word is assigned to var. |
| 2902 | * In all cases, final value of var is substituted. |
| 2903 | * ${var+word} - use alternative value |
| 2904 | * If var is unset, null is substituted. |
| 2905 | * Otherwise, word is substituted. |
| 2906 | * |
| 2907 | * Word is subjected to tilde expansion, parameter expansion, |
| 2908 | * command substitution, and arithmetic expansion. |
| 2909 | * If word is not needed, it is not expanded. |
| 2910 | * |
| 2911 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 2912 | * but also treat null var as if it is unset. |
| 2913 | */ |
| 2914 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2915 | if (exp_op == '+') |
Denys Vlasenko | 53b5133 | 2010-05-20 21:46:45 +0200 | [diff] [blame] | 2916 | use_word = !use_word; |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2917 | 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] | 2918 | (exp_save == ':') ? "true" : "false", use_word); |
| 2919 | if (use_word) { |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2920 | to_be_freed = expand_pseudo_dquoted(exp_word); |
| 2921 | if (to_be_freed) |
| 2922 | exp_word = to_be_freed; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2923 | if (exp_op == '?') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2924 | /* mimic bash message */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2925 | die_if_script("%s: %s", |
| 2926 | var, |
| 2927 | exp_word[0] ? exp_word : "parameter null or not set" |
| 2928 | ); |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2929 | //TODO: how interactive bash aborts expansion mid-command? |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2930 | } else { |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2931 | val = exp_word; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 2932 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2933 | |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2934 | if (exp_op == '=') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2935 | /* ${var=[word]} or ${var:=[word]} */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2936 | if (isdigit(var[0]) || var[0] == '#') { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2937 | /* mimic bash message */ |
| 2938 | die_if_script("$%s: cannot assign in this way", var); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2939 | val = NULL; |
| 2940 | } else { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2941 | char *new_var = xasprintf("%s=%s", var, val); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 2942 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2943 | } |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2944 | } |
| 2945 | } |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 2946 | } /* one of "-=+?" */ |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 2947 | |
Denys Vlasenko | e3be784 | 2010-05-20 16:27:42 +0200 | [diff] [blame] | 2948 | *exp_saveptr = exp_save; |
Denys Vlasenko | 4d8e5fd | 2010-05-21 01:15:42 +0200 | [diff] [blame] | 2949 | } /* if (exp_op) */ |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2950 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2951 | arg[0] = first_ch; |
| 2952 | #if ENABLE_HUSH_TICK |
| 2953 | store_val: |
| 2954 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2955 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2956 | 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] | 2957 | if (val) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2958 | /* unquoted var's contents should be globbed, so don't escape */ |
| 2959 | smallint sv = output->o_escape; |
| 2960 | output->o_escape = 0; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2961 | n = expand_on_ifs(output, n, val); |
| 2962 | val = NULL; |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2963 | output->o_escape = sv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2964 | } |
| 2965 | } else { /* quoted $VAR, val will be appended below */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 2966 | 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] | 2967 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2968 | } /* default: */ |
| 2969 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
Denis Vlasenko | 5b7589e | 2009-04-26 11:25:19 +0000 | [diff] [blame] | 2970 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2971 | if (val) { |
| 2972 | o_addQstr(output, val, strlen(val)); |
| 2973 | } |
Denys Vlasenko | 3f78cec | 2010-05-21 17:54:46 +0200 | [diff] [blame] | 2974 | free(to_be_freed); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2975 | /* Do the check to avoid writing to a const string */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2976 | if (*p != SPECIAL_VAR_SYMBOL) |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 2977 | *p = SPECIAL_VAR_SYMBOL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2978 | |
| 2979 | #if ENABLE_HUSH_TICK |
| 2980 | o_free(&subst_result); |
| 2981 | #endif |
| 2982 | arg = ++p; |
| 2983 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 2984 | |
| 2985 | if (arg[0]) { |
| 2986 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 2987 | /* this part is literal, and it was already pre-quoted |
| 2988 | * if needed (much earlier), do not use o_addQstr here! */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2989 | o_addstr_with_NUL(output, arg); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2990 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 2991 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 2992 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 2993 | ) { |
| 2994 | n--; |
| 2995 | /* allow to reuse list[n] later without re-growth */ |
| 2996 | output->has_empty_slot = 1; |
| 2997 | } else { |
| 2998 | o_addchr(output, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2999 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3000 | return n; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3003 | static char **expand_variables(char **argv, int or_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3004 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3005 | int n; |
| 3006 | char **list; |
| 3007 | char **v; |
| 3008 | o_string output = NULL_O_STRING; |
| 3009 | |
| 3010 | if (or_mask & 0x100) { |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 3011 | output.o_escape = 1; /* protect against globbing for "$var" */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3012 | /* (unquoted $var will temporarily switch it off) */ |
| 3013 | output.o_glob = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3014 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3015 | |
| 3016 | n = 0; |
| 3017 | v = argv; |
| 3018 | while (*v) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3019 | n = expand_vars_to_list(&output, n, *v, (unsigned char)or_mask); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3020 | v++; |
| 3021 | } |
| 3022 | debug_print_list("expand_variables", &output, n); |
| 3023 | |
| 3024 | /* output.data (malloced in one block) gets returned in "list" */ |
| 3025 | list = o_finalize_list(&output, n); |
| 3026 | debug_print_strings("expand_variables[1]", list); |
| 3027 | return list; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3030 | static char **expand_strvec_to_strvec(char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3031 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3032 | return expand_variables(argv, 0x100); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3035 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3036 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 3037 | { |
| 3038 | return expand_variables(argv, 0x80); |
| 3039 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3040 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3041 | |
| 3042 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 3043 | static char **expand_strvec_to_strvec_singleword_noglob_cond(char **argv) |
| 3044 | { |
| 3045 | int n; |
| 3046 | char **list; |
| 3047 | char **v; |
| 3048 | o_string output = NULL_O_STRING; |
| 3049 | |
| 3050 | n = 0; |
| 3051 | v = argv; |
| 3052 | while (*v) { |
| 3053 | int is_var = is_well_formed_var_name(*v, '='); |
| 3054 | /* is_var * 0x80: singleword expansion for vars */ |
| 3055 | n = expand_vars_to_list(&output, n, *v, is_var * 0x80); |
| 3056 | |
| 3057 | /* Subtle! expand_vars_to_list did not glob last word yet. |
| 3058 | * It does this only when fed with further data. |
| 3059 | * Therefore we set globbing flags AFTER it, not before: |
| 3060 | */ |
| 3061 | |
| 3062 | /* if it is not recognizably abc=...; then: */ |
| 3063 | output.o_escape = !is_var; /* protect against globbing for "$var" */ |
| 3064 | /* (unquoted $var will temporarily switch it off) */ |
| 3065 | output.o_glob = !is_var; /* and indeed do globbing */ |
| 3066 | v++; |
| 3067 | } |
| 3068 | debug_print_list("expand_cond", &output, n); |
| 3069 | |
| 3070 | /* output.data (malloced in one block) gets returned in "list" */ |
| 3071 | list = o_finalize_list(&output, n); |
| 3072 | debug_print_strings("expand_cond[1]", list); |
| 3073 | return list; |
| 3074 | } |
| 3075 | #endif |
| 3076 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3077 | /* Used for expansion of right hand of assignments */ |
| 3078 | /* NB: should NOT do globbing! "export v=/bin/c*; env | grep ^v=" outputs |
| 3079 | * "v=/bin/c*" */ |
| 3080 | static char *expand_string_to_string(const char *str) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3081 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3082 | char *argv[2], **list; |
| 3083 | |
| 3084 | argv[0] = (char*)str; |
| 3085 | argv[1] = NULL; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3086 | list = expand_variables(argv, 0x80); /* 0x80: singleword expansion */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3087 | if (HUSH_DEBUG) |
| 3088 | if (!list[0] || list[1]) |
| 3089 | bb_error_msg_and_die("BUG in varexp2"); |
| 3090 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 3091 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3092 | unbackslash((char*)list); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3093 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 3094 | return (char*)list; |
| 3095 | } |
| 3096 | |
| 3097 | /* Used for "eval" builtin */ |
| 3098 | static char* expand_strvec_to_string(char **argv) |
| 3099 | { |
| 3100 | char **list; |
| 3101 | |
| 3102 | list = expand_variables(argv, 0x80); |
| 3103 | /* Convert all NULs to spaces */ |
| 3104 | if (list[0]) { |
| 3105 | int n = 1; |
| 3106 | while (list[n]) { |
| 3107 | if (HUSH_DEBUG) |
| 3108 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 3109 | bb_error_msg_and_die("BUG in varexp3"); |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 3110 | /* bash uses ' ' regardless of $IFS contents */ |
| 3111 | list[n][-1] = ' '; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3112 | n++; |
| 3113 | } |
| 3114 | } |
| 3115 | overlapping_strcpy((char*)list, list[0]); |
| 3116 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 3117 | return (char*)list; |
| 3118 | } |
| 3119 | |
| 3120 | static char **expand_assignments(char **argv, int count) |
| 3121 | { |
| 3122 | int i; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 3123 | char **p; |
| 3124 | |
| 3125 | G.expanded_assignments = p = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3126 | /* Expand assignments into one string each */ |
| 3127 | for (i = 0; i < count; i++) { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 3128 | G.expanded_assignments = p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3129 | } |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 3130 | G.expanded_assignments = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3131 | return p; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3134 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3135 | #if BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3136 | /* never called */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3137 | void re_execute_shell(char ***to_free, const char *s, |
| 3138 | char *g_argv0, char **g_argv, |
| 3139 | char **builtin_argv) NORETURN; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3140 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3141 | static void reset_traps_to_defaults(void) |
| 3142 | { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3143 | /* This function is always called in a child shell |
| 3144 | * after fork (not vfork, NOMMU doesn't use this function). |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 3145 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3146 | unsigned sig; |
| 3147 | unsigned mask; |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 3148 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3149 | /* Child shells are not interactive. |
| 3150 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 3151 | * Testcase: (while :; do :; done) + ^Z should background. |
| 3152 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 3153 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3154 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3155 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3156 | |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3157 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 3158 | * Stupid. It can be done with *single* &= op, but we can't use |
| 3159 | * the fact that G.blocked_set is implemented as a bitmask |
| 3160 | * in libc... */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3161 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 3162 | sig = 1; |
| 3163 | while (1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3164 | if (mask & 1) { |
| 3165 | /* Careful. Only if no trap or trap is not "" */ |
| 3166 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 3167 | sigdelset(&G.blocked_set, sig); |
| 3168 | } |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3169 | mask >>= 1; |
| 3170 | if (!mask) |
| 3171 | break; |
| 3172 | sig++; |
| 3173 | } |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3174 | /* Our homegrown sig mask is saner to work with :) */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3175 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3176 | |
| 3177 | /* Resetting all traps to default except empty ones */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3178 | mask = G.non_DFL_mask; |
| 3179 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3180 | if (!G.traps[sig] || !G.traps[sig][0]) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3181 | continue; |
| 3182 | free(G.traps[sig]); |
| 3183 | G.traps[sig] = NULL; |
| 3184 | /* There is no signal for 0 (EXIT) */ |
| 3185 | if (sig == 0) |
| 3186 | continue; |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3187 | /* There was a trap handler, we just removed it. |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3188 | * But if sig still has non-DFL handling, |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 3189 | * we should not unblock the sig. */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 3190 | if (mask & 1) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3191 | continue; |
| 3192 | sigdelset(&G.blocked_set, sig); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3193 | } |
Denis Vlasenko | 74a931a | 2009-04-15 23:29:44 +0000 | [diff] [blame] | 3194 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | #else /* !BB_MMU */ |
| 3198 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3199 | static void re_execute_shell(char ***to_free, const char *s, |
| 3200 | char *g_argv0, char **g_argv, |
| 3201 | char **builtin_argv) NORETURN; |
| 3202 | static void re_execute_shell(char ***to_free, const char *s, |
| 3203 | char *g_argv0, char **g_argv, |
| 3204 | char **builtin_argv) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3205 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 3206 | # 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] | 3207 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 3208 | 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] | 3209 | char *heredoc_argv[4]; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3210 | struct variable *cur; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3211 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3212 | struct function *funcp; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3213 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3214 | char **argv, **pp; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3215 | unsigned cnt; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3216 | unsigned long long empty_trap_mask; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3217 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3218 | if (!g_argv0) { /* heredoc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3219 | argv = heredoc_argv; |
| 3220 | argv[0] = (char *) G.argv0_for_re_execing; |
| 3221 | argv[1] = (char *) "-<"; |
| 3222 | argv[2] = (char *) s; |
| 3223 | argv[3] = NULL; |
Denis Vlasenko | f50caac | 2009-04-09 01:40:15 +0000 | [diff] [blame] | 3224 | pp = &argv[3]; /* used as pointer to empty environment */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3225 | goto do_exec; |
| 3226 | } |
| 3227 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3228 | cnt = 0; |
| 3229 | pp = builtin_argv; |
| 3230 | if (pp) while (*pp++) |
| 3231 | cnt++; |
| 3232 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3233 | empty_trap_mask = 0; |
| 3234 | if (G.traps) { |
| 3235 | int sig; |
| 3236 | for (sig = 1; sig < NSIG; sig++) { |
| 3237 | if (G.traps[sig] && !G.traps[sig][0]) |
| 3238 | empty_trap_mask |= 1LL << sig; |
| 3239 | } |
| 3240 | } |
| 3241 | |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3242 | sprintf(param_buf, NOMMU_HACK_FMT |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3243 | , (unsigned) G.root_pid |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 3244 | , (unsigned) G.root_ppid |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3245 | , (unsigned) G.last_bg_pid |
| 3246 | , (unsigned) G.last_exitcode |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3247 | , cnt |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 3248 | , empty_trap_mask |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 3249 | IF_HUSH_LOOPS(, G.depth_of_loop) |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3250 | ); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 3251 | # undef NOMMU_HACK_FMT |
Denys Vlasenko | 6c93b24 | 2010-01-12 19:28:10 +0100 | [diff] [blame] | 3252 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3253 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3254 | */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3255 | cnt += 6; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3256 | for (cur = G.top_var; cur; cur = cur->next) { |
| 3257 | if (!cur->flg_export || cur->flg_read_only) |
| 3258 | cnt += 2; |
| 3259 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3260 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3261 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 3262 | cnt += 3; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3263 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3264 | pp = g_argv; |
| 3265 | while (*pp++) |
| 3266 | cnt++; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3267 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3268 | *pp++ = (char *) G.argv0_for_re_execing; |
| 3269 | *pp++ = param_buf; |
| 3270 | for (cur = G.top_var; cur; cur = cur->next) { |
| 3271 | if (cur->varstr == hush_version_str) |
| 3272 | continue; |
| 3273 | if (cur->flg_read_only) { |
| 3274 | *pp++ = (char *) "-R"; |
| 3275 | *pp++ = cur->varstr; |
| 3276 | } else if (!cur->flg_export) { |
| 3277 | *pp++ = (char *) "-V"; |
| 3278 | *pp++ = cur->varstr; |
| 3279 | } |
| 3280 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3281 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3282 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 3283 | *pp++ = (char *) "-F"; |
| 3284 | *pp++ = funcp->name; |
| 3285 | *pp++ = funcp->body_as_string; |
| 3286 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3287 | # endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3288 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 3289 | * |
| 3290 | * However, POSIX says that subshells reset signals with traps |
| 3291 | * to SIG_DFL. |
| 3292 | * I tested bash-3.2 and it not only does that with true subshells |
| 3293 | * of the form ( list ), but with any forked children shells. |
| 3294 | * I set trap "echo W" WINCH; and then tried: |
| 3295 | * |
| 3296 | * { echo 1; sleep 20; echo 2; } & |
| 3297 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 3298 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 3299 | * |
| 3300 | * In all these cases sending SIGWINCH to the child shell |
| 3301 | * did not run the trap. If I add trap "echo V" WINCH; |
| 3302 | * _inside_ group (just before echo 1), it works. |
| 3303 | * |
| 3304 | * 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] | 3305 | * Even if we would use signal handlers instead of signal masking |
| 3306 | * in order to implement trap handling, |
| 3307 | * exec syscall below resets signals to SIG_DFL for us. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3308 | */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3309 | *pp++ = (char *) "-c"; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3310 | *pp++ = (char *) s; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3311 | if (builtin_argv) { |
| 3312 | while (*++builtin_argv) |
| 3313 | *pp++ = *builtin_argv; |
| 3314 | *pp++ = (char *) ""; |
| 3315 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3316 | *pp++ = g_argv0; |
| 3317 | while (*g_argv) |
| 3318 | *pp++ = *g_argv++; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3319 | /* *pp = NULL; - is already there */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3320 | pp = environ; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3321 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3322 | do_exec: |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3323 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 3324 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3325 | execve(bb_busybox_exec_path, argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3326 | /* Fallback. Useful for init=/bin/hush usage etc */ |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3327 | if (argv[0][0] == '/') |
| 3328 | execve(argv[0], argv, pp); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3329 | xfunc_error_retval = 127; |
| 3330 | bb_error_msg_and_die("can't re-execute the shell"); |
| 3331 | } |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3332 | #endif /* !BB_MMU */ |
| 3333 | |
| 3334 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3335 | static void setup_heredoc(struct redir_struct *redir) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3336 | { |
| 3337 | struct fd_pair pair; |
| 3338 | pid_t pid; |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3339 | int len, written; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3340 | /* the _body_ of heredoc (misleading field name) */ |
| 3341 | const char *heredoc = redir->rd_filename; |
| 3342 | char *expanded; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3343 | #if !BB_MMU |
| 3344 | char **to_free; |
| 3345 | #endif |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3346 | |
| 3347 | expanded = NULL; |
| 3348 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 3349 | expanded = expand_pseudo_dquoted(heredoc); |
| 3350 | if (expanded) |
| 3351 | heredoc = expanded; |
| 3352 | } |
| 3353 | len = strlen(heredoc); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3354 | |
Denis Vlasenko | a2218dd | 2009-04-09 01:39:02 +0000 | [diff] [blame] | 3355 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3356 | xpiped_pair(pair); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3357 | xmove_fd(pair.rd, redir->rd_fd); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3358 | |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3359 | /* Try writing without forking. Newer kernels have |
| 3360 | * dynamically growing pipes. Must use non-blocking write! */ |
| 3361 | ndelay_on(pair.wr); |
| 3362 | while (1) { |
| 3363 | written = write(pair.wr, heredoc, len); |
| 3364 | if (written <= 0) |
| 3365 | break; |
| 3366 | len -= written; |
| 3367 | if (len == 0) { |
| 3368 | close(pair.wr); |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 3369 | free(expanded); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3370 | return; |
| 3371 | } |
| 3372 | heredoc += written; |
| 3373 | } |
| 3374 | ndelay_off(pair.wr); |
| 3375 | |
| 3376 | /* Okay, pipe buffer was not big enough */ |
| 3377 | /* Note: we must not create a stray child (bastard? :) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3378 | * for the unsuspecting parent process. Child creates a grandchild |
| 3379 | * and exits before parent execs the process which consumes heredoc |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3380 | * (that exec happens after we return from this function) */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 3381 | #if !BB_MMU |
| 3382 | to_free = NULL; |
| 3383 | #endif |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 3384 | pid = xvfork(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3385 | if (pid == 0) { |
| 3386 | /* child */ |
Denis Vlasenko | 41ddecd | 2009-04-15 21:58:14 +0000 | [diff] [blame] | 3387 | disable_restore_tty_pgrp_on_exit(); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 3388 | pid = BB_MMU ? xfork() : xvfork(); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3389 | if (pid != 0) |
| 3390 | _exit(0); |
| 3391 | /* grandchild */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3392 | close(redir->rd_fd); /* read side of the pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3393 | #if BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3394 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3395 | _exit(0); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3396 | #else |
| 3397 | /* Delegate blocking writes to another process */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3398 | xmove_fd(pair.wr, STDOUT_FILENO); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3399 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 3400 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3401 | } |
| 3402 | /* parent */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 3403 | #if ENABLE_HUSH_FAST |
| 3404 | G.count_SIGCHLD++; |
| 3405 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 3406 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3407 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3408 | #if !BB_MMU |
| 3409 | free(to_free); |
| 3410 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3411 | close(pair.wr); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3412 | free(expanded); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3413 | wait(NULL); /* wait till child has died */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3416 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 3417 | * and stderr if they are redirected. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3418 | static int setup_redirects(struct command *prog, int squirrel[]) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3419 | { |
| 3420 | int openfd, mode; |
| 3421 | struct redir_struct *redir; |
| 3422 | |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3423 | for (redir = prog->redirects; redir; redir = redir->next) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3424 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3425 | /* rd_fd<<HERE case */ |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3426 | if (squirrel && redir->rd_fd < 3 |
| 3427 | && squirrel[redir->rd_fd] < 0 |
| 3428 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3429 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3430 | } |
| 3431 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 3432 | * of the heredoc */ |
| 3433 | debug_printf_parse("set heredoc '%s'\n", |
| 3434 | redir->rd_filename); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3435 | setup_heredoc(redir); |
Eric Andersen | 817e73c | 2001-06-06 17:56:09 +0000 | [diff] [blame] | 3436 | continue; |
| 3437 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3438 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3439 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 3440 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3441 | char *p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3442 | if (redir->rd_filename == NULL) { |
| 3443 | /* Something went wrong in the parse. |
| 3444 | * Pretend it didn't happen */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3445 | bb_error_msg("bug in redirect parse"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3446 | continue; |
| 3447 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3448 | mode = redir_table[redir->rd_type].mode; |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3449 | p = expand_string_to_string(redir->rd_filename); |
Denis Vlasenko | cccdc4e | 2007-11-23 21:08:38 +0000 | [diff] [blame] | 3450 | openfd = open_or_warn(p, mode); |
| 3451 | free(p); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3452 | if (openfd < 0) { |
| 3453 | /* this could get lost if stderr has been redirected, but |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3454 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 3455 | //what the above comment tries to say? |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3456 | return 1; |
| 3457 | } |
| 3458 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3459 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3460 | openfd = redir->rd_dup; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3463 | if (openfd != redir->rd_fd) { |
Denys Vlasenko | 1dd6cf8 | 2009-05-02 14:17:31 +0200 | [diff] [blame] | 3464 | if (squirrel && redir->rd_fd < 3 |
| 3465 | && squirrel[redir->rd_fd] < 0 |
| 3466 | ) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3467 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3468 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3469 | if (openfd == REDIRFD_CLOSE) { |
| 3470 | /* "n>-" means "close me" */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3471 | close(redir->rd_fd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3472 | } else { |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3473 | xdup2(openfd, redir->rd_fd); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3474 | if (redir->rd_dup == REDIRFD_TO_FILE) |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 3475 | close(openfd); |
Eric Andersen | 83a2ae2 | 2001-05-07 17:59:25 +0000 | [diff] [blame] | 3476 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3477 | } |
| 3478 | } |
| 3479 | return 0; |
| 3480 | } |
| 3481 | |
| 3482 | static void restore_redirects(int squirrel[]) |
| 3483 | { |
| 3484 | int i, fd; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 3485 | for (i = 0; i < 3; i++) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3486 | fd = squirrel[i]; |
| 3487 | if (fd != -1) { |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 3488 | /* We simply die on error */ |
| 3489 | xmove_fd(fd, i); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3494 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3495 | static void free_pipe_list(struct pipe *head); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3496 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3497 | /* Return code is the exit status of the pipe */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3498 | static void free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3499 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3500 | char **p; |
| 3501 | struct command *command; |
| 3502 | struct redir_struct *r, *rnext; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3503 | int a, i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3504 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3505 | if (pi->stopped_cmds > 0) /* why? */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3506 | return; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3507 | debug_printf_clean("run pipe: (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3508 | for (i = 0; i < pi->num_cmds; i++) { |
| 3509 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3510 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3511 | if (command->argv) { |
| 3512 | for (a = 0, p = command->argv; *p; a++, p++) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3513 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3514 | } |
| 3515 | free_strings(command->argv); |
| 3516 | command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3517 | } |
| 3518 | /* not "else if": on syntax error, we may have both! */ |
| 3519 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3520 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 3521 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3522 | free_pipe_list(command->group); |
| 3523 | debug_printf_clean(" end group\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3524 | command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3525 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 3526 | /* else is crucial here. |
| 3527 | * If group != NULL, child_func is meaningless */ |
| 3528 | #if ENABLE_HUSH_FUNCTIONS |
| 3529 | else if (command->child_func) { |
| 3530 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 3531 | command->child_func->parent_cmd = NULL; |
| 3532 | } |
| 3533 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3534 | #if !BB_MMU |
| 3535 | free(command->group_as_string); |
| 3536 | command->group_as_string = NULL; |
| 3537 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3538 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3539 | debug_printf_clean(" redirect %d%s", |
| 3540 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3541 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 3542 | if (r->rd_filename) { |
| 3543 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 3544 | free(r->rd_filename); |
| 3545 | r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3546 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3547 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3548 | rnext = r->next; |
| 3549 | free(r); |
| 3550 | } |
| 3551 | command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3552 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3553 | free(pi->cmds); /* children are an array, they get freed all at once */ |
| 3554 | pi->cmds = NULL; |
| 3555 | #if ENABLE_HUSH_JOB |
| 3556 | free(pi->cmdtext); |
| 3557 | pi->cmdtext = NULL; |
| 3558 | #endif |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3559 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3560 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3561 | static void free_pipe_list(struct pipe *head) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3562 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3563 | struct pipe *pi, *next; |
| 3564 | |
| 3565 | for (pi = head; pi; pi = next) { |
| 3566 | #if HAS_KEYWORDS |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3567 | debug_printf_clean(" pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3568 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3569 | free_pipe(pi); |
| 3570 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3571 | next = pi->next; |
| 3572 | /*pi->next = NULL;*/ |
| 3573 | free(pi); |
| 3574 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3578 | static int run_list(struct pipe *pi); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3579 | #if BB_MMU |
| 3580 | #define parse_stream(pstring, input, end_trigger) \ |
| 3581 | parse_stream(input, end_trigger) |
| 3582 | #endif |
| 3583 | static struct pipe *parse_stream(char **pstring, |
| 3584 | struct in_str *input, |
| 3585 | int end_trigger); |
| 3586 | static void parse_and_run_string(const char *s); |
| 3587 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3588 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 3589 | static char *find_in_path(const char *arg) |
| 3590 | { |
| 3591 | char *ret = NULL; |
| 3592 | const char *PATH = get_local_var_value("PATH"); |
| 3593 | |
| 3594 | if (!PATH) |
| 3595 | return NULL; |
| 3596 | |
| 3597 | while (1) { |
| 3598 | const char *end = strchrnul(PATH, ':'); |
| 3599 | int sz = end - PATH; /* must be int! */ |
| 3600 | |
| 3601 | free(ret); |
| 3602 | if (sz != 0) { |
| 3603 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 3604 | } else { |
| 3605 | /* We have xxx::yyyy in $PATH, |
| 3606 | * it means "use current dir" */ |
| 3607 | ret = xstrdup(arg); |
| 3608 | } |
| 3609 | if (access(ret, F_OK) == 0) |
| 3610 | break; |
| 3611 | |
| 3612 | if (*end == '\0') { |
| 3613 | free(ret); |
| 3614 | return NULL; |
| 3615 | } |
| 3616 | PATH = end + 1; |
| 3617 | } |
| 3618 | |
| 3619 | return ret; |
| 3620 | } |
| 3621 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3622 | static const struct built_in_command* find_builtin_helper(const char *name, |
| 3623 | const struct built_in_command *x, |
| 3624 | const struct built_in_command *end) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3625 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3626 | while (x != end) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 3627 | if (strcmp(name, x->b_cmd) != 0) { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3628 | x++; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3629 | continue; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3630 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3631 | debug_printf_exec("found builtin '%s'\n", name); |
| 3632 | return x; |
| 3633 | } |
| 3634 | return NULL; |
| 3635 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3636 | static const struct built_in_command* find_builtin1(const char *name) |
| 3637 | { |
| 3638 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 3639 | } |
| 3640 | static const struct built_in_command* find_builtin(const char *name) |
| 3641 | { |
| 3642 | const struct built_in_command *x = find_builtin1(name); |
| 3643 | if (x) |
| 3644 | return x; |
| 3645 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 3646 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3647 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3648 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3649 | static struct function **find_function_slot(const char *name) |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3650 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3651 | struct function **funcpp = &G.top_func; |
| 3652 | while (*funcpp) { |
| 3653 | if (strcmp(name, (*funcpp)->name) == 0) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3654 | break; |
| 3655 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3656 | funcpp = &(*funcpp)->next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3657 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3658 | return funcpp; |
| 3659 | } |
| 3660 | |
| 3661 | static const struct function *find_function(const char *name) |
| 3662 | { |
| 3663 | const struct function *funcp = *find_function_slot(name); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 3664 | if (funcp) |
| 3665 | debug_printf_exec("found function '%s'\n", name); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3666 | return funcp; |
| 3667 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3668 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3669 | /* Note: takes ownership on name ptr */ |
| 3670 | static struct function *new_function(char *name) |
| 3671 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3672 | struct function **funcpp = find_function_slot(name); |
| 3673 | struct function *funcp = *funcpp; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3674 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3675 | if (funcp != NULL) { |
| 3676 | struct command *cmd = funcp->parent_cmd; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3677 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 3678 | if (!cmd) { |
| 3679 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 3680 | free(funcp->name); |
| 3681 | /* Note: if !funcp->body, do not free body_as_string! |
| 3682 | * This is a special case of "-F name body" function: |
| 3683 | * body_as_string was not malloced! */ |
| 3684 | if (funcp->body) { |
| 3685 | free_pipe_list(funcp->body); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3686 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3687 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3688 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3689 | } |
| 3690 | } else { |
| 3691 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 3692 | cmd->argv[0] = funcp->name; |
| 3693 | cmd->group = funcp->body; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3694 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3695 | cmd->group_as_string = funcp->body_as_string; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3696 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3697 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3698 | } else { |
| 3699 | debug_printf_exec("remembering new function '%s'\n", name); |
| 3700 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 3701 | /*funcp->next = NULL;*/ |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3702 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3703 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3704 | funcp->name = name; |
| 3705 | return funcp; |
| 3706 | } |
| 3707 | |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3708 | static void unset_func(const char *name) |
| 3709 | { |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3710 | struct function **funcpp = find_function_slot(name); |
| 3711 | struct function *funcp = *funcpp; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3712 | |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3713 | if (funcp != NULL) { |
| 3714 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 3715 | *funcpp = funcp->next; |
| 3716 | /* funcp is unlinked now, deleting it. |
| 3717 | * Note: if !funcp->body, the function was created by |
| 3718 | * "-F name body", do not free ->body_as_string |
| 3719 | * and ->name as they were not malloced. */ |
| 3720 | if (funcp->body) { |
| 3721 | free_pipe_list(funcp->body); |
| 3722 | free(funcp->name); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3723 | # if !BB_MMU |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3724 | free(funcp->body_as_string); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3725 | # endif |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3726 | } |
Denys Vlasenko | 873273d | 2009-09-12 14:47:41 +0200 | [diff] [blame] | 3727 | free(funcp); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3731 | # if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3732 | #define exec_function(to_free, funcp, argv) \ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3733 | exec_function(funcp, argv) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3734 | # endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3735 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3736 | const struct function *funcp, |
| 3737 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3738 | static void exec_function(char ***to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3739 | const struct function *funcp, |
| 3740 | char **argv) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3741 | { |
| 3742 | # if BB_MMU |
| 3743 | int n = 1; |
| 3744 | |
| 3745 | argv[0] = G.global_argv[0]; |
| 3746 | G.global_argv = argv; |
| 3747 | while (*++argv) |
| 3748 | n++; |
| 3749 | G.global_argc = n; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3750 | /* On MMU, funcp->body is always non-NULL */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3751 | n = run_list(funcp->body); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3752 | fflush_all(); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3753 | _exit(n); |
| 3754 | # else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3755 | re_execute_shell(to_free, |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 3756 | funcp->body_as_string, |
| 3757 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3758 | argv + 1, |
| 3759 | NULL); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3760 | # endif |
| 3761 | } |
| 3762 | |
| 3763 | static int run_function(const struct function *funcp, char **argv) |
| 3764 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3765 | int rc; |
| 3766 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 3767 | smallint sv_flg; |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3768 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3769 | save_and_replace_G_args(&sv, argv); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3770 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3771 | /* "we are in function, ok to use return" */ |
| 3772 | sv_flg = G.flag_return_in_progress; |
| 3773 | G.flag_return_in_progress = -1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3774 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3775 | G.func_nest_level++; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3776 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3777 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3778 | /* On MMU, funcp->body is always non-NULL */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3779 | # if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3780 | if (!funcp->body) { |
| 3781 | /* Function defined by -F */ |
| 3782 | parse_and_run_string(funcp->body_as_string); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3783 | rc = G.last_exitcode; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3784 | } else |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3785 | # endif |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3786 | { |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3787 | rc = run_list(funcp->body); |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 3788 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3789 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3790 | # if ENABLE_HUSH_LOCAL |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3791 | { |
| 3792 | struct variable *var; |
| 3793 | struct variable **var_pp; |
| 3794 | |
| 3795 | var_pp = &G.top_var; |
| 3796 | while ((var = *var_pp) != NULL) { |
| 3797 | if (var->func_nest_level < G.func_nest_level) { |
| 3798 | var_pp = &var->next; |
| 3799 | continue; |
| 3800 | } |
| 3801 | /* Unexport */ |
| 3802 | if (var->flg_export) |
| 3803 | bb_unsetenv(var->varstr); |
| 3804 | /* Remove from global list */ |
| 3805 | *var_pp = var->next; |
| 3806 | /* Free */ |
| 3807 | if (!var->max_len) |
| 3808 | free(var->varstr); |
| 3809 | free(var); |
| 3810 | } |
| 3811 | G.func_nest_level--; |
| 3812 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3813 | # endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 3814 | G.flag_return_in_progress = sv_flg; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 3815 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3816 | restore_G_args(&sv, argv); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 3817 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 3818 | return rc; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3819 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3820 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3821 | |
| 3822 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3823 | #if BB_MMU |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3824 | #define exec_builtin(to_free, x, argv) \ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3825 | exec_builtin(x, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3826 | #else |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3827 | #define exec_builtin(to_free, x, argv) \ |
| 3828 | exec_builtin(to_free, argv) |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3829 | #endif |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3830 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3831 | const struct built_in_command *x, |
| 3832 | char **argv) NORETURN; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3833 | static void exec_builtin(char ***to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3834 | const struct built_in_command *x, |
| 3835 | char **argv) |
| 3836 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3837 | #if BB_MMU |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 3838 | int rcode = x->b_function(argv); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 3839 | fflush_all(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3840 | _exit(rcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3841 | #else |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3842 | /* On NOMMU, we must never block! |
| 3843 | * Example: { sleep 99 | read line; } & echo Ok |
| 3844 | */ |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3845 | re_execute_shell(to_free, |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3846 | argv[0], |
| 3847 | G.global_argv[0], |
| 3848 | G.global_argv + 1, |
| 3849 | argv); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3850 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3854 | static void execvp_or_die(char **argv) NORETURN; |
| 3855 | static void execvp_or_die(char **argv) |
| 3856 | { |
| 3857 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 3858 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3859 | execvp(argv[0], argv); |
| 3860 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 3861 | _exit(127); /* bash compat */ |
| 3862 | } |
| 3863 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 3864 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3865 | static void dump_cmd_in_x_mode(char **argv) |
| 3866 | { |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 3867 | if (G_x_mode && argv) { |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3868 | /* We want to output the line in one write op */ |
| 3869 | char *buf, *p; |
| 3870 | int len; |
| 3871 | int n; |
| 3872 | |
| 3873 | len = 3; |
| 3874 | n = 0; |
| 3875 | while (argv[n]) |
| 3876 | len += strlen(argv[n++]) + 1; |
| 3877 | buf = xmalloc(len); |
| 3878 | buf[0] = '+'; |
| 3879 | p = buf + 1; |
| 3880 | n = 0; |
| 3881 | while (argv[n]) |
| 3882 | p += sprintf(p, " %s", argv[n++]); |
| 3883 | *p++ = '\n'; |
| 3884 | *p = '\0'; |
| 3885 | fputs(buf, stderr); |
| 3886 | free(buf); |
| 3887 | } |
| 3888 | } |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 3889 | #else |
| 3890 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 3891 | #endif |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3892 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 3893 | #if BB_MMU |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 3894 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 3895 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 3896 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 3897 | pseudo_exec(command, argv_expanded) |
| 3898 | #endif |
| 3899 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 3900 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 3901 | * Never returns. |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 3902 | * Don't exit() here. If you don't exec, use _exit instead. |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3903 | * The at_exit handlers apparently confuse the calling process, |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3904 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3905 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 3906 | char **argv, int assignment_cnt, |
| 3907 | char **argv_expanded) NORETURN; |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 3908 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3909 | char **argv, int assignment_cnt, |
| 3910 | char **argv_expanded) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3911 | { |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3912 | char **new_env; |
Denis Vlasenko | 87cb2db | 2007-04-21 10:00:01 +0000 | [diff] [blame] | 3913 | |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 3914 | new_env = expand_assignments(argv, assignment_cnt); |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3915 | dump_cmd_in_x_mode(new_env); |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 3916 | |
| 3917 | if (!argv[assignment_cnt]) { |
| 3918 | /* Case when we are here: ... | var=val | ... |
| 3919 | * (note that we do not exit early, i.e., do not optimize out |
| 3920 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 3921 | */ |
| 3922 | free_strings(new_env); |
| 3923 | _exit(EXIT_SUCCESS); |
| 3924 | } |
| 3925 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3926 | #if BB_MMU |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3927 | set_vars_and_save_old(new_env); |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3928 | free(new_env); /* optional */ |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3929 | /* we can also destroy set_vars_and_save_old's return value, |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 3930 | * to save memory */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3931 | #else |
| 3932 | nommu_save->new_env = new_env; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 3933 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 3934 | #endif |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 3935 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3936 | if (argv_expanded) { |
| 3937 | argv = argv_expanded; |
| 3938 | } else { |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3939 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3940 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 3941 | nommu_save->argv = argv; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 3942 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 3943 | } |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 3944 | dump_cmd_in_x_mode(argv); |
Denis Vlasenko | 764d59d | 2007-05-14 16:23:23 +0000 | [diff] [blame] | 3945 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3946 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 3947 | if (strchr(argv[0], '/') != NULL) |
| 3948 | goto skip; |
| 3949 | #endif |
| 3950 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3951 | /* Check if the command matches any of the builtins. |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3952 | * Depending on context, this might be redundant. But it's |
| 3953 | * easier to waste a few CPU cycles than it is to figure out |
| 3954 | * if this is one of those cases. |
| 3955 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3956 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 3957 | /* On NOMMU, it is more expensive to re-execute shell |
| 3958 | * just in order to run echo or test builtin. |
| 3959 | * It's better to skip it here and run corresponding |
| 3960 | * non-builtin later. */ |
| 3961 | const struct built_in_command *x; |
| 3962 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3963 | if (x) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3964 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 3965 | } |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 3966 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3967 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3968 | /* Check if the command matches any functions */ |
| 3969 | { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3970 | const struct function *funcp = find_function(argv[0]); |
| 3971 | if (funcp) { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3972 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3973 | } |
| 3974 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3975 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 3976 | |
Denis Vlasenko | 80d14be | 2007-04-10 23:03:30 +0000 | [diff] [blame] | 3977 | #if ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3978 | /* Check if the command matches any busybox applets */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3979 | { |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3980 | int a = find_applet_by_name(argv[0]); |
| 3981 | if (a >= 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3982 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3983 | if (APPLET_IS_NOEXEC(a)) { |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3984 | debug_printf_exec("running applet '%s'\n", argv[0]); |
Denis Vlasenko | 1aa7e47 | 2007-11-28 06:49:03 +0000 | [diff] [blame] | 3985 | run_applet_no_and_exit(a, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3986 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3987 | # endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3988 | /* Re-exec ourselves */ |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3989 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 3990 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 3991 | execv(bb_busybox_exec_path, argv); |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3992 | /* If they called chroot or otherwise made the binary no longer |
| 3993 | * executable, fall through */ |
| 3994 | } |
| 3995 | } |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3996 | #endif |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 3997 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3998 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3999 | skip: |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4000 | #endif |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 4001 | execvp_or_die(argv); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4004 | /* Called after [v]fork() in run_pipe |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 4005 | */ |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4006 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 4007 | struct command *command, |
| 4008 | char **argv_expanded) NORETURN; |
| 4009 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 4010 | struct command *command, |
| 4011 | char **argv_expanded) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4012 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4013 | if (command->argv) { |
| 4014 | pseudo_exec_argv(nommu_save, command->argv, |
| 4015 | command->assignment_cnt, argv_expanded); |
| 4016 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4017 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4018 | if (command->group) { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4019 | /* Cases when we are here: |
| 4020 | * ( list ) |
| 4021 | * { list } & |
| 4022 | * ... | ( list ) | ... |
| 4023 | * ... | { list } | ... |
| 4024 | */ |
| 4025 | #if BB_MMU |
Denis Vlasenko | 3b49216 | 2007-12-24 14:26:57 +0000 | [diff] [blame] | 4026 | int rcode; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4027 | debug_printf_exec("pseudo_exec: run_list\n"); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 4028 | reset_traps_to_defaults(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4029 | rcode = run_list(command->group); |
Eric Andersen | bf7df04 | 2001-05-23 22:18:35 +0000 | [diff] [blame] | 4030 | /* OK to leak memory by not calling free_pipe_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4031 | * since this process is about to exit */ |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 4032 | _exit(rcode); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4033 | #else |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4034 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 4035 | command->group_as_string, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4036 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 4037 | G.global_argv + 1, |
| 4038 | NULL); |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 4039 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4040 | } |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4041 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 4042 | /* Case when we are here: ... | >file */ |
| 4043 | debug_printf_exec("pseudo_exec'ed null command\n"); |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4044 | _exit(EXIT_SUCCESS); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4047 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4048 | static const char *get_cmdtext(struct pipe *pi) |
| 4049 | { |
| 4050 | char **argv; |
| 4051 | char *p; |
| 4052 | int len; |
| 4053 | |
| 4054 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 4055 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4056 | * On subsequent bg argv is trashed, but we won't use it */ |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4057 | if (pi->cmdtext) |
| 4058 | return pi->cmdtext; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4059 | argv = pi->cmds[0].argv; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 4060 | if (!argv || !argv[0]) { |
| 4061 | pi->cmdtext = xzalloc(1); |
| 4062 | return pi->cmdtext; |
| 4063 | } |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4064 | |
| 4065 | len = 0; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4066 | do { |
| 4067 | len += strlen(*argv) + 1; |
| 4068 | } while (*++argv); |
| 4069 | p = xmalloc(len); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4070 | pi->cmdtext = p; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4071 | argv = pi->cmds[0].argv; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 4072 | do { |
| 4073 | len = strlen(*argv); |
| 4074 | memcpy(p, *argv, len); |
| 4075 | p += len; |
| 4076 | *p++ = ' '; |
| 4077 | } while (*++argv); |
| 4078 | p[-1] = '\0'; |
| 4079 | return pi->cmdtext; |
| 4080 | } |
| 4081 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4082 | static void insert_bg_job(struct pipe *pi) |
| 4083 | { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4084 | struct pipe *job, **jobp; |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4085 | int i; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4086 | |
| 4087 | /* Linear search for the ID of the job to use */ |
| 4088 | pi->jobid = 1; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4089 | for (job = G.job_list; job; job = job->next) |
| 4090 | if (job->jobid >= pi->jobid) |
| 4091 | pi->jobid = job->jobid + 1; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4092 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4093 | /* Add job to the list of running jobs */ |
| 4094 | jobp = &G.job_list; |
| 4095 | while ((job = *jobp) != NULL) |
| 4096 | jobp = &job->next; |
| 4097 | job = *jobp = xmalloc(sizeof(*job)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4098 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4099 | *job = *pi; /* physical copy */ |
| 4100 | job->next = NULL; |
| 4101 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 4102 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4103 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4104 | job->cmds[i].pid = pi->cmds[i].pid; |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4105 | /* all other fields are not used and stay zero */ |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4106 | } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4107 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4108 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4109 | if (G_interactive_fd) |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4110 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 4111 | /* Last command's pid goes to $! */ |
| 4112 | G.last_bg_pid = job->cmds[job->num_cmds - 1].pid; |
| 4113 | G.last_jobid = job->jobid; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4116 | static void remove_bg_job(struct pipe *pi) |
| 4117 | { |
| 4118 | struct pipe *prev_pipe; |
| 4119 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4120 | if (pi == G.job_list) { |
| 4121 | G.job_list = pi->next; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4122 | } else { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4123 | prev_pipe = G.job_list; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4124 | while (prev_pipe->next != pi) |
| 4125 | prev_pipe = prev_pipe->next; |
| 4126 | prev_pipe->next = pi->next; |
| 4127 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4128 | if (G.job_list) |
| 4129 | G.last_jobid = G.job_list->jobid; |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4130 | else |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4131 | G.last_jobid = 0; |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4132 | } |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 4133 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4134 | /* Remove a backgrounded job */ |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4135 | static void delete_finished_bg_job(struct pipe *pi) |
| 4136 | { |
| 4137 | remove_bg_job(pi); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4138 | pi->stopped_cmds = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4139 | free_pipe(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4140 | free(pi); |
| 4141 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4142 | #endif /* JOB */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4143 | |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4144 | /* Check to see if any processes have exited -- if they |
| 4145 | * have, figure out why and see if a job has completed */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4146 | static int checkjobs(struct pipe* fg_pipe) |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4147 | { |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4148 | int attributes; |
| 4149 | int status; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4150 | #if ENABLE_HUSH_JOB |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4151 | struct pipe *pi; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4152 | #endif |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4153 | pid_t childpid; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4154 | int rcode = 0; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4155 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4156 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 4157 | |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4158 | attributes = WUNTRACED; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4159 | if (fg_pipe == NULL) |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4160 | attributes |= WNOHANG; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4161 | |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 4162 | errno = 0; |
| 4163 | #if ENABLE_HUSH_FAST |
| 4164 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 4165 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 4166 | //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] | 4167 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 4168 | /* Avoid doing waitpid syscall if possible */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 4169 | if (!G.we_have_children) { |
| 4170 | errno = ECHILD; |
| 4171 | return -1; |
| 4172 | } |
| 4173 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 4174 | /* We have children, but they did not exit |
| 4175 | * or stop yet (we saw no SIGCHLD) */ |
| 4176 | return 0; |
| 4177 | } |
| 4178 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 4179 | } |
| 4180 | #endif |
| 4181 | |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4182 | /* Do we do this right? |
| 4183 | * bash-3.00# sleep 20 | false |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4184 | * <ctrl-Z pressed> |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4185 | * [3]+ Stopped sleep 20 | false |
| 4186 | * bash-3.00# echo $? |
| 4187 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4188 | * [hush 1.14.0: yes we do it right] |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4189 | */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4190 | wait_more: |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4191 | while (1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4192 | int i; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4193 | int dead; |
| 4194 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4195 | #if ENABLE_HUSH_FAST |
| 4196 | i = G.count_SIGCHLD; |
| 4197 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4198 | childpid = waitpid(-1, &status, attributes); |
| 4199 | if (childpid <= 0) { |
| 4200 | if (childpid && errno != ECHILD) |
| 4201 | bb_perror_msg("waitpid"); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4202 | #if ENABLE_HUSH_FAST |
| 4203 | else { /* Until next SIGCHLD, waitpid's are useless */ |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 4204 | G.we_have_children = (childpid == 0); |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4205 | G.handled_SIGCHLD = i; |
| 4206 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4207 | } |
| 4208 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 4209 | break; |
| 4210 | } |
| 4211 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 4212 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 4213 | #if DEBUG_JOBS |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4214 | if (WIFSTOPPED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 4215 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4216 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 4217 | if (WIFSIGNALED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 4218 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4219 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 4220 | if (WIFEXITED(status)) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 4221 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4222 | childpid, WEXITSTATUS(status)); |
| 4223 | #endif |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4224 | /* Were we asked to wait for fg pipe? */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4225 | if (fg_pipe) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4226 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 4227 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 4228 | if (fg_pipe->cmds[i].pid != childpid) |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4229 | continue; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4230 | if (dead) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4231 | fg_pipe->cmds[i].pid = 0; |
| 4232 | fg_pipe->alive_cmds--; |
| 4233 | if (i == fg_pipe->num_cmds - 1) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4234 | /* last process gives overall exitstatus */ |
| 4235 | rcode = WEXITSTATUS(status); |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 4236 | /* bash prints killer signal's name for *last* |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 4237 | * process in pipe (prints just newline for SIGINT). |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 4238 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 4239 | */ |
| 4240 | if (WIFSIGNALED(status)) { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 4241 | int sig = WTERMSIG(status); |
| 4242 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denys Vlasenko | a4899ef | 2010-01-04 11:37:09 +0100 | [diff] [blame] | 4243 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 4244 | * Maybe we need to use sig | 128? */ |
| 4245 | rcode = sig + 128; |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 4246 | } |
Denys Vlasenko | a4899ef | 2010-01-04 11:37:09 +0100 | [diff] [blame] | 4247 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4248 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4249 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4250 | fg_pipe->cmds[i].is_stopped = 1; |
| 4251 | fg_pipe->stopped_cmds++; |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4252 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4253 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 4254 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 4255 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4256 | /* All processes in fg pipe have exited or stopped */ |
| 4257 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 4258 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 4259 | * and "killall -STOP cat" */ |
| 4260 | if (G_interactive_fd) { |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4261 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4262 | if (fg_pipe->alive_cmds) |
| 4263 | insert_bg_job(fg_pipe); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4264 | #endif |
Denis Vlasenko | 4ea187f | 2009-04-17 14:35:43 +0000 | [diff] [blame] | 4265 | return rcode; |
| 4266 | } |
Denys Vlasenko | 6f22624 | 2009-06-03 14:37:30 +0200 | [diff] [blame] | 4267 | if (!fg_pipe->alive_cmds) |
| 4268 | return rcode; |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4269 | } |
| 4270 | /* There are still running processes in the fg pipe */ |
| 4271 | goto wait_more; /* do waitpid again */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4272 | } |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4273 | /* it wasnt fg_pipe, look for process in bg pipes */ |
Eric Andersen | c798b07 | 2001-06-22 06:23:03 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4276 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4277 | /* We asked to wait for bg or orphaned children */ |
| 4278 | /* No need to remember exitcode in this case */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4279 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4280 | for (i = 0; i < pi->num_cmds; i++) { |
| 4281 | if (pi->cmds[i].pid == childpid) |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4282 | goto found_pi_and_prognum; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4283 | } |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4284 | } |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4285 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 4286 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4287 | continue; /* do waitpid again */ |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 4288 | |
Denis Vlasenko | 5f786c2 | 2007-04-20 08:35:45 +0000 | [diff] [blame] | 4289 | found_pi_and_prognum: |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4290 | if (dead) { |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4291 | /* child exited */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4292 | pi->cmds[i].pid = 0; |
| 4293 | pi->alive_cmds--; |
| 4294 | if (!pi->alive_cmds) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4295 | if (G_interactive_fd) |
Mike Frysinger | 87824e0 | 2009-03-30 00:19:30 +0000 | [diff] [blame] | 4296 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 4297 | "Done", pi->cmdtext); |
Denis Vlasenko | 1359da6 | 2007-04-21 23:27:30 +0000 | [diff] [blame] | 4298 | delete_finished_bg_job(pi); |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4299 | } |
| 4300 | } else { |
| 4301 | /* child stopped */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4302 | pi->cmds[i].is_stopped = 1; |
| 4303 | pi->stopped_cmds++; |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4304 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4305 | #endif |
Denis Vlasenko | 003f9fb | 2008-06-24 00:47:58 +0000 | [diff] [blame] | 4306 | } /* while (waitpid succeeds)... */ |
Eric Andersen | bafd94f | 2001-05-02 16:11:59 +0000 | [diff] [blame] | 4307 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4308 | return rcode; |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4311 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4312 | static int checkjobs_and_fg_shell(struct pipe* fg_pipe) |
| 4313 | { |
| 4314 | pid_t p; |
| 4315 | int rcode = checkjobs(fg_pipe); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4316 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4317 | /* Job finished, move the shell to the foreground */ |
| 4318 | p = getpgrp(); /* our process group id */ |
| 4319 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 4320 | tcsetpgrp(G_interactive_fd, p); |
| 4321 | } |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4322 | return rcode; |
| 4323 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4324 | #endif |
Denis Vlasenko | 52881e9 | 2007-04-21 13:42:52 +0000 | [diff] [blame] | 4325 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 4326 | /* Start all the jobs, but don't wait for anything to finish. |
| 4327 | * See checkjobs(). |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4328 | * |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4329 | * 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] | 4330 | * to finish to determine the exit status of the pipe. If the pipe |
| 4331 | * is a simple builtin command, however, the action is done by the |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4332 | * time run_pipe returns, and the exit code is provided as the |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4333 | * return value. |
| 4334 | * |
Denis Vlasenko | 170435c | 2007-05-23 13:01:10 +0000 | [diff] [blame] | 4335 | * Returns -1 only if started some children. IOW: we have to |
| 4336 | * mask out retvals of builtins etc with 0xff! |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4337 | * |
| 4338 | * The only case when we do not need to [v]fork is when the pipe |
| 4339 | * is single, non-backgrounded, non-subshell command. Examples: |
| 4340 | * cmd ; ... { list } ; ... |
| 4341 | * cmd && ... { list } && ... |
| 4342 | * cmd || ... { list } || ... |
| 4343 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 4344 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 4345 | * 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] | 4346 | * |
| 4347 | * Cases when we must fork: |
| 4348 | * non-single: cmd | cmd |
| 4349 | * backgrounded: cmd & { list } & |
| 4350 | * subshell: ( list ) [&] |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4351 | */ |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4352 | #if !ENABLE_HUSH_MODE_X |
| 4353 | #define redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel, char argv_expanded) \ |
| 4354 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 4355 | #endif |
| 4356 | static int redirect_and_varexp_helper(char ***new_env_p, struct variable **old_vars_p, struct command *command, int squirrel[3], char **argv_expanded) |
| 4357 | { |
| 4358 | /* setup_redirects acts on file descriptors, not FILEs. |
| 4359 | * This is perfect for work that comes after exec(). |
| 4360 | * Is it really safe for inline use? Experimentally, |
| 4361 | * things seem to work. */ |
| 4362 | int rcode = setup_redirects(command, squirrel); |
| 4363 | if (rcode == 0) { |
| 4364 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 4365 | *new_env_p = new_env; |
| 4366 | dump_cmd_in_x_mode(new_env); |
| 4367 | dump_cmd_in_x_mode(argv_expanded); |
| 4368 | if (old_vars_p) |
| 4369 | *old_vars_p = set_vars_and_save_old(new_env); |
| 4370 | } |
| 4371 | return rcode; |
| 4372 | } |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 4373 | static NOINLINE int run_pipe(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4374 | { |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4375 | static const char *const null_ptr = NULL; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4376 | |
| 4377 | int cmd_no; |
| 4378 | int next_infd; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4379 | struct command *command; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4380 | char **argv_expanded; |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4381 | char **argv; |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4382 | /* it is not always needed, but we aim to smaller code */ |
| 4383 | int squirrel[] = { -1, -1, -1 }; |
| 4384 | int rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4385 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4386 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4387 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4388 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4389 | IF_HUSH_JOB(pi->pgrp = -1;) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4390 | pi->stopped_cmds = 0; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4391 | command = &pi->cmds[0]; |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4392 | argv_expanded = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4393 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4394 | if (pi->num_cmds != 1 |
| 4395 | || pi->followup == PIPE_BG |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4396 | || command->cmd_type == CMD_SUBSHELL |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4397 | ) { |
| 4398 | goto must_fork; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4399 | } |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4400 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4401 | pi->alive_cmds = 1; |
| 4402 | |
| 4403 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 4404 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 4405 | |
| 4406 | if (command->group) { |
| 4407 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4408 | if (command->cmd_type == CMD_FUNCDEF) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4409 | /* "executing" func () { list } */ |
| 4410 | struct function *funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4411 | |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 4412 | funcp = new_function(command->argv[0]); |
| 4413 | /* funcp->name is already set to argv[0] */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4414 | funcp->body = command->group; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4415 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4416 | funcp->body_as_string = command->group_as_string; |
| 4417 | command->group_as_string = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4418 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4419 | command->group = NULL; |
| 4420 | command->argv[0] = NULL; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 4421 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 4422 | funcp->parent_cmd = command; |
| 4423 | command->child_func = funcp; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4424 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4425 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 4426 | debug_leave(); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4427 | return EXIT_SUCCESS; |
| 4428 | } |
| 4429 | #endif |
| 4430 | /* { list } */ |
| 4431 | debug_printf("non-subshell group\n"); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4432 | rcode = 1; /* exitcode if redir failed */ |
| 4433 | if (setup_redirects(command, squirrel) == 0) { |
| 4434 | debug_printf_exec(": run_list\n"); |
| 4435 | rcode = run_list(command->group) & 0xff; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4436 | } |
Eric Andersen | 04407e5 | 2001-06-07 16:42:05 +0000 | [diff] [blame] | 4437 | restore_redirects(squirrel); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4438 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4439 | debug_leave(); |
| 4440 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4441 | return rcode; |
Denis Vlasenko | f03dbed | 2007-04-13 19:55:50 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4444 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 4445 | { |
| 4446 | const struct built_in_command *x; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4447 | #if ENABLE_HUSH_FUNCTIONS |
| 4448 | const struct function *funcp; |
| 4449 | #else |
| 4450 | enum { funcp = 0 }; |
| 4451 | #endif |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4452 | char **new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4453 | struct variable *old_vars = NULL; |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4454 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4455 | if (argv[command->assignment_cnt] == NULL) { |
| 4456 | /* Assignments, but no command */ |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4457 | /* Ensure redirects take effect (that is, create files). |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4458 | * Try "a=t >file" */ |
| 4459 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 4460 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 4461 | /* Set shell variables */ |
| 4462 | if (new_env) { |
| 4463 | argv = new_env; |
| 4464 | while (*argv) { |
| 4465 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 4466 | /* Do we need to flag set_local_var() errors? |
| 4467 | * "assignment to readonly var" and "putenv error" |
| 4468 | */ |
| 4469 | argv++; |
| 4470 | } |
| 4471 | } |
| 4472 | /* Redirect error sets $? to 1. Otherwise, |
| 4473 | * if evaluating assignment value set $?, retain it. |
| 4474 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 4475 | if (rcode == 0) |
| 4476 | rcode = G.last_exitcode; |
| 4477 | /* Exit, _skipping_ variable restoring code: */ |
| 4478 | goto clean_up_and_ret0; |
| 4479 | |
| 4480 | #else /* Older, bigger, but more correct code */ |
| 4481 | |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4482 | rcode = setup_redirects(command, squirrel); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4483 | restore_redirects(squirrel); |
| 4484 | /* Set shell variables */ |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 4485 | if (G_x_mode) |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4486 | bb_putchar_stderr('+'); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4487 | while (*argv) { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4488 | char *p = expand_string_to_string(*argv); |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 4489 | if (G_x_mode) |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4490 | fprintf(stderr, " %s", p); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4491 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 4492 | *argv, p); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4493 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4494 | /* Do we need to flag set_local_var() errors? |
| 4495 | * "assignment to readonly var" and "putenv error" |
| 4496 | */ |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4497 | argv++; |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4498 | } |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 4499 | if (G_x_mode) |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 4500 | bb_putchar_stderr('\n'); |
Denys Vlasenko | b3389de | 2010-07-15 12:33:37 +0200 | [diff] [blame] | 4501 | /* Redirect error sets $? to 1. Otherwise, |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4502 | * if evaluating assignment value set $?, retain it. |
| 4503 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 4504 | if (rcode == 0) |
| 4505 | rcode = G.last_exitcode; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4506 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4507 | debug_leave(); |
| 4508 | debug_printf_exec("run_pipe: return %d\n", rcode); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4509 | return rcode; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4510 | #endif |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 4511 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4512 | |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4513 | /* Expand the rest into (possibly) many strings each */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4514 | if (0) {} |
| 4515 | #if ENABLE_HUSH_BASH_COMPAT |
| 4516 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4517 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 4518 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4519 | #endif |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4520 | #ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 4521 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB_COND) { |
| 4522 | argv_expanded = expand_strvec_to_strvec_singleword_noglob_cond(argv + command->assignment_cnt); |
| 4523 | |
| 4524 | } |
| 4525 | #endif |
| 4526 | else { |
| 4527 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 4528 | } |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4529 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4530 | /* if someone gives us an empty string: `cmd with empty output` */ |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4531 | if (!argv_expanded[0]) { |
Denys Vlasenko | 385cc59 | 2010-01-12 06:47:39 +0100 | [diff] [blame] | 4532 | free(argv_expanded); |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4533 | debug_leave(); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 4534 | return G.last_exitcode; |
Mike Frysinger | 28736c3 | 2009-10-18 01:11:45 -0400 | [diff] [blame] | 4535 | } |
| 4536 | |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4537 | x = find_builtin(argv_expanded[0]); |
| 4538 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4539 | funcp = NULL; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4540 | if (!x) |
| 4541 | funcp = find_function(argv_expanded[0]); |
| 4542 | #endif |
| 4543 | if (x || funcp) { |
| 4544 | if (!funcp) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 4545 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4546 | debug_printf("exec with redirects only\n"); |
| 4547 | rcode = setup_redirects(command, NULL); |
| 4548 | goto clean_up_and_ret1; |
| 4549 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4550 | } |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4551 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4552 | if (rcode == 0) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4553 | if (!funcp) { |
| 4554 | debug_printf_exec(": builtin '%s' '%s'...\n", |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 4555 | x->b_cmd, argv_expanded[1]); |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 4556 | rcode = x->b_function(argv_expanded) & 0xff; |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 4557 | fflush_all(); |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4558 | } |
| 4559 | #if ENABLE_HUSH_FUNCTIONS |
| 4560 | else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4561 | # if ENABLE_HUSH_LOCAL |
| 4562 | struct variable **sv; |
| 4563 | sv = G.shadowed_vars_pp; |
| 4564 | G.shadowed_vars_pp = &old_vars; |
| 4565 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4566 | debug_printf_exec(": function '%s' '%s'...\n", |
| 4567 | funcp->name, argv_expanded[1]); |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 4568 | rcode = run_function(funcp, argv_expanded) & 0xff; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4569 | # if ENABLE_HUSH_LOCAL |
| 4570 | G.shadowed_vars_pp = sv; |
| 4571 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4572 | } |
| 4573 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4574 | } |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4575 | clean_up_and_ret: |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4576 | unset_vars(new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4577 | add_vars(old_vars); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4578 | /* clean_up_and_ret0: */ |
| 4579 | restore_redirects(squirrel); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4580 | clean_up_and_ret1: |
| 4581 | free(argv_expanded); |
| 4582 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4583 | debug_leave(); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 4584 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 4585 | return rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4586 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 4587 | |
Denys Vlasenko | 8fa1f5d | 2010-07-15 08:18:46 +0200 | [diff] [blame] | 4588 | if (ENABLE_FEATURE_SH_STANDALONE) { |
| 4589 | int n = find_applet_by_name(argv_expanded[0]); |
| 4590 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 4591 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
Denys Vlasenko | 8fa1f5d | 2010-07-15 08:18:46 +0200 | [diff] [blame] | 4592 | if (rcode == 0) { |
Denys Vlasenko | 8fa1f5d | 2010-07-15 08:18:46 +0200 | [diff] [blame] | 4593 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 4594 | argv_expanded[0], argv_expanded[1]); |
| 4595 | rcode = run_nofork_applet(n, argv_expanded); |
| 4596 | } |
| 4597 | goto clean_up_and_ret; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4598 | } |
Denis Vlasenko | f5294e1 | 2007-04-14 10:09:57 +0000 | [diff] [blame] | 4599 | } |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4600 | /* It is neither builtin nor applet. We must fork. */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4603 | must_fork: |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4604 | /* NB: argv_expanded may already be created, and that |
| 4605 | * might include `cmd` runs! Do not rerun it! We *must* |
| 4606 | * use argv_expanded if it's non-NULL */ |
| 4607 | |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4608 | /* Going to fork a child per each pipe member */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4609 | pi->alive_cmds = 0; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4610 | next_infd = 0; |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4611 | |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4612 | cmd_no = 0; |
| 4613 | while (cmd_no < pi->num_cmds) { |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4614 | struct fd_pair pipefds; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4615 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4616 | volatile nommu_save_t nommu_save; |
| 4617 | nommu_save.new_env = NULL; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4618 | nommu_save.old_vars = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4619 | nommu_save.argv = NULL; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4620 | nommu_save.argv_from_re_execing = NULL; |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 4621 | #endif |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4622 | command = &pi->cmds[cmd_no]; |
| 4623 | cmd_no++; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4624 | if (command->argv) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4625 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 4626 | command->argv[0], command->argv[1]); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4627 | } else { |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4628 | debug_printf_exec(": pipe member with no argv\n"); |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 4629 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4630 | |
| 4631 | /* pipes are inserted between pairs of commands */ |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4632 | pipefds.rd = 0; |
| 4633 | pipefds.wr = 1; |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4634 | if (cmd_no < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4635 | xpiped_pair(pipefds); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4636 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4637 | command->pid = BB_MMU ? fork() : vfork(); |
| 4638 | if (!command->pid) { /* child */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4639 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4640 | disable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 4641 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 4642 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4643 | /* Every child adds itself to new process group |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4644 | * with pgid == pid_of_first_child_in_pipe */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4645 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4646 | pid_t pgrp; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4647 | pgrp = pi->pgrp; |
| 4648 | if (pgrp < 0) /* true for 1st process only */ |
| 4649 | pgrp = getpid(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4650 | if (setpgid(0, pgrp) == 0 |
| 4651 | && pi->followup != PIPE_BG |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 4652 | && G_saved_tty_pgrp /* we have ctty */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 4653 | ) { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4654 | /* We do it in *every* child, not just first, |
| 4655 | * to avoid races */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4656 | tcsetpgrp(G_interactive_fd, pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4657 | } |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4658 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4659 | #endif |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4660 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 4661 | /* 1st cmd in backgrounded pipe |
| 4662 | * should have its stdin /dev/null'ed */ |
| 4663 | close(0); |
| 4664 | if (open(bb_dev_null, O_RDONLY)) |
| 4665 | xopen("/", O_RDONLY); |
| 4666 | } else { |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4667 | xmove_fd(next_infd, 0); |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4668 | } |
| 4669 | xmove_fd(pipefds.wr, 1); |
| 4670 | if (pipefds.rd > 1) |
| 4671 | close(pipefds.rd); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4672 | /* Like bash, explicit redirects override pipes, |
| 4673 | * and the pipe fd is available for dup'ing. */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 4674 | if (setup_redirects(command, NULL)) |
| 4675 | _exit(1); |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 4676 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 4677 | /* Restore default handlers just prior to exec */ |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 4678 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 4679 | |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4680 | /* Stores to nommu_save list of env vars putenv'ed |
| 4681 | * (NOMMU, on MMU we don't need that) */ |
| 4682 | /* cast away volatility... */ |
| 4683 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4684 | /* pseudo_exec() does not return */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4685 | } |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4686 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 4687 | /* parent or error */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 4688 | #if ENABLE_HUSH_FAST |
| 4689 | G.count_SIGCHLD++; |
| 4690 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 4691 | #endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4692 | enable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4693 | #if !BB_MMU |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 4694 | /* Clean up after vforked child */ |
| 4695 | free(nommu_save.argv); |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 4696 | free(nommu_save.argv_from_re_execing); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 4697 | unset_vars(nommu_save.new_env); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 4698 | add_vars(nommu_save.old_vars); |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 4699 | #endif |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 4700 | free(argv_expanded); |
| 4701 | argv_expanded = NULL; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4702 | if (command->pid < 0) { /* [v]fork failed */ |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4703 | /* Clearly indicate, was it fork or vfork */ |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 4704 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4705 | } else { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4706 | pi->alive_cmds++; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 4707 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4708 | /* Second and next children need to know pid of first one */ |
| 4709 | if (pi->pgrp < 0) |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4710 | pi->pgrp = command->pid; |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 4711 | #endif |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4712 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4713 | |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4714 | if (cmd_no > 1) |
| 4715 | close(next_infd); |
| 4716 | if (cmd_no < pi->num_cmds) |
Denis Vlasenko | 8c64e03 | 2009-04-20 00:34:01 +0000 | [diff] [blame] | 4717 | close(pipefds.wr); |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4718 | /* Pass read (output) pipe end to next iteration */ |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 4719 | next_infd = pipefds.rd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4720 | } |
Denis Vlasenko | d2c450c | 2008-01-08 20:32:12 +0000 | [diff] [blame] | 4721 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4722 | if (!pi->alive_cmds) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4723 | debug_leave(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4724 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 4725 | return 1; |
| 4726 | } |
| 4727 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4728 | debug_leave(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4729 | 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] | 4730 | return -1; |
| 4731 | } |
| 4732 | |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4733 | #ifndef debug_print_tree |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4734 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 4735 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4736 | static const char *const PIPE[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4737 | [PIPE_SEQ] = "SEQ", |
| 4738 | [PIPE_AND] = "AND", |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 4739 | [PIPE_OR ] = "OR" , |
| 4740 | [PIPE_BG ] = "BG" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4741 | }; |
| 4742 | static const char *RES[] = { |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4743 | [RES_NONE ] = "NONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4744 | # if ENABLE_HUSH_IF |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4745 | [RES_IF ] = "IF" , |
| 4746 | [RES_THEN ] = "THEN" , |
| 4747 | [RES_ELIF ] = "ELIF" , |
| 4748 | [RES_ELSE ] = "ELSE" , |
| 4749 | [RES_FI ] = "FI" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4750 | # endif |
| 4751 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4752 | [RES_FOR ] = "FOR" , |
| 4753 | [RES_WHILE] = "WHILE", |
| 4754 | [RES_UNTIL] = "UNTIL", |
| 4755 | [RES_DO ] = "DO" , |
| 4756 | [RES_DONE ] = "DONE" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4757 | # endif |
| 4758 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4759 | [RES_IN ] = "IN" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4760 | # endif |
| 4761 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4762 | [RES_CASE ] = "CASE" , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4763 | [RES_CASE_IN ] = "CASE_IN" , |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4764 | [RES_MATCH] = "MATCH", |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4765 | [RES_CASE_BODY] = "CASE_BODY", |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4766 | [RES_ESAC ] = "ESAC" , |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4767 | # endif |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4768 | [RES_XXXX ] = "XXXX" , |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4769 | [RES_SNTX ] = "SNTX" , |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4770 | }; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4771 | static const char *const CMDTYPE[] = { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4772 | "{}", |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 4773 | "()", |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 4774 | "[noglob]", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4775 | # if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4776 | "func()", |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4777 | # endif |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 4778 | }; |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4779 | |
| 4780 | int pin, prn; |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4781 | |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4782 | pin = 0; |
| 4783 | while (pi) { |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 4784 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 4785 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4786 | prn = 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4787 | while (prn < pi->num_cmds) { |
| 4788 | struct command *command = &pi->cmds[prn]; |
| 4789 | char **argv = command->argv; |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 4790 | |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4791 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4792 | lvl*2, "", prn, |
| 4793 | command->assignment_cnt); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4794 | if (command->group) { |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4795 | fprintf(stderr, " group %s: (argv=%p)%s%s\n", |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 4796 | CMDTYPE[command->cmd_type], |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4797 | argv |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4798 | # if !BB_MMU |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4799 | , " group_as_string:", command->group_as_string |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4800 | # else |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4801 | , "", "" |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4802 | # endif |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4803 | ); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4804 | debug_print_tree(command->group, lvl+1); |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4805 | prn++; |
| 4806 | continue; |
| 4807 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4808 | if (argv) while (*argv) { |
| 4809 | fprintf(stderr, " '%s'", *argv); |
| 4810 | argv++; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 4811 | } |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4812 | fprintf(stderr, "\n"); |
| 4813 | prn++; |
| 4814 | } |
| 4815 | pi = pi->next; |
| 4816 | pin++; |
| 4817 | } |
| 4818 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4819 | #endif /* debug_print_tree */ |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 4820 | |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 4821 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 4822 | * global data until exec/_exit (we can be a child after vfork!) */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 4823 | static int run_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4824 | { |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4825 | #if ENABLE_HUSH_CASE |
| 4826 | char *case_word = NULL; |
| 4827 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4828 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4829 | struct pipe *loop_top = NULL; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4830 | char **for_lcur = NULL; |
| 4831 | char **for_list = NULL; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4832 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4833 | smallint last_followup; |
| 4834 | smalluint rcode; |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 4835 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4836 | smalluint cond_code = 0; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4837 | #else |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4838 | enum { cond_code = 0 }; |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4839 | #endif |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4840 | #if HAS_KEYWORDS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 4841 | smallint rword; /* enum reserved_style */ |
| 4842 | smallint last_rword; /* ditto */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4843 | #endif |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4844 | |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4845 | 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] | 4846 | debug_enter(); |
Denis Vlasenko | 4ac530c | 2007-05-02 15:35:45 +0000 | [diff] [blame] | 4847 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4848 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4849 | /* Check syntax for "for" */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4850 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 4851 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4852 | continue; |
| 4853 | /* current word is FOR or IN (BOLD in comments below) */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4854 | if (cpipe->next == NULL) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4855 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4856 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4857 | 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] | 4858 | return 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4859 | } |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4860 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4861 | if (cpipe->next->res_word == RES_DO) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4862 | continue; |
| 4863 | /* 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] | 4864 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 4865 | || 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] | 4866 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4867 | syntax_error("malformed for"); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4868 | debug_leave(); |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4869 | 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] | 4870 | return 1; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4871 | } |
| 4872 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4873 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4874 | |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4875 | /* Past this point, all code paths should jump to ret: label |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4876 | * in order to return, no direct "return" statements please. |
| 4877 | * This helps to ensure that no memory is leaked. */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4878 | |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4879 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 4880 | G.run_list_level++; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4881 | #endif |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 4882 | |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4883 | #if HAS_KEYWORDS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4884 | rword = RES_NONE; |
| 4885 | last_rword = RES_XXXX; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4886 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4887 | last_followup = PIPE_SEQ; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4888 | rcode = G.last_exitcode; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4889 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4890 | /* Go through list of pipes, (maybe) executing them. */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4891 | 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] | 4892 | if (G.flag_SIGINT) |
| 4893 | break; |
| 4894 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4895 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4896 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 4897 | rword, cond_code, last_rword); |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4898 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4899 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4900 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
Denis Vlasenko | 4554b72 | 2008-07-29 13:36:09 +0000 | [diff] [blame] | 4901 | ) { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4902 | /* start of a loop: remember where loop starts */ |
| 4903 | loop_top = pi; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 4904 | G.depth_of_loop++; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4905 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4906 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4907 | /* Still in the same "if...", "then..." or "do..." branch? */ |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4908 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4909 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 4910 | || (rcode != 0 && last_followup == PIPE_AND) |
| 4911 | ) { |
| 4912 | /* It is "<true> || CMD" or "<false> && CMD" |
| 4913 | * and we should not execute CMD */ |
| 4914 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 4915 | last_followup = pi->followup; |
| 4916 | continue; |
| 4917 | } |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4918 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4919 | last_followup = pi->followup; |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4920 | IF_HAS_KEYWORDS(last_rword = rword;) |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4921 | #if ENABLE_HUSH_IF |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4922 | if (cond_code) { |
| 4923 | if (rword == RES_THEN) { |
Denis Vlasenko | 0e15138 | 2009-04-06 18:40:31 +0000 | [diff] [blame] | 4924 | /* if false; then ... fi has exitcode 0! */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4925 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4926 | /* "if <false> THEN cmd": skip cmd */ |
| 4927 | continue; |
| 4928 | } |
| 4929 | } else { |
| 4930 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 4931 | /* "if <true> then ... ELSE/ELIF cmd": |
| 4932 | * skip cmd and all following ones */ |
| 4933 | break; |
| 4934 | } |
| 4935 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4936 | #endif |
| 4937 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4938 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4939 | if (!for_lcur) { |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 4940 | /* first loop through for */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4941 | |
| 4942 | static const char encoded_dollar_at[] ALIGN1 = { |
| 4943 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 4944 | }; /* encoded representation of "$@" */ |
| 4945 | static const char *const encoded_dollar_at_argv[] = { |
| 4946 | encoded_dollar_at, NULL |
| 4947 | }; /* argv list with one element: "$@" */ |
| 4948 | char **vals; |
| 4949 | |
| 4950 | vals = (char**)encoded_dollar_at_argv; |
Denis Vlasenko | cf22c89 | 2008-07-28 15:17:44 +0000 | [diff] [blame] | 4951 | if (pi->next->res_word == RES_IN) { |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4952 | /* if no variable values after "in" we skip "for" */ |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4953 | if (!pi->next->cmds[0].argv) { |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 4954 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4955 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4956 | break; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4957 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4958 | vals = pi->next->cmds[0].argv; |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4959 | } /* else: "for var; do..." -> assume "$@" list */ |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4960 | /* create list of variable values */ |
Denis Vlasenko | ff182a3 | 2008-07-05 20:29:59 +0000 | [diff] [blame] | 4961 | debug_print_strings("for_list made from", vals); |
| 4962 | for_list = expand_strvec_to_strvec(vals); |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4963 | for_lcur = for_list; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4964 | debug_print_strings("for_list", for_list); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4965 | } |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4966 | if (!*for_lcur) { |
Denis Vlasenko | 12acec5 | 2008-07-28 15:15:59 +0000 | [diff] [blame] | 4967 | /* "for" loop is over, clean up */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4968 | free(for_list); |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 4969 | for_list = NULL; |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 4970 | for_lcur = NULL; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4971 | break; |
Eric Andersen | 4c9b68f | 2002-04-13 12:33:41 +0000 | [diff] [blame] | 4972 | } |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 4973 | /* Insert next value from for_lcur */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4974 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 4975 | 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] | 4976 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4977 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4978 | if (rword == RES_IN) { |
| 4979 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 4980 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 4981 | if (rword == RES_DONE) { |
| 4982 | continue; /* "done" has no cmds too */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4983 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 4984 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4985 | #if ENABLE_HUSH_CASE |
| 4986 | if (rword == RES_CASE) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4987 | case_word = expand_strvec_to_string(pi->cmds->argv); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4988 | continue; |
| 4989 | } |
| 4990 | if (rword == RES_MATCH) { |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4991 | char **argv; |
| 4992 | |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 4993 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 4994 | break; |
| 4995 | /* all prev words didn't match, does this one match? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 4996 | argv = pi->cmds->argv; |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4997 | while (*argv) { |
| 4998 | char *pattern = expand_string_to_string(*argv); |
| 4999 | /* TODO: which FNM_xxx flags to use? */ |
| 5000 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 5001 | free(pattern); |
| 5002 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 5003 | free(case_word); /* make future "word)" stop */ |
| 5004 | case_word = NULL; |
| 5005 | break; |
| 5006 | } |
| 5007 | argv++; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5008 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5009 | continue; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5010 | } |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5011 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5012 | if (cond_code != 0) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5013 | continue; /* not matched yet, skip this pipe */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5014 | } |
| 5015 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5016 | /* Just pressing <enter> in shell should check for jobs. |
| 5017 | * OTOH, in non-interactive shell this is useless |
| 5018 | * and only leads to extra job checks */ |
| 5019 | if (pi->num_cmds == 0) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5020 | if (G_interactive_fd) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5021 | goto check_jobs_and_continue; |
| 5022 | continue; |
| 5023 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5024 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5025 | /* After analyzing all keywords and conditions, we decided |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5026 | * to execute this pipe. NB: have to do checkjobs(NULL) |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5027 | * after run_pipe to collect any background children, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5028 | * even if list execution is to be stopped. */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5029 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5030 | { |
| 5031 | int r; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5032 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5033 | G.flag_break_continue = 0; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5034 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5035 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 5036 | if (r != -1) { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 5037 | /* We ran a builtin, function, or group. |
| 5038 | * rcode is already known |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5039 | * and we don't need to wait for anything. */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5040 | G.last_exitcode = rcode; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5041 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5042 | check_and_run_traps(0); |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5043 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5044 | /* Was it "break" or "continue"? */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5045 | if (G.flag_break_continue) { |
| 5046 | smallint fbc = G.flag_break_continue; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5047 | /* We might fall into outer *loop*, |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5048 | * don't want to break it too */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5049 | if (loop_top) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5050 | G.depth_break_continue--; |
| 5051 | if (G.depth_break_continue == 0) |
| 5052 | G.flag_break_continue = 0; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5053 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 5054 | } /* 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] | 5055 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5056 | goto check_jobs_and_break; |
| 5057 | /* "continue": simulate end of loop */ |
| 5058 | rword = RES_DONE; |
| 5059 | continue; |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5060 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 5061 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 5062 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 673e945 | 2009-05-27 14:39:35 +0200 | [diff] [blame] | 5063 | if (G.flag_return_in_progress == 1) { |
| 5064 | /* same as "goto check_jobs_and_break" */ |
| 5065 | checkjobs(NULL); |
| 5066 | break; |
| 5067 | } |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 5068 | #endif |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5069 | } else if (pi->followup == PIPE_BG) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5070 | /* What does bash do with attempts to background builtins? */ |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5071 | /* even bash 3.2 doesn't do that well with nested bg: |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5072 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 5073 | * I'm NOT treating inner &'s as jobs */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5074 | check_and_run_traps(0); |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5075 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5076 | if (G.run_list_level == 1) |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5077 | insert_bg_job(pi); |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 5078 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5079 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5080 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5081 | } else { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5082 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 5083 | if (G.run_list_level == 1 && G_interactive_fd) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5084 | /* Waits for completion, then fg's main shell */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5085 | rcode = checkjobs_and_fg_shell(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5086 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5087 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5088 | } else |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 5089 | #endif |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5090 | { /* This one just waits for completion */ |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5091 | rcode = checkjobs(pi); |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5092 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 5093 | check_and_run_traps(0); |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5094 | } |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5095 | G.last_exitcode = rcode; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5096 | } |
| 5097 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5098 | |
| 5099 | /* Analyze how result affects subsequent commands */ |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5100 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 5101 | if (rword == RES_IF || rword == RES_ELIF) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5102 | cond_code = rcode; |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5103 | #endif |
| 5104 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5105 | /* Beware of "while false; true; do ..."! */ |
| 5106 | if (pi->next && pi->next->res_word == RES_DO) { |
| 5107 | if (rword == RES_WHILE) { |
| 5108 | if (rcode) { |
| 5109 | /* "while false; do...done" - exitcode 0 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5110 | G.last_exitcode = rcode = EXIT_SUCCESS; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5111 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 5112 | goto check_jobs_and_break; |
| 5113 | } |
Denis Vlasenko | 918a34b | 2008-07-28 23:17:31 +0000 | [diff] [blame] | 5114 | } |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5115 | if (rword == RES_UNTIL) { |
| 5116 | if (!rcode) { |
| 5117 | debug_printf_exec(": until expr is true: breaking\n"); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5118 | check_jobs_and_break: |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5119 | checkjobs(NULL); |
| 5120 | break; |
| 5121 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 5122 | } |
Denis Vlasenko | 5e052ca | 2008-07-28 15:15:09 +0000 | [diff] [blame] | 5123 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5124 | #endif |
Mike Frysinger | 8ec1c9d | 2009-03-29 00:45:26 +0000 | [diff] [blame] | 5125 | |
| 5126 | check_jobs_and_continue: |
Eric Andersen | 028b65b | 2001-06-28 01:10:11 +0000 | [diff] [blame] | 5127 | checkjobs(NULL); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5128 | } /* for (pi) */ |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 5129 | |
| 5130 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 5131 | G.run_list_level--; |
Denis Vlasenko | ac0e5ab | 2007-05-04 21:37:27 +0000 | [diff] [blame] | 5132 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 5133 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 5134 | if (loop_top) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 5135 | G.depth_of_loop--; |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 5136 | free(for_list); |
| 5137 | #endif |
| 5138 | #if ENABLE_HUSH_CASE |
| 5139 | free(case_word); |
| 5140 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5141 | debug_leave(); |
| 5142 | 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] | 5143 | return rcode; |
| 5144 | } |
| 5145 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5146 | /* Select which version we will use */ |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5147 | static int run_and_free_list(struct pipe *pi) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5148 | { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 5149 | int rcode = 0; |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5150 | debug_printf_exec("run_and_free_list entered\n"); |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 5151 | if (!G.n_mode) { |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 5152 | 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] | 5153 | rcode = run_list(pi); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5154 | } |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5155 | /* free_pipe_list has the side effect of clearing memory. |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5156 | * In the long run that function can be merged with run_list, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5157 | * but doing that now would hobble the debugging effort. */ |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 5158 | free_pipe_list(pi); |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 5159 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5160 | return rcode; |
| 5161 | } |
| 5162 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5163 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 5164 | static struct pipe *new_pipe(void) |
| 5165 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5166 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 5167 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5168 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5169 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5170 | return pi; |
| 5171 | } |
| 5172 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5173 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 5174 | * if ctx->command is NULL. |
| 5175 | * No errors possible here. |
| 5176 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5177 | static int done_command(struct parse_context *ctx) |
| 5178 | { |
| 5179 | /* The command is really already in the pipe structure, so |
| 5180 | * advance the pipe counter and make a new, null command. */ |
| 5181 | struct pipe *pi = ctx->pipe; |
| 5182 | struct command *command = ctx->command; |
| 5183 | |
| 5184 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5185 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5186 | 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] | 5187 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5188 | } |
| 5189 | pi->num_cmds++; |
| 5190 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5191 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5192 | } else { |
| 5193 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 5194 | } |
| 5195 | |
| 5196 | /* Only real trickiness here is that the uncommitted |
| 5197 | * command structure is not counted in pi->num_cmds. */ |
| 5198 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5199 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 5200 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5201 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5202 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 5203 | } |
| 5204 | |
| 5205 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 5206 | { |
| 5207 | int not_null; |
| 5208 | |
| 5209 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 5210 | /* Close previous command */ |
| 5211 | not_null = done_command(ctx); |
| 5212 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5213 | #if HAS_KEYWORDS |
| 5214 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 5215 | ctx->ctx_inverted = 0; |
| 5216 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 5217 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5218 | |
| 5219 | /* Without this check, even just <enter> on command line generates |
| 5220 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5221 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5222 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 5223 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5224 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 5225 | #endif |
| 5226 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5227 | || ctx->ctx_res_w == RES_DONE |
| 5228 | || ctx->ctx_res_w == RES_FOR |
| 5229 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 5230 | #endif |
| 5231 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5232 | || ctx->ctx_res_w == RES_ESAC |
| 5233 | #endif |
| 5234 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5235 | struct pipe *new_p; |
| 5236 | debug_printf_parse("done_pipe: adding new pipe: " |
| 5237 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 5238 | not_null, ctx->ctx_res_w); |
| 5239 | new_p = new_pipe(); |
| 5240 | ctx->pipe->next = new_p; |
| 5241 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5242 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5243 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5244 | * This is used to control execution. |
| 5245 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 5246 | * cases where variable or value happens to match a keyword): |
| 5247 | */ |
| 5248 | #if ENABLE_HUSH_LOOPS |
| 5249 | if (ctx->ctx_res_w == RES_FOR |
| 5250 | || ctx->ctx_res_w == RES_IN) |
| 5251 | ctx->ctx_res_w = RES_NONE; |
| 5252 | #endif |
| 5253 | #if ENABLE_HUSH_CASE |
| 5254 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5255 | ctx->ctx_res_w = RES_CASE_BODY; |
| 5256 | if (ctx->ctx_res_w == RES_CASE) |
| 5257 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5258 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5259 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5260 | /* Create the memory for command, roughly: |
| 5261 | * ctx->pipe->cmds = new struct command; |
| 5262 | * ctx->command = &ctx->pipe->cmds[0]; |
| 5263 | */ |
| 5264 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 5265 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 5266 | } |
| 5267 | debug_printf_parse("done_pipe return\n"); |
| 5268 | } |
| 5269 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5270 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5271 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5272 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5273 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5274 | /* Create the memory for command, roughly: |
| 5275 | * ctx->pipe->cmds = new struct command; |
| 5276 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5277 | */ |
| 5278 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5281 | /* If a reserved word is found and processed, parse context is modified |
| 5282 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5283 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5284 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5285 | struct reserved_combo { |
| 5286 | char literal[6]; |
| 5287 | unsigned char res; |
| 5288 | unsigned char assignment_flag; |
| 5289 | int flag; |
| 5290 | }; |
| 5291 | enum { |
| 5292 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5293 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5294 | FLAG_IF = (1 << RES_IF ), |
| 5295 | FLAG_THEN = (1 << RES_THEN ), |
| 5296 | FLAG_ELIF = (1 << RES_ELIF ), |
| 5297 | FLAG_ELSE = (1 << RES_ELSE ), |
| 5298 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5299 | # endif |
| 5300 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5301 | FLAG_FOR = (1 << RES_FOR ), |
| 5302 | FLAG_WHILE = (1 << RES_WHILE), |
| 5303 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 5304 | FLAG_DO = (1 << RES_DO ), |
| 5305 | FLAG_DONE = (1 << RES_DONE ), |
| 5306 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5307 | # endif |
| 5308 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5309 | FLAG_MATCH = (1 << RES_MATCH), |
| 5310 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5311 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5312 | FLAG_START = (1 << RES_XXXX ), |
| 5313 | }; |
| 5314 | |
| 5315 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 5316 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5317 | /* Mostly a list of accepted follow-up reserved words. |
| 5318 | * FLAG_END means we are done with the sequence, and are ready |
| 5319 | * to turn the compound list into a command. |
| 5320 | * FLAG_START means the word must start a new compound list. |
| 5321 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5322 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5323 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5324 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 5325 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 5326 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 5327 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 5328 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 5329 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5330 | # endif |
| 5331 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5332 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 5333 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 5334 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 5335 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 5336 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 5337 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5338 | # endif |
| 5339 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5340 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 5341 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5342 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5343 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5344 | const struct reserved_combo *r; |
| 5345 | |
| 5346 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 5347 | if (strcmp(word->data, r->literal) == 0) |
| 5348 | return r; |
| 5349 | } |
| 5350 | return NULL; |
| 5351 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5352 | /* Return 0: not a keyword, 1: keyword |
| 5353 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5354 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 5355 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5356 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5357 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5358 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5359 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5360 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5361 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 5362 | |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5363 | if (word->o_quoted) |
| 5364 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5365 | r = match_reserved_word(word); |
| 5366 | if (!r) |
| 5367 | return 0; |
| 5368 | |
| 5369 | 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] | 5370 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5371 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 5372 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5373 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5374 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5375 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5376 | if (r->flag == 0) { /* '!' */ |
| 5377 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5378 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5379 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5380 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5381 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 5382 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5383 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5384 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5385 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5386 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5387 | old = xmalloc(sizeof(*old)); |
| 5388 | debug_printf_parse("push stack %p\n", old); |
| 5389 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5390 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5391 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5392 | } 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] | 5393 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5394 | ctx->ctx_res_w = RES_SNTX; |
| 5395 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5396 | } else { |
| 5397 | /* "{...} fi" is ok. "{...} if" is not |
| 5398 | * Example: |
| 5399 | * if { echo foo; } then { echo bar; } fi */ |
| 5400 | if (ctx->command->group) |
| 5401 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5402 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5403 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5404 | ctx->ctx_res_w = r->res; |
| 5405 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5406 | word->o_assignment = r->assignment_flag; |
| 5407 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5408 | if (ctx->old_flag & FLAG_END) { |
| 5409 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5410 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5411 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5412 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5413 | old = ctx->stack; |
| 5414 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5415 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5416 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5417 | o_addstr(&old->as_string, ctx->as_string.data); |
| 5418 | o_free_unsafe(&ctx->as_string); |
| 5419 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 5420 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 5421 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5422 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5423 | *ctx = *old; /* physical copy */ |
| 5424 | free(old); |
| 5425 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 5426 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5427 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5428 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5429 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5430 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5431 | * Normal return is 0. Syntax errors return 1. |
| 5432 | * Note: on return, word is reset, but not o_free'd! |
| 5433 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5434 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5435 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5436 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5437 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5438 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5439 | if (word->length == 0 && word->o_quoted == 0) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5440 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 5441 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5442 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 5443 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5444 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5445 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 5446 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5447 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5448 | * "2.7 Redirection |
| 5449 | * ...the word that follows the redirection operator |
| 5450 | * shall be subjected to tilde expansion, parameter expansion, |
| 5451 | * command substitution, arithmetic expansion, and quote |
| 5452 | * removal. Pathname expansion shall not be performed |
| 5453 | * on the word by a non-interactive shell; an interactive |
| 5454 | * shell may perform it, but shall do so only when |
| 5455 | * the expansion would result in one word." |
| 5456 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5457 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5458 | /* Cater for >\file case: |
| 5459 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 5460 | * Same with heredocs: |
| 5461 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 5462 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 5463 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 5464 | unbackslash(ctx->pending_redirect->rd_filename); |
| 5465 | /* Is it <<"HEREDOC"? */ |
| 5466 | if (word->o_quoted) { |
| 5467 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 5468 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5469 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5470 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5471 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5472 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5473 | /* If this word wasn't an assignment, next ones definitely |
| 5474 | * can't be assignments. Even if they look like ones. */ |
| 5475 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 5476 | && word->o_assignment != WORD_IS_KEYWORD |
| 5477 | ) { |
| 5478 | word->o_assignment = NOT_ASSIGNMENT; |
| 5479 | } else { |
| 5480 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 5481 | command->assignment_cnt++; |
| 5482 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 5483 | } |
| 5484 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5485 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5486 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 5487 | if (ctx->ctx_dsemicolon |
| 5488 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 5489 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 5490 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5491 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 5492 | ctx->ctx_dsemicolon = 0; |
| 5493 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5494 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5495 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5496 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5497 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 5498 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5499 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 5500 | # if ENABLE_HUSH_CASE |
| 5501 | && ctx->ctx_res_w != RES_CASE |
| 5502 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5503 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 5504 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 5505 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5506 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 5507 | debug_printf_parse("done_word return %d\n", |
| 5508 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5509 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5510 | } |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5511 | # ifdef CMD_SINGLEWORD_NOGLOB_COND |
| 5512 | if (strcmp(word->data, "export") == 0 |
| 5513 | # if ENABLE_HUSH_LOCAL |
| 5514 | || strcmp(word->data, "local") == 0 |
| 5515 | # endif |
| 5516 | ) { |
| 5517 | command->cmd_type = CMD_SINGLEWORD_NOGLOB_COND; |
| 5518 | } else |
| 5519 | # endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5520 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 5521 | if (strcmp(word->data, "[[") == 0) { |
| 5522 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 5523 | } |
| 5524 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 5525 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5526 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 5527 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5528 | if (command->group) { |
| 5529 | /* "{ echo foo; } echo bar" - bad */ |
| 5530 | syntax_error_at(word->data); |
| 5531 | debug_printf_parse("done_word return 1: syntax error, " |
| 5532 | "groups and arglists don't mix\n"); |
| 5533 | return 1; |
| 5534 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5535 | 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] | 5536 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 5537 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5538 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 5539 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5540 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5541 | char *p = word->data; |
| 5542 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 5543 | && (p[1] & 0x7f) == '@' |
| 5544 | && p[2] == SPECIAL_VAR_SYMBOL |
| 5545 | ) { |
| 5546 | p += 3; |
| 5547 | } |
| 5548 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5549 | /* saw no "$@", or not only "$@" but some |
| 5550 | * real text is there too */ |
| 5551 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 5552 | * e.g. "", $empty"" etc to not disappear */ |
| 5553 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5554 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 5555 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 5556 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 5557 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5558 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5559 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5560 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5561 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5562 | if (ctx->ctx_res_w == RES_FOR) { |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5563 | if (word->o_quoted |
| 5564 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 5565 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5566 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5567 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5568 | return 1; |
| 5569 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5570 | /* Force FOR to have just one word (variable name) */ |
| 5571 | /* NB: basically, this makes hush see "for v in ..." |
| 5572 | * syntax as if it is "for v; in ...". FOR and IN become |
| 5573 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 5574 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 5575 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 5576 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 5577 | #if ENABLE_HUSH_CASE |
| 5578 | /* Force CASE to have just one word */ |
| 5579 | if (ctx->ctx_res_w == RES_CASE) { |
| 5580 | done_pipe(ctx, PIPE_SEQ); |
| 5581 | } |
| 5582 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5583 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5584 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 5585 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5586 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5587 | return 0; |
| 5588 | } |
| 5589 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5590 | |
| 5591 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 5592 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5593 | * Return: |
| 5594 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 5595 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 5596 | * REDIRFD_TO_FILE if no & was seen, |
| 5597 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5598 | */ |
| 5599 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5600 | #define parse_redir_right_fd(as_string, input) \ |
| 5601 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5602 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5603 | 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] | 5604 | { |
| 5605 | int ch, d, ok; |
| 5606 | |
| 5607 | ch = i_peek(input); |
| 5608 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5609 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5610 | |
| 5611 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5612 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5613 | ch = i_peek(input); |
| 5614 | if (ch == '-') { |
| 5615 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5616 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5617 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5618 | } |
| 5619 | d = 0; |
| 5620 | ok = 0; |
| 5621 | while (ch != EOF && isdigit(ch)) { |
| 5622 | d = d*10 + (ch-'0'); |
| 5623 | ok = 1; |
| 5624 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5625 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5626 | ch = i_peek(input); |
| 5627 | } |
| 5628 | if (ok) return d; |
| 5629 | |
| 5630 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 5631 | |
| 5632 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5633 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5634 | } |
| 5635 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5636 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5637 | */ |
| 5638 | static int parse_redirect(struct parse_context *ctx, |
| 5639 | int fd, |
| 5640 | redir_type style, |
| 5641 | struct in_str *input) |
| 5642 | { |
| 5643 | struct command *command = ctx->command; |
| 5644 | struct redir_struct *redir; |
| 5645 | struct redir_struct **redirp; |
| 5646 | int dup_num; |
| 5647 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5648 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5649 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5650 | /* Check for a '>&1' type redirect */ |
| 5651 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 5652 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 5653 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5654 | } else { |
| 5655 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5656 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5657 | if (dup_num) { /* <<-... */ |
| 5658 | ch = i_getch(input); |
| 5659 | nommu_addchr(&ctx->as_string, ch); |
| 5660 | ch = i_peek(input); |
| 5661 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5662 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5663 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5664 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5665 | int ch = i_peek(input); |
| 5666 | if (ch == '|') { |
| 5667 | /* >|FILE redirect ("clobbering" >). |
| 5668 | * Since we do not support "set -o noclobber" yet, |
| 5669 | * >| and > are the same for now. Just eat |. |
| 5670 | */ |
| 5671 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5672 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5673 | } |
| 5674 | } |
| 5675 | |
| 5676 | /* Create a new redir_struct and append it to the linked list */ |
| 5677 | redirp = &command->redirects; |
| 5678 | while ((redir = *redirp) != NULL) { |
| 5679 | redirp = &(redir->next); |
| 5680 | } |
| 5681 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 5682 | /* redir->next = NULL; */ |
| 5683 | /* redir->rd_filename = NULL; */ |
| 5684 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5685 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5686 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5687 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 5688 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5689 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5690 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 5691 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5692 | /* Erik had a check here that the file descriptor in question |
| 5693 | * is legit; I postpone that to "run time" |
| 5694 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5695 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 5696 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5697 | } else { |
| 5698 | /* Set ctx->pending_redirect, so we know what to do at the |
| 5699 | * end of the next parsed word. */ |
| 5700 | ctx->pending_redirect = redir; |
| 5701 | } |
| 5702 | return 0; |
| 5703 | } |
| 5704 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5705 | /* If a redirect is immediately preceded by a number, that number is |
| 5706 | * supposed to tell which file descriptor to redirect. This routine |
| 5707 | * looks for such preceding numbers. In an ideal world this routine |
| 5708 | * needs to handle all the following classes of redirects... |
| 5709 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 5710 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 5711 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 5712 | * 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] | 5713 | * |
| 5714 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 5715 | * "2.7 Redirection |
| 5716 | * ... If n is quoted, the number shall not be recognized as part of |
| 5717 | * the redirection expression. For example: |
| 5718 | * echo \2>a |
| 5719 | * writes the character 2 into file a" |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5720 | * We are getting it right by setting ->o_quoted on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5721 | * |
| 5722 | * A -1 return means no valid number was found, |
| 5723 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5724 | */ |
| 5725 | static int redirect_opt_num(o_string *o) |
| 5726 | { |
| 5727 | int num; |
| 5728 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5729 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 5730 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5731 | num = bb_strtou(o->data, NULL, 10); |
| 5732 | if (errno || num < 0) |
| 5733 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 5734 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5735 | return num; |
| 5736 | } |
| 5737 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5738 | #if BB_MMU |
| 5739 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 5740 | fetch_till_str(input, word, skip_tabs) |
| 5741 | #endif |
| 5742 | static char *fetch_till_str(o_string *as_string, |
| 5743 | struct in_str *input, |
| 5744 | const char *word, |
| 5745 | int skip_tabs) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5746 | { |
| 5747 | o_string heredoc = NULL_O_STRING; |
| 5748 | int past_EOL = 0; |
| 5749 | int ch; |
| 5750 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5751 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5752 | while (1) { |
| 5753 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5754 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5755 | if (ch == '\n') { |
| 5756 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 5757 | heredoc.data[past_EOL] = '\0'; |
| 5758 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 5759 | return heredoc.data; |
| 5760 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5761 | do { |
| 5762 | o_addchr(&heredoc, ch); |
| 5763 | past_EOL = heredoc.length; |
| 5764 | jump_in: |
| 5765 | do { |
| 5766 | ch = i_getch(input); |
| 5767 | nommu_addchr(as_string, ch); |
| 5768 | } while (skip_tabs && ch == '\t'); |
| 5769 | } while (ch == '\n'); |
| 5770 | } |
| 5771 | if (ch == EOF) { |
| 5772 | o_free_unsafe(&heredoc); |
| 5773 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5774 | } |
| 5775 | o_addchr(&heredoc, ch); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5776 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5777 | } |
| 5778 | } |
| 5779 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5780 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 5781 | * and load them all. There should be exactly heredoc_cnt of them. |
| 5782 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5783 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 5784 | { |
| 5785 | struct pipe *pi = ctx->list_head; |
| 5786 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5787 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5788 | int i; |
| 5789 | struct command *cmd = pi->cmds; |
| 5790 | |
| 5791 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 5792 | pi->num_cmds, |
| 5793 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 5794 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5795 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5796 | |
| 5797 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 5798 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5799 | while (redir) { |
| 5800 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5801 | char *p; |
| 5802 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5803 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 5804 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5805 | p = fetch_till_str(&ctx->as_string, input, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 5806 | redir->rd_filename, redir->rd_dup & HEREDOC_SKIPTABS); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5807 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5808 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5809 | return 1; |
| 5810 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5811 | free(redir->rd_filename); |
| 5812 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5813 | heredoc_cnt--; |
| 5814 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5815 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5816 | } |
| 5817 | cmd++; |
| 5818 | } |
| 5819 | pi = pi->next; |
| 5820 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5821 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5822 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 5823 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 5824 | bb_error_msg_and_die("heredoc BUG 2"); |
| 5825 | #endif |
| 5826 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 5830 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5831 | 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] | 5832 | { |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5833 | pid_t pid; |
| 5834 | int channel[2]; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5835 | # if !BB_MMU |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 5836 | char **to_free = NULL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5837 | # endif |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5838 | |
Denis Vlasenko | 5a6aedd | 2007-05-26 16:44:20 +0000 | [diff] [blame] | 5839 | xpipe(channel); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 5840 | pid = BB_MMU ? xfork() : xvfork(); |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 5841 | if (pid == 0) { /* child */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5842 | disable_restore_tty_pgrp_on_exit(); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 5843 | /* Process substitution is not considered to be usual |
| 5844 | * 'command execution'. |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 5845 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5846 | */ |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 5847 | bb_signals(0 |
| 5848 | + (1 << SIGTSTP) |
| 5849 | + (1 << SIGTTIN) |
| 5850 | + (1 << SIGTTOU) |
| 5851 | , SIG_IGN); |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 5852 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5853 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5854 | xmove_fd(channel[1], 1); |
| 5855 | /* Prevent it from trying to handle ctrl-z etc */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 5856 | IF_HUSH_JOB(G.run_list_level = 1;) |
Denys Vlasenko | 91836ba | 2009-09-23 01:46:19 +0200 | [diff] [blame] | 5857 | /* Awful hack for `trap` or $(trap). |
| 5858 | * |
| 5859 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5860 | * contains an example where "trap" is executed in a subshell: |
| 5861 | * |
| 5862 | * save_traps=$(trap) |
| 5863 | * ... |
| 5864 | * eval "$save_traps" |
| 5865 | * |
| 5866 | * Standard does not say that "trap" in subshell shall print |
| 5867 | * parent shell's traps. It only says that its output |
| 5868 | * must have suitable form, but then, in the above example |
| 5869 | * (which is not supposed to be normative), it implies that. |
| 5870 | * |
| 5871 | * bash (and probably other shell) does implement it |
| 5872 | * (traps are reset to defaults, but "trap" still shows them), |
| 5873 | * but as a result, "trap" logic is hopelessly messed up: |
| 5874 | * |
| 5875 | * # trap |
| 5876 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5877 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5878 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5879 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5880 | * trap -- 'echo Ho' SIGWINCH |
| 5881 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5882 | * trap -- 'echo Ho' SIGWINCH |
| 5883 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5884 | * trap -- 'echo Ho' SIGWINCH |
| 5885 | * |
| 5886 | * The rules when to forget and when to not forget traps |
| 5887 | * get really complex and nonsensical. |
| 5888 | * |
| 5889 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5890 | */ |
| 5891 | s = skip_whitespace(s); |
| 5892 | if (strncmp(s, "trap", 4) == 0 && (*skip_whitespace(s + 4) == '\0')) |
| 5893 | { |
| 5894 | static const char *const argv[] = { NULL, NULL }; |
| 5895 | builtin_trap((char**)argv); |
| 5896 | exit(0); /* not _exit() - we need to fflush */ |
| 5897 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5898 | # if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 5899 | reset_traps_to_defaults(); |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5900 | parse_and_run_string(s); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 5901 | _exit(G.last_exitcode); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5902 | # else |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5903 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 5904 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5905 | * huge=`cat BIG` # was blocking here forever |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5906 | * echo OK |
| 5907 | */ |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5908 | re_execute_shell(&to_free, |
| 5909 | s, |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5910 | G.global_argv[0], |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 5911 | G.global_argv + 1, |
| 5912 | NULL); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5913 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5914 | } |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5915 | |
| 5916 | /* parent */ |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5917 | *pid_p = pid; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5918 | # if ENABLE_HUSH_FAST |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 5919 | G.count_SIGCHLD++; |
| 5920 | //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] | 5921 | # endif |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 5922 | enable_restore_tty_pgrp_on_exit(); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5923 | # if !BB_MMU |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 5924 | free(to_free); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5925 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5926 | close(channel[1]); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5927 | close_on_exec_on(channel[0]); |
| 5928 | return xfdopen_for_read(channel[0]); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
Denis Vlasenko | 21f0d4c | 2007-05-06 14:15:42 +0000 | [diff] [blame] | 5931 | /* Return code is exit status of the process that is run. */ |
Denis Vlasenko | db2a9b6 | 2009-04-03 22:31:18 +0000 | [diff] [blame] | 5932 | static int process_command_subs(o_string *dest, const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5933 | { |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5934 | FILE *fp; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5935 | struct in_str pipe_str; |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5936 | pid_t pid; |
| 5937 | int status, ch, eol_cnt; |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5938 | |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5939 | fp = generate_stream_from_string(s, &pid); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5940 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 5941 | /* Now send results of command back into original context */ |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5942 | setup_file_in_str(&pipe_str, fp); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5943 | eol_cnt = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 5944 | while ((ch = i_getch(&pipe_str)) != EOF) { |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5945 | if (ch == '\n') { |
| 5946 | eol_cnt++; |
| 5947 | continue; |
| 5948 | } |
| 5949 | while (eol_cnt) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 5950 | o_addchr(dest, '\n'); |
Denis Vlasenko | 3e9aaae | 2007-05-11 12:56:43 +0000 | [diff] [blame] | 5951 | eol_cnt--; |
| 5952 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 5953 | o_addQchr(dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5954 | } |
| 5955 | |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5956 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 5957 | fclose(fp); |
Denys Vlasenko | 647553a | 2009-11-15 19:58:19 +0100 | [diff] [blame] | 5958 | /* We need to extract exitcode. Test case |
| 5959 | * "true; echo `sleep 1; false` $?" |
| 5960 | * should print 1 */ |
| 5961 | safe_waitpid(pid, &status, 0); |
| 5962 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5963 | return WEXITSTATUS(status); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5964 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 5965 | #endif /* ENABLE_HUSH_TICK */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5966 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 5967 | #if !ENABLE_HUSH_FUNCTIONS |
| 5968 | #define parse_group(dest, ctx, input, ch) \ |
| 5969 | parse_group(ctx, input, ch) |
| 5970 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5971 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5972 | struct in_str *input, int ch) |
| 5973 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5974 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 5975 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5976 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5977 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 5978 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 5979 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 5980 | |
| 5981 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 5982 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5983 | if (ch == '(' && !dest->o_quoted) { |
| 5984 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 5985 | if (done_word(dest, ctx)) |
| 5986 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5987 | if (!command->argv) |
| 5988 | goto skip; /* (... */ |
| 5989 | if (command->argv[1]) { /* word word ... (... */ |
| 5990 | syntax_error_unexpected_ch('('); |
| 5991 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 5992 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 5993 | /* it is "word(..." or "word (..." */ |
| 5994 | do |
| 5995 | ch = i_getch(input); |
| 5996 | while (ch == ' ' || ch == '\t'); |
| 5997 | if (ch != ')') { |
| 5998 | syntax_error_unexpected_ch(ch); |
| 5999 | return 1; |
| 6000 | } |
| 6001 | nommu_addchr(&ctx->as_string, ch); |
| 6002 | do |
| 6003 | ch = i_getch(input); |
| 6004 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 6005 | if (ch != '{') { |
| 6006 | syntax_error_unexpected_ch(ch); |
| 6007 | return 1; |
| 6008 | } |
| 6009 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 6010 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6011 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 6012 | } |
| 6013 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6014 | |
| 6015 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 6016 | if (command->argv /* word [word]{... */ |
| 6017 | || dest->length /* word{... */ |
| 6018 | || dest->o_quoted /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6019 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6020 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6021 | debug_printf_parse("parse_group return 1: " |
| 6022 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6023 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6024 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6025 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 6026 | |
| 6027 | #if ENABLE_HUSH_FUNCTIONS |
| 6028 | skip: |
| 6029 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6030 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6031 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6032 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 6033 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 6034 | } else { |
| 6035 | /* bash does not allow "{echo...", requires whitespace */ |
| 6036 | ch = i_getch(input); |
| 6037 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 6038 | syntax_error_unexpected_ch(ch); |
| 6039 | return 1; |
| 6040 | } |
| 6041 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6042 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 6043 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6044 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6045 | #if BB_MMU |
| 6046 | # define as_string NULL |
| 6047 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6048 | char *as_string = NULL; |
| 6049 | #endif |
| 6050 | pipe_list = parse_stream(&as_string, input, endch); |
| 6051 | #if !BB_MMU |
| 6052 | if (as_string) |
| 6053 | o_addstr(&ctx->as_string, as_string); |
| 6054 | #endif |
| 6055 | /* empty ()/{} or parse error? */ |
| 6056 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 6057 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6058 | if (!BB_MMU) |
| 6059 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6060 | debug_printf_parse("parse_group return 1: " |
| 6061 | "parse_stream returned %p\n", pipe_list); |
| 6062 | return 1; |
| 6063 | } |
| 6064 | command->group = pipe_list; |
| 6065 | #if !BB_MMU |
| 6066 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 6067 | command->group_as_string = as_string; |
| 6068 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 6069 | command->group_as_string); |
| 6070 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6071 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6072 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6073 | debug_printf_parse("parse_group return 0\n"); |
| 6074 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 6075 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6078 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6079 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6080 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6081 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6082 | 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] | 6083 | { |
| 6084 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6085 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6086 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6087 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6088 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6089 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6090 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6091 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6092 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6093 | } |
| 6094 | } |
| 6095 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6096 | 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] | 6097 | { |
| 6098 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6099 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6100 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6101 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6102 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6103 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6104 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6105 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6106 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6107 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6108 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6109 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6110 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6111 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6112 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6113 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6114 | continue; |
| 6115 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 6116 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6117 | } |
| 6118 | } |
| 6119 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 6120 | * \` quoting. |
| 6121 | * "Within the backquoted style of command substitution, backslash |
| 6122 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 6123 | * The search for the matching backquote shall be satisfied by the first |
| 6124 | * backquote found without a preceding backslash; during this search, |
| 6125 | * if a non-escaped backquote is encountered within a shell comment, |
| 6126 | * a here-document, an embedded command substitution of the $(command) |
| 6127 | * form, or a quoted string, undefined results occur. A single-quoted |
| 6128 | * or double-quoted string that begins, but does not end, within the |
| 6129 | * "`...`" sequence produces undefined results." |
| 6130 | * Example Output |
| 6131 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 6132 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6133 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6134 | { |
| 6135 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6136 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6137 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6138 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6139 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6140 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6141 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6142 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6143 | if (ch == '\\') { |
| 6144 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6145 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6146 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6147 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6148 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6149 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6150 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6151 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6152 | ch = ch2; |
| 6153 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6154 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 6158 | * quoting and nested ()s. |
| 6159 | * "With the $(command) style of command substitution, all characters |
| 6160 | * following the open parenthesis to the matching closing parenthesis |
| 6161 | * constitute the command. Any valid shell script can be used for command, |
| 6162 | * except a script consisting solely of redirections which produces |
| 6163 | * unspecified results." |
| 6164 | * Example Output |
| 6165 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 6166 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 6167 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6168 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6169 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6170 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6171 | * In bash compat mode, it needs to also be able to stop on '}' or ':' |
| 6172 | * for ${var:N[:M]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6173 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6174 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6175 | 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] | 6176 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6177 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6178 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6179 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6180 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6181 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6182 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 6183 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6184 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6185 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6186 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6187 | syntax_error_unterm_ch(end_ch); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6188 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6189 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6190 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6191 | if (!dbl) |
| 6192 | break; |
| 6193 | /* we look for closing )) of $((EXPR)) */ |
| 6194 | if (i_peek(input) == end_ch) { |
| 6195 | i_getch(input); /* eat second ')' */ |
| 6196 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6197 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6198 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6199 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6200 | if (ch == '(' || ch == '{') { |
| 6201 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6202 | add_till_closing_bracket(dest, input, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6203 | o_addchr(dest, ch); |
| 6204 | continue; |
| 6205 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6206 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6207 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6208 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6209 | continue; |
| 6210 | } |
| 6211 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6212 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6213 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6214 | continue; |
| 6215 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 6216 | if (ch == '`') { |
| 6217 | add_till_backquote(dest, input); |
| 6218 | o_addchr(dest, ch); |
| 6219 | continue; |
| 6220 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6221 | if (ch == '\\') { |
| 6222 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6223 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6224 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6225 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6226 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6227 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 6228 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6229 | continue; |
| 6230 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6231 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6232 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6233 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6234 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6235 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 6236 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6237 | #if BB_MMU |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6238 | #define parse_dollar(as_string, dest, input) \ |
| 6239 | parse_dollar(dest, input) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6240 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6241 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6242 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6243 | o_string *dest, |
| 6244 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6245 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6246 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6247 | unsigned char quote_mask = dest->o_escape ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6248 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6249 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 6250 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6251 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6252 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 6253 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6254 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6255 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 6256 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6257 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 6258 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6259 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 6260 | if (!isalnum(ch) && ch != '_') |
| 6261 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6262 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6263 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6264 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6265 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6266 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 6267 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6268 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6269 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6270 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 6271 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6272 | o_addchr(dest, ch | quote_mask); |
| 6273 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6274 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6275 | case '$': /* pid */ |
| 6276 | case '!': /* last bg pid */ |
| 6277 | case '?': /* last exit code */ |
| 6278 | case '#': /* number of args */ |
| 6279 | case '*': /* args */ |
| 6280 | case '@': /* args */ |
| 6281 | goto make_one_char_var; |
| 6282 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 6283 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6284 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6285 | ch = i_getch(input); /* eat '{' */ |
| 6286 | nommu_addchr(as_string, ch); |
| 6287 | |
| 6288 | ch = i_getch(input); /* first char after '{' */ |
| 6289 | nommu_addchr(as_string, ch); |
| 6290 | /* It should be ${?}, or ${#var}, |
| 6291 | * or even ${?+subst} - operator acting on a special variable, |
| 6292 | * or the beginning of variable name. |
| 6293 | */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 6294 | if (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) { /* not one of those */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6295 | bad_dollar_syntax: |
| 6296 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6297 | debug_printf_parse("parse_dollar return 1: unterminated ${name}\n"); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6298 | return 1; |
| 6299 | } |
| 6300 | ch |= quote_mask; |
| 6301 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6302 | /* 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] | 6303 | * However, this regresses some of our testsuite cases |
| 6304 | * which check invalid constructs like ${%}. |
| 6305 | * Oh well... let's check that the var name part is fine... */ |
| 6306 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6307 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6308 | unsigned pos; |
| 6309 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6310 | o_addchr(dest, ch); |
| 6311 | debug_printf_parse(": '%c'\n", ch); |
| 6312 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6313 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6314 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6315 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6316 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 6317 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6318 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6319 | unsigned end_ch; |
| 6320 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6321 | /* handle parameter expansions |
| 6322 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 6323 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6324 | if (!strchr("%#:-=+?", ch)) /* ${var<bad_char>... */ |
| 6325 | goto bad_dollar_syntax; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6326 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6327 | |
| 6328 | /* Eat everything until closing '}' (or ':') */ |
| 6329 | end_ch = '}'; |
| 6330 | if (ENABLE_HUSH_BASH_COMPAT |
| 6331 | && ch == ':' |
| 6332 | && !strchr("%#:-=+?"+3, i_peek(input)) |
| 6333 | ) { |
| 6334 | /* It's ${var:N[:M]} thing */ |
| 6335 | end_ch = '}' * 0x100 + ':'; |
| 6336 | } |
| 6337 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6338 | if (!BB_MMU) |
| 6339 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6340 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6341 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 6342 | #else |
| 6343 | #error Simple code to only allow ${var} is not implemented |
| 6344 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6345 | if (as_string) { |
| 6346 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6347 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6348 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 6349 | |
| 6350 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 6351 | /* close the first block: */ |
| 6352 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6353 | /* while parsing N from ${var:N[:M]}... */ |
| 6354 | if ((end_ch & 0xff) == last_ch) { |
| 6355 | /* ...got ':' - parse the rest */ |
| 6356 | end_ch = '}'; |
| 6357 | goto again; |
| 6358 | } |
| 6359 | /* ...got '}', not ':' - it's ${var:N}! emulate :999999999 */ |
| 6360 | o_addstr(dest, "999999999"); |
| 6361 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6362 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6363 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 6364 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6365 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6366 | break; |
| 6367 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6368 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6369 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6370 | unsigned pos; |
| 6371 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6372 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6373 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6374 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6375 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6376 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6377 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6378 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6379 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6380 | if (!BB_MMU) |
| 6381 | pos = dest->length; |
| 6382 | add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6383 | if (as_string) { |
| 6384 | o_addstr(as_string, dest->data + pos); |
| 6385 | o_addchr(as_string, ')'); |
| 6386 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6387 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6388 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6389 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 6390 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6391 | # endif |
| 6392 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6393 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6394 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6395 | if (!BB_MMU) |
| 6396 | pos = dest->length; |
| 6397 | add_till_closing_bracket(dest, input, ')'); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6398 | if (as_string) { |
| 6399 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 6400 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6401 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6402 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6403 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6404 | break; |
| 6405 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 6406 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6407 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6408 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6409 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6410 | ch = i_peek(input); |
| 6411 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 6412 | ch = '_'; |
| 6413 | goto make_var; |
| 6414 | } |
| 6415 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 6416 | /* TODO: $_ and $-: */ |
| 6417 | /* $_ Shell or shell script name; or last argument of last command |
| 6418 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 6419 | * 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] | 6420 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 6421 | default: |
| 6422 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6423 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6424 | debug_printf_parse("parse_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6425 | return 0; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6426 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6427 | } |
| 6428 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6429 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6430 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6431 | parse_stream_dquoted(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6432 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6433 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6434 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6435 | o_string *dest, |
| 6436 | struct in_str *input, |
| 6437 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6438 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6439 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6440 | int next; |
| 6441 | |
| 6442 | again: |
| 6443 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6444 | if (ch != EOF) |
| 6445 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6446 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6447 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6448 | dest->o_escape ^= 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6449 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 6450 | return 0; |
| 6451 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6452 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6453 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6454 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6455 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6456 | } |
| 6457 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6458 | if (ch != '\n') { |
| 6459 | next = i_peek(input); |
| 6460 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6461 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 6462 | ch, ch, dest->o_escape); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6463 | if (ch == '\\') { |
| 6464 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6465 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6466 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6467 | } |
| 6468 | /* bash: |
| 6469 | * "The backslash retains its special meaning [in "..."] |
| 6470 | * only when followed by one of the following characters: |
| 6471 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6472 | * within double quotes by preceding it with a backslash." |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6473 | */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6474 | if (strchr("$`\"\\\n", next) != NULL) { |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 6475 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6476 | if (ch != '\n') { |
| 6477 | o_addqchr(dest, ch); |
| 6478 | nommu_addchr(as_string, ch); |
| 6479 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6480 | } else { |
| 6481 | o_addqchr(dest, '\\'); |
Denis Vlasenko | 5729300 | 2009-04-26 20:06:14 +0000 | [diff] [blame] | 6482 | nommu_addchr(as_string, '\\'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6483 | } |
| 6484 | goto again; |
| 6485 | } |
| 6486 | if (ch == '$') { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6487 | if (parse_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6488 | debug_printf_parse("parse_stream_dquoted return 1: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6489 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6490 | return 1; |
| 6491 | } |
| 6492 | goto again; |
| 6493 | } |
| 6494 | #if ENABLE_HUSH_TICK |
| 6495 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6496 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6497 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6498 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6499 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6500 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 6501 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6502 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6503 | } |
| 6504 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6505 | o_addQchr(dest, ch); |
| 6506 | if (ch == '=' |
| 6507 | && (dest->o_assignment == MAYBE_ASSIGNMENT |
| 6508 | || dest->o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6509 | && is_well_formed_var_name(dest->data, '=') |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 6510 | ) { |
| 6511 | dest->o_assignment = DEFINITELY_ASSIGNMENT; |
| 6512 | } |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6513 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 6514 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6517 | /* |
| 6518 | * Scan input until EOF or end_trigger char. |
| 6519 | * Return a list of pipes to execute, or NULL on EOF |
| 6520 | * or if end_trigger character is met. |
| 6521 | * On syntax error, exit is shell is not interactive, |
| 6522 | * reset parsing machinery and start parsing anew, |
| 6523 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6524 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6525 | static struct pipe *parse_stream(char **pstring, |
| 6526 | struct in_str *input, |
| 6527 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6528 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6529 | struct parse_context ctx; |
| 6530 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6531 | int is_in_dquote; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6532 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6533 | |
Denis Vlasenko | b7aaae9 | 2009-04-02 20:17:49 +0000 | [diff] [blame] | 6534 | /* Double-quote state is handled in the state variable is_in_dquote. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6535 | * 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] | 6536 | * found. When recursing, quote state is passed in via dest->o_escape. |
| 6537 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6538 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 6539 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6540 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6541 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 6542 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 6543 | * Preventing this: */ |
| 6544 | o_addchr(&dest, '\0'); |
| 6545 | dest.length = 0; |
| 6546 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6547 | G.ifs = get_local_var_value("IFS"); |
| 6548 | if (G.ifs == NULL) |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 6549 | G.ifs = defifs; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6550 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6551 | reset: |
| 6552 | #if ENABLE_HUSH_INTERACTIVE |
| 6553 | input->promptmode = 0; /* PS1 */ |
| 6554 | #endif |
| 6555 | /* dest.o_assignment = MAYBE_ASSIGNMENT; - already is */ |
| 6556 | initialize_context(&ctx); |
| 6557 | is_in_dquote = 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6558 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6559 | while (1) { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6560 | const char *is_ifs; |
| 6561 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6562 | int ch; |
| 6563 | int next; |
| 6564 | int redir_fd; |
| 6565 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6566 | |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6567 | if (is_in_dquote) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6568 | /* dest.o_quoted = 1; - already is (see below) */ |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 6569 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6570 | goto parse_error; |
| 6571 | } |
| 6572 | /* We reached closing '"' */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6573 | is_in_dquote = 0; |
| 6574 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6575 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6576 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
| 6577 | ch, ch, dest.o_escape); |
| 6578 | if (ch == EOF) { |
| 6579 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6580 | |
| 6581 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6582 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6583 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6584 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6585 | /* end_trigger == '}' case errors out earlier, |
| 6586 | * checking only ')' */ |
| 6587 | if (end_trigger == ')') { |
| 6588 | syntax_error_unterm_ch('('); /* exits */ |
| 6589 | /* goto parse_error; */ |
| 6590 | } |
| 6591 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6592 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 6593 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6594 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6595 | o_free(&dest); |
| 6596 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6597 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6598 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6599 | /* (this makes bare "&" cmd a no-op. |
| 6600 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6601 | if (pi->num_cmds == 0 |
| 6602 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 6603 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6604 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6605 | pi = NULL; |
| 6606 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6607 | #if !BB_MMU |
| 6608 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6609 | if (pstring) |
| 6610 | *pstring = ctx.as_string.data; |
| 6611 | else |
| 6612 | o_free_unsafe(&ctx.as_string); |
| 6613 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6614 | debug_leave(); |
| 6615 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6616 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 6617 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6618 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6619 | |
| 6620 | next = '\0'; |
| 6621 | if (ch != '\n') |
| 6622 | next = i_peek(input); |
| 6623 | |
| 6624 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 6625 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 6626 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 6627 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 6628 | || dest.length /* word{... - non-special */ |
| 6629 | || dest.o_quoted /* ""{... - non-special */ |
| 6630 | || (next != ';' /* }; - special */ |
| 6631 | && next != ')' /* }) - special */ |
| 6632 | && next != '&' /* }& and }&& ... - special */ |
| 6633 | && next != '|' /* }|| ... - special */ |
| 6634 | && !strchr(G.ifs, next) /* {word - non-special */ |
| 6635 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 6636 | ) { |
| 6637 | /* They are not special, skip "{}" */ |
| 6638 | is_special += 2; |
| 6639 | } |
| 6640 | is_special = strchr(is_special, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6641 | is_ifs = strchr(G.ifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6642 | |
| 6643 | if (!is_special && !is_ifs) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 6644 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6645 | o_addQchr(&dest, ch); |
| 6646 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 6647 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6648 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6649 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6650 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6651 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6652 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6653 | continue; |
| 6654 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6655 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6656 | if (is_ifs) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6657 | if (done_word(&dest, &ctx)) { |
| 6658 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 6659 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6660 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6661 | #if ENABLE_HUSH_CASE |
| 6662 | /* "case ... in <newline> word) ..." - |
| 6663 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6664 | if (ctx.command->argv == NULL |
| 6665 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6666 | ) { |
| 6667 | continue; |
| 6668 | } |
| 6669 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6670 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6671 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6672 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 6673 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6674 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6675 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 6676 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6677 | heredoc_cnt = 0; |
| 6678 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6679 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6680 | ch = ';'; |
Denis Vlasenko | 7c98612 | 2009-04-04 12:15:42 +0000 | [diff] [blame] | 6681 | /* note: if (is_ifs) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6682 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6683 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6684 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6685 | |
| 6686 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 6687 | * } is an ordinary char in this case, even inside { cmd; } |
| 6688 | * Pathological example: { ""}; } should exec "}" cmd |
| 6689 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6690 | if (ch == '}') { |
| 6691 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 6692 | || dest.length != 0 /* word} */ |
| 6693 | || dest.o_quoted /* ""} */ |
| 6694 | ) { |
| 6695 | goto ordinary_char; |
| 6696 | } |
| 6697 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 6698 | goto skip_end_trigger; |
| 6699 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 6700 | } |
| 6701 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6702 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6703 | && (ch != ';' || heredoc_cnt == 0) |
| 6704 | #if ENABLE_HUSH_CASE |
| 6705 | && (ch != ')' |
| 6706 | || ctx.ctx_res_w != RES_MATCH |
| 6707 | || (!dest.o_quoted && strcmp(dest.data, "esac") == 0) |
| 6708 | ) |
| 6709 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6710 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6711 | if (heredoc_cnt) { |
| 6712 | /* This is technically valid: |
| 6713 | * { cat <<HERE; }; echo Ok |
| 6714 | * heredoc |
| 6715 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6716 | * HERE |
| 6717 | * but we don't support this. |
| 6718 | * We require heredoc to be in enclosing {}/(), |
| 6719 | * if any. |
| 6720 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6721 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 6722 | goto parse_error; |
| 6723 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6724 | if (done_word(&dest, &ctx)) { |
| 6725 | goto parse_error; |
| 6726 | } |
| 6727 | done_pipe(&ctx, PIPE_SEQ); |
| 6728 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 6729 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 6730 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6731 | 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] | 6732 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6733 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 6734 | #if !BB_MMU |
| 6735 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 6736 | if (pstring) |
| 6737 | *pstring = ctx.as_string.data; |
| 6738 | else |
| 6739 | o_free_unsafe(&ctx.as_string); |
| 6740 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 6741 | debug_leave(); |
| 6742 | debug_printf_parse("parse_stream return %p: " |
| 6743 | "end_trigger char found\n", |
| 6744 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6745 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6746 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6747 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 6748 | skip_end_trigger: |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 6749 | if (is_ifs) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6750 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 6751 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 6752 | /* Catch <, > before deciding whether this word is |
| 6753 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 6754 | switch (ch) { |
| 6755 | case '>': |
| 6756 | redir_fd = redirect_opt_num(&dest); |
| 6757 | if (done_word(&dest, &ctx)) { |
| 6758 | goto parse_error; |
| 6759 | } |
| 6760 | redir_style = REDIRECT_OVERWRITE; |
| 6761 | if (next == '>') { |
| 6762 | redir_style = REDIRECT_APPEND; |
| 6763 | ch = i_getch(input); |
| 6764 | nommu_addchr(&ctx.as_string, ch); |
| 6765 | } |
| 6766 | #if 0 |
| 6767 | else if (next == '(') { |
| 6768 | syntax_error(">(process) not supported"); |
| 6769 | goto parse_error; |
| 6770 | } |
| 6771 | #endif |
| 6772 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6773 | goto parse_error; |
| 6774 | continue; /* back to top of while (1) */ |
| 6775 | case '<': |
| 6776 | redir_fd = redirect_opt_num(&dest); |
| 6777 | if (done_word(&dest, &ctx)) { |
| 6778 | goto parse_error; |
| 6779 | } |
| 6780 | redir_style = REDIRECT_INPUT; |
| 6781 | if (next == '<') { |
| 6782 | redir_style = REDIRECT_HEREDOC; |
| 6783 | heredoc_cnt++; |
| 6784 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 6785 | ch = i_getch(input); |
| 6786 | nommu_addchr(&ctx.as_string, ch); |
| 6787 | } else if (next == '>') { |
| 6788 | redir_style = REDIRECT_IO; |
| 6789 | ch = i_getch(input); |
| 6790 | nommu_addchr(&ctx.as_string, ch); |
| 6791 | } |
| 6792 | #if 0 |
| 6793 | else if (next == '(') { |
| 6794 | syntax_error("<(process) not supported"); |
| 6795 | goto parse_error; |
| 6796 | } |
| 6797 | #endif |
| 6798 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 6799 | goto parse_error; |
| 6800 | continue; /* back to top of while (1) */ |
| 6801 | } |
| 6802 | |
| 6803 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 6804 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 6805 | && !ctx.pending_redirect |
| 6806 | ) { |
| 6807 | /* ch is a special char and thus this word |
| 6808 | * cannot be an assignment */ |
| 6809 | dest.o_assignment = NOT_ASSIGNMENT; |
| 6810 | } |
| 6811 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6812 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 6813 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6814 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6815 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6816 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6817 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6818 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6819 | if (ch == EOF || ch == '\n') |
| 6820 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6821 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6822 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6823 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6824 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6825 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6826 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6827 | } |
| 6828 | break; |
| 6829 | case '\\': |
| 6830 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 6831 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6832 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6833 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6834 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6835 | if (ch != '\n') { |
| 6836 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6837 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6838 | o_addchr(&dest, ch); |
| 6839 | nommu_addchr(&ctx.as_string, ch); |
| 6840 | /* Example: echo Hello \2>file |
| 6841 | * we need to know that word 2 is quoted */ |
| 6842 | dest.o_quoted = 1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6843 | } |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6844 | #if !BB_MMU |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 6845 | else { |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 6846 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 6847 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 6848 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 6849 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6850 | break; |
| 6851 | case '$': |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6852 | if (parse_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 6853 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6854 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6855 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6856 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6857 | break; |
| 6858 | case '\'': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6859 | dest.o_quoted = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6860 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 6861 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6862 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 6863 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6864 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6865 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6866 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6867 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 6868 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 6869 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6870 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6871 | break; |
| 6872 | case '"': |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6873 | dest.o_quoted = 1; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 6874 | is_in_dquote ^= 1; /* invert */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6875 | if (dest.o_assignment == NOT_ASSIGNMENT) |
| 6876 | dest.o_escape ^= 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6877 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6878 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6879 | case '`': { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6880 | unsigned pos; |
| 6881 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6882 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6883 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6884 | pos = dest.length; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 6885 | add_till_backquote(&dest, input); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6886 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 6887 | o_addstr(&ctx.as_string, dest.data + pos); |
| 6888 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 6889 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6890 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 6891 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6892 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 6893 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 6894 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6895 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6896 | #if ENABLE_HUSH_CASE |
| 6897 | case_semi: |
| 6898 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6899 | if (done_word(&dest, &ctx)) { |
| 6900 | goto parse_error; |
| 6901 | } |
| 6902 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6903 | #if ENABLE_HUSH_CASE |
| 6904 | /* Eat multiple semicolons, detect |
| 6905 | * whether it means something special */ |
| 6906 | while (1) { |
| 6907 | ch = i_peek(input); |
| 6908 | if (ch != ';') |
| 6909 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6910 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6911 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 6912 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6913 | ctx.ctx_dsemicolon = 1; |
| 6914 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6915 | break; |
| 6916 | } |
| 6917 | } |
| 6918 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6919 | new_cmd: |
| 6920 | /* We just finished a cmd. New one may start |
| 6921 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6922 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6923 | break; |
| 6924 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6925 | if (done_word(&dest, &ctx)) { |
| 6926 | goto parse_error; |
| 6927 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 6928 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6929 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6930 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6931 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6932 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6933 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6934 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6935 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6936 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6937 | if (done_word(&dest, &ctx)) { |
| 6938 | goto parse_error; |
| 6939 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6940 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6941 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6942 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 6943 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6944 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 6945 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 6946 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6947 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6948 | } else { |
| 6949 | /* we could pick up a file descriptor choice here |
| 6950 | * with redirect_opt_num(), but bash doesn't do it. |
| 6951 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6952 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 6953 | #if !BB_MMU |
| 6954 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 6955 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6956 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6957 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6958 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6959 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 6960 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6961 | if (ctx.ctx_res_w == RES_MATCH |
| 6962 | && ctx.command->argv == NULL /* not (word|(... */ |
| 6963 | && dest.length == 0 /* not word(... */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 6964 | && dest.o_quoted == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6965 | ) { |
| 6966 | continue; |
| 6967 | } |
| 6968 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6969 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6970 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 6971 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 6972 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 6973 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6974 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6975 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6976 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 6977 | goto case_semi; |
| 6978 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6979 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 6980 | /* proper use of this character is caught by end_trigger: |
| 6981 | * if we see {, we call parse_group(..., end_trigger='}') |
| 6982 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 6983 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6984 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6985 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 6986 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 6987 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 6988 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 6989 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 6990 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6991 | parse_error: |
| 6992 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 6993 | struct parse_context *pctx; |
| 6994 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6995 | |
| 6996 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 6997 | * Sample for finding leaks on syntax error recovery path. |
| 6998 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 6999 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 7000 | * Samples to catch leaks at execution: |
| 7001 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 7002 | * 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] | 7003 | */ |
| 7004 | pctx = &ctx; |
| 7005 | do { |
| 7006 | /* Update pipe/command counts, |
| 7007 | * otherwise freeing may miss some */ |
| 7008 | done_pipe(pctx, PIPE_SEQ); |
| 7009 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 7010 | pctx->list_head, pctx); |
| 7011 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 7012 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7013 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7014 | #if !BB_MMU |
| 7015 | o_free_unsafe(&pctx->as_string); |
| 7016 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7017 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7018 | if (pctx != &ctx) { |
| 7019 | free(pctx); |
| 7020 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7021 | IF_HAS_KEYWORDS(pctx = p2;) |
| 7022 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7023 | /* Free text, clear all dest fields */ |
| 7024 | o_free(&dest); |
| 7025 | /* If we are not in top-level parse, we return, |
| 7026 | * our caller will propagate error. |
| 7027 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7028 | if (end_trigger != ';') { |
| 7029 | #if !BB_MMU |
| 7030 | if (pstring) |
| 7031 | *pstring = NULL; |
| 7032 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 7033 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7034 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7035 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7036 | /* Discard cached input, force prompt */ |
| 7037 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 7038 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7039 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 7040 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7043 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7044 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 7045 | * end_trigger controls how often we stop parsing |
| 7046 | * NUL: parse all, execute, return |
| 7047 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 7048 | */ |
| 7049 | 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] | 7050 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7051 | /* Why we need empty flag? |
| 7052 | * An obscure corner case "false; ``; echo $?": |
| 7053 | * empty command in `` should still set $? to 0. |
| 7054 | * But we can't just set $? to 0 at the start, |
| 7055 | * this breaks "false; echo `echo $?`" case. |
| 7056 | */ |
| 7057 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7058 | while (1) { |
| 7059 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 7060 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 7061 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7062 | if (!pipe_list) { /* EOF */ |
| 7063 | if (empty) |
| 7064 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7065 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7066 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7067 | debug_print_tree(pipe_list, 0); |
| 7068 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 7069 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 7070 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7071 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7074 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7075 | { |
| 7076 | struct in_str input; |
| 7077 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7078 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7079 | } |
| 7080 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7081 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7082 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7083 | struct in_str input; |
| 7084 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7085 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7086 | } |
| 7087 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7088 | /* Called a few times only (or even once if "sh -c") */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7089 | static void init_sigmasks(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7090 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7091 | unsigned sig; |
| 7092 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7093 | sigset_t old_blocked_set; |
| 7094 | |
| 7095 | if (!G.inherited_set_is_saved) { |
| 7096 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 7097 | G.inherited_set = G.blocked_set; |
| 7098 | } |
| 7099 | old_blocked_set = G.blocked_set; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7100 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7101 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7102 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 7103 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7104 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7105 | mask |= SPECIAL_JOB_SIGS; |
| 7106 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7107 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7108 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7109 | sig = 0; |
| 7110 | while (mask) { |
| 7111 | if (mask & 1) |
| 7112 | sigaddset(&G.blocked_set, sig); |
| 7113 | mask >>= 1; |
| 7114 | sig++; |
| 7115 | } |
| 7116 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7117 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7118 | if (memcmp(&old_blocked_set, &G.blocked_set, sizeof(old_blocked_set)) != 0) |
| 7119 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7120 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7121 | /* POSIX allows shell to re-enable SIGCHLD |
| 7122 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7123 | #if ENABLE_HUSH_FAST |
| 7124 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7125 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7126 | signal(SIGCHLD, SIGCHLD_handler); |
| 7127 | #else |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7128 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7129 | signal(SIGCHLD, SIG_DFL); |
| 7130 | #endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7131 | |
| 7132 | G.inherited_set_is_saved = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | #if ENABLE_HUSH_JOB |
| 7136 | /* helper */ |
| 7137 | static void maybe_set_to_sigexit(int sig) |
| 7138 | { |
| 7139 | void (*handler)(int); |
| 7140 | /* non_DFL_mask'ed signals are, well, masked, |
| 7141 | * no need to set handler for them. |
| 7142 | */ |
| 7143 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 7144 | handler = signal(sig, sigexit); |
| 7145 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 7146 | signal(sig, handler); |
| 7147 | } |
| 7148 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7149 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7150 | static void set_fatal_handlers(void) |
| 7151 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 7152 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7153 | if (HUSH_DEBUG) { |
| 7154 | maybe_set_to_sigexit(SIGILL ); |
| 7155 | maybe_set_to_sigexit(SIGFPE ); |
| 7156 | maybe_set_to_sigexit(SIGBUS ); |
| 7157 | maybe_set_to_sigexit(SIGSEGV); |
| 7158 | maybe_set_to_sigexit(SIGTRAP); |
| 7159 | } /* else: hush is perfect. what SEGV? */ |
| 7160 | maybe_set_to_sigexit(SIGABRT); |
| 7161 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7162 | maybe_set_to_sigexit(SIGPIPE); |
| 7163 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7164 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7165 | * if we aren't interactive... but in this case |
| 7166 | * 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] | 7167 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7168 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 7169 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 7170 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7171 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7172 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7173 | static int set_mode(const char cstate, const char mode) |
| 7174 | { |
| 7175 | int state = (cstate == '-' ? 1 : 0); |
| 7176 | switch (mode) { |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 7177 | case 'n': G.n_mode = state; break; |
| 7178 | case 'x': IF_HUSH_MODE_X(G_x_mode = state;) break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7179 | default: return EXIT_FAILURE; |
| 7180 | } |
| 7181 | return EXIT_SUCCESS; |
| 7182 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7183 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7184 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7185 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7186 | { |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7187 | static const struct variable const_shell_ver = { |
| 7188 | .next = NULL, |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 7189 | .varstr = (char*)hush_version_str, |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7190 | .max_len = 1, /* 0 can provoke free(name) */ |
| 7191 | .flg_export = 1, |
| 7192 | .flg_read_only = 1, |
| 7193 | }; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7194 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7195 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7196 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7197 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7198 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7199 | INIT_G(); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 7200 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7201 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7202 | #if !BB_MMU |
| 7203 | G.argv0_for_re_execing = argv[0]; |
| 7204 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7205 | /* Deal with HUSH_VERSION */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7206 | G.shell_ver = const_shell_ver; /* copying struct here */ |
| 7207 | G.top_var = &G.shell_ver; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7208 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7209 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
| 7210 | /* Initialize our shell local variables with the values |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7211 | * currently living in the environment */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7212 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7213 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7214 | if (e) while (*e) { |
| 7215 | char *value = strchr(*e, '='); |
| 7216 | if (value) { /* paranoia */ |
| 7217 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7218 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7219 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7220 | cur_var->max_len = strlen(*e); |
| 7221 | cur_var->flg_export = 1; |
| 7222 | } |
| 7223 | e++; |
| 7224 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7225 | /* reinstate HUSH_VERSION */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7226 | debug_printf_env("putenv '%s'\n", hush_version_str); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7227 | putenv((char *)hush_version_str); |
| 7228 | |
| 7229 | /* Export PWD */ |
| 7230 | set_pwd_var(/*exp:*/ 1); |
| 7231 | /* bash also exports SHLVL and _, |
| 7232 | * and sets (but doesn't export) the following variables: |
| 7233 | * BASH=/bin/bash |
| 7234 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7235 | * BASH_VERSION='3.2.0(1)-release' |
| 7236 | * HOSTTYPE=i386 |
| 7237 | * MACHTYPE=i386-pc-linux-gnu |
| 7238 | * OSTYPE=linux-gnu |
| 7239 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7240 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7241 | * EUID=<NNNNN> |
| 7242 | * UID=<NNNNN> |
| 7243 | * GROUPS=() |
| 7244 | * LINES=<NNN> |
| 7245 | * COLUMNS=<NNN> |
| 7246 | * BASH_ARGC=() |
| 7247 | * BASH_ARGV=() |
| 7248 | * BASH_LINENO=() |
| 7249 | * BASH_SOURCE=() |
| 7250 | * DIRSTACK=() |
| 7251 | * PIPESTATUS=([0]="0") |
| 7252 | * HISTFILE=/<xxx>/.bash_history |
| 7253 | * HISTFILESIZE=500 |
| 7254 | * HISTSIZE=500 |
| 7255 | * MAILCHECK=60 |
| 7256 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7257 | * SHELL=/bin/bash |
| 7258 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7259 | * TERM=dumb |
| 7260 | * OPTERR=1 |
| 7261 | * OPTIND=1 |
| 7262 | * IFS=$' \t\n' |
| 7263 | * PS1='\s-\v\$ ' |
| 7264 | * PS2='> ' |
| 7265 | * PS4='+ ' |
| 7266 | */ |
| 7267 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7268 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7269 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7270 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7271 | G.global_argc = argc; |
| 7272 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7273 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7274 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7275 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7276 | if (setjmp(die_jmp)) { |
| 7277 | /* xfunc has failed! die die die */ |
| 7278 | /* no EXIT traps, this is an escape hatch! */ |
| 7279 | G.exiting = 1; |
| 7280 | hush_exit(xfunc_error_retval); |
| 7281 | } |
| 7282 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7283 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7284 | * init_sigmasks() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7285 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7286 | * If we later decide that we are interactive, we run init_sigmasks() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7287 | * in order to intercept (more) signals. |
| 7288 | */ |
| 7289 | |
| 7290 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7291 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7292 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7293 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 7294 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7295 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7296 | "<:$:R:V:" |
| 7297 | # if ENABLE_HUSH_FUNCTIONS |
| 7298 | "F:" |
| 7299 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7300 | #endif |
| 7301 | ); |
| 7302 | if (opt <= 0) |
| 7303 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7304 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7305 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7306 | /* Possibilities: |
| 7307 | * sh ... -c 'script' |
| 7308 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7309 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7310 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7311 | * "" needs to be replaced with NULL |
| 7312 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7313 | * Note: the form without ARG0 never happens: |
| 7314 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7315 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7316 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7317 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7318 | G.root_ppid = getppid(); |
| 7319 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7320 | G.global_argv = argv + optind; |
| 7321 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7322 | if (builtin_argc) { |
| 7323 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7324 | const struct built_in_command *x; |
| 7325 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7326 | init_sigmasks(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7327 | x = find_builtin(optarg); |
| 7328 | if (x) { /* paranoia */ |
| 7329 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7330 | G.global_argv += builtin_argc; |
| 7331 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7332 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7333 | } |
| 7334 | goto final_return; |
| 7335 | } |
| 7336 | if (!G.global_argv[0]) { |
| 7337 | /* -c 'script' (no params): prevent empty $0 */ |
| 7338 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7339 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7340 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7341 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7342 | init_sigmasks(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7343 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7344 | goto final_return; |
| 7345 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7346 | /* Well, we cannot just declare interactiveness, |
| 7347 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7348 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7349 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7350 | case 's': |
| 7351 | /* "-s" means "read from stdin", but this is how we always |
| 7352 | * operate, so simply do nothing here. */ |
| 7353 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7354 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7355 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7356 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7357 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7358 | case '$': { |
| 7359 | unsigned long long empty_trap_mask; |
| 7360 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7361 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7362 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7363 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7364 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7365 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7366 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7367 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7368 | optarg++; |
| 7369 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7370 | optarg++; |
| 7371 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7372 | if (empty_trap_mask != 0) { |
| 7373 | int sig; |
| 7374 | init_sigmasks(); |
| 7375 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7376 | for (sig = 1; sig < NSIG; sig++) { |
| 7377 | if (empty_trap_mask & (1LL << sig)) { |
| 7378 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7379 | sigaddset(&G.blocked_set, sig); |
| 7380 | } |
| 7381 | } |
| 7382 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7383 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7384 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7385 | optarg++; |
| 7386 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7387 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7388 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7389 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7390 | case 'R': |
| 7391 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7392 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7393 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7394 | # if ENABLE_HUSH_FUNCTIONS |
| 7395 | case 'F': { |
| 7396 | struct function *funcp = new_function(optarg); |
| 7397 | /* funcp->name is already set to optarg */ |
| 7398 | /* funcp->body is set to NULL. It's a special case. */ |
| 7399 | funcp->body_as_string = argv[optind]; |
| 7400 | optind++; |
| 7401 | break; |
| 7402 | } |
| 7403 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7404 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7405 | case 'n': |
| 7406 | case 'x': |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 7407 | if (set_mode('-', opt) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7408 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7409 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7410 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7411 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7412 | " or: sh -c command [args]...\n\n"); |
| 7413 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7414 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7415 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7416 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7417 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7418 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7419 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7420 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7421 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7422 | G.root_ppid = getppid(); |
| 7423 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7424 | |
| 7425 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7426 | if (argv[0] && argv[0][0] == '-') { |
| 7427 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7428 | debug_printf("sourcing /etc/profile\n"); |
| 7429 | input = fopen_for_read("/etc/profile"); |
| 7430 | if (input != NULL) { |
| 7431 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7432 | init_sigmasks(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7433 | parse_and_run_file(input); |
| 7434 | fclose(input); |
| 7435 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7436 | /* bash: after sourcing /etc/profile, |
| 7437 | * tries to source (in the given order): |
| 7438 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7439 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7440 | * bash also sources ~/.bash_logout on exit. |
| 7441 | * If called as sh, skips .bash_XXX files. |
| 7442 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7443 | } |
| 7444 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7445 | if (argv[optind]) { |
| 7446 | FILE *input; |
| 7447 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7448 | * "bash <script>" (which is never interactive (unless -i?)) |
| 7449 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7450 | * If called as sh, does the same but with $ENV. |
| 7451 | */ |
| 7452 | debug_printf("running script '%s'\n", argv[optind]); |
| 7453 | G.global_argv = argv + optind; |
| 7454 | G.global_argc = argc - optind; |
| 7455 | input = xfopen_for_read(argv[optind]); |
| 7456 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7457 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7458 | parse_and_run_file(input); |
| 7459 | #if ENABLE_FEATURE_CLEAN_UP |
| 7460 | fclose(input); |
| 7461 | #endif |
| 7462 | goto final_return; |
| 7463 | } |
| 7464 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7465 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7466 | * NB: don't forget to (re)run init_sigmasks() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7467 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7468 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7469 | /* A shell is interactive if the '-i' flag was given, |
| 7470 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 7471 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7472 | * no arguments remaining or the -s flag given |
| 7473 | * standard input is a terminal |
| 7474 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7475 | * Refer to Posix.2, the description of the 'sh' utility. |
| 7476 | */ |
| 7477 | #if ENABLE_HUSH_JOB |
| 7478 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7479 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 7480 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 7481 | if (G_saved_tty_pgrp < 0) |
| 7482 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7483 | |
| 7484 | /* try to dup stdin to high fd#, >= 255 */ |
| 7485 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7486 | if (G_interactive_fd < 0) { |
| 7487 | /* try to dup to any fd */ |
| 7488 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7489 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7490 | /* give up */ |
| 7491 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7492 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7493 | } |
| 7494 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7495 | // TODO: track & disallow any attempts of user |
| 7496 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7497 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7498 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7499 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7500 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7501 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7502 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7503 | /* If we were run as 'hush &', sleep until we are |
| 7504 | * in the foreground (tty pgrp == our pgrp). |
| 7505 | * If we get started under a job aware app (like bash), |
| 7506 | * make sure we are now in charge so we don't fight over |
| 7507 | * who gets the foreground */ |
| 7508 | while (1) { |
| 7509 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7510 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 7511 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7512 | break; |
| 7513 | /* send TTIN to ourself (should stop us) */ |
| 7514 | kill(- shell_pgrp, SIGTTIN); |
| 7515 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7516 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7517 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7518 | /* Block some signals */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7519 | init_sigmasks(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7520 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7521 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7522 | /* Set other signals to restore saved_tty_pgrp */ |
| 7523 | set_fatal_handlers(); |
| 7524 | /* Put ourselves in our own process group |
| 7525 | * (bash, too, does this only if ctty is available) */ |
| 7526 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 7527 | /* Grab control of the terminal */ |
| 7528 | tcsetpgrp(G_interactive_fd, getpid()); |
| 7529 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 7530 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 7531 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 7532 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7533 | } else { |
| 7534 | init_sigmasks(); |
| 7535 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7536 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7537 | /* No job control compiled in, only prompt/line editing */ |
| 7538 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7539 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7540 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7541 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7542 | G_interactive_fd = dup(STDIN_FILENO); |
| 7543 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7544 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7545 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7546 | } |
| 7547 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7548 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7549 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7550 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7551 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7552 | #else |
| 7553 | /* We have interactiveness code disabled */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7554 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7555 | #endif |
| 7556 | /* bash: |
| 7557 | * if interactive but not a login shell, sources ~/.bashrc |
| 7558 | * (--norc turns this off, --rcfile <file> overrides) |
| 7559 | */ |
| 7560 | |
| 7561 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7562 | /* note: ash and hush share this string */ |
| 7563 | printf("\n\n%s %s\n" |
| 7564 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7565 | "\n", |
| 7566 | bb_banner, |
| 7567 | "hush - the humble shell" |
| 7568 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7569 | } |
| 7570 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7571 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7572 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7573 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7574 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7575 | if (G.cwd != bb_msg_unknown) |
| 7576 | free((char*)G.cwd); |
| 7577 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7578 | while (cur_var) { |
| 7579 | struct variable *tmp = cur_var; |
| 7580 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7581 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7582 | cur_var = cur_var->next; |
| 7583 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 7584 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7585 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7586 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7587 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7588 | |
| 7589 | |
| 7590 | #if ENABLE_LASH |
| 7591 | int lash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7592 | int lash_main(int argc, char **argv) |
| 7593 | { |
Denys Vlasenko | 0793491 | 2009-08-20 03:40:04 +0200 | [diff] [blame] | 7594 | bb_error_msg("lash is deprecated, please use hush instead"); |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7595 | return hush_main(argc, argv); |
| 7596 | } |
| 7597 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7598 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7599 | #if ENABLE_MSH |
| 7600 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7601 | int msh_main(int argc, char **argv) |
| 7602 | { |
| 7603 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7604 | return hush_main(argc, argv); |
| 7605 | } |
| 7606 | #endif |
| 7607 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7608 | |
| 7609 | /* |
| 7610 | * Built-ins |
| 7611 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7612 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7613 | { |
| 7614 | return 0; |
| 7615 | } |
| 7616 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7617 | 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] | 7618 | { |
| 7619 | int argc = 0; |
| 7620 | while (*argv) { |
| 7621 | argc++; |
| 7622 | argv++; |
| 7623 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7624 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7625 | } |
| 7626 | |
| 7627 | static int FAST_FUNC builtin_test(char **argv) |
| 7628 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7629 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7630 | } |
| 7631 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7632 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7633 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7634 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7635 | } |
| 7636 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7637 | #if ENABLE_PRINTF |
| 7638 | static int FAST_FUNC builtin_printf(char **argv) |
| 7639 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7640 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7641 | } |
| 7642 | #endif |
| 7643 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7644 | static char **skip_dash_dash(char **argv) |
| 7645 | { |
| 7646 | argv++; |
| 7647 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 7648 | argv++; |
| 7649 | return argv; |
| 7650 | } |
| 7651 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7652 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7653 | { |
| 7654 | int rcode = EXIT_SUCCESS; |
| 7655 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7656 | argv = skip_dash_dash(argv); |
| 7657 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7658 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7659 | /* bash: |
| 7660 | * eval "echo Hi; done" ("done" is syntax error): |
| 7661 | * "echo Hi" will not execute too. |
| 7662 | */ |
| 7663 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7664 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7665 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7666 | } |
| 7667 | return rcode; |
| 7668 | } |
| 7669 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7670 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7671 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7672 | const char *newdir; |
| 7673 | |
| 7674 | argv = skip_dash_dash(argv); |
| 7675 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7676 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7677 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7678 | * bash says "bash: cd: HOME not set" and does nothing |
| 7679 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7680 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7681 | const char *home = get_local_var_value("HOME"); |
| 7682 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7683 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7684 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7685 | /* Mimic bash message exactly */ |
| 7686 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7687 | return EXIT_FAILURE; |
| 7688 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7689 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7690 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7691 | * set it again, but do not export. bash does the same. |
| 7692 | */ |
| 7693 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7694 | return EXIT_SUCCESS; |
| 7695 | } |
| 7696 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7697 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7698 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7699 | argv = skip_dash_dash(argv); |
| 7700 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7701 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7702 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7703 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7704 | * tty pgrp then, only top-level shell process does that */ |
| 7705 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7706 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7707 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7708 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 7709 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 7710 | * and tcsetpgrp, and this is inherently racy. |
| 7711 | */ |
| 7712 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7713 | } |
| 7714 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7715 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7716 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 7717 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7718 | |
| 7719 | /* interactive bash: |
| 7720 | * # trap "echo EEE" EXIT |
| 7721 | * # exit |
| 7722 | * exit |
| 7723 | * There are stopped jobs. |
| 7724 | * (if there are _stopped_ jobs, running ones don't count) |
| 7725 | * # exit |
| 7726 | * exit |
| 7727 | # EEE (then bash exits) |
| 7728 | * |
| 7729 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 7730 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 7731 | |
| 7732 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7733 | argv = skip_dash_dash(argv); |
| 7734 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7735 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7736 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 7737 | xfunc_error_retval = 255; |
| 7738 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7739 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7740 | } |
| 7741 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7742 | static void print_escaped(const char *s) |
| 7743 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7744 | if (*s == '\'') |
| 7745 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7746 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7747 | const char *p = strchrnul(s, '\''); |
| 7748 | /* print 'xxxx', possibly just '' */ |
| 7749 | printf("'%.*s'", (int)(p - s), s); |
| 7750 | if (*p == '\0') |
| 7751 | break; |
| 7752 | s = p; |
| 7753 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7754 | /* s points to '; print "'''...'''" */ |
| 7755 | putchar('"'); |
| 7756 | do putchar('\''); while (*++s == '\''); |
| 7757 | putchar('"'); |
| 7758 | } while (*s); |
| 7759 | } |
| 7760 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7761 | #if !ENABLE_HUSH_LOCAL |
| 7762 | #define helper_export_local(argv, exp, lvl) \ |
| 7763 | helper_export_local(argv, exp) |
| 7764 | #endif |
| 7765 | static void helper_export_local(char **argv, int exp, int lvl) |
| 7766 | { |
| 7767 | do { |
| 7768 | char *name = *argv; |
| 7769 | |
| 7770 | /* So far we do not check that name is valid (TODO?) */ |
| 7771 | |
| 7772 | if (strchr(name, '=') == NULL) { |
| 7773 | struct variable *var; |
| 7774 | |
| 7775 | var = get_local_var(name); |
| 7776 | if (exp == -1) { /* unexporting? */ |
| 7777 | /* export -n NAME (without =VALUE) */ |
| 7778 | if (var) { |
| 7779 | var->flg_export = 0; |
| 7780 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 7781 | unsetenv(name); |
| 7782 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 7783 | continue; |
| 7784 | } |
| 7785 | if (exp == 1) { /* exporting? */ |
| 7786 | /* export NAME (without =VALUE) */ |
| 7787 | if (var) { |
| 7788 | var->flg_export = 1; |
| 7789 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 7790 | putenv(var->varstr); |
| 7791 | continue; |
| 7792 | } |
| 7793 | } |
| 7794 | /* Exporting non-existing variable. |
| 7795 | * bash does not put it in environment, |
| 7796 | * but remembers that it is exported, |
| 7797 | * and does put it in env when it is set later. |
| 7798 | * We just set it to "" and export. */ |
| 7799 | /* Or, it's "local NAME" (without =VALUE). |
| 7800 | * bash sets the value to "". */ |
| 7801 | name = xasprintf("%s=", name); |
| 7802 | } else { |
| 7803 | /* (Un)exporting/making local NAME=VALUE */ |
| 7804 | name = xstrdup(name); |
| 7805 | } |
| 7806 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 7807 | } while (*++argv); |
| 7808 | } |
| 7809 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7810 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7811 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 7812 | unsigned opt_unexport; |
| 7813 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 7814 | #if ENABLE_HUSH_EXPORT_N |
| 7815 | /* "!": do not abort on errors */ |
| 7816 | opt_unexport = getopt32(argv, "!n"); |
| 7817 | if (opt_unexport == (uint32_t)-1) |
| 7818 | return EXIT_FAILURE; |
| 7819 | argv += optind; |
| 7820 | #else |
| 7821 | opt_unexport = 0; |
| 7822 | argv++; |
| 7823 | #endif |
| 7824 | |
| 7825 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7826 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7827 | if (e) { |
| 7828 | while (*e) { |
| 7829 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7830 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7831 | #else |
| 7832 | /* ash emits: export VAR='VAL' |
| 7833 | * bash: declare -x VAR="VAL" |
| 7834 | * we follow ash example */ |
| 7835 | const char *s = *e++; |
| 7836 | const char *p = strchr(s, '='); |
| 7837 | |
| 7838 | if (!p) /* wtf? take next variable */ |
| 7839 | continue; |
| 7840 | /* export var= */ |
| 7841 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7842 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7843 | putchar('\n'); |
| 7844 | #endif |
| 7845 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7846 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7847 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7848 | return EXIT_SUCCESS; |
| 7849 | } |
| 7850 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7851 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7852 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7853 | return EXIT_SUCCESS; |
| 7854 | } |
| 7855 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7856 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7857 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7858 | { |
| 7859 | if (G.func_nest_level == 0) { |
| 7860 | bb_error_msg("%s: not in a function", argv[0]); |
| 7861 | return EXIT_FAILURE; /* bash compat */ |
| 7862 | } |
| 7863 | helper_export_local(argv, 0, G.func_nest_level); |
| 7864 | return EXIT_SUCCESS; |
| 7865 | } |
| 7866 | #endif |
| 7867 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7868 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7869 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7870 | int sig; |
| 7871 | char *new_cmd; |
| 7872 | |
| 7873 | if (!G.traps) |
| 7874 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7875 | |
| 7876 | argv++; |
| 7877 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7878 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7879 | /* No args: print all trapped */ |
| 7880 | for (i = 0; i < NSIG; ++i) { |
| 7881 | if (G.traps[i]) { |
| 7882 | printf("trap -- "); |
| 7883 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 7884 | /* note: bash adds "SIG", but only if invoked |
| 7885 | * as "bash". If called as "sh", or if set -o posix, |
| 7886 | * then it prints short signal names. |
| 7887 | * We are printing short names: */ |
| 7888 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7889 | } |
| 7890 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7891 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7892 | return EXIT_SUCCESS; |
| 7893 | } |
| 7894 | |
| 7895 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7896 | /* If first arg is a number: reset all specified signals */ |
| 7897 | sig = bb_strtou(*argv, NULL, 10); |
| 7898 | if (errno == 0) { |
| 7899 | int ret; |
| 7900 | process_sig_list: |
| 7901 | ret = EXIT_SUCCESS; |
| 7902 | while (*argv) { |
| 7903 | sig = get_signum(*argv++); |
| 7904 | if (sig < 0 || sig >= NSIG) { |
| 7905 | ret = EXIT_FAILURE; |
| 7906 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7907 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7908 | continue; |
| 7909 | } |
| 7910 | |
| 7911 | free(G.traps[sig]); |
| 7912 | G.traps[sig] = xstrdup(new_cmd); |
| 7913 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7914 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7915 | get_signame(sig), sig, G.traps[sig]); |
| 7916 | |
| 7917 | /* There is no signal for 0 (EXIT) */ |
| 7918 | if (sig == 0) |
| 7919 | continue; |
| 7920 | |
| 7921 | if (new_cmd) { |
| 7922 | sigaddset(&G.blocked_set, sig); |
| 7923 | } else { |
| 7924 | /* There was a trap handler, we are removing it |
| 7925 | * (if sig has non-DFL handling, |
| 7926 | * we don't need to do anything) */ |
| 7927 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 7928 | continue; |
| 7929 | sigdelset(&G.blocked_set, sig); |
| 7930 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7931 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7932 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7933 | return ret; |
| 7934 | } |
| 7935 | |
| 7936 | if (!argv[1]) { /* no second arg */ |
| 7937 | bb_error_msg("trap: invalid arguments"); |
| 7938 | return EXIT_FAILURE; |
| 7939 | } |
| 7940 | |
| 7941 | /* First arg is "-": reset all specified to default */ |
| 7942 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 7943 | /* Everything else: set arg as signal handler |
| 7944 | * (includes "" case, which ignores signal) */ |
| 7945 | if (argv[0][0] == '-') { |
| 7946 | if (argv[0][1] == '\0') { /* "-" */ |
| 7947 | /* new_cmd remains NULL: "reset these sigs" */ |
| 7948 | goto reset_traps; |
| 7949 | } |
| 7950 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 7951 | argv++; |
| 7952 | } |
| 7953 | /* else: "-something", no special meaning */ |
| 7954 | } |
| 7955 | new_cmd = *argv; |
| 7956 | reset_traps: |
| 7957 | argv++; |
| 7958 | goto process_sig_list; |
| 7959 | } |
| 7960 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7961 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7962 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7963 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7964 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7965 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7966 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7967 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 7968 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7969 | |
| 7970 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7971 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7972 | type = "an alias";*/ |
| 7973 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7974 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7975 | type = "a function"; |
| 7976 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7977 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7978 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7979 | else if ((path = find_in_path(*argv)) != NULL) |
| 7980 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7981 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 7982 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7983 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7984 | continue; |
| 7985 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7986 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 7987 | printf("%s is %s\n", *argv, type); |
| 7988 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 7989 | } |
| 7990 | |
| 7991 | return ret; |
| 7992 | } |
| 7993 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7994 | #if ENABLE_HUSH_JOB |
| 7995 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7996 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7997 | { |
| 7998 | int i, jobnum; |
| 7999 | struct pipe *pi; |
| 8000 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8001 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8002 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8003 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8004 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 8005 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8006 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8007 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8008 | goto found; |
| 8009 | } |
| 8010 | } |
| 8011 | bb_error_msg("%s: no current job", argv[0]); |
| 8012 | return EXIT_FAILURE; |
| 8013 | } |
| 8014 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 8015 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 8016 | return EXIT_FAILURE; |
| 8017 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8018 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8019 | if (pi->jobid == jobnum) { |
| 8020 | goto found; |
| 8021 | } |
| 8022 | } |
| 8023 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 8024 | return EXIT_FAILURE; |
| 8025 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 8026 | /* TODO: bash prints a string representation |
| 8027 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8028 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8029 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8030 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8034 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 8035 | for (i = 0; i < pi->num_cmds; i++) { |
| 8036 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 8037 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8038 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8039 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8040 | |
| 8041 | i = kill(- pi->pgrp, SIGCONT); |
| 8042 | if (i < 0) { |
| 8043 | if (errno == ESRCH) { |
| 8044 | delete_finished_bg_job(pi); |
| 8045 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8046 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8047 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8048 | } |
| 8049 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8050 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8051 | remove_bg_job(pi); |
| 8052 | return checkjobs_and_fg_shell(pi); |
| 8053 | } |
| 8054 | return EXIT_SUCCESS; |
| 8055 | } |
| 8056 | #endif |
| 8057 | |
| 8058 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8059 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8060 | { |
| 8061 | const struct built_in_command *x; |
| 8062 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8063 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8064 | "Built-in commands:\n" |
| 8065 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8066 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8067 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8068 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8069 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8070 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8071 | return EXIT_SUCCESS; |
| 8072 | } |
| 8073 | #endif |
| 8074 | |
| 8075 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8076 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8077 | { |
| 8078 | struct pipe *job; |
| 8079 | const char *status_string; |
| 8080 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8081 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8082 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8083 | status_string = "Stopped"; |
| 8084 | else |
| 8085 | status_string = "Running"; |
| 8086 | |
| 8087 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8088 | } |
| 8089 | return EXIT_SUCCESS; |
| 8090 | } |
| 8091 | #endif |
| 8092 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8093 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8094 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8095 | { |
| 8096 | void *p; |
| 8097 | unsigned long l; |
| 8098 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8099 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8100 | /* Optional. Reduces probability of false positives */ |
| 8101 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8102 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8103 | /* Crude attempt to find where "free memory" starts, |
| 8104 | * sans fragmentation. */ |
| 8105 | p = malloc(240); |
| 8106 | l = (unsigned long)p; |
| 8107 | free(p); |
| 8108 | p = malloc(3400); |
| 8109 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8110 | free(p); |
| 8111 | |
| 8112 | if (!G.memleak_value) |
| 8113 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8114 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8115 | l -= G.memleak_value; |
| 8116 | if ((long)l < 0) |
| 8117 | l = 0; |
| 8118 | l /= 1024; |
| 8119 | if (l > 127) |
| 8120 | l = 127; |
| 8121 | |
| 8122 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8123 | return l; |
| 8124 | } |
| 8125 | #endif |
| 8126 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8127 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8128 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8129 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8130 | return EXIT_SUCCESS; |
| 8131 | } |
| 8132 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8133 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8134 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8135 | const char *r; |
| 8136 | char *opt_n = NULL; |
| 8137 | char *opt_p = NULL; |
| 8138 | char *opt_t = NULL; |
| 8139 | char *opt_u = NULL; |
| 8140 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8141 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8142 | /* "!": do not abort on errors. |
| 8143 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8144 | */ |
| 8145 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8146 | if (read_flags == (uint32_t)-1) |
| 8147 | return EXIT_FAILURE; |
| 8148 | argv += optind; |
| 8149 | |
| 8150 | r = shell_builtin_read(set_local_var_from_halves, |
| 8151 | argv, |
| 8152 | get_local_var_value("IFS"), /* can be NULL */ |
| 8153 | read_flags, |
| 8154 | opt_n, |
| 8155 | opt_p, |
| 8156 | opt_t, |
| 8157 | opt_u |
| 8158 | ); |
| 8159 | |
| 8160 | if ((uintptr_t)r > 1) { |
| 8161 | bb_error_msg("%s", r); |
| 8162 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8163 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8164 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8165 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8166 | } |
| 8167 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8168 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8169 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8170 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8171 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8172 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8173 | * set -- [argument...] |
| 8174 | * set -o |
| 8175 | * set +o |
| 8176 | * Implementations shall support the options in both their hyphen and |
| 8177 | * plus-sign forms. These options can also be specified as options to sh. |
| 8178 | * Examples: |
| 8179 | * Write out all variables and their values: set |
| 8180 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8181 | * Turn on the -x and -v options: set -xv |
| 8182 | * Unset all positional parameters: set -- |
| 8183 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8184 | * Set the positional parameters to the expansion of x, even if x expands |
| 8185 | * with a leading '-' or '+': set -- $x |
| 8186 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8187 | * 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] | 8188 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8189 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8190 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8191 | int n; |
| 8192 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8193 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8194 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8195 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8196 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8197 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8198 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8199 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8200 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8201 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8202 | do { |
| 8203 | if (!strcmp(arg, "--")) { |
| 8204 | ++argv; |
| 8205 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8206 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8207 | if (arg[0] != '+' && arg[0] != '-') |
| 8208 | break; |
| 8209 | for (n = 1; arg[n]; ++n) |
| 8210 | if (set_mode(arg[0], arg[n])) |
| 8211 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8212 | } while ((arg = *++argv) != NULL); |
| 8213 | /* Now argv[0] is 1st argument */ |
| 8214 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8215 | if (arg == NULL) |
| 8216 | return EXIT_SUCCESS; |
| 8217 | set_argv: |
| 8218 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8219 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8220 | g_argv = G.global_argv; |
| 8221 | if (G.global_args_malloced) { |
| 8222 | pp = g_argv; |
| 8223 | while (*++pp) |
| 8224 | free(*pp); |
| 8225 | g_argv[1] = NULL; |
| 8226 | } else { |
| 8227 | G.global_args_malloced = 1; |
| 8228 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8229 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8230 | g_argv = pp; |
| 8231 | } |
| 8232 | /* This realloc's G.global_argv */ |
| 8233 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8234 | |
| 8235 | n = 1; |
| 8236 | while (*++pp) |
| 8237 | n++; |
| 8238 | G.global_argc = n; |
| 8239 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8240 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8241 | |
| 8242 | /* Nothing known, so abort */ |
| 8243 | error: |
| 8244 | bb_error_msg("set: %s: invalid option", arg); |
| 8245 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8246 | } |
| 8247 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8248 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8249 | { |
| 8250 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8251 | argv = skip_dash_dash(argv); |
| 8252 | if (argv[0]) { |
| 8253 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8254 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8255 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8256 | if (G.global_args_malloced) { |
| 8257 | int m = 1; |
| 8258 | while (m <= n) |
| 8259 | free(G.global_argv[m++]); |
| 8260 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8261 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8262 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8263 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8264 | return EXIT_SUCCESS; |
| 8265 | } |
| 8266 | return EXIT_FAILURE; |
| 8267 | } |
| 8268 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8269 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8270 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8271 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8272 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8273 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8274 | #if ENABLE_HUSH_FUNCTIONS |
| 8275 | smallint sv_flg; |
| 8276 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8277 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8278 | argv = skip_dash_dash(argv); |
| 8279 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8280 | if (!filename) { |
| 8281 | /* bash says: "bash: .: filename argument required" */ |
| 8282 | return 2; /* bash compat */ |
| 8283 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8284 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8285 | if (!strchr(filename, '/')) { |
| 8286 | arg_path = find_in_path(filename); |
| 8287 | if (arg_path) |
| 8288 | filename = arg_path; |
| 8289 | } |
| 8290 | input = fopen_or_warn(filename, "r"); |
| 8291 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8292 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8293 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8294 | return EXIT_FAILURE; |
| 8295 | } |
| 8296 | close_on_exec_on(fileno(input)); |
| 8297 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8298 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8299 | sv_flg = G.flag_return_in_progress; |
| 8300 | /* "we are inside sourced file, ok to use return" */ |
| 8301 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8302 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8303 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8304 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8305 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8306 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8307 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8308 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8309 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8310 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8311 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8312 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8313 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8314 | } |
| 8315 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8316 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8317 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8318 | int rc; |
| 8319 | mode_t mask; |
| 8320 | |
| 8321 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8322 | argv = skip_dash_dash(argv); |
| 8323 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8324 | mode_t old_mask = mask; |
| 8325 | |
| 8326 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8327 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8328 | mask ^= 0777; |
| 8329 | if (rc == 0) { |
| 8330 | mask = old_mask; |
| 8331 | /* bash messages: |
| 8332 | * bash: umask: 'q': invalid symbolic mode operator |
| 8333 | * bash: umask: 999: octal number out of range |
| 8334 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8335 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8336 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8337 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8338 | rc = 1; |
| 8339 | /* Mimic bash */ |
| 8340 | printf("%04o\n", (unsigned) mask); |
| 8341 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8342 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8343 | umask(mask); |
| 8344 | |
| 8345 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8346 | } |
| 8347 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8348 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8349 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8350 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8351 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8352 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8353 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8354 | /* "!": do not abort on errors */ |
| 8355 | /* "+": stop at 1st non-option */ |
| 8356 | opts = getopt32(argv, "!+vf"); |
| 8357 | if (opts == (unsigned)-1) |
| 8358 | return EXIT_FAILURE; |
| 8359 | if (opts == 3) { |
| 8360 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8361 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8362 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8363 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8364 | |
| 8365 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8366 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8367 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8368 | if (unset_local_var(*argv)) { |
| 8369 | /* unset <nonexistent_var> doesn't fail. |
| 8370 | * Error is when one tries to unset RO var. |
| 8371 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8372 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8373 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8374 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8375 | #if ENABLE_HUSH_FUNCTIONS |
| 8376 | else { |
| 8377 | unset_func(*argv); |
| 8378 | } |
| 8379 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8380 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8381 | } |
| 8382 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8383 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8384 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8385 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8386 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8387 | { |
| 8388 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8389 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8390 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8391 | argv = skip_dash_dash(argv); |
| 8392 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8393 | /* Don't care about wait results */ |
| 8394 | /* Note 1: must wait until there are no more children */ |
| 8395 | /* Note 2: must be interruptible */ |
| 8396 | /* Examples: |
| 8397 | * $ sleep 3 & sleep 6 & wait |
| 8398 | * [1] 30934 sleep 3 |
| 8399 | * [2] 30935 sleep 6 |
| 8400 | * [1] Done sleep 3 |
| 8401 | * [2] Done sleep 6 |
| 8402 | * $ sleep 3 & sleep 6 & wait |
| 8403 | * [1] 30936 sleep 3 |
| 8404 | * [2] 30937 sleep 6 |
| 8405 | * [1] Done sleep 3 |
| 8406 | * ^C <-- after ~4 sec from keyboard |
| 8407 | * $ |
| 8408 | */ |
| 8409 | sigaddset(&G.blocked_set, SIGCHLD); |
| 8410 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8411 | while (1) { |
| 8412 | checkjobs(NULL); |
| 8413 | if (errno == ECHILD) |
| 8414 | break; |
| 8415 | /* Wait for SIGCHLD or any other signal of interest */ |
| 8416 | /* sigtimedwait with infinite timeout: */ |
| 8417 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 8418 | if (sig > 0) { |
| 8419 | sig = check_and_run_traps(sig); |
| 8420 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 8421 | ret = 128 + sig; |
| 8422 | break; |
| 8423 | } |
| 8424 | } |
| 8425 | } |
| 8426 | sigdelset(&G.blocked_set, SIGCHLD); |
| 8427 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8428 | return ret; |
| 8429 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8430 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8431 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8432 | while (*argv) { |
| 8433 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 8434 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8435 | /* mimic bash message */ |
| 8436 | 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] | 8437 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8438 | } |
| 8439 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8440 | if (WIFSIGNALED(status)) |
| 8441 | ret = 128 + WTERMSIG(status); |
| 8442 | else if (WIFEXITED(status)) |
| 8443 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8444 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8445 | ret = EXIT_FAILURE; |
| 8446 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8447 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8448 | ret = 127; |
| 8449 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8450 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8451 | } |
| 8452 | |
| 8453 | return ret; |
| 8454 | } |
| 8455 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8456 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 8457 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 8458 | { |
| 8459 | if (argv[1]) { |
| 8460 | def = bb_strtou(argv[1], NULL, 10); |
| 8461 | if (errno || def < def_min || argv[2]) { |
| 8462 | bb_error_msg("%s: bad arguments", argv[0]); |
| 8463 | def = UINT_MAX; |
| 8464 | } |
| 8465 | } |
| 8466 | return def; |
| 8467 | } |
| 8468 | #endif |
| 8469 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8470 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8471 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8472 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8473 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8474 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8475 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 8476 | return EXIT_SUCCESS; /* bash compat */ |
| 8477 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8478 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8479 | |
| 8480 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 8481 | if (depth == UINT_MAX) |
| 8482 | G.flag_break_continue = BC_BREAK; |
| 8483 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8484 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8485 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8486 | return EXIT_SUCCESS; |
| 8487 | } |
| 8488 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8489 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8490 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8491 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 8492 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8493 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8494 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8495 | |
| 8496 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8497 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8498 | { |
| 8499 | int rc; |
| 8500 | |
| 8501 | if (G.flag_return_in_progress != -1) { |
| 8502 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 8503 | return EXIT_FAILURE; /* bash compat */ |
| 8504 | } |
| 8505 | |
| 8506 | G.flag_return_in_progress = 1; |
| 8507 | |
| 8508 | /* bash: |
| 8509 | * out of range: wraps around at 256, does not error out |
| 8510 | * non-numeric param: |
| 8511 | * f() { false; return qwe; }; f; echo $? |
| 8512 | * bash: return: qwe: numeric argument required <== we do this |
| 8513 | * 255 <== we also do this |
| 8514 | */ |
| 8515 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 8516 | return rc; |
| 8517 | } |
| 8518 | #endif |