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 >& |
| 53 | * brace expansion: one/{two,three,four} |
| 54 | * reserved words: function select |
| 55 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 56 | * process substitution: <(list) and >(list) |
| 57 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 58 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 59 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 60 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 61 | * 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] | 62 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 63 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 64 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 65 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 66 | * export builtin should be special, its arguments are assignments |
| 67 | * and therefore expansion of them should be "one-word" expansion: |
| 68 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 69 | * compare with: |
| 70 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 71 | * ls: cannot access i=a: No such file or directory |
| 72 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 73 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 74 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 75 | * is not quoted: |
| 76 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 77 | * aaa bbb |
| 78 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 79 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 80 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 81 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 82 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 83 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 84 | #include <malloc.h> /* for malloc_trim */ |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 85 | #include <glob.h> |
| 86 | /* #include <dmalloc.h> */ |
| 87 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 88 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 89 | #endif |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 90 | |
| 91 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 92 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 93 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 94 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 95 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 96 | #else |
| 97 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 98 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 99 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 100 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 101 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 102 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 103 | //applet:IF_HUSH(APPLET(hush, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 104 | //applet:IF_MSH(APPLET(msh, _BB_DIR_BIN, _BB_SUID_DROP)) |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 105 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, _BB_DIR_BIN, _BB_SUID_DROP, sh)) |
| 106 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, _BB_DIR_BIN, _BB_SUID_DROP, bash)) |
| 107 | |
| 108 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 109 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 110 | |
| 111 | //config:config HUSH |
| 112 | //config: bool "hush" |
| 113 | //config: default y |
| 114 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 115 | //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] | 116 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 117 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 118 | //config: and functions are supported. |
| 119 | //config: |
| 120 | //config: It will compile and work on no-mmu systems. |
| 121 | //config: |
| 122 | //config: It does not handle select, aliases, brace expansion, |
| 123 | //config: tilde expansion, &>file and >&file redirection of stdout+stderr. |
| 124 | //config: |
| 125 | //config:config HUSH_BASH_COMPAT |
| 126 | //config: bool "bash-compatible extensions" |
| 127 | //config: default y |
| 128 | //config: depends on HUSH |
| 129 | //config: help |
| 130 | //config: Enable bash-compatible extensions. |
| 131 | //config: |
| 132 | //config:config HUSH_HELP |
| 133 | //config: bool "help builtin" |
| 134 | //config: default y |
| 135 | //config: depends on HUSH |
| 136 | //config: help |
| 137 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 138 | //config: |
| 139 | //config:config HUSH_INTERACTIVE |
| 140 | //config: bool "Interactive mode" |
| 141 | //config: default y |
| 142 | //config: depends on HUSH |
| 143 | //config: help |
| 144 | //config: Enable interactive mode (prompt and command editing). |
| 145 | //config: Without this, hush simply reads and executes commands |
| 146 | //config: from stdin just like a shell script from a file. |
| 147 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 148 | //config: |
| 149 | //config:config HUSH_JOB |
| 150 | //config: bool "Job control" |
| 151 | //config: default y |
| 152 | //config: depends on HUSH_INTERACTIVE |
| 153 | //config: help |
| 154 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 155 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 156 | //config: "cmd &" still works by simply spawning a process and immediately |
| 157 | //config: prompting for next command (or executing next command in a script), |
| 158 | //config: but no separate process group is formed. |
| 159 | //config: |
| 160 | //config:config HUSH_TICK |
| 161 | //config: bool "Process substitution" |
| 162 | //config: default y |
| 163 | //config: depends on HUSH |
| 164 | //config: help |
| 165 | //config: Enable process substitution `command` and $(command) in hush. |
| 166 | //config: |
| 167 | //config:config HUSH_IF |
| 168 | //config: bool "Support if/then/elif/else/fi" |
| 169 | //config: default y |
| 170 | //config: depends on HUSH |
| 171 | //config: help |
| 172 | //config: Enable if/then/elif/else/fi in hush. |
| 173 | //config: |
| 174 | //config:config HUSH_LOOPS |
| 175 | //config: bool "Support for, while and until loops" |
| 176 | //config: default y |
| 177 | //config: depends on HUSH |
| 178 | //config: help |
| 179 | //config: Enable for, while and until loops in hush. |
| 180 | //config: |
| 181 | //config:config HUSH_CASE |
| 182 | //config: bool "Support case ... esac statement" |
| 183 | //config: default y |
| 184 | //config: depends on HUSH |
| 185 | //config: help |
| 186 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 187 | //config: |
| 188 | //config:config HUSH_FUNCTIONS |
| 189 | //config: bool "Support funcname() { commands; } syntax" |
| 190 | //config: default y |
| 191 | //config: depends on HUSH |
| 192 | //config: help |
| 193 | //config: Enable support for shell functions in hush. +800 bytes. |
| 194 | //config: |
| 195 | //config:config HUSH_LOCAL |
| 196 | //config: bool "Support local builtin" |
| 197 | //config: default y |
| 198 | //config: depends on HUSH_FUNCTIONS |
| 199 | //config: help |
| 200 | //config: Enable support for local variables in functions. |
| 201 | //config: |
| 202 | //config:config HUSH_RANDOM_SUPPORT |
| 203 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 204 | //config: default y |
| 205 | //config: depends on HUSH |
| 206 | //config: help |
| 207 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 208 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 209 | //config: |
| 210 | //config:config HUSH_EXPORT_N |
| 211 | //config: bool "Support 'export -n' option" |
| 212 | //config: default y |
| 213 | //config: depends on HUSH |
| 214 | //config: help |
| 215 | //config: export -n unexports variables. It is a bash extension. |
| 216 | //config: |
| 217 | //config:config HUSH_MODE_X |
| 218 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 219 | //config: default y |
| 220 | //config: depends on HUSH |
| 221 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 222 | //config: This instructs hush to print commands before execution. |
| 223 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 224 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 225 | //config:config MSH |
| 226 | //config: bool "msh (deprecated: aliased to hush)" |
| 227 | //config: default n |
| 228 | //config: select HUSH |
| 229 | //config: help |
| 230 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 231 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 232 | |
| 233 | //usage:#define hush_trivial_usage NOUSAGE_STR |
| 234 | //usage:#define hush_full_usage "" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 235 | //usage:#define msh_trivial_usage NOUSAGE_STR |
| 236 | //usage:#define msh_full_usage "" |
| 237 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 238 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 239 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 240 | #define LEAK_HUNTING 0 |
| 241 | #define BUILD_AS_NOMMU 0 |
| 242 | /* Enable/disable sanity checks. Ok to enable in production, |
| 243 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 244 | * Keeping 1 for now even in released versions. |
| 245 | */ |
| 246 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 247 | /* Slightly bigger (+200 bytes), but faster hush. |
| 248 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 249 | * which allows us to do fewer waitpid's. |
| 250 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 251 | * and therefore waitpid will return the same result as last time) |
| 252 | */ |
| 253 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 254 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 255 | * So far ${var%...} ops are always enabled: |
| 256 | */ |
| 257 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 258 | |
| 259 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 260 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 261 | # undef BB_MMU |
| 262 | # undef USE_FOR_NOMMU |
| 263 | # undef USE_FOR_MMU |
| 264 | # define BB_MMU 0 |
| 265 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 266 | # define USE_FOR_MMU(...) |
| 267 | #endif |
| 268 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 269 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 270 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 271 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 272 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 273 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 274 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 275 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 276 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 277 | # define IF_FEATURE_SH_STANDALONE(...) |
| 278 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 279 | #endif |
| 280 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 281 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 282 | # undef ENABLE_FEATURE_EDITING |
| 283 | # define ENABLE_FEATURE_EDITING 0 |
| 284 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 285 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 286 | #endif |
| 287 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 288 | /* Do we support ANY keywords? */ |
| 289 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 290 | # define HAS_KEYWORDS 1 |
| 291 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 292 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 293 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 294 | # define HAS_KEYWORDS 0 |
| 295 | # define IF_HAS_KEYWORDS(...) |
| 296 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 297 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 298 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 299 | /* If you comment out one of these below, it will be #defined later |
| 300 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 301 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 302 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 303 | #define debug_printf_parse(...) do {} while (0) |
| 304 | #define debug_print_tree(a, b) do {} while (0) |
| 305 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 306 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 307 | #define debug_printf_jobs(...) do {} while (0) |
| 308 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 309 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 310 | #define debug_printf_glob(...) do {} while (0) |
| 311 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 312 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 313 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 314 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 315 | #define ERR_PTR ((void*)(long)1) |
| 316 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 317 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 318 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 319 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 320 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 321 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 322 | #if ENABLE_HUSH_BASH_COMPAT |
| 323 | /* Support / and // replace ops */ |
| 324 | /* Note that // is stored as \ in "encoded" string representation */ |
| 325 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 326 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 327 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 328 | #else |
| 329 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 330 | # define VAR_SUBST_OPS "%#:-=+?" |
| 331 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 332 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 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 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 352 | enum { |
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 |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 381 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 382 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 383 | enum { |
| 384 | EXP_FLAG_GLOB = 0x200, |
| 385 | EXP_FLAG_ESC_GLOB_CHARS = 0x100, |
| 386 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 387 | }; |
| 388 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 389 | typedef struct o_string { |
| 390 | char *data; |
| 391 | int length; /* position where data is appended */ |
| 392 | int maxlen; |
| 393 | /* Protect newly added chars against globbing |
| 394 | * (by prepending \ to *, ?, [, \) */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 395 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 396 | /* At least some part of the string was inside '' or "", |
| 397 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 398 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 399 | smallint has_empty_slot; |
| 400 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 401 | } o_string; |
| 402 | enum { |
| 403 | MAYBE_ASSIGNMENT = 0, |
| 404 | DEFINITELY_ASSIGNMENT = 1, |
| 405 | NOT_ASSIGNMENT = 2, |
| 406 | WORD_IS_KEYWORD = 3, /* not assigment, but next word may be: "if v=xyz cmd;" */ |
| 407 | }; |
| 408 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 409 | #define NULL_O_STRING { NULL } |
| 410 | |
| 411 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 412 | * available? Where is it documented? */ |
| 413 | typedef struct in_str { |
| 414 | const char *p; |
| 415 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 416 | char eof_flag; /* meaningless if ->p == NULL */ |
| 417 | char peek_buf[2]; |
| 418 | #if ENABLE_HUSH_INTERACTIVE |
| 419 | smallint promptme; |
| 420 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 421 | #endif |
| 422 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 423 | int (*get) (struct in_str *) FAST_FUNC; |
| 424 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 425 | } in_str; |
| 426 | #define i_getch(input) ((input)->get(input)) |
| 427 | #define i_peek(input) ((input)->peek(input)) |
| 428 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 429 | /* The descrip member of this structure is only used to make |
| 430 | * debugging output pretty */ |
| 431 | static const struct { |
| 432 | int mode; |
| 433 | signed char default_fd; |
| 434 | char descrip[3]; |
| 435 | } redir_table[] = { |
| 436 | { O_RDONLY, 0, "<" }, |
| 437 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 438 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 439 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 440 | { O_RDONLY, 0, "<<" }, |
| 441 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 442 | /* { O_RDONLY, 77, "<<" }, */ |
| 443 | }; |
| 444 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 445 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 446 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 447 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 448 | int rd_fd; /* fd to redirect */ |
| 449 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 450 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 451 | smallint rd_type; /* (enum redir_type) */ |
| 452 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 453 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 454 | * bit 0: do we need to trim leading tabs? |
| 455 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 456 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 457 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 458 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 459 | REDIRECT_INPUT = 0, |
| 460 | REDIRECT_OVERWRITE = 1, |
| 461 | REDIRECT_APPEND = 2, |
| 462 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 463 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 464 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 465 | |
| 466 | REDIRFD_CLOSE = -3, |
| 467 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 468 | REDIRFD_TO_FILE = -1, |
| 469 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 470 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 471 | HEREDOC_SKIPTABS = 1, |
| 472 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 473 | } redir_type; |
| 474 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 475 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 476 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 477 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 478 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 479 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 480 | smallint cmd_type; /* CMD_xxx */ |
| 481 | #define CMD_NORMAL 0 |
| 482 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 483 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 484 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 485 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 486 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 487 | #if ENABLE_HUSH_FUNCTIONS |
| 488 | # define CMD_FUNCDEF 3 |
| 489 | #endif |
| 490 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 491 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 492 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 493 | #if !BB_MMU |
| 494 | char *group_as_string; |
| 495 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 496 | #if ENABLE_HUSH_FUNCTIONS |
| 497 | struct function *child_func; |
| 498 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 499 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 500 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 501 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 502 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 503 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 504 | * we put those fields back into cmd->xxx |
| 505 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 506 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 507 | * Without this trick, loop would execute a;b;b;b;... |
| 508 | * instead of correct sequence a;b;a;b;... |
| 509 | * When command is freed, it severs the link |
| 510 | * (sets ->child_func->parent_cmd to NULL). |
| 511 | */ |
| 512 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 513 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 514 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 515 | * and on execution these are substituted with their values. |
| 516 | * Substitution can make _several_ words out of one argv[n]! |
| 517 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 518 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 519 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 520 | struct redir_struct *redirects; /* I/O redirections */ |
| 521 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 522 | /* Is there anything in this command at all? */ |
| 523 | #define IS_NULL_CMD(cmd) \ |
| 524 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 525 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 526 | |
| 527 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 528 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 529 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 530 | int alive_cmds; /* number of commands running (not exited) */ |
| 531 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 532 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 533 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 534 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 535 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 536 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 537 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 538 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 539 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 540 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 541 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 542 | typedef enum pipe_style { |
| 543 | PIPE_SEQ = 1, |
| 544 | PIPE_AND = 2, |
| 545 | PIPE_OR = 3, |
| 546 | PIPE_BG = 4, |
| 547 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 548 | /* Is there anything in this pipe at all? */ |
| 549 | #define IS_NULL_PIPE(pi) \ |
| 550 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 551 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 552 | /* This holds pointers to the various results of parsing */ |
| 553 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 554 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 555 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 556 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 557 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 558 | /* last command in pipe (being constructed right now) */ |
| 559 | struct command *command; |
| 560 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 561 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 562 | #if !BB_MMU |
| 563 | o_string as_string; |
| 564 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 565 | #if HAS_KEYWORDS |
| 566 | smallint ctx_res_w; |
| 567 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 568 | #if ENABLE_HUSH_CASE |
| 569 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 570 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 571 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 572 | int old_flag; |
| 573 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 574 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 575 | * when we see "if" or "then", we malloc and copy current context, |
| 576 | * and make ->stack point to it. then we parse pipeN. |
| 577 | * when closing "then" / fi" / whatever is found, |
| 578 | * we move list_head into ->stack->command->group, |
| 579 | * copy ->stack into current context, and delete ->stack. |
| 580 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 581 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 582 | struct parse_context *stack; |
| 583 | #endif |
| 584 | }; |
| 585 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 586 | /* On program start, environ points to initial environment. |
| 587 | * putenv adds new pointers into it, unsetenv removes them. |
| 588 | * Neither of these (de)allocates the strings. |
| 589 | * setenv allocates new strings in malloc space and does putenv, |
| 590 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 591 | #define setenv(...) setenv_is_leaky_dont_use() |
| 592 | struct variable { |
| 593 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 594 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 595 | #if ENABLE_HUSH_LOCAL |
| 596 | unsigned func_nest_level; |
| 597 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 598 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 599 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 600 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 601 | }; |
| 602 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 603 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 604 | BC_BREAK = 1, |
| 605 | BC_CONTINUE = 2, |
| 606 | }; |
| 607 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 608 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 609 | struct function { |
| 610 | struct function *next; |
| 611 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 612 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 613 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 614 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 615 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 616 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 617 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 618 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 619 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 620 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 621 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 622 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 623 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 624 | /* interactive_fd != 0 means we are an interactive shell. |
| 625 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 626 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 627 | * job control still works, but terminal signals |
| 628 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 629 | * process groups can only be created with "cmd &". |
| 630 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 631 | * to give tty to the foreground process group, |
| 632 | * and will take it back when the group is stopped (^Z) |
| 633 | * or killed (^C). |
| 634 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 635 | #if ENABLE_HUSH_INTERACTIVE |
| 636 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 637 | * _AND_ if we decided to act interactively */ |
| 638 | int interactive_fd; |
| 639 | const char *PS1; |
| 640 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 641 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 642 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 643 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 644 | #endif |
| 645 | #if ENABLE_FEATURE_EDITING |
| 646 | line_input_t *line_input_state; |
| 647 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 648 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 649 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 650 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 651 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 652 | random_t random_gen; |
| 653 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 654 | #if ENABLE_HUSH_JOB |
| 655 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 656 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 657 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 658 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 659 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 660 | #else |
| 661 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 662 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 663 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 664 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 665 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 666 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 667 | #if ENABLE_HUSH_FUNCTIONS |
| 668 | /* 0: outside of a function (or sourced file) |
| 669 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 670 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 671 | */ |
| 672 | smallint flag_return_in_progress; |
| 673 | #endif |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 674 | smallint n_mode; |
| 675 | #if ENABLE_HUSH_MODE_X |
Denys Vlasenko | 3f5fae0 | 2010-07-16 12:35:35 +0200 | [diff] [blame] | 676 | smallint x_mode; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 677 | # define G_x_mode (G.x_mode) |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 678 | #else |
| 679 | # define G_x_mode 0 |
| 680 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 681 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 682 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 683 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 684 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 685 | smalluint global_args_malloced; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 686 | smalluint inherited_set_is_saved; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 687 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 688 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 689 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 690 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 691 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 692 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 693 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 694 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 695 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 696 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 697 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 698 | const char *cwd; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 699 | struct variable *top_var; /* = &G.shell_ver (set in main()) */ |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 700 | struct variable shell_ver; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 701 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 702 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 703 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 704 | # if ENABLE_HUSH_LOCAL |
| 705 | struct variable **shadowed_vars_pp; |
| 706 | unsigned func_nest_level; |
| 707 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 708 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 709 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 710 | #if ENABLE_HUSH_FAST |
| 711 | unsigned count_SIGCHLD; |
| 712 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 713 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 714 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 715 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 716 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 717 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 718 | sigset_t blocked_set; |
| 719 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 720 | #if HUSH_DEBUG |
| 721 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 722 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 723 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 724 | 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] | 725 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 726 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 727 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 728 | * (too many defines). Also, I actually prefer to see when a variable |
| 729 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 730 | #define INIT_G() do { \ |
| 731 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 732 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 733 | |
| 734 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 735 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 736 | static int builtin_cd(char **argv) FAST_FUNC; |
| 737 | static int builtin_echo(char **argv) FAST_FUNC; |
| 738 | static int builtin_eval(char **argv) FAST_FUNC; |
| 739 | static int builtin_exec(char **argv) FAST_FUNC; |
| 740 | static int builtin_exit(char **argv) FAST_FUNC; |
| 741 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 742 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 743 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 744 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 745 | #endif |
| 746 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 747 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 748 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 749 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 750 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 751 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 752 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 753 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 754 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 755 | #if ENABLE_PRINTF |
| 756 | static int builtin_printf(char **argv) FAST_FUNC; |
| 757 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 758 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 759 | static int builtin_read(char **argv) FAST_FUNC; |
| 760 | static int builtin_set(char **argv) FAST_FUNC; |
| 761 | static int builtin_shift(char **argv) FAST_FUNC; |
| 762 | static int builtin_source(char **argv) FAST_FUNC; |
| 763 | static int builtin_test(char **argv) FAST_FUNC; |
| 764 | static int builtin_trap(char **argv) FAST_FUNC; |
| 765 | static int builtin_type(char **argv) FAST_FUNC; |
| 766 | static int builtin_true(char **argv) FAST_FUNC; |
| 767 | static int builtin_umask(char **argv) FAST_FUNC; |
| 768 | static int builtin_unset(char **argv) FAST_FUNC; |
| 769 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 770 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 771 | static int builtin_break(char **argv) FAST_FUNC; |
| 772 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 773 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 774 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 775 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 776 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 777 | |
| 778 | /* Table of built-in functions. They can be forked or not, depending on |
| 779 | * context: within pipes, they fork. As simple commands, they do not. |
| 780 | * When used in non-forking context, they can change global variables |
| 781 | * in the parent shell process. If forked, of course they cannot. |
| 782 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 783 | * still be set at the end. */ |
| 784 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 785 | const char *b_cmd; |
| 786 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 787 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 788 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 789 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 790 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 791 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 792 | #endif |
| 793 | }; |
| 794 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 795 | static const struct built_in_command bltins1[] = { |
| 796 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 797 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 798 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 799 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 800 | #endif |
| 801 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 802 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 803 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 804 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 805 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 806 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 807 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 808 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 809 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 810 | BLTIN("exit" , builtin_exit , "Exit"), |
| 811 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 812 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 813 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 814 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 815 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 816 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 817 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 818 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 819 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 820 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 821 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 822 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 823 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 824 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 825 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 826 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 827 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 828 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 829 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 830 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 831 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 832 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 833 | #if ENABLE_HUSH_BASH_COMPAT |
| 834 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 835 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 836 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 837 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 838 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 839 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 840 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 841 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 842 | }; |
| 843 | /* For now, echo and test are unconditionally enabled. |
| 844 | * Maybe make it configurable? */ |
| 845 | static const struct built_in_command bltins2[] = { |
| 846 | BLTIN("[" , builtin_test , NULL), |
| 847 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 848 | #if ENABLE_PRINTF |
| 849 | BLTIN("printf" , builtin_printf , NULL), |
| 850 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 851 | BLTIN("pwd" , builtin_pwd , NULL), |
| 852 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 853 | }; |
| 854 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 855 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 856 | /* Debug printouts. |
| 857 | */ |
| 858 | #if HUSH_DEBUG |
| 859 | /* prevent disasters with G.debug_indent < 0 */ |
| 860 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 861 | # define debug_enter() (G.debug_indent++) |
| 862 | # define debug_leave() (G.debug_indent--) |
| 863 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 864 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 865 | # define debug_enter() ((void)0) |
| 866 | # define debug_leave() ((void)0) |
| 867 | #endif |
| 868 | |
| 869 | #ifndef debug_printf |
| 870 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 871 | #endif |
| 872 | |
| 873 | #ifndef debug_printf_parse |
| 874 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 875 | #endif |
| 876 | |
| 877 | #ifndef debug_printf_exec |
| 878 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 879 | #endif |
| 880 | |
| 881 | #ifndef debug_printf_env |
| 882 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 883 | #endif |
| 884 | |
| 885 | #ifndef debug_printf_jobs |
| 886 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 887 | # define DEBUG_JOBS 1 |
| 888 | #else |
| 889 | # define DEBUG_JOBS 0 |
| 890 | #endif |
| 891 | |
| 892 | #ifndef debug_printf_expand |
| 893 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 894 | # define DEBUG_EXPAND 1 |
| 895 | #else |
| 896 | # define DEBUG_EXPAND 0 |
| 897 | #endif |
| 898 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 899 | #ifndef debug_printf_varexp |
| 900 | # define debug_printf_varexp(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 901 | #endif |
| 902 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 903 | #ifndef debug_printf_glob |
| 904 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 905 | # define DEBUG_GLOB 1 |
| 906 | #else |
| 907 | # define DEBUG_GLOB 0 |
| 908 | #endif |
| 909 | |
| 910 | #ifndef debug_printf_list |
| 911 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 912 | #endif |
| 913 | |
| 914 | #ifndef debug_printf_subst |
| 915 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 916 | #endif |
| 917 | |
| 918 | #ifndef debug_printf_clean |
| 919 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 920 | # define DEBUG_CLEAN 1 |
| 921 | #else |
| 922 | # define DEBUG_CLEAN 0 |
| 923 | #endif |
| 924 | |
| 925 | #if DEBUG_EXPAND |
| 926 | static void debug_print_strings(const char *prefix, char **vv) |
| 927 | { |
| 928 | indent(); |
| 929 | fprintf(stderr, "%s:\n", prefix); |
| 930 | while (*vv) |
| 931 | fprintf(stderr, " '%s'\n", *vv++); |
| 932 | } |
| 933 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 934 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 935 | #endif |
| 936 | |
| 937 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 938 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 939 | */ |
| 940 | #if LEAK_HUNTING |
| 941 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 942 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 943 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 944 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 945 | return ptr; |
| 946 | } |
| 947 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 948 | { |
| 949 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 950 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 951 | return ptr; |
| 952 | } |
| 953 | static char *xxstrdup(int lineno, const char *str) |
| 954 | { |
| 955 | char *ptr = xstrdup(str); |
| 956 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 957 | return ptr; |
| 958 | } |
| 959 | static void xxfree(void *ptr) |
| 960 | { |
| 961 | fdprintf(2, "free %p\n", ptr); |
| 962 | free(ptr); |
| 963 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 964 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 965 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 966 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 967 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 968 | #endif |
| 969 | |
| 970 | |
| 971 | /* Syntax and runtime errors. They always abort scripts. |
| 972 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 973 | * and return to the prompt. |
| 974 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 975 | */ |
| 976 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 977 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 978 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 979 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 980 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 981 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 982 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 983 | #endif |
| 984 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 985 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 986 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 987 | va_list p; |
| 988 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 989 | #if HUSH_DEBUG >= 2 |
| 990 | bb_error_msg("hush.c:%u", lineno); |
| 991 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 992 | va_start(p, fmt); |
| 993 | bb_verror_msg(fmt, p, NULL); |
| 994 | va_end(p); |
| 995 | if (!G_interactive_fd) |
| 996 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 997 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 998 | |
| 999 | static void syntax_error(unsigned lineno, const char *msg) |
| 1000 | { |
| 1001 | if (msg) |
| 1002 | die_if_script(lineno, "syntax error: %s", msg); |
| 1003 | else |
| 1004 | die_if_script(lineno, "syntax error", NULL); |
| 1005 | } |
| 1006 | |
| 1007 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 1008 | { |
| 1009 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 1010 | } |
| 1011 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1012 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 1013 | { |
| 1014 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 1015 | } |
| 1016 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1017 | /* It so happens that all such cases are totally fatal |
| 1018 | * even if shell is interactive: EOF while looking for closing |
| 1019 | * delimiter. There is nowhere to read stuff from after that, |
| 1020 | * it's EOF! The only choice is to terminate. |
| 1021 | */ |
| 1022 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1023 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1024 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1025 | char msg[2] = { ch, '\0' }; |
| 1026 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1027 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1030 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1031 | { |
| 1032 | char msg[2]; |
| 1033 | msg[0] = ch; |
| 1034 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1035 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1038 | #if HUSH_DEBUG < 2 |
| 1039 | # undef die_if_script |
| 1040 | # undef syntax_error |
| 1041 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1042 | # undef syntax_error_unterm_ch |
| 1043 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1044 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1045 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1046 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1047 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1048 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1049 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1050 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1051 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1052 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1053 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1054 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1055 | #if ENABLE_HUSH_INTERACTIVE |
| 1056 | static void cmdedit_update_prompt(void); |
| 1057 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1058 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1059 | #endif |
| 1060 | |
| 1061 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1062 | /* Utility functions |
| 1063 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1064 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1065 | static char *unbackslash(char *src) |
| 1066 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1067 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1068 | while (1) { |
| 1069 | if (*src == '\\') |
| 1070 | src++; |
| 1071 | if ((*dst++ = *src++) == '\0') |
| 1072 | break; |
| 1073 | } |
| 1074 | return dst; |
| 1075 | } |
| 1076 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1077 | 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] | 1078 | { |
| 1079 | int i; |
| 1080 | unsigned count1; |
| 1081 | unsigned count2; |
| 1082 | char **v; |
| 1083 | |
| 1084 | v = strings; |
| 1085 | count1 = 0; |
| 1086 | if (v) { |
| 1087 | while (*v) { |
| 1088 | count1++; |
| 1089 | v++; |
| 1090 | } |
| 1091 | } |
| 1092 | count2 = 0; |
| 1093 | v = add; |
| 1094 | while (*v) { |
| 1095 | count2++; |
| 1096 | v++; |
| 1097 | } |
| 1098 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1099 | v[count1 + count2] = NULL; |
| 1100 | i = count2; |
| 1101 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1102 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1103 | return v; |
| 1104 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1105 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1106 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1107 | { |
| 1108 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1109 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1110 | return ptr; |
| 1111 | } |
| 1112 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1113 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1114 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1115 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1116 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1117 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1118 | { |
| 1119 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1120 | v[0] = add; |
| 1121 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1122 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1123 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1124 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1125 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1126 | { |
| 1127 | char **ptr = add_string_to_strings(strings, add); |
| 1128 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1129 | return ptr; |
| 1130 | } |
| 1131 | #define add_string_to_strings(strings, add) \ |
| 1132 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1133 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1134 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1135 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1136 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1137 | char **v; |
| 1138 | |
| 1139 | if (!strings) |
| 1140 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1141 | v = strings; |
| 1142 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1143 | free(*v); |
| 1144 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1145 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1146 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1149 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1150 | /* Helpers for setting new $n and restoring them back |
| 1151 | */ |
| 1152 | typedef struct save_arg_t { |
| 1153 | char *sv_argv0; |
| 1154 | char **sv_g_argv; |
| 1155 | int sv_g_argc; |
| 1156 | smallint sv_g_malloced; |
| 1157 | } save_arg_t; |
| 1158 | |
| 1159 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1160 | { |
| 1161 | int n; |
| 1162 | |
| 1163 | sv->sv_argv0 = argv[0]; |
| 1164 | sv->sv_g_argv = G.global_argv; |
| 1165 | sv->sv_g_argc = G.global_argc; |
| 1166 | sv->sv_g_malloced = G.global_args_malloced; |
| 1167 | |
| 1168 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1169 | G.global_argv = argv; |
| 1170 | G.global_args_malloced = 0; |
| 1171 | |
| 1172 | n = 1; |
| 1173 | while (*++argv) |
| 1174 | n++; |
| 1175 | G.global_argc = n; |
| 1176 | } |
| 1177 | |
| 1178 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1179 | { |
| 1180 | char **pp; |
| 1181 | |
| 1182 | if (G.global_args_malloced) { |
| 1183 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1184 | pp = G.global_argv; |
| 1185 | while (*++pp) /* note: does not free $0 */ |
| 1186 | free(*pp); |
| 1187 | free(G.global_argv); |
| 1188 | } |
| 1189 | argv[0] = sv->sv_argv0; |
| 1190 | G.global_argv = sv->sv_g_argv; |
| 1191 | G.global_argc = sv->sv_g_argc; |
| 1192 | G.global_args_malloced = sv->sv_g_malloced; |
| 1193 | } |
| 1194 | |
| 1195 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1196 | /* Basic theory of signal handling in shell |
| 1197 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1198 | * This does not describe what hush does, rather, it is current understanding |
| 1199 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1200 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1201 | * |
| 1202 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1203 | * is finished or backgrounded. It is the same in interactive and |
| 1204 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1205 | * 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] | 1206 | * ^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] | 1207 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1208 | * pipe. |
| 1209 | * |
| 1210 | * Wait builtin in interruptible by signals for which user trap is set |
| 1211 | * or by SIGINT in interactive shell. |
| 1212 | * |
| 1213 | * Trap handlers will execute even within trap handlers. (right?) |
| 1214 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1215 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1216 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1217 | * |
| 1218 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1219 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1220 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1221 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1222 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1223 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1224 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1225 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1226 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1227 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1228 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1229 | * |
| 1230 | * SIGQUIT: ignore |
| 1231 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1232 | * SIGHUP (interactive): |
| 1233 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1234 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1235 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1236 | * that all pipe members are stopped. Try this in bash: |
| 1237 | * while :; do :; done - ^Z does not background it |
| 1238 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1239 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1240 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1241 | * to interactive shell while shell is waiting for a pipe, |
| 1242 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1243 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1244 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1245 | * Example 2: this does not wait and does not execute ls: |
| 1246 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1247 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1248 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1249 | * |
| 1250 | * (What happens to signals which are IGN on shell start?) |
| 1251 | * (What happens with signal mask on shell start?) |
| 1252 | * |
| 1253 | * Implementation in hush |
| 1254 | * ====================== |
| 1255 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1256 | * We block all signals which we don't want to take action immediately, |
| 1257 | * i.e. we block all signals which need to have special handling as described |
| 1258 | * above, and all signals which have traps set. |
| 1259 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1260 | * and act on them. |
| 1261 | * |
| 1262 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1263 | * sigset_t blocked_set: current blocked signal set |
| 1264 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1265 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1266 | * 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] | 1267 | * "trap 'cmd' SIGxxx": |
| 1268 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1269 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1270 | * unblock signals with special interactive handling |
| 1271 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1272 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1273 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1274 | * 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] | 1275 | * 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] | 1276 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1277 | * 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] | 1278 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1279 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1280 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1281 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1282 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1283 | * 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] | 1284 | * 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] | 1285 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1286 | enum { |
| 1287 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1288 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1289 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1290 | | (1 << SIGHUP) |
| 1291 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1292 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1293 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1294 | | (1 << SIGTTIN) |
| 1295 | | (1 << SIGTTOU) |
| 1296 | | (1 << SIGTSTP) |
| 1297 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1298 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1299 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1300 | #if ENABLE_HUSH_FAST |
| 1301 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1302 | { |
| 1303 | G.count_SIGCHLD++; |
| 1304 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1305 | } |
| 1306 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1307 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1308 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1309 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1310 | /* 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] | 1311 | # define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1312 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1313 | # define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1314 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1315 | /* Restores tty foreground process group, and exits. |
| 1316 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1317 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1318 | * or called directly with -EXITCODE. |
| 1319 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1320 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1321 | static void sigexit(int sig) |
| 1322 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1323 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1324 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1325 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1326 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1327 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1328 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1329 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1330 | |
| 1331 | /* Not a signal, just exit */ |
| 1332 | if (sig <= 0) |
| 1333 | _exit(- sig); |
| 1334 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1335 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1336 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1337 | #else |
| 1338 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1339 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1340 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1341 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1342 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1343 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1344 | /* Restores tty foreground process group, and exits. */ |
| 1345 | static void hush_exit(int exitcode) NORETURN; |
| 1346 | static void hush_exit(int exitcode) |
| 1347 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1348 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1349 | /* Prevent recursion: |
| 1350 | * trap "echo Hi; exit" EXIT; exit |
| 1351 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1352 | char *argv[3]; |
| 1353 | /* argv[0] is unused */ |
| 1354 | argv[1] = G.traps[0]; |
| 1355 | argv[2] = NULL; |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1356 | G.traps[0] = NULL; |
| 1357 | G.exiting = 1; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1358 | builtin_eval(argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1359 | /* free(argv[1]); - why bother */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1360 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1361 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1362 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1363 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1364 | sigexit(- (exitcode & 0xff)); |
| 1365 | #else |
| 1366 | exit(exitcode); |
| 1367 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1370 | static int check_and_run_traps(int sig) |
| 1371 | { |
Dan Fandrich | fdd7b56 | 2010-06-18 22:37:42 -0700 | [diff] [blame] | 1372 | static const struct timespec zero_timespec; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1373 | smalluint save_rcode; |
| 1374 | int last_sig = 0; |
| 1375 | |
| 1376 | if (sig) |
| 1377 | goto jump_in; |
| 1378 | while (1) { |
| 1379 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1380 | if (sig <= 0) |
| 1381 | break; |
| 1382 | jump_in: |
| 1383 | last_sig = sig; |
| 1384 | if (G.traps && G.traps[sig]) { |
| 1385 | if (G.traps[sig][0]) { |
| 1386 | /* We have user-defined handler */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1387 | char *argv[3]; |
| 1388 | /* argv[0] is unused */ |
| 1389 | argv[1] = G.traps[sig]; |
| 1390 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1391 | save_rcode = G.last_exitcode; |
| 1392 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 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 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1452 | static struct variable **get_ptr_to_local_var(const char *name, unsigned len) |
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; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1456 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1457 | pp = &G.top_var; |
| 1458 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1459 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1460 | return pp; |
| 1461 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1462 | } |
| 1463 | return NULL; |
| 1464 | } |
| 1465 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1466 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1467 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1468 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1469 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1470 | |
| 1471 | if (G.expanded_assignments) { |
| 1472 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1473 | while (*cpp) { |
| 1474 | char *cp = *cpp; |
| 1475 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1476 | return cp + len + 1; |
| 1477 | cpp++; |
| 1478 | } |
| 1479 | } |
| 1480 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1481 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1482 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1483 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1484 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1485 | if (strcmp(name, "PPID") == 0) |
| 1486 | return utoa(G.root_ppid); |
| 1487 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1488 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1489 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1490 | return utoa(next_random(&G.random_gen)); |
| 1491 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1492 | return NULL; |
| 1493 | } |
| 1494 | |
| 1495 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1496 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1497 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1498 | * 0: do not change export flag |
| 1499 | * (if creating new variable, flag will be 0) |
| 1500 | * 1: set export flag and putenv the variable |
| 1501 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1502 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1503 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1504 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1505 | /* all params are used */ |
| 1506 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1507 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1508 | set_local_var(str, flg_export, local_lvl) |
| 1509 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1510 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1511 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1512 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1513 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1514 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1515 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1516 | 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] | 1517 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1518 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1519 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1520 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1521 | int name_len; |
| 1522 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1523 | eq_sign = strchr(str, '='); |
| 1524 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1525 | free(str); |
| 1526 | return -1; |
| 1527 | } |
| 1528 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1529 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1530 | var_pp = &G.top_var; |
| 1531 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1532 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1533 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1534 | continue; |
| 1535 | } |
| 1536 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1537 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1538 | #if !BB_MMU |
| 1539 | if (!flg_read_only) |
| 1540 | #endif |
| 1541 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1542 | free(str); |
| 1543 | return -1; |
| 1544 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1545 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1546 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1547 | *eq_sign = '\0'; |
| 1548 | unsetenv(str); |
| 1549 | *eq_sign = '='; |
| 1550 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1551 | #if ENABLE_HUSH_LOCAL |
| 1552 | if (cur->func_nest_level < local_lvl) { |
| 1553 | /* New variable is declared as local, |
| 1554 | * and existing one is global, or local |
| 1555 | * from enclosing function. |
| 1556 | * Remove and save old one: */ |
| 1557 | *var_pp = cur->next; |
| 1558 | cur->next = *G.shadowed_vars_pp; |
| 1559 | *G.shadowed_vars_pp = cur; |
| 1560 | /* bash 3.2.33(1) and exported vars: |
| 1561 | * # export z=z |
| 1562 | * # f() { local z=a; env | grep ^z; } |
| 1563 | * # f |
| 1564 | * z=a |
| 1565 | * # env | grep ^z |
| 1566 | * z=z |
| 1567 | */ |
| 1568 | if (cur->flg_export) |
| 1569 | flg_export = 1; |
| 1570 | break; |
| 1571 | } |
| 1572 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1573 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1574 | free_and_exp: |
| 1575 | free(str); |
| 1576 | goto exp; |
| 1577 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1578 | if (cur->max_len != 0) { |
| 1579 | if (cur->max_len >= strlen(str)) { |
| 1580 | /* This one is from startup env, reuse space */ |
| 1581 | strcpy(cur->varstr, str); |
| 1582 | goto free_and_exp; |
| 1583 | } |
| 1584 | } else { |
| 1585 | /* max_len == 0 signifies "malloced" var, which we can |
| 1586 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1587 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1588 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1589 | cur->max_len = 0; |
| 1590 | goto set_str_and_exp; |
| 1591 | } |
| 1592 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1593 | /* Not found - create new variable struct */ |
| 1594 | cur = xzalloc(sizeof(*cur)); |
| 1595 | #if ENABLE_HUSH_LOCAL |
| 1596 | cur->func_nest_level = local_lvl; |
| 1597 | #endif |
| 1598 | cur->next = *var_pp; |
| 1599 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1600 | |
| 1601 | set_str_and_exp: |
| 1602 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1603 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1604 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1605 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1606 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1607 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1608 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1609 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1610 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1611 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1612 | if (flg_export == -1) { |
| 1613 | cur->flg_export = 0; |
| 1614 | /* unsetenv was already done */ |
| 1615 | } else { |
| 1616 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1617 | return putenv(cur->varstr); |
| 1618 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1619 | } |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1623 | /* Used at startup and after each cd */ |
| 1624 | static void set_pwd_var(int exp) |
| 1625 | { |
| 1626 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1627 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1628 | } |
| 1629 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1630 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1631 | { |
| 1632 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1633 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1634 | |
| 1635 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1636 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1637 | var_pp = &G.top_var; |
| 1638 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1639 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1640 | if (cur->flg_read_only) { |
| 1641 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1642 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1643 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1644 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1645 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1646 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1647 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1648 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1649 | if (!cur->max_len) |
| 1650 | free(cur->varstr); |
| 1651 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1652 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1653 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1654 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1655 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1656 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1659 | static int unset_local_var(const char *name) |
| 1660 | { |
| 1661 | return unset_local_var_len(name, strlen(name)); |
| 1662 | } |
| 1663 | |
| 1664 | static void unset_vars(char **strings) |
| 1665 | { |
| 1666 | char **v; |
| 1667 | |
| 1668 | if (!strings) |
| 1669 | return; |
| 1670 | v = strings; |
| 1671 | while (*v) { |
| 1672 | const char *eq = strchrnul(*v, '='); |
| 1673 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1674 | v++; |
| 1675 | } |
| 1676 | free(strings); |
| 1677 | } |
| 1678 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1679 | 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] | 1680 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1681 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1682 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1683 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1684 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1685 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1686 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1687 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1688 | */ |
| 1689 | static void add_vars(struct variable *var) |
| 1690 | { |
| 1691 | struct variable *next; |
| 1692 | |
| 1693 | while (var) { |
| 1694 | next = var->next; |
| 1695 | var->next = G.top_var; |
| 1696 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1697 | if (var->flg_export) { |
| 1698 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1699 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1700 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1701 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1702 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1703 | var = next; |
| 1704 | } |
| 1705 | } |
| 1706 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1707 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1708 | { |
| 1709 | char **s; |
| 1710 | struct variable *old = NULL; |
| 1711 | |
| 1712 | if (!strings) |
| 1713 | return old; |
| 1714 | s = strings; |
| 1715 | while (*s) { |
| 1716 | struct variable *var_p; |
| 1717 | struct variable **var_pp; |
| 1718 | char *eq; |
| 1719 | |
| 1720 | eq = strchr(*s, '='); |
| 1721 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1722 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1723 | if (var_pp) { |
| 1724 | /* Remove variable from global linked list */ |
| 1725 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1726 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1727 | *var_pp = var_p->next; |
| 1728 | /* Add it to returned list */ |
| 1729 | var_p->next = old; |
| 1730 | old = var_p; |
| 1731 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1732 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1733 | } |
| 1734 | s++; |
| 1735 | } |
| 1736 | return old; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1741 | * in_str support |
| 1742 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1743 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1744 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1745 | int ch = *i->p; |
| 1746 | if (ch != '\0') { |
| 1747 | i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1748 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1749 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1750 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1753 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1754 | { |
| 1755 | return *i->p; |
| 1756 | } |
| 1757 | |
| 1758 | #if ENABLE_HUSH_INTERACTIVE |
| 1759 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1760 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1761 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1762 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1763 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1764 | if (G.PS1 == NULL) |
| 1765 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1766 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1767 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1768 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1769 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1770 | if (G.PS2 == NULL) |
| 1771 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1772 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1773 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 1774 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1775 | { |
| 1776 | const char *prompt_str; |
| 1777 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1778 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1779 | /* Set up the prompt */ |
| 1780 | if (promptmode == 0) { /* PS1 */ |
| 1781 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1782 | /* bash uses $PWD value, even if it is set by user. |
| 1783 | * It uses current dir only if PWD is unset. |
| 1784 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1785 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1786 | prompt_str = G.PS1; |
| 1787 | } else |
| 1788 | prompt_str = G.PS2; |
| 1789 | } else |
| 1790 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1791 | debug_printf("result '%s'\n", prompt_str); |
| 1792 | return prompt_str; |
| 1793 | } |
| 1794 | |
| 1795 | static void get_user_input(struct in_str *i) |
| 1796 | { |
| 1797 | int r; |
| 1798 | const char *prompt_str; |
| 1799 | |
| 1800 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1801 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1802 | /* Enable command line editing only while a command line |
| 1803 | * is actually being read */ |
| 1804 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1805 | G.flag_SIGINT = 0; |
| 1806 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1807 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1808 | 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] | 1809 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1810 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1811 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1812 | i->eof_flag = (r < 0); |
| 1813 | if (i->eof_flag) { /* EOF/error detected */ |
| 1814 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1815 | G.user_input_buf[1] = '\0'; |
| 1816 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1817 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1818 | do { |
| 1819 | G.flag_SIGINT = 0; |
| 1820 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1821 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1822 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1823 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1824 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1825 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1826 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1827 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1828 | i->p = G.user_input_buf; |
| 1829 | } |
| 1830 | |
| 1831 | #endif /* INTERACTIVE */ |
| 1832 | |
| 1833 | /* This is the magic location that prints prompts |
| 1834 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1835 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1836 | { |
| 1837 | int ch; |
| 1838 | |
| 1839 | /* If there is data waiting, eat it up */ |
| 1840 | if (i->p && *i->p) { |
| 1841 | #if ENABLE_HUSH_INTERACTIVE |
| 1842 | take_cached: |
| 1843 | #endif |
| 1844 | ch = *i->p++; |
| 1845 | if (i->eof_flag && !*i->p) |
| 1846 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1847 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1848 | } else { |
| 1849 | /* need to double check i->file because we might be doing something |
| 1850 | * more complicated by now, like sourcing or substituting. */ |
| 1851 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1852 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1853 | do { |
| 1854 | get_user_input(i); |
| 1855 | } while (!*i->p); /* need non-empty line */ |
| 1856 | i->promptmode = 1; /* PS2 */ |
| 1857 | i->promptme = 0; |
| 1858 | goto take_cached; |
| 1859 | } |
| 1860 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1861 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1862 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1863 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1864 | #if ENABLE_HUSH_INTERACTIVE |
| 1865 | if (ch == '\n') |
| 1866 | i->promptme = 1; |
| 1867 | #endif |
| 1868 | return ch; |
| 1869 | } |
| 1870 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1871 | /* All callers guarantee this routine will never |
| 1872 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1873 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1874 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1875 | { |
| 1876 | int ch; |
| 1877 | if (i->p && *i->p) { |
| 1878 | if (i->eof_flag && !i->p[1]) |
| 1879 | return EOF; |
| 1880 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1881 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1882 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1883 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1884 | i->eof_flag = (ch == EOF); |
| 1885 | i->peek_buf[0] = ch; |
| 1886 | i->peek_buf[1] = '\0'; |
| 1887 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1888 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1889 | return ch; |
| 1890 | } |
| 1891 | |
| 1892 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1893 | { |
| 1894 | i->peek = file_peek; |
| 1895 | i->get = file_get; |
| 1896 | #if ENABLE_HUSH_INTERACTIVE |
| 1897 | i->promptme = 1; |
| 1898 | i->promptmode = 0; /* PS1 */ |
| 1899 | #endif |
| 1900 | i->file = f; |
| 1901 | i->p = NULL; |
| 1902 | } |
| 1903 | |
| 1904 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1905 | { |
| 1906 | i->peek = static_peek; |
| 1907 | i->get = static_get; |
| 1908 | #if ENABLE_HUSH_INTERACTIVE |
| 1909 | i->promptme = 1; |
| 1910 | i->promptmode = 0; /* PS1 */ |
| 1911 | #endif |
| 1912 | i->p = s; |
| 1913 | i->eof_flag = 0; |
| 1914 | } |
| 1915 | |
| 1916 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1917 | /* |
| 1918 | * o_string support |
| 1919 | */ |
| 1920 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1921 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1922 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1923 | { |
| 1924 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 1925 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1926 | if (o->data) |
| 1927 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1930 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1931 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1932 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1933 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1936 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 1937 | { |
| 1938 | free(o->data); |
| 1939 | } |
| 1940 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1941 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1942 | { |
| 1943 | if (o->length + len > o->maxlen) { |
| 1944 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 1945 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 1946 | } |
| 1947 | } |
| 1948 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1949 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1950 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1951 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 1952 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1953 | o->data[o->length] = ch; |
| 1954 | o->length++; |
| 1955 | o->data[o->length] = '\0'; |
| 1956 | } |
| 1957 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1958 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1959 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 1960 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1961 | memcpy(&o->data[o->length], str, len); |
| 1962 | o->length += len; |
| 1963 | o->data[o->length] = '\0'; |
| 1964 | } |
| 1965 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1966 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1967 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1968 | o_addblock(o, str, strlen(str)); |
| 1969 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 1970 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 1971 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1972 | static void nommu_addchr(o_string *o, int ch) |
| 1973 | { |
| 1974 | if (o) |
| 1975 | o_addchr(o, ch); |
| 1976 | } |
| 1977 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1978 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1979 | #endif |
| 1980 | |
| 1981 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 1982 | { |
| 1983 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 1986 | 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] | 1987 | { |
| 1988 | while (len) { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1989 | len--; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 1990 | o_addchr(o, *str); |
| 1991 | if (*str++ == '\\') { |
| 1992 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 1993 | o_addchr(o, '\\'); |
| 1994 | if (len) { |
| 1995 | len--; |
| 1996 | o_addchr(o, '\\'); |
| 1997 | o_addchr(o, *str++); |
| 1998 | } |
| 1999 | } |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2000 | } |
| 2001 | } |
| 2002 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2003 | #undef HUSH_BRACE_EXP |
| 2004 | /* |
| 2005 | * HUSH_BRACE_EXP code needs corresponding quoting on variable expansion side. |
| 2006 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2007 | * Apparently, on unquoted $v bash still does globbing |
| 2008 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2009 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2010 | * quoting mechanisms on $v expansion side: one protects |
| 2011 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2012 | * We have only second one. |
| 2013 | */ |
| 2014 | |
| 2015 | #ifdef HUSH_BRACE_EXP |
| 2016 | # define MAYBE_BRACES "{}" |
| 2017 | #else |
| 2018 | # define MAYBE_BRACES "" |
| 2019 | #endif |
| 2020 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2021 | /* My analysis of quoting semantics tells me that state information |
| 2022 | * is associated with a destination, not a source. |
| 2023 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2024 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2025 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2026 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2027 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2028 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2029 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2030 | o_grow_by(o, sz); |
| 2031 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2032 | o->data[o->length] = '\\'; |
| 2033 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2034 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2035 | o->data[o->length] = ch; |
| 2036 | o->length++; |
| 2037 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2040 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2041 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2042 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2043 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2044 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2045 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2046 | sz++; |
| 2047 | o->data[o->length] = '\\'; |
| 2048 | o->length++; |
| 2049 | } |
| 2050 | o_grow_by(o, sz); |
| 2051 | o->data[o->length] = ch; |
| 2052 | o->length++; |
| 2053 | o->data[o->length] = '\0'; |
| 2054 | } |
| 2055 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2056 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2057 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2058 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2059 | char ch; |
| 2060 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2061 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2062 | if (ordinary_cnt > len) /* paranoia */ |
| 2063 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2064 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2065 | if (ordinary_cnt == len) |
| 2066 | return; |
| 2067 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2068 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2069 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2070 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2071 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2072 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2073 | sz++; |
| 2074 | o->data[o->length] = '\\'; |
| 2075 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2076 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2077 | o_grow_by(o, sz); |
| 2078 | o->data[o->length] = ch; |
| 2079 | o->length++; |
| 2080 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2081 | } |
| 2082 | } |
| 2083 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2084 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2085 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2086 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2087 | o_addblock(o, str, len); |
| 2088 | return; |
| 2089 | } |
| 2090 | o_addqblock(o, str, len); |
| 2091 | } |
| 2092 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2093 | static void o_addQstr(o_string *o, const char *str) |
| 2094 | { |
| 2095 | o_addQblock(o, str, strlen(str)); |
| 2096 | } |
| 2097 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2098 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2099 | * 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] | 2100 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2101 | * list[i] contains an INDEX (int!) into this string data. |
| 2102 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2103 | * but list[i]'s need not be modified. |
| 2104 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2105 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2106 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2107 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2108 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2109 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2110 | { |
| 2111 | char **list = (char**)o->data; |
| 2112 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2113 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2114 | |
| 2115 | indent(); |
Denys Vlasenko | e298ce6 | 2010-09-04 19:52:44 +0200 | [diff] [blame] | 2116 | fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d glob:%d quoted:%d escape:%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2117 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2118 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2119 | o->has_quoted_part, |
| 2120 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2121 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2122 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2123 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 2124 | o->data + (int)list[i] + string_start, |
| 2125 | o->data + (int)list[i] + string_start); |
| 2126 | i++; |
| 2127 | } |
| 2128 | if (n) { |
| 2129 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2130 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2131 | 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] | 2132 | } |
| 2133 | } |
| 2134 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2135 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2136 | #endif |
| 2137 | |
| 2138 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2139 | * in list[n] so that it points past last stored byte so far. |
| 2140 | * It returns n+1. */ |
| 2141 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2142 | { |
| 2143 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2144 | int string_start; |
| 2145 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2146 | |
| 2147 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2148 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2149 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2150 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2151 | 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] | 2152 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2153 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2154 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2155 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2156 | memmove(list + n + 0x10, list + n, string_len); |
| 2157 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2158 | } else { |
| 2159 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2160 | n, string_len, string_start); |
| 2161 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2162 | } else { |
| 2163 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2164 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2165 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2166 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2167 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2168 | o->has_empty_slot = 0; |
| 2169 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2170 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2171 | return n + 1; |
| 2172 | } |
| 2173 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2174 | /* "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] | 2175 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2176 | { |
| 2177 | char **list = (char**)o->data; |
| 2178 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2179 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2180 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2183 | #ifdef HUSH_BRACE_EXP |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2184 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2185 | * first, it processes even {a} (no commas), second, |
| 2186 | * 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] | 2187 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2188 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2189 | |
| 2190 | /* Helper */ |
| 2191 | static int glob_needed(const char *s) |
| 2192 | { |
| 2193 | while (*s) { |
| 2194 | if (*s == '\\') { |
| 2195 | if (!s[1]) |
| 2196 | return 0; |
| 2197 | s += 2; |
| 2198 | continue; |
| 2199 | } |
| 2200 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2201 | return 1; |
| 2202 | s++; |
| 2203 | } |
| 2204 | return 0; |
| 2205 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2206 | /* Return pointer to next closing brace or to comma */ |
| 2207 | static const char *next_brace_sub(const char *cp) |
| 2208 | { |
| 2209 | unsigned depth = 0; |
| 2210 | cp++; |
| 2211 | while (*cp != '\0') { |
| 2212 | if (*cp == '\\') { |
| 2213 | if (*++cp == '\0') |
| 2214 | break; |
| 2215 | cp++; |
| 2216 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2217 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2218 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2219 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2220 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2221 | depth++; |
| 2222 | } |
| 2223 | |
| 2224 | return *cp != '\0' ? cp : NULL; |
| 2225 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2226 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2227 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2228 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2229 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2230 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2231 | const char *next; |
| 2232 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2233 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2234 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2235 | |
| 2236 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2237 | |
| 2238 | begin = pattern; |
| 2239 | while (1) { |
| 2240 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2241 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2242 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2243 | /* Find the first sub-pattern and at the same time |
| 2244 | * find the rest after the closing brace */ |
| 2245 | next = next_brace_sub(begin); |
| 2246 | if (next == NULL) { |
| 2247 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2248 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2249 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2250 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2251 | /* "{abc}" with no commas - illegal |
| 2252 | * brace expr, disregard and skip it */ |
| 2253 | begin = next + 1; |
| 2254 | continue; |
| 2255 | } |
| 2256 | break; |
| 2257 | } |
| 2258 | if (*begin == '\\' && begin[1] != '\0') |
| 2259 | begin++; |
| 2260 | begin++; |
| 2261 | } |
| 2262 | debug_printf_glob("begin:%s\n", begin); |
| 2263 | debug_printf_glob("next:%s\n", next); |
| 2264 | |
| 2265 | /* Now find the end of the whole brace expression */ |
| 2266 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2267 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2268 | rest = next_brace_sub(rest); |
| 2269 | if (rest == NULL) { |
| 2270 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2271 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2272 | } |
| 2273 | debug_printf_glob("rest:%s\n", rest); |
| 2274 | } |
| 2275 | rest_len = strlen(++rest) + 1; |
| 2276 | |
| 2277 | /* We are sure the brace expression is well-formed */ |
| 2278 | |
| 2279 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2280 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2281 | |
| 2282 | /* We have a brace expression. BEGIN points to the opening {, |
| 2283 | * NEXT points past the terminator of the first element, and REST |
| 2284 | * points past the final }. We will accumulate result names from |
| 2285 | * recursive runs for each brace alternative in the buffer using |
| 2286 | * GLOB_APPEND. */ |
| 2287 | |
| 2288 | p = begin + 1; |
| 2289 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2290 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2291 | memcpy( |
| 2292 | mempcpy( |
| 2293 | mempcpy(new_pattern_buf, |
| 2294 | /* We know the prefix for all sub-patterns */ |
| 2295 | pattern, begin - pattern), |
| 2296 | p, next - p), |
| 2297 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2298 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2299 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2300 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2301 | */ |
| 2302 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2303 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2304 | /* We saw the last entry */ |
| 2305 | break; |
| 2306 | } |
| 2307 | p = next + 1; |
| 2308 | next = next_brace_sub(next); |
| 2309 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2310 | free(new_pattern_buf); |
| 2311 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2312 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2313 | simple_glob: |
| 2314 | { |
| 2315 | int gr; |
| 2316 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2317 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2318 | memset(&globdata, 0, sizeof(globdata)); |
| 2319 | gr = glob(pattern, 0, NULL, &globdata); |
| 2320 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2321 | if (gr != 0) { |
| 2322 | if (gr == GLOB_NOMATCH) { |
| 2323 | globfree(&globdata); |
| 2324 | /* NB: garbles parameter */ |
| 2325 | unbackslash(pattern); |
| 2326 | o_addstr_with_NUL(o, pattern); |
| 2327 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2328 | return o_save_ptr_helper(o, n); |
| 2329 | } |
| 2330 | if (gr == GLOB_NOSPACE) |
| 2331 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2332 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2333 | * but we didn't specify it. Paranoia again. */ |
| 2334 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2335 | } |
| 2336 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2337 | char **argv = globdata.gl_pathv; |
| 2338 | while (1) { |
| 2339 | o_addstr_with_NUL(o, *argv); |
| 2340 | n = o_save_ptr_helper(o, n); |
| 2341 | argv++; |
| 2342 | if (!*argv) |
| 2343 | break; |
| 2344 | } |
| 2345 | } |
| 2346 | globfree(&globdata); |
| 2347 | } |
| 2348 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2349 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2350 | /* Performs globbing on last list[], |
| 2351 | * saving each result as a new list[]. |
| 2352 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2353 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2354 | { |
| 2355 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2356 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2357 | debug_printf_glob("start perform_glob: n:%d o->data:%p\n", n, o->data); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2358 | if (!o->data) |
| 2359 | return o_save_ptr_helper(o, n); |
| 2360 | pattern = o->data + o_get_last_ptr(o, n); |
| 2361 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2362 | if (!glob_needed(pattern)) { |
| 2363 | /* unbackslash last string in o in place, fix length */ |
| 2364 | o->length = unbackslash(pattern) - o->data; |
| 2365 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2366 | return o_save_ptr_helper(o, n); |
| 2367 | } |
| 2368 | |
| 2369 | copy = xstrdup(pattern); |
| 2370 | /* "forget" pattern in o */ |
| 2371 | o->length = pattern - o->data; |
| 2372 | n = glob_brace(copy, o, n); |
| 2373 | free(copy); |
| 2374 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2375 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2376 | return n; |
| 2377 | } |
| 2378 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2379 | #else /* !HUSH_BRACE_EXP */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2380 | |
| 2381 | /* Helper */ |
| 2382 | static int glob_needed(const char *s) |
| 2383 | { |
| 2384 | while (*s) { |
| 2385 | if (*s == '\\') { |
| 2386 | if (!s[1]) |
| 2387 | return 0; |
| 2388 | s += 2; |
| 2389 | continue; |
| 2390 | } |
| 2391 | if (*s == '*' || *s == '[' || *s == '?') |
| 2392 | return 1; |
| 2393 | s++; |
| 2394 | } |
| 2395 | return 0; |
| 2396 | } |
| 2397 | /* Performs globbing on last list[], |
| 2398 | * saving each result as a new list[]. |
| 2399 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2400 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2401 | { |
| 2402 | glob_t globdata; |
| 2403 | int gr; |
| 2404 | char *pattern; |
| 2405 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2406 | debug_printf_glob("start perform_glob: n:%d o->data:%p\n", n, o->data); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2407 | if (!o->data) |
| 2408 | return o_save_ptr_helper(o, n); |
| 2409 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2410 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2411 | if (!glob_needed(pattern)) { |
| 2412 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2413 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2414 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2415 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2416 | return o_save_ptr_helper(o, n); |
| 2417 | } |
| 2418 | |
| 2419 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2420 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2421 | * If we glob "*.\*" and don't find anything, we need |
| 2422 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2423 | * will return "*.\*"! |
| 2424 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2425 | gr = glob(pattern, 0, NULL, &globdata); |
| 2426 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2427 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2428 | if (gr == GLOB_NOMATCH) { |
| 2429 | globfree(&globdata); |
| 2430 | goto literal; |
| 2431 | } |
| 2432 | if (gr == GLOB_NOSPACE) |
| 2433 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2434 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2435 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2436 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2437 | } |
| 2438 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2439 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2440 | /* "forget" pattern in o */ |
| 2441 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2442 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2443 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2444 | n = o_save_ptr_helper(o, n); |
| 2445 | argv++; |
| 2446 | if (!*argv) |
| 2447 | break; |
| 2448 | } |
| 2449 | } |
| 2450 | globfree(&globdata); |
| 2451 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2452 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2453 | return n; |
| 2454 | } |
| 2455 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 2456 | #endif /* !HUSH_BRACE_EXP */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2457 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2458 | /* If o->o_expflags & EXP_FLAG_GLOB, glob the string so far remembered. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2459 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2460 | static int o_save_ptr(o_string *o, int n) |
| 2461 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2462 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2463 | /* If o->has_empty_slot, list[n] was already globbed |
| 2464 | * (if it was requested back then when it was filled) |
| 2465 | * so don't do that again! */ |
| 2466 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2467 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2468 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2469 | return o_save_ptr_helper(o, n); |
| 2470 | } |
| 2471 | |
| 2472 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2473 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2474 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2475 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2476 | int string_start; |
| 2477 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2478 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2479 | if (DEBUG_EXPAND) |
| 2480 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2481 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2482 | list = (char**)o->data; |
| 2483 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2484 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2485 | while (n) { |
| 2486 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2487 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2488 | } |
| 2489 | return list; |
| 2490 | } |
| 2491 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2492 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2493 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2494 | /* Returns pi->next - next pipe in the list */ |
| 2495 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2496 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2497 | struct pipe *next; |
| 2498 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2499 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2500 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2501 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2502 | struct command *command; |
| 2503 | struct redir_struct *r, *rnext; |
| 2504 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2505 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2506 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2507 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2508 | if (DEBUG_CLEAN) { |
| 2509 | int a; |
| 2510 | char **p; |
| 2511 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2512 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2513 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2514 | } |
| 2515 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2516 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2517 | } |
| 2518 | /* not "else if": on syntax error, we may have both! */ |
| 2519 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2520 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2521 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2522 | free_pipe_list(command->group); |
| 2523 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2524 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2525 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2526 | /* else is crucial here. |
| 2527 | * If group != NULL, child_func is meaningless */ |
| 2528 | #if ENABLE_HUSH_FUNCTIONS |
| 2529 | else if (command->child_func) { |
| 2530 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2531 | command->child_func->parent_cmd = NULL; |
| 2532 | } |
| 2533 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2534 | #if !BB_MMU |
| 2535 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2536 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2537 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2538 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2539 | debug_printf_clean(" redirect %d%s", |
| 2540 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2541 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2542 | if (r->rd_filename) { |
| 2543 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2544 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2545 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2546 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2547 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2548 | rnext = r->next; |
| 2549 | free(r); |
| 2550 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2551 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2552 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2553 | free(pi->cmds); /* children are an array, they get freed all at once */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2554 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2555 | #if ENABLE_HUSH_JOB |
| 2556 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2557 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2558 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2559 | |
| 2560 | next = pi->next; |
| 2561 | free(pi); |
| 2562 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2563 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2564 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2565 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2566 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2567 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2568 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2569 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2570 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2571 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2572 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2573 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 2577 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2578 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2579 | static struct pipe *new_pipe(void) |
| 2580 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2581 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2582 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2583 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2584 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2585 | return pi; |
| 2586 | } |
| 2587 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2588 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 2589 | * if ctx->command is NULL. |
| 2590 | * No errors possible here. |
| 2591 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2592 | static int done_command(struct parse_context *ctx) |
| 2593 | { |
| 2594 | /* The command is really already in the pipe structure, so |
| 2595 | * advance the pipe counter and make a new, null command. */ |
| 2596 | struct pipe *pi = ctx->pipe; |
| 2597 | struct command *command = ctx->command; |
| 2598 | |
| 2599 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2600 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2601 | 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] | 2602 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2603 | } |
| 2604 | pi->num_cmds++; |
| 2605 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2606 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2607 | } else { |
| 2608 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 2609 | } |
| 2610 | |
| 2611 | /* Only real trickiness here is that the uncommitted |
| 2612 | * command structure is not counted in pi->num_cmds. */ |
| 2613 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2614 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 2615 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2616 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2617 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 2618 | } |
| 2619 | |
| 2620 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 2621 | { |
| 2622 | int not_null; |
| 2623 | |
| 2624 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 2625 | /* Close previous command */ |
| 2626 | not_null = done_command(ctx); |
| 2627 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2628 | #if HAS_KEYWORDS |
| 2629 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 2630 | ctx->ctx_inverted = 0; |
| 2631 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 2632 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2633 | |
| 2634 | /* Without this check, even just <enter> on command line generates |
| 2635 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2636 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2637 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2638 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2639 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2640 | #endif |
| 2641 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2642 | || ctx->ctx_res_w == RES_DONE |
| 2643 | || ctx->ctx_res_w == RES_FOR |
| 2644 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2645 | #endif |
| 2646 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2647 | || ctx->ctx_res_w == RES_ESAC |
| 2648 | #endif |
| 2649 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2650 | struct pipe *new_p; |
| 2651 | debug_printf_parse("done_pipe: adding new pipe: " |
| 2652 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 2653 | not_null, ctx->ctx_res_w); |
| 2654 | new_p = new_pipe(); |
| 2655 | ctx->pipe->next = new_p; |
| 2656 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2657 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2658 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2659 | * This is used to control execution. |
| 2660 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 2661 | * cases where variable or value happens to match a keyword): |
| 2662 | */ |
| 2663 | #if ENABLE_HUSH_LOOPS |
| 2664 | if (ctx->ctx_res_w == RES_FOR |
| 2665 | || ctx->ctx_res_w == RES_IN) |
| 2666 | ctx->ctx_res_w = RES_NONE; |
| 2667 | #endif |
| 2668 | #if ENABLE_HUSH_CASE |
| 2669 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2670 | ctx->ctx_res_w = RES_CASE_BODY; |
| 2671 | if (ctx->ctx_res_w == RES_CASE) |
| 2672 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2673 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2674 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2675 | /* Create the memory for command, roughly: |
| 2676 | * ctx->pipe->cmds = new struct command; |
| 2677 | * ctx->command = &ctx->pipe->cmds[0]; |
| 2678 | */ |
| 2679 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2680 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2681 | } |
| 2682 | debug_printf_parse("done_pipe return\n"); |
| 2683 | } |
| 2684 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2685 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2686 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2687 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2688 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2689 | /* Create the memory for command, roughly: |
| 2690 | * ctx->pipe->cmds = new struct command; |
| 2691 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2692 | */ |
| 2693 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2696 | /* If a reserved word is found and processed, parse context is modified |
| 2697 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2698 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2699 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2700 | struct reserved_combo { |
| 2701 | char literal[6]; |
| 2702 | unsigned char res; |
| 2703 | unsigned char assignment_flag; |
| 2704 | int flag; |
| 2705 | }; |
| 2706 | enum { |
| 2707 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2708 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2709 | FLAG_IF = (1 << RES_IF ), |
| 2710 | FLAG_THEN = (1 << RES_THEN ), |
| 2711 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2712 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2713 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2714 | # endif |
| 2715 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2716 | FLAG_FOR = (1 << RES_FOR ), |
| 2717 | FLAG_WHILE = (1 << RES_WHILE), |
| 2718 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2719 | FLAG_DO = (1 << RES_DO ), |
| 2720 | FLAG_DONE = (1 << RES_DONE ), |
| 2721 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2722 | # endif |
| 2723 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2724 | FLAG_MATCH = (1 << RES_MATCH), |
| 2725 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2726 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2727 | FLAG_START = (1 << RES_XXXX ), |
| 2728 | }; |
| 2729 | |
| 2730 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 2731 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2732 | /* Mostly a list of accepted follow-up reserved words. |
| 2733 | * FLAG_END means we are done with the sequence, and are ready |
| 2734 | * to turn the compound list into a command. |
| 2735 | * FLAG_START means the word must start a new compound list. |
| 2736 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2737 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2738 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2739 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 2740 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 2741 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 2742 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 2743 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 2744 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2745 | # endif |
| 2746 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2747 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 2748 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2749 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2750 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 2751 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 2752 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2753 | # endif |
| 2754 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2755 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 2756 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2757 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2758 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2759 | const struct reserved_combo *r; |
| 2760 | |
| 2761 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
| 2762 | if (strcmp(word->data, r->literal) == 0) |
| 2763 | return r; |
| 2764 | } |
| 2765 | return NULL; |
| 2766 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2767 | /* Return 0: not a keyword, 1: keyword |
| 2768 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2769 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 2770 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2771 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2772 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2773 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2774 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2775 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2776 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2777 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2778 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2779 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2780 | r = match_reserved_word(word); |
| 2781 | if (!r) |
| 2782 | return 0; |
| 2783 | |
| 2784 | 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] | 2785 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2786 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 2787 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2788 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2789 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2790 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2791 | if (r->flag == 0) { /* '!' */ |
| 2792 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2793 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2794 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2795 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2796 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2797 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2798 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2799 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2800 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2801 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2802 | old = xmalloc(sizeof(*old)); |
| 2803 | debug_printf_parse("push stack %p\n", old); |
| 2804 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2805 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2806 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2807 | } 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] | 2808 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2809 | ctx->ctx_res_w = RES_SNTX; |
| 2810 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2811 | } else { |
| 2812 | /* "{...} fi" is ok. "{...} if" is not |
| 2813 | * Example: |
| 2814 | * if { echo foo; } then { echo bar; } fi */ |
| 2815 | if (ctx->command->group) |
| 2816 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2817 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2818 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2819 | ctx->ctx_res_w = r->res; |
| 2820 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2821 | word->o_assignment = r->assignment_flag; |
| 2822 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2823 | if (ctx->old_flag & FLAG_END) { |
| 2824 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2825 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2826 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2827 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2828 | old = ctx->stack; |
| 2829 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2830 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2831 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2832 | o_addstr(&old->as_string, ctx->as_string.data); |
| 2833 | o_free_unsafe(&ctx->as_string); |
| 2834 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 2835 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 2836 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2837 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2838 | *ctx = *old; /* physical copy */ |
| 2839 | free(old); |
| 2840 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2841 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2842 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2843 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2844 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2845 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2846 | * Normal return is 0. Syntax errors return 1. |
| 2847 | * Note: on return, word is reset, but not o_free'd! |
| 2848 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2849 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2850 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2851 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2852 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2853 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2854 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2855 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 2856 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2857 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2858 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2859 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2860 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 2861 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2862 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 2863 | * "2.7 Redirection |
| 2864 | * ...the word that follows the redirection operator |
| 2865 | * shall be subjected to tilde expansion, parameter expansion, |
| 2866 | * command substitution, arithmetic expansion, and quote |
| 2867 | * removal. Pathname expansion shall not be performed |
| 2868 | * on the word by a non-interactive shell; an interactive |
| 2869 | * shell may perform it, but shall do so only when |
| 2870 | * the expansion would result in one word." |
| 2871 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2872 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2873 | /* Cater for >\file case: |
| 2874 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 2875 | * Same with heredocs: |
| 2876 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 2877 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2878 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 2879 | unbackslash(ctx->pending_redirect->rd_filename); |
| 2880 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2881 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 2882 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 2883 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2884 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2885 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 2886 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2887 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2888 | /* If this word wasn't an assignment, next ones definitely |
| 2889 | * can't be assignments. Even if they look like ones. */ |
| 2890 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 2891 | && word->o_assignment != WORD_IS_KEYWORD |
| 2892 | ) { |
| 2893 | word->o_assignment = NOT_ASSIGNMENT; |
| 2894 | } else { |
| 2895 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 2896 | command->assignment_cnt++; |
| 2897 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 2898 | } |
| 2899 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2900 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2901 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 2902 | if (ctx->ctx_dsemicolon |
| 2903 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 2904 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 2905 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2906 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 2907 | ctx->ctx_dsemicolon = 0; |
| 2908 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2909 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2910 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2911 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2912 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 2913 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2914 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2915 | # if ENABLE_HUSH_CASE |
| 2916 | && ctx->ctx_res_w != RES_CASE |
| 2917 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2918 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2919 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2920 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2921 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2922 | debug_printf_parse("done_word return %d\n", |
| 2923 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2924 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2925 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2926 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2927 | if (strcmp(word->data, "[[") == 0) { |
| 2928 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 2929 | } |
| 2930 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 2931 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2932 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2933 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2934 | if (command->group) { |
| 2935 | /* "{ echo foo; } echo bar" - bad */ |
| 2936 | syntax_error_at(word->data); |
| 2937 | debug_printf_parse("done_word return 1: syntax error, " |
| 2938 | "groups and arglists don't mix\n"); |
| 2939 | return 1; |
| 2940 | } |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2941 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 2942 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 2943 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2944 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 2945 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2946 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2947 | char *p = word->data; |
| 2948 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 2949 | && (p[1] & 0x7f) == '@' |
| 2950 | && p[2] == SPECIAL_VAR_SYMBOL |
| 2951 | ) { |
| 2952 | p += 3; |
| 2953 | } |
| 2954 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2955 | /* saw no "$@", or not only "$@" but some |
| 2956 | * real text is there too */ |
| 2957 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 2958 | * e.g. "", $empty"" etc to not disappear */ |
| 2959 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 2960 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 2961 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 2962 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 2963 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2964 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2965 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2966 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2967 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2968 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2969 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 2970 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 2971 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 2972 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 2973 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2974 | return 1; |
| 2975 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 2976 | /* Force FOR to have just one word (variable name) */ |
| 2977 | /* NB: basically, this makes hush see "for v in ..." |
| 2978 | * syntax as if it is "for v; in ...". FOR and IN become |
| 2979 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 2980 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 2981 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 2982 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2983 | #if ENABLE_HUSH_CASE |
| 2984 | /* Force CASE to have just one word */ |
| 2985 | if (ctx->ctx_res_w == RES_CASE) { |
| 2986 | done_pipe(ctx, PIPE_SEQ); |
| 2987 | } |
| 2988 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 2989 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2990 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 2991 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 2992 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2993 | return 0; |
| 2994 | } |
| 2995 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 2996 | |
| 2997 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 2998 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 2999 | * Return: |
| 3000 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3001 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3002 | * REDIRFD_TO_FILE if no & was seen, |
| 3003 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3004 | */ |
| 3005 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3006 | #define parse_redir_right_fd(as_string, input) \ |
| 3007 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3008 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3009 | 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] | 3010 | { |
| 3011 | int ch, d, ok; |
| 3012 | |
| 3013 | ch = i_peek(input); |
| 3014 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3015 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3016 | |
| 3017 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3018 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3019 | ch = i_peek(input); |
| 3020 | if (ch == '-') { |
| 3021 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3022 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3023 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3024 | } |
| 3025 | d = 0; |
| 3026 | ok = 0; |
| 3027 | while (ch != EOF && isdigit(ch)) { |
| 3028 | d = d*10 + (ch-'0'); |
| 3029 | ok = 1; |
| 3030 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3031 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3032 | ch = i_peek(input); |
| 3033 | } |
| 3034 | if (ok) return d; |
| 3035 | |
| 3036 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3037 | |
| 3038 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3039 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3042 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3043 | */ |
| 3044 | static int parse_redirect(struct parse_context *ctx, |
| 3045 | int fd, |
| 3046 | redir_type style, |
| 3047 | struct in_str *input) |
| 3048 | { |
| 3049 | struct command *command = ctx->command; |
| 3050 | struct redir_struct *redir; |
| 3051 | struct redir_struct **redirp; |
| 3052 | int dup_num; |
| 3053 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3054 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3055 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3056 | /* Check for a '>&1' type redirect */ |
| 3057 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3058 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3059 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3060 | } else { |
| 3061 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3062 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3063 | if (dup_num) { /* <<-... */ |
| 3064 | ch = i_getch(input); |
| 3065 | nommu_addchr(&ctx->as_string, ch); |
| 3066 | ch = i_peek(input); |
| 3067 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3068 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3069 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3070 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3071 | int ch = i_peek(input); |
| 3072 | if (ch == '|') { |
| 3073 | /* >|FILE redirect ("clobbering" >). |
| 3074 | * Since we do not support "set -o noclobber" yet, |
| 3075 | * >| and > are the same for now. Just eat |. |
| 3076 | */ |
| 3077 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3078 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3079 | } |
| 3080 | } |
| 3081 | |
| 3082 | /* Create a new redir_struct and append it to the linked list */ |
| 3083 | redirp = &command->redirects; |
| 3084 | while ((redir = *redirp) != NULL) { |
| 3085 | redirp = &(redir->next); |
| 3086 | } |
| 3087 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3088 | /* redir->next = NULL; */ |
| 3089 | /* redir->rd_filename = NULL; */ |
| 3090 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3091 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3092 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3093 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3094 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3095 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3096 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3097 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3098 | /* Erik had a check here that the file descriptor in question |
| 3099 | * is legit; I postpone that to "run time" |
| 3100 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3101 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3102 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3103 | } else { |
| 3104 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3105 | * end of the next parsed word. */ |
| 3106 | ctx->pending_redirect = redir; |
| 3107 | } |
| 3108 | return 0; |
| 3109 | } |
| 3110 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3111 | /* If a redirect is immediately preceded by a number, that number is |
| 3112 | * supposed to tell which file descriptor to redirect. This routine |
| 3113 | * looks for such preceding numbers. In an ideal world this routine |
| 3114 | * needs to handle all the following classes of redirects... |
| 3115 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3116 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3117 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3118 | * 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] | 3119 | * |
| 3120 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3121 | * "2.7 Redirection |
| 3122 | * ... If n is quoted, the number shall not be recognized as part of |
| 3123 | * the redirection expression. For example: |
| 3124 | * echo \2>a |
| 3125 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3126 | * We are getting it right by setting ->has_quoted_part on any \<char> |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3127 | * |
| 3128 | * A -1 return means no valid number was found, |
| 3129 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3130 | */ |
| 3131 | static int redirect_opt_num(o_string *o) |
| 3132 | { |
| 3133 | int num; |
| 3134 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3135 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3136 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3137 | num = bb_strtou(o->data, NULL, 10); |
| 3138 | if (errno || num < 0) |
| 3139 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3140 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3141 | return num; |
| 3142 | } |
| 3143 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3144 | #if BB_MMU |
| 3145 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3146 | fetch_till_str(input, word, skip_tabs) |
| 3147 | #endif |
| 3148 | static char *fetch_till_str(o_string *as_string, |
| 3149 | struct in_str *input, |
| 3150 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3151 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3152 | { |
| 3153 | o_string heredoc = NULL_O_STRING; |
| 3154 | int past_EOL = 0; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3155 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3156 | int ch; |
| 3157 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3158 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3159 | while (1) { |
| 3160 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3161 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3162 | if (ch == '\n' |
Denys Vlasenko | 83b900f | 2010-09-06 11:47:55 +0200 | [diff] [blame] | 3163 | /* TODO: or EOF? (heredoc delimiter may end with <eof>, not only <eol>) */ |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3164 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3165 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3166 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3167 | heredoc.data[past_EOL] = '\0'; |
| 3168 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3169 | return heredoc.data; |
| 3170 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3171 | do { |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3172 | o_addchr(&heredoc, '\n'); |
| 3173 | prev = 0; /* not \ */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3174 | past_EOL = heredoc.length; |
| 3175 | jump_in: |
| 3176 | do { |
| 3177 | ch = i_getch(input); |
| 3178 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3179 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3180 | } while (ch == '\n'); |
| 3181 | } |
| 3182 | if (ch == EOF) { |
| 3183 | o_free_unsafe(&heredoc); |
| 3184 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3185 | } |
| 3186 | o_addchr(&heredoc, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3187 | if (prev == '\\' && ch == '\\') |
| 3188 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3189 | prev = 0; /* not \ */ |
| 3190 | else |
| 3191 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3192 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3196 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3197 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3198 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3199 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3200 | { |
| 3201 | struct pipe *pi = ctx->list_head; |
| 3202 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3203 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3204 | int i; |
| 3205 | struct command *cmd = pi->cmds; |
| 3206 | |
| 3207 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3208 | pi->num_cmds, |
| 3209 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3210 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3211 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3212 | |
| 3213 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3214 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3215 | while (redir) { |
| 3216 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3217 | char *p; |
| 3218 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3219 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3220 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3221 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3222 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3223 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3224 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3225 | return 1; |
| 3226 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3227 | free(redir->rd_filename); |
| 3228 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3229 | heredoc_cnt--; |
| 3230 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3231 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3232 | } |
| 3233 | cmd++; |
| 3234 | } |
| 3235 | pi = pi->next; |
| 3236 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3237 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3238 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3239 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3240 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3241 | #endif |
| 3242 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3246 | static int run_list(struct pipe *pi); |
| 3247 | #if BB_MMU |
| 3248 | #define parse_stream(pstring, input, end_trigger) \ |
| 3249 | parse_stream(input, end_trigger) |
| 3250 | #endif |
| 3251 | static struct pipe *parse_stream(char **pstring, |
| 3252 | struct in_str *input, |
| 3253 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3254 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3255 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3256 | #if !ENABLE_HUSH_FUNCTIONS |
| 3257 | #define parse_group(dest, ctx, input, ch) \ |
| 3258 | parse_group(ctx, input, ch) |
| 3259 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3260 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3261 | struct in_str *input, int ch) |
| 3262 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3263 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3264 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3265 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3266 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3267 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3268 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3269 | |
| 3270 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3271 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3272 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3273 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3274 | if (done_word(dest, ctx)) |
| 3275 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3276 | if (!command->argv) |
| 3277 | goto skip; /* (... */ |
| 3278 | if (command->argv[1]) { /* word word ... (... */ |
| 3279 | syntax_error_unexpected_ch('('); |
| 3280 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3281 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3282 | /* it is "word(..." or "word (..." */ |
| 3283 | do |
| 3284 | ch = i_getch(input); |
| 3285 | while (ch == ' ' || ch == '\t'); |
| 3286 | if (ch != ')') { |
| 3287 | syntax_error_unexpected_ch(ch); |
| 3288 | return 1; |
| 3289 | } |
| 3290 | nommu_addchr(&ctx->as_string, ch); |
| 3291 | do |
| 3292 | ch = i_getch(input); |
| 3293 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3294 | if (ch != '{') { |
| 3295 | syntax_error_unexpected_ch(ch); |
| 3296 | return 1; |
| 3297 | } |
| 3298 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3299 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3300 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3301 | } |
| 3302 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3303 | |
| 3304 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3305 | if (command->argv /* word [word]{... */ |
| 3306 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3307 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3308 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3309 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3310 | debug_printf_parse("parse_group return 1: " |
| 3311 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3312 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3313 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3314 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3315 | |
| 3316 | #if ENABLE_HUSH_FUNCTIONS |
| 3317 | skip: |
| 3318 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3319 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3320 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3321 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3322 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3323 | } else { |
| 3324 | /* bash does not allow "{echo...", requires whitespace */ |
| 3325 | ch = i_getch(input); |
| 3326 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 3327 | syntax_error_unexpected_ch(ch); |
| 3328 | return 1; |
| 3329 | } |
| 3330 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3331 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3332 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3333 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3334 | #if BB_MMU |
| 3335 | # define as_string NULL |
| 3336 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3337 | char *as_string = NULL; |
| 3338 | #endif |
| 3339 | pipe_list = parse_stream(&as_string, input, endch); |
| 3340 | #if !BB_MMU |
| 3341 | if (as_string) |
| 3342 | o_addstr(&ctx->as_string, as_string); |
| 3343 | #endif |
| 3344 | /* empty ()/{} or parse error? */ |
| 3345 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3346 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3347 | if (!BB_MMU) |
| 3348 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3349 | debug_printf_parse("parse_group return 1: " |
| 3350 | "parse_stream returned %p\n", pipe_list); |
| 3351 | return 1; |
| 3352 | } |
| 3353 | command->group = pipe_list; |
| 3354 | #if !BB_MMU |
| 3355 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3356 | command->group_as_string = as_string; |
| 3357 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3358 | command->group_as_string); |
| 3359 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3360 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3361 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3362 | debug_printf_parse("parse_group return 0\n"); |
| 3363 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3364 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3365 | } |
| 3366 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3367 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3368 | /* Subroutines for copying $(...) and `...` things */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3369 | static void add_till_backquote(o_string *dest, struct in_str *input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3370 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3371 | 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] | 3372 | { |
| 3373 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3374 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3375 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3376 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3377 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3378 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3379 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3380 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3381 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3382 | } |
| 3383 | } |
| 3384 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3385 | 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] | 3386 | { |
| 3387 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3388 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3389 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3390 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3391 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3392 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3393 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3394 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3395 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3396 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3397 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3398 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3399 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3400 | if (ch == '`') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3401 | add_till_backquote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3402 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3403 | continue; |
| 3404 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3405 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3406 | } |
| 3407 | } |
| 3408 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3409 | * \` quoting. |
| 3410 | * "Within the backquoted style of command substitution, backslash |
| 3411 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3412 | * The search for the matching backquote shall be satisfied by the first |
| 3413 | * backquote found without a preceding backslash; during this search, |
| 3414 | * if a non-escaped backquote is encountered within a shell comment, |
| 3415 | * a here-document, an embedded command substitution of the $(command) |
| 3416 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3417 | * or double-quoted string that begins, but does not end, within the |
| 3418 | * "`...`" sequence produces undefined results." |
| 3419 | * Example Output |
| 3420 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3421 | */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3422 | static void add_till_backquote(o_string *dest, struct in_str *input) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3423 | { |
| 3424 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3425 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3426 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3427 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3428 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3429 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3430 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3431 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3432 | if (ch == '\\') { |
| 3433 | /* \x. Copy both chars unless it is \` */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3434 | int ch2 = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3435 | if (ch2 == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3436 | syntax_error_unterm_ch('`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3437 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3438 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3439 | if (ch2 != '`' && ch2 != '$' && ch2 != '\\') |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3440 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3441 | ch = ch2; |
| 3442 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3443 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3447 | * quoting and nested ()s. |
| 3448 | * "With the $(command) style of command substitution, all characters |
| 3449 | * following the open parenthesis to the matching closing parenthesis |
| 3450 | * constitute the command. Any valid shell script can be used for command, |
| 3451 | * except a script consisting solely of redirections which produces |
| 3452 | * unspecified results." |
| 3453 | * Example Output |
| 3454 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3455 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3456 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3457 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3458 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3459 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3460 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 3461 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3462 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3463 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3464 | 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] | 3465 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3466 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3467 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3468 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3469 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3470 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3471 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 3472 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3473 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3474 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3475 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3476 | syntax_error_unterm_ch(end_ch); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3477 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3478 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3479 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3480 | if (!dbl) |
| 3481 | break; |
| 3482 | /* we look for closing )) of $((EXPR)) */ |
| 3483 | if (i_peek(input) == end_ch) { |
| 3484 | i_getch(input); /* eat second ')' */ |
| 3485 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3486 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3487 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3488 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3489 | if (ch == '(' || ch == '{') { |
| 3490 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3491 | add_till_closing_bracket(dest, input, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3492 | o_addchr(dest, ch); |
| 3493 | continue; |
| 3494 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3495 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3496 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3497 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3498 | continue; |
| 3499 | } |
| 3500 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3501 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3502 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3503 | continue; |
| 3504 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3505 | if (ch == '`') { |
| 3506 | add_till_backquote(dest, input); |
| 3507 | o_addchr(dest, ch); |
| 3508 | continue; |
| 3509 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3510 | if (ch == '\\') { |
| 3511 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3512 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3513 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3514 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3515 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3516 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3517 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3518 | continue; |
| 3519 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3520 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3521 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3522 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3523 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3524 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3525 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3526 | #if BB_MMU |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3527 | #define parse_dollar(as_string, dest, input) \ |
| 3528 | parse_dollar(dest, input) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3529 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3530 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3531 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3532 | o_string *dest, |
| 3533 | struct in_str *input) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3534 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3535 | int ch = i_peek(input); /* first character after the $ */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3536 | unsigned char quote_mask = (dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) ? 0x80 : 0; |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3537 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3538 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3539 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3540 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3541 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3542 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3543 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3544 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3545 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3546 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3547 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3548 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3549 | if (!isalnum(ch) && ch != '_') |
| 3550 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3551 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3552 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3553 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3554 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3555 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3556 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3557 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3558 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3559 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3560 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3561 | o_addchr(dest, ch | quote_mask); |
| 3562 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3563 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3564 | case '$': /* pid */ |
| 3565 | case '!': /* last bg pid */ |
| 3566 | case '?': /* last exit code */ |
| 3567 | case '#': /* number of args */ |
| 3568 | case '*': /* args */ |
| 3569 | case '@': /* args */ |
| 3570 | goto make_one_char_var; |
| 3571 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 3572 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3573 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3574 | ch = i_getch(input); /* eat '{' */ |
| 3575 | nommu_addchr(as_string, ch); |
| 3576 | |
| 3577 | ch = i_getch(input); /* first char after '{' */ |
| 3578 | nommu_addchr(as_string, ch); |
| 3579 | /* It should be ${?}, or ${#var}, |
| 3580 | * or even ${?+subst} - operator acting on a special variable, |
| 3581 | * or the beginning of variable name. |
| 3582 | */ |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 3583 | if (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) { /* not one of those */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3584 | bad_dollar_syntax: |
| 3585 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3586 | debug_printf_parse("parse_dollar return 1: unterminated ${name}\n"); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3587 | return 1; |
| 3588 | } |
| 3589 | ch |= quote_mask; |
| 3590 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3591 | /* 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] | 3592 | * However, this regresses some of our testsuite cases |
| 3593 | * which check invalid constructs like ${%}. |
| 3594 | * Oh well... let's check that the var name part is fine... */ |
| 3595 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3596 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3597 | unsigned pos; |
| 3598 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3599 | o_addchr(dest, ch); |
| 3600 | debug_printf_parse(": '%c'\n", ch); |
| 3601 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3602 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3603 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3604 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3605 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3606 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3607 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3608 | unsigned end_ch; |
| 3609 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3610 | /* handle parameter expansions |
| 3611 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3612 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3613 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3614 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3615 | |
| 3616 | /* Eat everything until closing '}' (or ':') */ |
| 3617 | end_ch = '}'; |
| 3618 | if (ENABLE_HUSH_BASH_COMPAT |
| 3619 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3620 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3621 | ) { |
| 3622 | /* It's ${var:N[:M]} thing */ |
| 3623 | end_ch = '}' * 0x100 + ':'; |
| 3624 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3625 | if (ENABLE_HUSH_BASH_COMPAT |
| 3626 | && ch == '/' |
| 3627 | ) { |
| 3628 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 3629 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 3630 | i_getch(input); |
| 3631 | nommu_addchr(as_string, '/'); |
| 3632 | ch = '\\'; |
| 3633 | } |
| 3634 | end_ch = '}' * 0x100 + '/'; |
| 3635 | } |
| 3636 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3637 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3638 | if (!BB_MMU) |
| 3639 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3640 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3641 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3642 | #else |
| 3643 | #error Simple code to only allow ${var} is not implemented |
| 3644 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3645 | if (as_string) { |
| 3646 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3647 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3648 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3649 | |
| 3650 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 3651 | /* close the first block: */ |
| 3652 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3653 | /* while parsing N from ${var:N[:M]} |
| 3654 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3655 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3656 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3657 | end_ch = '}'; |
| 3658 | goto again; |
| 3659 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3660 | /* got '}' */ |
| 3661 | if (end_ch == '}' * 0x100 + ':') { |
| 3662 | /* it's ${var:N} - emulate :999999999 */ |
| 3663 | o_addstr(dest, "999999999"); |
| 3664 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3665 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3666 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3667 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3668 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3669 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3670 | break; |
| 3671 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3672 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3673 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3674 | unsigned pos; |
| 3675 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3676 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3677 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3678 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3679 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3680 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3681 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3682 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3683 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3684 | if (!BB_MMU) |
| 3685 | pos = dest->length; |
| 3686 | add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3687 | if (as_string) { |
| 3688 | o_addstr(as_string, dest->data + pos); |
| 3689 | o_addchr(as_string, ')'); |
| 3690 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3691 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3692 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3693 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3694 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3695 | # endif |
| 3696 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3697 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3698 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3699 | if (!BB_MMU) |
| 3700 | pos = dest->length; |
| 3701 | add_till_closing_bracket(dest, input, ')'); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3702 | if (as_string) { |
| 3703 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 3704 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3705 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3706 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3707 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3708 | break; |
| 3709 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3710 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3711 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3712 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3713 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3714 | ch = i_peek(input); |
| 3715 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3716 | ch = '_'; |
| 3717 | goto make_var; |
| 3718 | } |
| 3719 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 3720 | /* TODO: $_ and $-: */ |
| 3721 | /* $_ Shell or shell script name; or last argument of last command |
| 3722 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 3723 | * 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] | 3724 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 3725 | default: |
| 3726 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3727 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3728 | debug_printf_parse("parse_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3729 | return 0; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3730 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3733 | #if BB_MMU |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3734 | #define parse_stream_dquoted(as_string, dest, input, dquote_end) \ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3735 | parse_stream_dquoted(dest, input, dquote_end) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3736 | #define as_string NULL |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3737 | #endif |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3738 | static int parse_stream_dquoted(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3739 | o_string *dest, |
| 3740 | struct in_str *input, |
| 3741 | int dquote_end) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3742 | { |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3743 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3744 | int next; |
| 3745 | |
| 3746 | again: |
| 3747 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3748 | if (ch != EOF) |
| 3749 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3750 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3751 | if (dest->o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3752 | dest->o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3753 | debug_printf_parse("parse_stream_dquoted return 0\n"); |
| 3754 | return 0; |
| 3755 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3756 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3757 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3758 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3759 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3760 | } |
| 3761 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3762 | if (ch != '\n') { |
| 3763 | next = i_peek(input); |
| 3764 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 3765 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3766 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3767 | if (ch == '\\') { |
| 3768 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3769 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3770 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3771 | } |
| 3772 | /* bash: |
| 3773 | * "The backslash retains its special meaning [in "..."] |
| 3774 | * only when followed by one of the following characters: |
| 3775 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3776 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3777 | * NB: in (unquoted) heredoc, above does not apply to ". |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3778 | */ |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3779 | if (next == dquote_end || strchr("$`\\\n", next) != NULL) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame^] | 3780 | ch = i_getch(input); /* eat next */ |
| 3781 | if (ch == '\n') |
| 3782 | goto again; /* skip \<newline> */ |
| 3783 | } /* else: ch remains == '\\', and we double it */ |
| 3784 | o_addqchr(dest, ch); |
| 3785 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3786 | goto again; |
| 3787 | } |
| 3788 | if (ch == '$') { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3789 | if (parse_dollar(as_string, dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3790 | debug_printf_parse("parse_stream_dquoted return 1: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3791 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3792 | return 1; |
| 3793 | } |
| 3794 | goto again; |
| 3795 | } |
| 3796 | #if ENABLE_HUSH_TICK |
| 3797 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3798 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3799 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3800 | o_addchr(dest, 0x80 | '`'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3801 | add_till_backquote(dest, input); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3802 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3803 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 3804 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3805 | } |
| 3806 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 3807 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3808 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3809 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3812 | /* |
| 3813 | * Scan input until EOF or end_trigger char. |
| 3814 | * Return a list of pipes to execute, or NULL on EOF |
| 3815 | * or if end_trigger character is met. |
| 3816 | * On syntax error, exit is shell is not interactive, |
| 3817 | * reset parsing machinery and start parsing anew, |
| 3818 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3819 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3820 | static struct pipe *parse_stream(char **pstring, |
| 3821 | struct in_str *input, |
| 3822 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3823 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3824 | struct parse_context ctx; |
| 3825 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3826 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3827 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 3828 | /* Single-quote triggers a bypass of the main loop until its mate is |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3829 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3830 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3831 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 3832 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3833 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3834 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 3835 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 3836 | * Preventing this: */ |
| 3837 | o_addchr(&dest, '\0'); |
| 3838 | dest.length = 0; |
| 3839 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3840 | /* We used to separate words on $IFS here. This was wrong. |
| 3841 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 3842 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3843 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 3844 | |
| 3845 | reset: /* we come back here only on syntax errors in interactive shell */ |
| 3846 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3847 | #if ENABLE_HUSH_INTERACTIVE |
| 3848 | input->promptmode = 0; /* PS1 */ |
| 3849 | #endif |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 3850 | if (MAYBE_ASSIGNMENT != 0) |
| 3851 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3852 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3853 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3854 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3855 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3856 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3857 | int ch; |
| 3858 | int next; |
| 3859 | int redir_fd; |
| 3860 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3861 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3862 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3863 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3864 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3865 | if (ch == EOF) { |
| 3866 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3867 | |
| 3868 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3869 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 3870 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3871 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 3872 | /* end_trigger == '}' case errors out earlier, |
| 3873 | * checking only ')' */ |
| 3874 | if (end_trigger == ')') { |
| 3875 | syntax_error_unterm_ch('('); /* exits */ |
| 3876 | /* goto parse_error; */ |
| 3877 | } |
| 3878 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3879 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 3880 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3881 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3882 | o_free(&dest); |
| 3883 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3884 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3885 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3886 | /* (this makes bare "&" cmd a no-op. |
| 3887 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3888 | if (pi->num_cmds == 0 |
| 3889 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 3890 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3891 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3892 | pi = NULL; |
| 3893 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3894 | #if !BB_MMU |
| 3895 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 3896 | if (pstring) |
| 3897 | *pstring = ctx.as_string.data; |
| 3898 | else |
| 3899 | o_free_unsafe(&ctx.as_string); |
| 3900 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 3901 | debug_leave(); |
| 3902 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3903 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 3904 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3905 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3906 | |
| 3907 | next = '\0'; |
| 3908 | if (ch != '\n') |
| 3909 | next = i_peek(input); |
| 3910 | |
| 3911 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 3912 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 3913 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 3914 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 3915 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3916 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3917 | || (next != ';' /* }; - special */ |
| 3918 | && next != ')' /* }) - special */ |
| 3919 | && next != '&' /* }& and }&& ... - special */ |
| 3920 | && next != '|' /* }|| ... - special */ |
| 3921 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 3922 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3923 | ) { |
| 3924 | /* They are not special, skip "{}" */ |
| 3925 | is_special += 2; |
| 3926 | } |
| 3927 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3928 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3929 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3930 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 3931 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3932 | o_addQchr(&dest, ch); |
| 3933 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 3934 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3935 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3936 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3937 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3938 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3939 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3940 | continue; |
| 3941 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3942 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3943 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3944 | if (done_word(&dest, &ctx)) { |
| 3945 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 3946 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 3947 | if (ch == '\n') { |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3948 | #if ENABLE_HUSH_CASE |
| 3949 | /* "case ... in <newline> word) ..." - |
| 3950 | * newlines are ignored (but ';' wouldn't be) */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3951 | if (ctx.command->argv == NULL |
| 3952 | && ctx.ctx_res_w == RES_MATCH |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 3953 | ) { |
| 3954 | continue; |
| 3955 | } |
| 3956 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3957 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3958 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3959 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 3960 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3961 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3962 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3963 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3964 | heredoc_cnt = 0; |
| 3965 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3966 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3967 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 3968 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3969 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3970 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3971 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 3972 | |
| 3973 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 3974 | * } is an ordinary char in this case, even inside { cmd; } |
| 3975 | * Pathological example: { ""}; } should exec "}" cmd |
| 3976 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 3977 | if (ch == '}') { |
| 3978 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 3979 | || dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3980 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 3981 | ) { |
| 3982 | goto ordinary_char; |
| 3983 | } |
| 3984 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 3985 | goto skip_end_trigger; |
| 3986 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3989 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3990 | && (ch != ';' || heredoc_cnt == 0) |
| 3991 | #if ENABLE_HUSH_CASE |
| 3992 | && (ch != ')' |
| 3993 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3994 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3995 | ) |
| 3996 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3997 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3998 | if (heredoc_cnt) { |
| 3999 | /* This is technically valid: |
| 4000 | * { cat <<HERE; }; echo Ok |
| 4001 | * heredoc |
| 4002 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4003 | * HERE |
| 4004 | * but we don't support this. |
| 4005 | * We require heredoc to be in enclosing {}/(), |
| 4006 | * if any. |
| 4007 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4008 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4009 | goto parse_error; |
| 4010 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4011 | if (done_word(&dest, &ctx)) { |
| 4012 | goto parse_error; |
| 4013 | } |
| 4014 | done_pipe(&ctx, PIPE_SEQ); |
| 4015 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4016 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4017 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4018 | 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] | 4019 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4020 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4021 | #if !BB_MMU |
| 4022 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4023 | if (pstring) |
| 4024 | *pstring = ctx.as_string.data; |
| 4025 | else |
| 4026 | o_free_unsafe(&ctx.as_string); |
| 4027 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4028 | debug_leave(); |
| 4029 | debug_printf_parse("parse_stream return %p: " |
| 4030 | "end_trigger char found\n", |
| 4031 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4032 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4033 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4034 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4035 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4036 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4037 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4038 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4039 | /* Catch <, > before deciding whether this word is |
| 4040 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4041 | switch (ch) { |
| 4042 | case '>': |
| 4043 | redir_fd = redirect_opt_num(&dest); |
| 4044 | if (done_word(&dest, &ctx)) { |
| 4045 | goto parse_error; |
| 4046 | } |
| 4047 | redir_style = REDIRECT_OVERWRITE; |
| 4048 | if (next == '>') { |
| 4049 | redir_style = REDIRECT_APPEND; |
| 4050 | ch = i_getch(input); |
| 4051 | nommu_addchr(&ctx.as_string, ch); |
| 4052 | } |
| 4053 | #if 0 |
| 4054 | else if (next == '(') { |
| 4055 | syntax_error(">(process) not supported"); |
| 4056 | goto parse_error; |
| 4057 | } |
| 4058 | #endif |
| 4059 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4060 | goto parse_error; |
| 4061 | continue; /* back to top of while (1) */ |
| 4062 | case '<': |
| 4063 | redir_fd = redirect_opt_num(&dest); |
| 4064 | if (done_word(&dest, &ctx)) { |
| 4065 | goto parse_error; |
| 4066 | } |
| 4067 | redir_style = REDIRECT_INPUT; |
| 4068 | if (next == '<') { |
| 4069 | redir_style = REDIRECT_HEREDOC; |
| 4070 | heredoc_cnt++; |
| 4071 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4072 | ch = i_getch(input); |
| 4073 | nommu_addchr(&ctx.as_string, ch); |
| 4074 | } else if (next == '>') { |
| 4075 | redir_style = REDIRECT_IO; |
| 4076 | ch = i_getch(input); |
| 4077 | nommu_addchr(&ctx.as_string, ch); |
| 4078 | } |
| 4079 | #if 0 |
| 4080 | else if (next == '(') { |
| 4081 | syntax_error("<(process) not supported"); |
| 4082 | goto parse_error; |
| 4083 | } |
| 4084 | #endif |
| 4085 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4086 | goto parse_error; |
| 4087 | continue; /* back to top of while (1) */ |
| 4088 | } |
| 4089 | |
| 4090 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4091 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4092 | && !ctx.pending_redirect |
| 4093 | ) { |
| 4094 | /* ch is a special char and thus this word |
| 4095 | * cannot be an assignment */ |
| 4096 | dest.o_assignment = NOT_ASSIGNMENT; |
| 4097 | } |
| 4098 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4099 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4100 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4101 | switch (ch) { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4102 | case '#': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4103 | if (dest.length == 0) { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4104 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4105 | ch = i_peek(input); |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4106 | if (ch == EOF || ch == '\n') |
| 4107 | break; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4108 | i_getch(input); |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4109 | /* note: we do not add it to &ctx.as_string */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4110 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4111 | nommu_addchr(&ctx.as_string, '\n'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4112 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4113 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4114 | } |
| 4115 | break; |
| 4116 | case '\\': |
| 4117 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4118 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4119 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4120 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4121 | ch = i_getch(input); |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 4122 | if (ch != '\n') { |
| 4123 | o_addchr(&dest, '\\'); |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4124 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 4125 | o_addchr(&dest, ch); |
| 4126 | nommu_addchr(&ctx.as_string, ch); |
| 4127 | /* Example: echo Hello \2>file |
| 4128 | * we need to know that word 2 is quoted */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4129 | dest.has_quoted_part = 1; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4130 | } |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4131 | #if !BB_MMU |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 4132 | else { |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4133 | /* It's "\<newline>". Remove trailing '\' from ctx.as_string */ |
| 4134 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
Denys Vlasenko | e19e193 | 2009-05-03 02:15:18 +0200 | [diff] [blame] | 4135 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 4136 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4137 | break; |
| 4138 | case '$': |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4139 | if (parse_dollar(&ctx.as_string, &dest, input) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4140 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4141 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4142 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4143 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4144 | break; |
| 4145 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4146 | dest.has_quoted_part = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4147 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4148 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4149 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4150 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4151 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4152 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4153 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4154 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4155 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4156 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4157 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4158 | break; |
| 4159 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4160 | dest.has_quoted_part = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4161 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4162 | dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4163 | if (parse_stream_dquoted(&ctx.as_string, &dest, input, '"')) |
| 4164 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4165 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4166 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4167 | case '`': { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4168 | unsigned pos; |
| 4169 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4170 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4171 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4172 | pos = dest.length; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4173 | add_till_backquote(&dest, input); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4174 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4175 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4176 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4177 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4178 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4179 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4180 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4181 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4182 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4183 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4184 | #if ENABLE_HUSH_CASE |
| 4185 | case_semi: |
| 4186 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4187 | if (done_word(&dest, &ctx)) { |
| 4188 | goto parse_error; |
| 4189 | } |
| 4190 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4191 | #if ENABLE_HUSH_CASE |
| 4192 | /* Eat multiple semicolons, detect |
| 4193 | * whether it means something special */ |
| 4194 | while (1) { |
| 4195 | ch = i_peek(input); |
| 4196 | if (ch != ';') |
| 4197 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4198 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4199 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4200 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4201 | ctx.ctx_dsemicolon = 1; |
| 4202 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4203 | break; |
| 4204 | } |
| 4205 | } |
| 4206 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4207 | new_cmd: |
| 4208 | /* We just finished a cmd. New one may start |
| 4209 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4210 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4211 | break; |
| 4212 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4213 | if (done_word(&dest, &ctx)) { |
| 4214 | goto parse_error; |
| 4215 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4216 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4217 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4218 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4219 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4220 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4221 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4222 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4223 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4224 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4225 | if (done_word(&dest, &ctx)) { |
| 4226 | goto parse_error; |
| 4227 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4228 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4229 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4230 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4231 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4232 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4233 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4234 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4235 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4236 | } else { |
| 4237 | /* we could pick up a file descriptor choice here |
| 4238 | * with redirect_opt_num(), but bash doesn't do it. |
| 4239 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4240 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4241 | #if !BB_MMU |
| 4242 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 4243 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4244 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4245 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4246 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4247 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4248 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4249 | if (ctx.ctx_res_w == RES_MATCH |
| 4250 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4251 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4252 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4253 | ) { |
| 4254 | continue; |
| 4255 | } |
| 4256 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4257 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4258 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4259 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4260 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4261 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4262 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4263 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4264 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4265 | goto case_semi; |
| 4266 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4267 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4268 | /* proper use of this character is caught by end_trigger: |
| 4269 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4270 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4271 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4272 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4273 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4274 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4275 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4276 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4277 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4278 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4279 | parse_error: |
| 4280 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4281 | struct parse_context *pctx; |
| 4282 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4283 | |
| 4284 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4285 | * Sample for finding leaks on syntax error recovery path. |
| 4286 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4287 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4288 | * Samples to catch leaks at execution: |
| 4289 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4290 | * 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] | 4291 | */ |
| 4292 | pctx = &ctx; |
| 4293 | do { |
| 4294 | /* Update pipe/command counts, |
| 4295 | * otherwise freeing may miss some */ |
| 4296 | done_pipe(pctx, PIPE_SEQ); |
| 4297 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4298 | pctx->list_head, pctx); |
| 4299 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4300 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4301 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4302 | #if !BB_MMU |
| 4303 | o_free_unsafe(&pctx->as_string); |
| 4304 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4305 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4306 | if (pctx != &ctx) { |
| 4307 | free(pctx); |
| 4308 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4309 | IF_HAS_KEYWORDS(pctx = p2;) |
| 4310 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4311 | /* Free text, clear all dest fields */ |
| 4312 | o_free(&dest); |
| 4313 | /* If we are not in top-level parse, we return, |
| 4314 | * our caller will propagate error. |
| 4315 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4316 | if (end_trigger != ';') { |
| 4317 | #if !BB_MMU |
| 4318 | if (pstring) |
| 4319 | *pstring = NULL; |
| 4320 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4321 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4322 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4323 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4324 | /* Discard cached input, force prompt */ |
| 4325 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4326 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4327 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4328 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4329 | } |
| 4330 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4331 | |
| 4332 | /*** Execution routines ***/ |
| 4333 | |
| 4334 | /* Expansion can recurse, need forward decls: */ |
| 4335 | static char *expand_string_to_string(const char *str); |
| 4336 | static int process_command_subs(o_string *dest, const char *s); |
| 4337 | |
| 4338 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 4339 | * all variable references within and returns a pointer to |
| 4340 | * a list of expanded strings, possibly with larger number |
| 4341 | * of strings. (Think VAR="a b"; echo $VAR). |
| 4342 | * This new list is allocated as a single malloc block. |
| 4343 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4344 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4345 | * Caller can deallocate entire list by single free(list). */ |
| 4346 | |
| 4347 | /* Store given string, finalizing the word and starting new one whenever |
| 4348 | * we encounter IFS char(s). This is used for expanding variable values. |
| 4349 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 4350 | static int expand_on_ifs(o_string *output, int n, const char *str) |
| 4351 | { |
| 4352 | while (1) { |
| 4353 | int word_len = strcspn(str, G.ifs); |
| 4354 | if (word_len) { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4355 | if (output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4356 | o_addqblock(output, str, word_len); |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4357 | else if (!(output->o_expflags & EXP_FLAG_GLOB)) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4358 | o_addblock(output, str, word_len); |
| 4359 | else /* if (!escape && glob) */ { |
| 4360 | /* Protect backslashes against globbing up :) |
| 4361 | * Example: "v='\*'; echo b$v" |
| 4362 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4363 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4364 | /*/ Why can't we do it easier? */ |
| 4365 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 4366 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 4367 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4368 | str += word_len; |
| 4369 | } |
| 4370 | if (!*str) /* EOL - do not finalize word */ |
| 4371 | break; |
| 4372 | o_addchr(output, '\0'); |
| 4373 | debug_print_list("expand_on_ifs", output, n); |
| 4374 | n = o_save_ptr(output, n); |
| 4375 | str += strspn(str, G.ifs); /* skip ifs chars */ |
| 4376 | } |
| 4377 | debug_print_list("expand_on_ifs[1]", output, n); |
| 4378 | return n; |
| 4379 | } |
| 4380 | |
| 4381 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 4382 | * they are in double quotes, with the exception that they are not :). |
| 4383 | * Just the rules are similar: "expand only $var and `cmd`" |
| 4384 | * |
| 4385 | * Returns malloced string. |
| 4386 | * As an optimization, we return NULL if expansion is not needed. |
| 4387 | */ |
| 4388 | static char *expand_pseudo_dquoted(const char *str) |
| 4389 | { |
| 4390 | char *exp_str; |
| 4391 | struct in_str input; |
| 4392 | o_string dest = NULL_O_STRING; |
| 4393 | |
| 4394 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4395 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4396 | #if ENABLE_HUSH_TICK |
| 4397 | && !strchr(str, '`') |
| 4398 | #endif |
| 4399 | ) { |
| 4400 | return NULL; |
| 4401 | } |
| 4402 | |
| 4403 | /* We need to expand. Example: |
| 4404 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 4405 | */ |
| 4406 | setup_string_in_str(&input, str); |
| 4407 | parse_stream_dquoted(NULL, &dest, &input, EOF); |
| 4408 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
| 4409 | exp_str = expand_string_to_string(dest.data); |
| 4410 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 4411 | o_free_unsafe(&dest); |
| 4412 | return exp_str; |
| 4413 | } |
| 4414 | |
| 4415 | #if ENABLE_SH_MATH_SUPPORT |
| 4416 | static arith_t expand_and_evaluate_arith(const char *arg, int *errcode_p) |
| 4417 | { |
| 4418 | arith_eval_hooks_t hooks; |
| 4419 | arith_t res; |
| 4420 | char *exp_str; |
| 4421 | |
| 4422 | hooks.lookupvar = get_local_var_value; |
| 4423 | hooks.setvar = set_local_var_from_halves; |
Denys Vlasenko | 8b2f13d | 2010-09-07 12:19:33 +0200 | [diff] [blame] | 4424 | //hooks.endofname = endofname; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4425 | exp_str = expand_pseudo_dquoted(arg); |
| 4426 | res = arith(exp_str ? exp_str : arg, errcode_p, &hooks); |
| 4427 | free(exp_str); |
| 4428 | return res; |
| 4429 | } |
| 4430 | #endif |
| 4431 | |
| 4432 | #if ENABLE_HUSH_BASH_COMPAT |
| 4433 | /* ${var/[/]pattern[/repl]} helpers */ |
| 4434 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 4435 | { |
| 4436 | while (1) { |
| 4437 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 4438 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 4439 | if (end) { |
| 4440 | *size = end - val; |
| 4441 | return val; |
| 4442 | } |
| 4443 | if (*val == '\0') |
| 4444 | return NULL; |
| 4445 | /* Optimization: if "*pat" did not match the start of "string", |
| 4446 | * we know that "tring", "ring" etc will not match too: |
| 4447 | */ |
| 4448 | if (pattern[0] == '*') |
| 4449 | return NULL; |
| 4450 | val++; |
| 4451 | } |
| 4452 | } |
| 4453 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 4454 | { |
| 4455 | char *result = NULL; |
| 4456 | unsigned res_len = 0; |
| 4457 | unsigned repl_len = strlen(repl); |
| 4458 | |
| 4459 | while (1) { |
| 4460 | int size; |
| 4461 | char *s = strstr_pattern(val, pattern, &size); |
| 4462 | if (!s) |
| 4463 | break; |
| 4464 | |
| 4465 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 4466 | memcpy(result + res_len, val, s - val); |
| 4467 | res_len += s - val; |
| 4468 | strcpy(result + res_len, repl); |
| 4469 | res_len += repl_len; |
| 4470 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 4471 | |
| 4472 | val = s + size; |
| 4473 | if (exp_op == '/') |
| 4474 | break; |
| 4475 | } |
| 4476 | if (val[0] && result) { |
| 4477 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 4478 | strcpy(result + res_len, val); |
| 4479 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 4480 | } |
| 4481 | debug_printf_varexp("result:'%s'\n", result); |
| 4482 | return result; |
| 4483 | } |
| 4484 | #endif |
| 4485 | |
| 4486 | /* Helper: |
| 4487 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 4488 | */ |
| 4489 | static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, char **pp, char first_ch) |
| 4490 | { |
| 4491 | const char *val = NULL; |
| 4492 | char *to_be_freed = NULL; |
| 4493 | char *p = *pp; |
| 4494 | char *var; |
| 4495 | char first_char; |
| 4496 | char exp_op; |
| 4497 | char exp_save = exp_save; /* for compiler */ |
| 4498 | char *exp_saveptr; /* points to expansion operator */ |
| 4499 | char *exp_word = exp_word; /* for compiler */ |
| 4500 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4501 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4502 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4503 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
| 4504 | first_char = arg[0] = first_ch & 0x7f; |
| 4505 | exp_op = 0; |
| 4506 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4507 | if (first_char == '#' /* ${#... */ |
| 4508 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 4509 | ) { |
| 4510 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4511 | var++; |
| 4512 | exp_op = 'L'; |
| 4513 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4514 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4515 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 4516 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 4517 | ) { |
| 4518 | /* ${?:0}, ${#[:]%0} etc */ |
| 4519 | exp_saveptr = var + 1; |
| 4520 | } else { |
| 4521 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 4522 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 4523 | } |
| 4524 | exp_op = exp_save = *exp_saveptr; |
| 4525 | if (exp_op) { |
| 4526 | exp_word = exp_saveptr + 1; |
| 4527 | if (exp_op == ':') { |
| 4528 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4529 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4530 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4531 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4532 | ) { |
| 4533 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 4534 | exp_op = ':'; |
| 4535 | exp_word--; |
| 4536 | } |
| 4537 | } |
| 4538 | *exp_saveptr = '\0'; |
| 4539 | } /* else: it's not an expansion op, but bare ${var} */ |
| 4540 | } |
| 4541 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4542 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4543 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4544 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4545 | int n = xatoi_positive(var); |
| 4546 | if (n < G.global_argc) |
| 4547 | val = G.global_argv[n]; |
| 4548 | /* else val remains NULL: $N with too big N */ |
| 4549 | } else { |
| 4550 | switch (var[0]) { |
| 4551 | case '$': /* pid */ |
| 4552 | val = utoa(G.root_pid); |
| 4553 | break; |
| 4554 | case '!': /* bg pid */ |
| 4555 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 4556 | break; |
| 4557 | case '?': /* exitcode */ |
| 4558 | val = utoa(G.last_exitcode); |
| 4559 | break; |
| 4560 | case '#': /* argc */ |
| 4561 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 4562 | break; |
| 4563 | default: |
| 4564 | val = get_local_var_value(var); |
| 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | /* Handle any expansions */ |
| 4569 | if (exp_op == 'L') { |
| 4570 | debug_printf_expand("expand: length(%s)=", val); |
| 4571 | val = utoa(val ? strlen(val) : 0); |
| 4572 | debug_printf_expand("%s\n", val); |
| 4573 | } else if (exp_op) { |
| 4574 | if (exp_op == '%' || exp_op == '#') { |
| 4575 | /* Standard-mandated substring removal ops: |
| 4576 | * ${parameter%word} - remove smallest suffix pattern |
| 4577 | * ${parameter%%word} - remove largest suffix pattern |
| 4578 | * ${parameter#word} - remove smallest prefix pattern |
| 4579 | * ${parameter##word} - remove largest prefix pattern |
| 4580 | * |
| 4581 | * Word is expanded to produce a glob pattern. |
| 4582 | * Then var's value is matched to it and matching part removed. |
| 4583 | */ |
| 4584 | if (val && val[0]) { |
| 4585 | char *exp_exp_word; |
| 4586 | char *loc; |
| 4587 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
| 4588 | if (exp_op == *exp_word) /* ## or %% */ |
| 4589 | exp_word++; |
| 4590 | //TODO: avoid xstrdup unless needed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4591 | // (see HACK ALERT below for an example) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4592 | val = to_be_freed = xstrdup(val); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4593 | //TODO: fix expansion rules: |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4594 | exp_exp_word = expand_pseudo_dquoted(exp_word); |
| 4595 | if (exp_exp_word) |
| 4596 | exp_word = exp_exp_word; |
| 4597 | loc = scan_and_match(to_be_freed, exp_word, scan_flags); |
| 4598 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
| 4599 | // exp_op, to_be_freed, exp_word, loc); |
| 4600 | free(exp_exp_word); |
| 4601 | if (loc) { /* match was found */ |
| 4602 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
| 4603 | val = loc; |
| 4604 | else /* %[%] */ |
| 4605 | *loc = '\0'; |
| 4606 | } |
| 4607 | } |
| 4608 | } |
| 4609 | #if ENABLE_HUSH_BASH_COMPAT |
| 4610 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4611 | /* It's ${var/[/]pattern[/repl]} thing. |
| 4612 | * Note that in encoded form it has TWO parts: |
| 4613 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
| 4614 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4615 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4616 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4617 | if (val && val[0]) { |
| 4618 | /* It's ${var/[/]pattern[/repl]} thing */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4619 | /* |
| 4620 | * Pattern is taken literally, while |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4621 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4622 | * by the usual expansion rules: |
| 4623 | * >az; >bz; |
| 4624 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 4625 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 4626 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 4627 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 4628 | * (note that a*z _pattern_ is never globbed!) |
| 4629 | */ |
| 4630 | //TODO: fix expansion rules: |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4631 | char *pattern, *repl, *t; |
| 4632 | pattern = expand_pseudo_dquoted(exp_word); |
| 4633 | if (!pattern) |
| 4634 | pattern = xstrdup(exp_word); |
| 4635 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 4636 | *p++ = SPECIAL_VAR_SYMBOL; |
| 4637 | exp_word = p; |
| 4638 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4639 | *p = '\0'; |
| 4640 | repl = expand_pseudo_dquoted(exp_word); |
| 4641 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 4642 | /* HACK ALERT. We depend here on the fact that |
| 4643 | * G.global_argv and results of utoa and get_local_var_value |
| 4644 | * are actually in writable memory: |
| 4645 | * replace_pattern momentarily stores NULs there. */ |
| 4646 | t = (char*)val; |
| 4647 | to_be_freed = replace_pattern(t, |
| 4648 | pattern, |
| 4649 | (repl ? repl : exp_word), |
| 4650 | exp_op); |
| 4651 | if (to_be_freed) /* at least one replace happened */ |
| 4652 | val = to_be_freed; |
| 4653 | free(pattern); |
| 4654 | free(repl); |
| 4655 | } |
| 4656 | } |
| 4657 | #endif |
| 4658 | else if (exp_op == ':') { |
| 4659 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 4660 | /* It's ${var:N[:M]} bashism. |
| 4661 | * Note that in encoded form it has TWO parts: |
| 4662 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 4663 | */ |
| 4664 | arith_t beg, len; |
| 4665 | int errcode = 0; |
| 4666 | |
| 4667 | beg = expand_and_evaluate_arith(exp_word, &errcode); |
| 4668 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 4669 | *p++ = SPECIAL_VAR_SYMBOL; |
| 4670 | exp_word = p; |
| 4671 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4672 | *p = '\0'; |
| 4673 | len = expand_and_evaluate_arith(exp_word, &errcode); |
| 4674 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
| 4675 | |
| 4676 | if (errcode >= 0 && len >= 0) { /* bash compat: len < 0 is illegal */ |
| 4677 | if (beg < 0) /* bash compat */ |
| 4678 | beg = 0; |
| 4679 | debug_printf_varexp("from val:'%s'\n", val); |
| 4680 | if (len == 0 || !val || beg >= strlen(val)) |
| 4681 | val = ""; |
| 4682 | else { |
| 4683 | /* Paranoia. What if user entered 9999999999999 |
| 4684 | * which fits in arith_t but not int? */ |
| 4685 | if (len >= INT_MAX) |
| 4686 | len = INT_MAX; |
| 4687 | val = to_be_freed = xstrndup(val + beg, len); |
| 4688 | } |
| 4689 | debug_printf_varexp("val:'%s'\n", val); |
| 4690 | } else |
| 4691 | #endif |
| 4692 | { |
| 4693 | die_if_script("malformed ${%s:...}", var); |
| 4694 | val = ""; |
| 4695 | } |
| 4696 | } else { /* one of "-=+?" */ |
| 4697 | /* Standard-mandated substitution ops: |
| 4698 | * ${var?word} - indicate error if unset |
| 4699 | * If var is unset, word (or a message indicating it is unset |
| 4700 | * if word is null) is written to standard error |
| 4701 | * and the shell exits with a non-zero exit status. |
| 4702 | * Otherwise, the value of var is substituted. |
| 4703 | * ${var-word} - use default value |
| 4704 | * If var is unset, word is substituted. |
| 4705 | * ${var=word} - assign and use default value |
| 4706 | * If var is unset, word is assigned to var. |
| 4707 | * In all cases, final value of var is substituted. |
| 4708 | * ${var+word} - use alternative value |
| 4709 | * If var is unset, null is substituted. |
| 4710 | * Otherwise, word is substituted. |
| 4711 | * |
| 4712 | * Word is subjected to tilde expansion, parameter expansion, |
| 4713 | * command substitution, and arithmetic expansion. |
| 4714 | * If word is not needed, it is not expanded. |
| 4715 | * |
| 4716 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 4717 | * but also treat null var as if it is unset. |
| 4718 | */ |
| 4719 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 4720 | if (exp_op == '+') |
| 4721 | use_word = !use_word; |
| 4722 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 4723 | (exp_save == ':') ? "true" : "false", use_word); |
| 4724 | if (use_word) { |
| 4725 | to_be_freed = expand_pseudo_dquoted(exp_word); |
| 4726 | if (to_be_freed) |
| 4727 | exp_word = to_be_freed; |
| 4728 | if (exp_op == '?') { |
| 4729 | /* mimic bash message */ |
| 4730 | die_if_script("%s: %s", |
| 4731 | var, |
| 4732 | exp_word[0] ? exp_word : "parameter null or not set" |
| 4733 | ); |
| 4734 | //TODO: how interactive bash aborts expansion mid-command? |
| 4735 | } else { |
| 4736 | val = exp_word; |
| 4737 | } |
| 4738 | |
| 4739 | if (exp_op == '=') { |
| 4740 | /* ${var=[word]} or ${var:=[word]} */ |
| 4741 | if (isdigit(var[0]) || var[0] == '#') { |
| 4742 | /* mimic bash message */ |
| 4743 | die_if_script("$%s: cannot assign in this way", var); |
| 4744 | val = NULL; |
| 4745 | } else { |
| 4746 | char *new_var = xasprintf("%s=%s", var, val); |
| 4747 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | } /* one of "-=+?" */ |
| 4752 | |
| 4753 | *exp_saveptr = exp_save; |
| 4754 | } /* if (exp_op) */ |
| 4755 | |
| 4756 | arg[0] = first_ch; |
| 4757 | |
| 4758 | *pp = p; |
| 4759 | *to_be_freed_pp = to_be_freed; |
| 4760 | return val; |
| 4761 | } |
| 4762 | |
| 4763 | /* Expand all variable references in given string, adding words to list[] |
| 4764 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 4765 | * to be filled). This routine is extremely tricky: has to deal with |
| 4766 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 4767 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4768 | static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4769 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4770 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4771 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4772 | */ |
| 4773 | char ored_ch; |
| 4774 | char *p; |
| 4775 | |
| 4776 | ored_ch = 0; |
| 4777 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4778 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 4779 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4780 | debug_print_list("expand_vars_to_list", output, n); |
| 4781 | n = o_save_ptr(output, n); |
| 4782 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 4783 | |
| 4784 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 4785 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4786 | char *to_be_freed = NULL; |
| 4787 | const char *val = NULL; |
| 4788 | #if ENABLE_HUSH_TICK |
| 4789 | o_string subst_result = NULL_O_STRING; |
| 4790 | #endif |
| 4791 | #if ENABLE_SH_MATH_SUPPORT |
| 4792 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 4793 | #endif |
| 4794 | o_addblock(output, arg, p - arg); |
| 4795 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 4796 | arg = ++p; |
| 4797 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4798 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4799 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4800 | /* "$@" is special. Even if quoted, it can still |
| 4801 | * expand to nothing (not even an empty string) */ |
| 4802 | if ((first_ch & 0x7f) != '@') |
| 4803 | ored_ch |= first_ch; |
| 4804 | |
| 4805 | switch (first_ch & 0x7f) { |
| 4806 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 4807 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4808 | case '@': { |
| 4809 | int i; |
| 4810 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4811 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4812 | i = 1; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4813 | ored_ch |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
| 4814 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4815 | int sv = output->o_expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4816 | /* unquoted var's contents should be globbed, so don't escape */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4817 | output->o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4818 | while (G.global_argv[i]) { |
| 4819 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 4820 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 4821 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 4822 | /* this argv[] is not empty and not last: |
| 4823 | * put terminating NUL, start new word */ |
| 4824 | o_addchr(output, '\0'); |
| 4825 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 4826 | n = o_save_ptr(output, n); |
| 4827 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 4828 | } |
| 4829 | } |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4830 | output->o_expflags = sv; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4831 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4832 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4833 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4834 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
| 4835 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) |
| 4836 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4837 | while (1) { |
| 4838 | o_addQstr(output, G.global_argv[i]); |
| 4839 | if (++i >= G.global_argc) |
| 4840 | break; |
| 4841 | o_addchr(output, '\0'); |
| 4842 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 4843 | n = o_save_ptr(output, n); |
| 4844 | } |
| 4845 | } else { /* quoted $*: add as one word */ |
| 4846 | while (1) { |
| 4847 | o_addQstr(output, G.global_argv[i]); |
| 4848 | if (!G.global_argv[++i]) |
| 4849 | break; |
| 4850 | if (G.ifs[0]) |
| 4851 | o_addchr(output, G.ifs[0]); |
| 4852 | } |
| 4853 | } |
| 4854 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4855 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4856 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 4857 | /* "Empty variable", used to make "" etc to not disappear */ |
| 4858 | arg++; |
| 4859 | ored_ch = 0x80; |
| 4860 | break; |
| 4861 | #if ENABLE_HUSH_TICK |
| 4862 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
| 4863 | *p = '\0'; |
| 4864 | arg++; |
| 4865 | /* Can't just stuff it into output o_string, |
| 4866 | * expanded result may need to be globbed |
| 4867 | * and $IFS-splitted */ |
| 4868 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 4869 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 4870 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 4871 | val = subst_result.data; |
| 4872 | goto store_val; |
| 4873 | #endif |
| 4874 | #if ENABLE_SH_MATH_SUPPORT |
| 4875 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 4876 | arith_t res; |
| 4877 | int errcode; |
| 4878 | |
| 4879 | arg++; /* skip '+' */ |
| 4880 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 4881 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
| 4882 | res = expand_and_evaluate_arith(arg, &errcode); |
| 4883 | |
| 4884 | if (errcode < 0) { |
| 4885 | const char *msg = "error in arithmetic"; |
| 4886 | switch (errcode) { |
| 4887 | case -3: |
| 4888 | msg = "exponent less than 0"; |
| 4889 | break; |
| 4890 | case -2: |
| 4891 | msg = "divide by 0"; |
| 4892 | break; |
| 4893 | case -5: |
| 4894 | msg = "expression recursion loop detected"; |
| 4895 | break; |
| 4896 | } |
| 4897 | die_if_script(msg); |
| 4898 | } |
| 4899 | debug_printf_subst("ARITH RES '"arith_t_fmt"'\n", res); |
| 4900 | sprintf(arith_buf, arith_t_fmt, res); |
| 4901 | val = arith_buf; |
| 4902 | break; |
| 4903 | } |
| 4904 | #endif |
| 4905 | default: |
| 4906 | val = expand_one_var(&to_be_freed, arg, &p, first_ch); |
| 4907 | IF_HUSH_TICK(store_val:) |
| 4908 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4909 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 4910 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4911 | if (val && val[0]) { |
| 4912 | /* unquoted var's contents should be globbed, so don't escape */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4913 | int sv = output->o_expflags; |
| 4914 | output->o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4915 | n = expand_on_ifs(output, n, val); |
| 4916 | val = NULL; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4917 | output->o_expflags = sv; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4918 | } |
| 4919 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4920 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 4921 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4922 | } |
| 4923 | break; |
| 4924 | |
| 4925 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 4926 | |
| 4927 | if (val && val[0]) { |
| 4928 | o_addQstr(output, val); |
| 4929 | } |
| 4930 | free(to_be_freed); |
| 4931 | /* Do the check to avoid writing to a const string */ |
| 4932 | if (*p != SPECIAL_VAR_SYMBOL) |
| 4933 | *p = SPECIAL_VAR_SYMBOL; |
| 4934 | |
| 4935 | #if ENABLE_HUSH_TICK |
| 4936 | o_free(&subst_result); |
| 4937 | #endif |
| 4938 | arg = ++p; |
| 4939 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 4940 | |
| 4941 | if (arg[0]) { |
| 4942 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 4943 | /* this part is literal, and it was already pre-quoted |
| 4944 | * if needed (much earlier), do not use o_addQstr here! */ |
| 4945 | o_addstr_with_NUL(output, arg); |
| 4946 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 4947 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
| 4948 | && !(ored_ch & 0x80) /* and all vars were not quoted. */ |
| 4949 | ) { |
| 4950 | n--; |
| 4951 | /* allow to reuse list[n] later without re-growth */ |
| 4952 | output->has_empty_slot = 1; |
| 4953 | } else { |
| 4954 | o_addchr(output, '\0'); |
| 4955 | } |
| 4956 | |
| 4957 | return n; |
| 4958 | } |
| 4959 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4960 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4961 | { |
| 4962 | int n; |
| 4963 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4964 | o_string output = NULL_O_STRING; |
| 4965 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4966 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4967 | |
| 4968 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 4969 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 4970 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 4971 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4972 | } |
| 4973 | debug_print_list("expand_variables", &output, n); |
| 4974 | |
| 4975 | /* output.data (malloced in one block) gets returned in "list" */ |
| 4976 | list = o_finalize_list(&output, n); |
| 4977 | debug_print_strings("expand_variables[1]", list); |
| 4978 | return list; |
| 4979 | } |
| 4980 | |
| 4981 | static char **expand_strvec_to_strvec(char **argv) |
| 4982 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4983 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | #if ENABLE_HUSH_BASH_COMPAT |
| 4987 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 4988 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4989 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4990 | } |
| 4991 | #endif |
| 4992 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4993 | /* Used for expansion of right hand of assignments, |
| 4994 | * $((...)), heredocs, variable espansion parts. |
| 4995 | * |
| 4996 | * NB: should NOT do globbing! |
| 4997 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 4998 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4999 | static char *expand_string_to_string(const char *str) |
| 5000 | { |
| 5001 | char *argv[2], **list; |
| 5002 | |
| 5003 | /* This is generally an optimization, but it also |
| 5004 | * handles "", which otherwise trips over !list[0] check below. |
| 5005 | * (is this ever happens that we actually get str="" here?) |
| 5006 | */ |
| 5007 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5008 | //TODO: Can use on strings with \ too, just unbackslash() them? |
| 5009 | debug_printf_expand("string_to_string(fast)='%s'\n", str); |
| 5010 | return xstrdup(str); |
| 5011 | } |
| 5012 | |
| 5013 | argv[0] = (char*)str; |
| 5014 | argv[1] = NULL; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5015 | list = expand_variables(argv, EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5016 | if (HUSH_DEBUG) |
| 5017 | if (!list[0] || list[1]) |
| 5018 | bb_error_msg_and_die("BUG in varexp2"); |
| 5019 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5020 | overlapping_strcpy((char*)list, list[0]); |
| 5021 | unbackslash((char*)list); |
| 5022 | debug_printf_expand("string_to_string='%s'\n", (char*)list); |
| 5023 | return (char*)list; |
| 5024 | } |
| 5025 | |
| 5026 | /* Used for "eval" builtin */ |
| 5027 | static char* expand_strvec_to_string(char **argv) |
| 5028 | { |
| 5029 | char **list; |
| 5030 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5031 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5032 | /* Convert all NULs to spaces */ |
| 5033 | if (list[0]) { |
| 5034 | int n = 1; |
| 5035 | while (list[n]) { |
| 5036 | if (HUSH_DEBUG) |
| 5037 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5038 | bb_error_msg_and_die("BUG in varexp3"); |
| 5039 | /* bash uses ' ' regardless of $IFS contents */ |
| 5040 | list[n][-1] = ' '; |
| 5041 | n++; |
| 5042 | } |
| 5043 | } |
| 5044 | overlapping_strcpy((char*)list, list[0]); |
| 5045 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5046 | return (char*)list; |
| 5047 | } |
| 5048 | |
| 5049 | static char **expand_assignments(char **argv, int count) |
| 5050 | { |
| 5051 | int i; |
| 5052 | char **p; |
| 5053 | |
| 5054 | G.expanded_assignments = p = NULL; |
| 5055 | /* Expand assignments into one string each */ |
| 5056 | for (i = 0; i < count; i++) { |
| 5057 | G.expanded_assignments = p = add_string_to_strings(p, expand_string_to_string(argv[i])); |
| 5058 | } |
| 5059 | G.expanded_assignments = NULL; |
| 5060 | return p; |
| 5061 | } |
| 5062 | |
| 5063 | |
| 5064 | #if BB_MMU |
| 5065 | /* never called */ |
| 5066 | void re_execute_shell(char ***to_free, const char *s, |
| 5067 | char *g_argv0, char **g_argv, |
| 5068 | char **builtin_argv) NORETURN; |
| 5069 | |
| 5070 | static void reset_traps_to_defaults(void) |
| 5071 | { |
| 5072 | /* This function is always called in a child shell |
| 5073 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5074 | */ |
| 5075 | unsigned sig; |
| 5076 | unsigned mask; |
| 5077 | |
| 5078 | /* Child shells are not interactive. |
| 5079 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5080 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5081 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5082 | */ |
| 5083 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 5084 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
| 5085 | |
| 5086 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 5087 | * Stupid. It can be done with *single* &= op, but we can't use |
| 5088 | * the fact that G.blocked_set is implemented as a bitmask |
| 5089 | * in libc... */ |
| 5090 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 5091 | sig = 1; |
| 5092 | while (1) { |
| 5093 | if (mask & 1) { |
| 5094 | /* Careful. Only if no trap or trap is not "" */ |
| 5095 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 5096 | sigdelset(&G.blocked_set, sig); |
| 5097 | } |
| 5098 | mask >>= 1; |
| 5099 | if (!mask) |
| 5100 | break; |
| 5101 | sig++; |
| 5102 | } |
| 5103 | /* Our homegrown sig mask is saner to work with :) */ |
| 5104 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 5105 | |
| 5106 | /* Resetting all traps to default except empty ones */ |
| 5107 | mask = G.non_DFL_mask; |
| 5108 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 5109 | if (!G.traps[sig] || !G.traps[sig][0]) |
| 5110 | continue; |
| 5111 | free(G.traps[sig]); |
| 5112 | G.traps[sig] = NULL; |
| 5113 | /* There is no signal for 0 (EXIT) */ |
| 5114 | if (sig == 0) |
| 5115 | continue; |
| 5116 | /* There was a trap handler, we just removed it. |
| 5117 | * But if sig still has non-DFL handling, |
| 5118 | * we should not unblock the sig. */ |
| 5119 | if (mask & 1) |
| 5120 | continue; |
| 5121 | sigdelset(&G.blocked_set, sig); |
| 5122 | } |
| 5123 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5124 | } |
| 5125 | |
| 5126 | #else /* !BB_MMU */ |
| 5127 | |
| 5128 | static void re_execute_shell(char ***to_free, const char *s, |
| 5129 | char *g_argv0, char **g_argv, |
| 5130 | char **builtin_argv) NORETURN; |
| 5131 | static void re_execute_shell(char ***to_free, const char *s, |
| 5132 | char *g_argv0, char **g_argv, |
| 5133 | char **builtin_argv) |
| 5134 | { |
| 5135 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5136 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5137 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5138 | char *heredoc_argv[4]; |
| 5139 | struct variable *cur; |
| 5140 | # if ENABLE_HUSH_FUNCTIONS |
| 5141 | struct function *funcp; |
| 5142 | # endif |
| 5143 | char **argv, **pp; |
| 5144 | unsigned cnt; |
| 5145 | unsigned long long empty_trap_mask; |
| 5146 | |
| 5147 | if (!g_argv0) { /* heredoc */ |
| 5148 | argv = heredoc_argv; |
| 5149 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5150 | argv[1] = (char *) "-<"; |
| 5151 | argv[2] = (char *) s; |
| 5152 | argv[3] = NULL; |
| 5153 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5154 | goto do_exec; |
| 5155 | } |
| 5156 | |
| 5157 | cnt = 0; |
| 5158 | pp = builtin_argv; |
| 5159 | if (pp) while (*pp++) |
| 5160 | cnt++; |
| 5161 | |
| 5162 | empty_trap_mask = 0; |
| 5163 | if (G.traps) { |
| 5164 | int sig; |
| 5165 | for (sig = 1; sig < NSIG; sig++) { |
| 5166 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5167 | empty_trap_mask |= 1LL << sig; |
| 5168 | } |
| 5169 | } |
| 5170 | |
| 5171 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5172 | , (unsigned) G.root_pid |
| 5173 | , (unsigned) G.root_ppid |
| 5174 | , (unsigned) G.last_bg_pid |
| 5175 | , (unsigned) G.last_exitcode |
| 5176 | , cnt |
| 5177 | , empty_trap_mask |
| 5178 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5179 | ); |
| 5180 | # undef NOMMU_HACK_FMT |
| 5181 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5182 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5183 | */ |
| 5184 | cnt += 6; |
| 5185 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5186 | if (!cur->flg_export || cur->flg_read_only) |
| 5187 | cnt += 2; |
| 5188 | } |
| 5189 | # if ENABLE_HUSH_FUNCTIONS |
| 5190 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5191 | cnt += 3; |
| 5192 | # endif |
| 5193 | pp = g_argv; |
| 5194 | while (*pp++) |
| 5195 | cnt++; |
| 5196 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 5197 | *pp++ = (char *) G.argv0_for_re_execing; |
| 5198 | *pp++ = param_buf; |
| 5199 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5200 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 5201 | continue; |
| 5202 | if (cur->flg_read_only) { |
| 5203 | *pp++ = (char *) "-R"; |
| 5204 | *pp++ = cur->varstr; |
| 5205 | } else if (!cur->flg_export) { |
| 5206 | *pp++ = (char *) "-V"; |
| 5207 | *pp++ = cur->varstr; |
| 5208 | } |
| 5209 | } |
| 5210 | # if ENABLE_HUSH_FUNCTIONS |
| 5211 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 5212 | *pp++ = (char *) "-F"; |
| 5213 | *pp++ = funcp->name; |
| 5214 | *pp++ = funcp->body_as_string; |
| 5215 | } |
| 5216 | # endif |
| 5217 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 5218 | * |
| 5219 | * However, POSIX says that subshells reset signals with traps |
| 5220 | * to SIG_DFL. |
| 5221 | * I tested bash-3.2 and it not only does that with true subshells |
| 5222 | * of the form ( list ), but with any forked children shells. |
| 5223 | * I set trap "echo W" WINCH; and then tried: |
| 5224 | * |
| 5225 | * { echo 1; sleep 20; echo 2; } & |
| 5226 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 5227 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 5228 | * |
| 5229 | * In all these cases sending SIGWINCH to the child shell |
| 5230 | * did not run the trap. If I add trap "echo V" WINCH; |
| 5231 | * _inside_ group (just before echo 1), it works. |
| 5232 | * |
| 5233 | * I conclude it means we don't need to pass active traps here. |
| 5234 | * Even if we would use signal handlers instead of signal masking |
| 5235 | * in order to implement trap handling, |
| 5236 | * exec syscall below resets signals to SIG_DFL for us. |
| 5237 | */ |
| 5238 | *pp++ = (char *) "-c"; |
| 5239 | *pp++ = (char *) s; |
| 5240 | if (builtin_argv) { |
| 5241 | while (*++builtin_argv) |
| 5242 | *pp++ = *builtin_argv; |
| 5243 | *pp++ = (char *) ""; |
| 5244 | } |
| 5245 | *pp++ = g_argv0; |
| 5246 | while (*g_argv) |
| 5247 | *pp++ = *g_argv++; |
| 5248 | /* *pp = NULL; - is already there */ |
| 5249 | pp = environ; |
| 5250 | |
| 5251 | do_exec: |
| 5252 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 5253 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 5254 | execve(bb_busybox_exec_path, argv, pp); |
| 5255 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 5256 | if (argv[0][0] == '/') |
| 5257 | execve(argv[0], argv, pp); |
| 5258 | xfunc_error_retval = 127; |
| 5259 | bb_error_msg_and_die("can't re-execute the shell"); |
| 5260 | } |
| 5261 | #endif /* !BB_MMU */ |
| 5262 | |
| 5263 | |
| 5264 | static int run_and_free_list(struct pipe *pi); |
| 5265 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5266 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5267 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5268 | * end_trigger controls how often we stop parsing |
| 5269 | * NUL: parse all, execute, return |
| 5270 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5271 | */ |
| 5272 | 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] | 5273 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5274 | /* Why we need empty flag? |
| 5275 | * An obscure corner case "false; ``; echo $?": |
| 5276 | * empty command in `` should still set $? to 0. |
| 5277 | * But we can't just set $? to 0 at the start, |
| 5278 | * this breaks "false; echo `echo $?`" case. |
| 5279 | */ |
| 5280 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5281 | while (1) { |
| 5282 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5283 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5284 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5285 | if (!pipe_list) { /* EOF */ |
| 5286 | if (empty) |
| 5287 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5288 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5289 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5290 | debug_print_tree(pipe_list, 0); |
| 5291 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5292 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5293 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5294 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5297 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5298 | { |
| 5299 | struct in_str input; |
| 5300 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5301 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5302 | } |
| 5303 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5304 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5305 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5306 | struct in_str input; |
| 5307 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5308 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5311 | #if ENABLE_HUSH_TICK |
| 5312 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 5313 | { |
| 5314 | pid_t pid; |
| 5315 | int channel[2]; |
| 5316 | # if !BB_MMU |
| 5317 | char **to_free = NULL; |
| 5318 | # endif |
| 5319 | |
| 5320 | xpipe(channel); |
| 5321 | pid = BB_MMU ? xfork() : xvfork(); |
| 5322 | if (pid == 0) { /* child */ |
| 5323 | disable_restore_tty_pgrp_on_exit(); |
| 5324 | /* Process substitution is not considered to be usual |
| 5325 | * 'command execution'. |
| 5326 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5327 | */ |
| 5328 | bb_signals(0 |
| 5329 | + (1 << SIGTSTP) |
| 5330 | + (1 << SIGTTIN) |
| 5331 | + (1 << SIGTTOU) |
| 5332 | , SIG_IGN); |
| 5333 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 5334 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5335 | xmove_fd(channel[1], 1); |
| 5336 | /* Prevent it from trying to handle ctrl-z etc */ |
| 5337 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 5338 | /* Awful hack for `trap` or $(trap). |
| 5339 | * |
| 5340 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5341 | * contains an example where "trap" is executed in a subshell: |
| 5342 | * |
| 5343 | * save_traps=$(trap) |
| 5344 | * ... |
| 5345 | * eval "$save_traps" |
| 5346 | * |
| 5347 | * Standard does not say that "trap" in subshell shall print |
| 5348 | * parent shell's traps. It only says that its output |
| 5349 | * must have suitable form, but then, in the above example |
| 5350 | * (which is not supposed to be normative), it implies that. |
| 5351 | * |
| 5352 | * bash (and probably other shell) does implement it |
| 5353 | * (traps are reset to defaults, but "trap" still shows them), |
| 5354 | * but as a result, "trap" logic is hopelessly messed up: |
| 5355 | * |
| 5356 | * # trap |
| 5357 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5358 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5359 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5360 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5361 | * trap -- 'echo Ho' SIGWINCH |
| 5362 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5363 | * trap -- 'echo Ho' SIGWINCH |
| 5364 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5365 | * trap -- 'echo Ho' SIGWINCH |
| 5366 | * |
| 5367 | * The rules when to forget and when to not forget traps |
| 5368 | * get really complex and nonsensical. |
| 5369 | * |
| 5370 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5371 | */ |
| 5372 | s = skip_whitespace(s); |
| 5373 | if (strncmp(s, "trap", 4) == 0 |
| 5374 | && skip_whitespace(s + 4)[0] == '\0' |
| 5375 | ) { |
| 5376 | static const char *const argv[] = { NULL, NULL }; |
| 5377 | builtin_trap((char**)argv); |
| 5378 | exit(0); /* not _exit() - we need to fflush */ |
| 5379 | } |
| 5380 | # if BB_MMU |
| 5381 | reset_traps_to_defaults(); |
| 5382 | parse_and_run_string(s); |
| 5383 | _exit(G.last_exitcode); |
| 5384 | # else |
| 5385 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 5386 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5387 | * huge=`cat BIG` # was blocking here forever |
| 5388 | * echo OK |
| 5389 | */ |
| 5390 | re_execute_shell(&to_free, |
| 5391 | s, |
| 5392 | G.global_argv[0], |
| 5393 | G.global_argv + 1, |
| 5394 | NULL); |
| 5395 | # endif |
| 5396 | } |
| 5397 | |
| 5398 | /* parent */ |
| 5399 | *pid_p = pid; |
| 5400 | # if ENABLE_HUSH_FAST |
| 5401 | G.count_SIGCHLD++; |
| 5402 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 5403 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 5404 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5405 | # endif |
| 5406 | enable_restore_tty_pgrp_on_exit(); |
| 5407 | # if !BB_MMU |
| 5408 | free(to_free); |
| 5409 | # endif |
| 5410 | close(channel[1]); |
| 5411 | close_on_exec_on(channel[0]); |
| 5412 | return xfdopen_for_read(channel[0]); |
| 5413 | } |
| 5414 | |
| 5415 | /* Return code is exit status of the process that is run. */ |
| 5416 | static int process_command_subs(o_string *dest, const char *s) |
| 5417 | { |
| 5418 | FILE *fp; |
| 5419 | struct in_str pipe_str; |
| 5420 | pid_t pid; |
| 5421 | int status, ch, eol_cnt; |
| 5422 | |
| 5423 | fp = generate_stream_from_string(s, &pid); |
| 5424 | |
| 5425 | /* Now send results of command back into original context */ |
| 5426 | setup_file_in_str(&pipe_str, fp); |
| 5427 | eol_cnt = 0; |
| 5428 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 5429 | if (ch == '\n') { |
| 5430 | eol_cnt++; |
| 5431 | continue; |
| 5432 | } |
| 5433 | while (eol_cnt) { |
| 5434 | o_addchr(dest, '\n'); |
| 5435 | eol_cnt--; |
| 5436 | } |
| 5437 | o_addQchr(dest, ch); |
| 5438 | } |
| 5439 | |
| 5440 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
| 5441 | fclose(fp); |
| 5442 | /* We need to extract exitcode. Test case |
| 5443 | * "true; echo `sleep 1; false` $?" |
| 5444 | * should print 1 */ |
| 5445 | safe_waitpid(pid, &status, 0); |
| 5446 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5447 | return WEXITSTATUS(status); |
| 5448 | } |
| 5449 | #endif /* ENABLE_HUSH_TICK */ |
| 5450 | |
| 5451 | |
| 5452 | static void setup_heredoc(struct redir_struct *redir) |
| 5453 | { |
| 5454 | struct fd_pair pair; |
| 5455 | pid_t pid; |
| 5456 | int len, written; |
| 5457 | /* the _body_ of heredoc (misleading field name) */ |
| 5458 | const char *heredoc = redir->rd_filename; |
| 5459 | char *expanded; |
| 5460 | #if !BB_MMU |
| 5461 | char **to_free; |
| 5462 | #endif |
| 5463 | |
| 5464 | expanded = NULL; |
| 5465 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
| 5466 | expanded = expand_pseudo_dquoted(heredoc); |
| 5467 | if (expanded) |
| 5468 | heredoc = expanded; |
| 5469 | } |
| 5470 | len = strlen(heredoc); |
| 5471 | |
| 5472 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 5473 | xpiped_pair(pair); |
| 5474 | xmove_fd(pair.rd, redir->rd_fd); |
| 5475 | |
| 5476 | /* Try writing without forking. Newer kernels have |
| 5477 | * dynamically growing pipes. Must use non-blocking write! */ |
| 5478 | ndelay_on(pair.wr); |
| 5479 | while (1) { |
| 5480 | written = write(pair.wr, heredoc, len); |
| 5481 | if (written <= 0) |
| 5482 | break; |
| 5483 | len -= written; |
| 5484 | if (len == 0) { |
| 5485 | close(pair.wr); |
| 5486 | free(expanded); |
| 5487 | return; |
| 5488 | } |
| 5489 | heredoc += written; |
| 5490 | } |
| 5491 | ndelay_off(pair.wr); |
| 5492 | |
| 5493 | /* Okay, pipe buffer was not big enough */ |
| 5494 | /* Note: we must not create a stray child (bastard? :) |
| 5495 | * for the unsuspecting parent process. Child creates a grandchild |
| 5496 | * and exits before parent execs the process which consumes heredoc |
| 5497 | * (that exec happens after we return from this function) */ |
| 5498 | #if !BB_MMU |
| 5499 | to_free = NULL; |
| 5500 | #endif |
| 5501 | pid = xvfork(); |
| 5502 | if (pid == 0) { |
| 5503 | /* child */ |
| 5504 | disable_restore_tty_pgrp_on_exit(); |
| 5505 | pid = BB_MMU ? xfork() : xvfork(); |
| 5506 | if (pid != 0) |
| 5507 | _exit(0); |
| 5508 | /* grandchild */ |
| 5509 | close(redir->rd_fd); /* read side of the pipe */ |
| 5510 | #if BB_MMU |
| 5511 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 5512 | _exit(0); |
| 5513 | #else |
| 5514 | /* Delegate blocking writes to another process */ |
| 5515 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 5516 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 5517 | #endif |
| 5518 | } |
| 5519 | /* parent */ |
| 5520 | #if ENABLE_HUSH_FAST |
| 5521 | G.count_SIGCHLD++; |
| 5522 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5523 | #endif |
| 5524 | enable_restore_tty_pgrp_on_exit(); |
| 5525 | #if !BB_MMU |
| 5526 | free(to_free); |
| 5527 | #endif |
| 5528 | close(pair.wr); |
| 5529 | free(expanded); |
| 5530 | wait(NULL); /* wait till child has died */ |
| 5531 | } |
| 5532 | |
| 5533 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 5534 | * and stderr if they are redirected. */ |
| 5535 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 5536 | { |
| 5537 | int openfd, mode; |
| 5538 | struct redir_struct *redir; |
| 5539 | |
| 5540 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 5541 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
| 5542 | /* rd_fd<<HERE case */ |
| 5543 | if (squirrel && redir->rd_fd < 3 |
| 5544 | && squirrel[redir->rd_fd] < 0 |
| 5545 | ) { |
| 5546 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 5547 | } |
| 5548 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 5549 | * of the heredoc */ |
| 5550 | debug_printf_parse("set heredoc '%s'\n", |
| 5551 | redir->rd_filename); |
| 5552 | setup_heredoc(redir); |
| 5553 | continue; |
| 5554 | } |
| 5555 | |
| 5556 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 5557 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
| 5558 | char *p; |
| 5559 | if (redir->rd_filename == NULL) { |
| 5560 | /* Something went wrong in the parse. |
| 5561 | * Pretend it didn't happen */ |
| 5562 | bb_error_msg("bug in redirect parse"); |
| 5563 | continue; |
| 5564 | } |
| 5565 | mode = redir_table[redir->rd_type].mode; |
| 5566 | p = expand_string_to_string(redir->rd_filename); |
| 5567 | openfd = open_or_warn(p, mode); |
| 5568 | free(p); |
| 5569 | if (openfd < 0) { |
| 5570 | /* this could get lost if stderr has been redirected, but |
| 5571 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 5572 | //what the above comment tries to say? |
| 5573 | return 1; |
| 5574 | } |
| 5575 | } else { |
| 5576 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
| 5577 | openfd = redir->rd_dup; |
| 5578 | } |
| 5579 | |
| 5580 | if (openfd != redir->rd_fd) { |
| 5581 | if (squirrel && redir->rd_fd < 3 |
| 5582 | && squirrel[redir->rd_fd] < 0 |
| 5583 | ) { |
| 5584 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 5585 | } |
| 5586 | if (openfd == REDIRFD_CLOSE) { |
| 5587 | /* "n>-" means "close me" */ |
| 5588 | close(redir->rd_fd); |
| 5589 | } else { |
| 5590 | xdup2(openfd, redir->rd_fd); |
| 5591 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 5592 | close(openfd); |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | return 0; |
| 5597 | } |
| 5598 | |
| 5599 | static void restore_redirects(int squirrel[]) |
| 5600 | { |
| 5601 | int i, fd; |
| 5602 | for (i = 0; i < 3; i++) { |
| 5603 | fd = squirrel[i]; |
| 5604 | if (fd != -1) { |
| 5605 | /* We simply die on error */ |
| 5606 | xmove_fd(fd, i); |
| 5607 | } |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | static char *find_in_path(const char *arg) |
| 5612 | { |
| 5613 | char *ret = NULL; |
| 5614 | const char *PATH = get_local_var_value("PATH"); |
| 5615 | |
| 5616 | if (!PATH) |
| 5617 | return NULL; |
| 5618 | |
| 5619 | while (1) { |
| 5620 | const char *end = strchrnul(PATH, ':'); |
| 5621 | int sz = end - PATH; /* must be int! */ |
| 5622 | |
| 5623 | free(ret); |
| 5624 | if (sz != 0) { |
| 5625 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 5626 | } else { |
| 5627 | /* We have xxx::yyyy in $PATH, |
| 5628 | * it means "use current dir" */ |
| 5629 | ret = xstrdup(arg); |
| 5630 | } |
| 5631 | if (access(ret, F_OK) == 0) |
| 5632 | break; |
| 5633 | |
| 5634 | if (*end == '\0') { |
| 5635 | free(ret); |
| 5636 | return NULL; |
| 5637 | } |
| 5638 | PATH = end + 1; |
| 5639 | } |
| 5640 | |
| 5641 | return ret; |
| 5642 | } |
| 5643 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5644 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5645 | const struct built_in_command *x, |
| 5646 | const struct built_in_command *end) |
| 5647 | { |
| 5648 | while (x != end) { |
| 5649 | if (strcmp(name, x->b_cmd) != 0) { |
| 5650 | x++; |
| 5651 | continue; |
| 5652 | } |
| 5653 | debug_printf_exec("found builtin '%s'\n", name); |
| 5654 | return x; |
| 5655 | } |
| 5656 | return NULL; |
| 5657 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5658 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5659 | { |
| 5660 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 5661 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5662 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5663 | { |
| 5664 | const struct built_in_command *x = find_builtin1(name); |
| 5665 | if (x) |
| 5666 | return x; |
| 5667 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 5668 | } |
| 5669 | |
| 5670 | #if ENABLE_HUSH_FUNCTIONS |
| 5671 | static struct function **find_function_slot(const char *name) |
| 5672 | { |
| 5673 | struct function **funcpp = &G.top_func; |
| 5674 | while (*funcpp) { |
| 5675 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 5676 | break; |
| 5677 | } |
| 5678 | funcpp = &(*funcpp)->next; |
| 5679 | } |
| 5680 | return funcpp; |
| 5681 | } |
| 5682 | |
| 5683 | static const struct function *find_function(const char *name) |
| 5684 | { |
| 5685 | const struct function *funcp = *find_function_slot(name); |
| 5686 | if (funcp) |
| 5687 | debug_printf_exec("found function '%s'\n", name); |
| 5688 | return funcp; |
| 5689 | } |
| 5690 | |
| 5691 | /* Note: takes ownership on name ptr */ |
| 5692 | static struct function *new_function(char *name) |
| 5693 | { |
| 5694 | struct function **funcpp = find_function_slot(name); |
| 5695 | struct function *funcp = *funcpp; |
| 5696 | |
| 5697 | if (funcp != NULL) { |
| 5698 | struct command *cmd = funcp->parent_cmd; |
| 5699 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 5700 | if (!cmd) { |
| 5701 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 5702 | free(funcp->name); |
| 5703 | /* Note: if !funcp->body, do not free body_as_string! |
| 5704 | * This is a special case of "-F name body" function: |
| 5705 | * body_as_string was not malloced! */ |
| 5706 | if (funcp->body) { |
| 5707 | free_pipe_list(funcp->body); |
| 5708 | # if !BB_MMU |
| 5709 | free(funcp->body_as_string); |
| 5710 | # endif |
| 5711 | } |
| 5712 | } else { |
| 5713 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 5714 | cmd->argv[0] = funcp->name; |
| 5715 | cmd->group = funcp->body; |
| 5716 | # if !BB_MMU |
| 5717 | cmd->group_as_string = funcp->body_as_string; |
| 5718 | # endif |
| 5719 | } |
| 5720 | } else { |
| 5721 | debug_printf_exec("remembering new function '%s'\n", name); |
| 5722 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 5723 | /*funcp->next = NULL;*/ |
| 5724 | } |
| 5725 | |
| 5726 | funcp->name = name; |
| 5727 | return funcp; |
| 5728 | } |
| 5729 | |
| 5730 | static void unset_func(const char *name) |
| 5731 | { |
| 5732 | struct function **funcpp = find_function_slot(name); |
| 5733 | struct function *funcp = *funcpp; |
| 5734 | |
| 5735 | if (funcp != NULL) { |
| 5736 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 5737 | *funcpp = funcp->next; |
| 5738 | /* funcp is unlinked now, deleting it. |
| 5739 | * Note: if !funcp->body, the function was created by |
| 5740 | * "-F name body", do not free ->body_as_string |
| 5741 | * and ->name as they were not malloced. */ |
| 5742 | if (funcp->body) { |
| 5743 | free_pipe_list(funcp->body); |
| 5744 | free(funcp->name); |
| 5745 | # if !BB_MMU |
| 5746 | free(funcp->body_as_string); |
| 5747 | # endif |
| 5748 | } |
| 5749 | free(funcp); |
| 5750 | } |
| 5751 | } |
| 5752 | |
| 5753 | # if BB_MMU |
| 5754 | #define exec_function(to_free, funcp, argv) \ |
| 5755 | exec_function(funcp, argv) |
| 5756 | # endif |
| 5757 | static void exec_function(char ***to_free, |
| 5758 | const struct function *funcp, |
| 5759 | char **argv) NORETURN; |
| 5760 | static void exec_function(char ***to_free, |
| 5761 | const struct function *funcp, |
| 5762 | char **argv) |
| 5763 | { |
| 5764 | # if BB_MMU |
| 5765 | int n = 1; |
| 5766 | |
| 5767 | argv[0] = G.global_argv[0]; |
| 5768 | G.global_argv = argv; |
| 5769 | while (*++argv) |
| 5770 | n++; |
| 5771 | G.global_argc = n; |
| 5772 | /* On MMU, funcp->body is always non-NULL */ |
| 5773 | n = run_list(funcp->body); |
| 5774 | fflush_all(); |
| 5775 | _exit(n); |
| 5776 | # else |
| 5777 | re_execute_shell(to_free, |
| 5778 | funcp->body_as_string, |
| 5779 | G.global_argv[0], |
| 5780 | argv + 1, |
| 5781 | NULL); |
| 5782 | # endif |
| 5783 | } |
| 5784 | |
| 5785 | static int run_function(const struct function *funcp, char **argv) |
| 5786 | { |
| 5787 | int rc; |
| 5788 | save_arg_t sv; |
| 5789 | smallint sv_flg; |
| 5790 | |
| 5791 | save_and_replace_G_args(&sv, argv); |
| 5792 | |
| 5793 | /* "we are in function, ok to use return" */ |
| 5794 | sv_flg = G.flag_return_in_progress; |
| 5795 | G.flag_return_in_progress = -1; |
| 5796 | # if ENABLE_HUSH_LOCAL |
| 5797 | G.func_nest_level++; |
| 5798 | # endif |
| 5799 | |
| 5800 | /* On MMU, funcp->body is always non-NULL */ |
| 5801 | # if !BB_MMU |
| 5802 | if (!funcp->body) { |
| 5803 | /* Function defined by -F */ |
| 5804 | parse_and_run_string(funcp->body_as_string); |
| 5805 | rc = G.last_exitcode; |
| 5806 | } else |
| 5807 | # endif |
| 5808 | { |
| 5809 | rc = run_list(funcp->body); |
| 5810 | } |
| 5811 | |
| 5812 | # if ENABLE_HUSH_LOCAL |
| 5813 | { |
| 5814 | struct variable *var; |
| 5815 | struct variable **var_pp; |
| 5816 | |
| 5817 | var_pp = &G.top_var; |
| 5818 | while ((var = *var_pp) != NULL) { |
| 5819 | if (var->func_nest_level < G.func_nest_level) { |
| 5820 | var_pp = &var->next; |
| 5821 | continue; |
| 5822 | } |
| 5823 | /* Unexport */ |
| 5824 | if (var->flg_export) |
| 5825 | bb_unsetenv(var->varstr); |
| 5826 | /* Remove from global list */ |
| 5827 | *var_pp = var->next; |
| 5828 | /* Free */ |
| 5829 | if (!var->max_len) |
| 5830 | free(var->varstr); |
| 5831 | free(var); |
| 5832 | } |
| 5833 | G.func_nest_level--; |
| 5834 | } |
| 5835 | # endif |
| 5836 | G.flag_return_in_progress = sv_flg; |
| 5837 | |
| 5838 | restore_G_args(&sv, argv); |
| 5839 | |
| 5840 | return rc; |
| 5841 | } |
| 5842 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 5843 | |
| 5844 | |
| 5845 | #if BB_MMU |
| 5846 | #define exec_builtin(to_free, x, argv) \ |
| 5847 | exec_builtin(x, argv) |
| 5848 | #else |
| 5849 | #define exec_builtin(to_free, x, argv) \ |
| 5850 | exec_builtin(to_free, argv) |
| 5851 | #endif |
| 5852 | static void exec_builtin(char ***to_free, |
| 5853 | const struct built_in_command *x, |
| 5854 | char **argv) NORETURN; |
| 5855 | static void exec_builtin(char ***to_free, |
| 5856 | const struct built_in_command *x, |
| 5857 | char **argv) |
| 5858 | { |
| 5859 | #if BB_MMU |
| 5860 | int rcode = x->b_function(argv); |
| 5861 | fflush_all(); |
| 5862 | _exit(rcode); |
| 5863 | #else |
| 5864 | /* On NOMMU, we must never block! |
| 5865 | * Example: { sleep 99 | read line; } & echo Ok |
| 5866 | */ |
| 5867 | re_execute_shell(to_free, |
| 5868 | argv[0], |
| 5869 | G.global_argv[0], |
| 5870 | G.global_argv + 1, |
| 5871 | argv); |
| 5872 | #endif |
| 5873 | } |
| 5874 | |
| 5875 | |
| 5876 | static void execvp_or_die(char **argv) NORETURN; |
| 5877 | static void execvp_or_die(char **argv) |
| 5878 | { |
| 5879 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 5880 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 5881 | execvp(argv[0], argv); |
| 5882 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 5883 | _exit(127); /* bash compat */ |
| 5884 | } |
| 5885 | |
| 5886 | #if ENABLE_HUSH_MODE_X |
| 5887 | static void dump_cmd_in_x_mode(char **argv) |
| 5888 | { |
| 5889 | if (G_x_mode && argv) { |
| 5890 | /* We want to output the line in one write op */ |
| 5891 | char *buf, *p; |
| 5892 | int len; |
| 5893 | int n; |
| 5894 | |
| 5895 | len = 3; |
| 5896 | n = 0; |
| 5897 | while (argv[n]) |
| 5898 | len += strlen(argv[n++]) + 1; |
| 5899 | buf = xmalloc(len); |
| 5900 | buf[0] = '+'; |
| 5901 | p = buf + 1; |
| 5902 | n = 0; |
| 5903 | while (argv[n]) |
| 5904 | p += sprintf(p, " %s", argv[n++]); |
| 5905 | *p++ = '\n'; |
| 5906 | *p = '\0'; |
| 5907 | fputs(buf, stderr); |
| 5908 | free(buf); |
| 5909 | } |
| 5910 | } |
| 5911 | #else |
| 5912 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 5913 | #endif |
| 5914 | |
| 5915 | #if BB_MMU |
| 5916 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 5917 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 5918 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 5919 | pseudo_exec(command, argv_expanded) |
| 5920 | #endif |
| 5921 | |
| 5922 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 5923 | * Never returns. |
| 5924 | * Don't exit() here. If you don't exec, use _exit instead. |
| 5925 | * The at_exit handlers apparently confuse the calling process, |
| 5926 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
| 5927 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 5928 | char **argv, int assignment_cnt, |
| 5929 | char **argv_expanded) NORETURN; |
| 5930 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 5931 | char **argv, int assignment_cnt, |
| 5932 | char **argv_expanded) |
| 5933 | { |
| 5934 | char **new_env; |
| 5935 | |
| 5936 | new_env = expand_assignments(argv, assignment_cnt); |
| 5937 | dump_cmd_in_x_mode(new_env); |
| 5938 | |
| 5939 | if (!argv[assignment_cnt]) { |
| 5940 | /* Case when we are here: ... | var=val | ... |
| 5941 | * (note that we do not exit early, i.e., do not optimize out |
| 5942 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 5943 | */ |
| 5944 | free_strings(new_env); |
| 5945 | _exit(EXIT_SUCCESS); |
| 5946 | } |
| 5947 | |
| 5948 | #if BB_MMU |
| 5949 | set_vars_and_save_old(new_env); |
| 5950 | free(new_env); /* optional */ |
| 5951 | /* we can also destroy set_vars_and_save_old's return value, |
| 5952 | * to save memory */ |
| 5953 | #else |
| 5954 | nommu_save->new_env = new_env; |
| 5955 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 5956 | #endif |
| 5957 | |
| 5958 | if (argv_expanded) { |
| 5959 | argv = argv_expanded; |
| 5960 | } else { |
| 5961 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 5962 | #if !BB_MMU |
| 5963 | nommu_save->argv = argv; |
| 5964 | #endif |
| 5965 | } |
| 5966 | dump_cmd_in_x_mode(argv); |
| 5967 | |
| 5968 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 5969 | if (strchr(argv[0], '/') != NULL) |
| 5970 | goto skip; |
| 5971 | #endif |
| 5972 | |
| 5973 | /* Check if the command matches any of the builtins. |
| 5974 | * Depending on context, this might be redundant. But it's |
| 5975 | * easier to waste a few CPU cycles than it is to figure out |
| 5976 | * if this is one of those cases. |
| 5977 | */ |
| 5978 | { |
| 5979 | /* On NOMMU, it is more expensive to re-execute shell |
| 5980 | * just in order to run echo or test builtin. |
| 5981 | * It's better to skip it here and run corresponding |
| 5982 | * non-builtin later. */ |
| 5983 | const struct built_in_command *x; |
| 5984 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 5985 | if (x) { |
| 5986 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 5987 | } |
| 5988 | } |
| 5989 | #if ENABLE_HUSH_FUNCTIONS |
| 5990 | /* Check if the command matches any functions */ |
| 5991 | { |
| 5992 | const struct function *funcp = find_function(argv[0]); |
| 5993 | if (funcp) { |
| 5994 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 5995 | } |
| 5996 | } |
| 5997 | #endif |
| 5998 | |
| 5999 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6000 | /* Check if the command matches any busybox applets */ |
| 6001 | { |
| 6002 | int a = find_applet_by_name(argv[0]); |
| 6003 | if (a >= 0) { |
| 6004 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6005 | if (APPLET_IS_NOEXEC(a)) { |
| 6006 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6007 | run_applet_no_and_exit(a, argv); |
| 6008 | } |
| 6009 | # endif |
| 6010 | /* Re-exec ourselves */ |
| 6011 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
| 6012 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 6013 | execv(bb_busybox_exec_path, argv); |
| 6014 | /* If they called chroot or otherwise made the binary no longer |
| 6015 | * executable, fall through */ |
| 6016 | } |
| 6017 | } |
| 6018 | #endif |
| 6019 | |
| 6020 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6021 | skip: |
| 6022 | #endif |
| 6023 | execvp_or_die(argv); |
| 6024 | } |
| 6025 | |
| 6026 | /* Called after [v]fork() in run_pipe |
| 6027 | */ |
| 6028 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6029 | struct command *command, |
| 6030 | char **argv_expanded) NORETURN; |
| 6031 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6032 | struct command *command, |
| 6033 | char **argv_expanded) |
| 6034 | { |
| 6035 | if (command->argv) { |
| 6036 | pseudo_exec_argv(nommu_save, command->argv, |
| 6037 | command->assignment_cnt, argv_expanded); |
| 6038 | } |
| 6039 | |
| 6040 | if (command->group) { |
| 6041 | /* Cases when we are here: |
| 6042 | * ( list ) |
| 6043 | * { list } & |
| 6044 | * ... | ( list ) | ... |
| 6045 | * ... | { list } | ... |
| 6046 | */ |
| 6047 | #if BB_MMU |
| 6048 | int rcode; |
| 6049 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6050 | reset_traps_to_defaults(); |
| 6051 | rcode = run_list(command->group); |
| 6052 | /* OK to leak memory by not calling free_pipe_list, |
| 6053 | * since this process is about to exit */ |
| 6054 | _exit(rcode); |
| 6055 | #else |
| 6056 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6057 | command->group_as_string, |
| 6058 | G.global_argv[0], |
| 6059 | G.global_argv + 1, |
| 6060 | NULL); |
| 6061 | #endif |
| 6062 | } |
| 6063 | |
| 6064 | /* Case when we are here: ... | >file */ |
| 6065 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6066 | _exit(EXIT_SUCCESS); |
| 6067 | } |
| 6068 | |
| 6069 | #if ENABLE_HUSH_JOB |
| 6070 | static const char *get_cmdtext(struct pipe *pi) |
| 6071 | { |
| 6072 | char **argv; |
| 6073 | char *p; |
| 6074 | int len; |
| 6075 | |
| 6076 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6077 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6078 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6079 | if (pi->cmdtext) |
| 6080 | return pi->cmdtext; |
| 6081 | argv = pi->cmds[0].argv; |
| 6082 | if (!argv || !argv[0]) { |
| 6083 | pi->cmdtext = xzalloc(1); |
| 6084 | return pi->cmdtext; |
| 6085 | } |
| 6086 | |
| 6087 | len = 0; |
| 6088 | do { |
| 6089 | len += strlen(*argv) + 1; |
| 6090 | } while (*++argv); |
| 6091 | p = xmalloc(len); |
| 6092 | pi->cmdtext = p; |
| 6093 | argv = pi->cmds[0].argv; |
| 6094 | do { |
| 6095 | len = strlen(*argv); |
| 6096 | memcpy(p, *argv, len); |
| 6097 | p += len; |
| 6098 | *p++ = ' '; |
| 6099 | } while (*++argv); |
| 6100 | p[-1] = '\0'; |
| 6101 | return pi->cmdtext; |
| 6102 | } |
| 6103 | |
| 6104 | static void insert_bg_job(struct pipe *pi) |
| 6105 | { |
| 6106 | struct pipe *job, **jobp; |
| 6107 | int i; |
| 6108 | |
| 6109 | /* Linear search for the ID of the job to use */ |
| 6110 | pi->jobid = 1; |
| 6111 | for (job = G.job_list; job; job = job->next) |
| 6112 | if (job->jobid >= pi->jobid) |
| 6113 | pi->jobid = job->jobid + 1; |
| 6114 | |
| 6115 | /* Add job to the list of running jobs */ |
| 6116 | jobp = &G.job_list; |
| 6117 | while ((job = *jobp) != NULL) |
| 6118 | jobp = &job->next; |
| 6119 | job = *jobp = xmalloc(sizeof(*job)); |
| 6120 | |
| 6121 | *job = *pi; /* physical copy */ |
| 6122 | job->next = NULL; |
| 6123 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 6124 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 6125 | for (i = 0; i < pi->num_cmds; i++) { |
| 6126 | job->cmds[i].pid = pi->cmds[i].pid; |
| 6127 | /* all other fields are not used and stay zero */ |
| 6128 | } |
| 6129 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 6130 | |
| 6131 | if (G_interactive_fd) |
| 6132 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 6133 | G.last_jobid = job->jobid; |
| 6134 | } |
| 6135 | |
| 6136 | static void remove_bg_job(struct pipe *pi) |
| 6137 | { |
| 6138 | struct pipe *prev_pipe; |
| 6139 | |
| 6140 | if (pi == G.job_list) { |
| 6141 | G.job_list = pi->next; |
| 6142 | } else { |
| 6143 | prev_pipe = G.job_list; |
| 6144 | while (prev_pipe->next != pi) |
| 6145 | prev_pipe = prev_pipe->next; |
| 6146 | prev_pipe->next = pi->next; |
| 6147 | } |
| 6148 | if (G.job_list) |
| 6149 | G.last_jobid = G.job_list->jobid; |
| 6150 | else |
| 6151 | G.last_jobid = 0; |
| 6152 | } |
| 6153 | |
| 6154 | /* Remove a backgrounded job */ |
| 6155 | static void delete_finished_bg_job(struct pipe *pi) |
| 6156 | { |
| 6157 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6158 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6159 | } |
| 6160 | #endif /* JOB */ |
| 6161 | |
| 6162 | /* Check to see if any processes have exited -- if they |
| 6163 | * have, figure out why and see if a job has completed */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 6164 | static int checkjobs(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6165 | { |
| 6166 | int attributes; |
| 6167 | int status; |
| 6168 | #if ENABLE_HUSH_JOB |
| 6169 | struct pipe *pi; |
| 6170 | #endif |
| 6171 | pid_t childpid; |
| 6172 | int rcode = 0; |
| 6173 | |
| 6174 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 6175 | |
| 6176 | attributes = WUNTRACED; |
| 6177 | if (fg_pipe == NULL) |
| 6178 | attributes |= WNOHANG; |
| 6179 | |
| 6180 | errno = 0; |
| 6181 | #if ENABLE_HUSH_FAST |
| 6182 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 6183 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 6184 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 6185 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 6186 | /* Avoid doing waitpid syscall if possible */ |
| 6187 | if (!G.we_have_children) { |
| 6188 | errno = ECHILD; |
| 6189 | return -1; |
| 6190 | } |
| 6191 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 6192 | /* We have children, but they did not exit |
| 6193 | * or stop yet (we saw no SIGCHLD) */ |
| 6194 | return 0; |
| 6195 | } |
| 6196 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 6197 | } |
| 6198 | #endif |
| 6199 | |
| 6200 | /* Do we do this right? |
| 6201 | * bash-3.00# sleep 20 | false |
| 6202 | * <ctrl-Z pressed> |
| 6203 | * [3]+ Stopped sleep 20 | false |
| 6204 | * bash-3.00# echo $? |
| 6205 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 6206 | * [hush 1.14.0: yes we do it right] |
| 6207 | */ |
| 6208 | wait_more: |
| 6209 | while (1) { |
| 6210 | int i; |
| 6211 | int dead; |
| 6212 | |
| 6213 | #if ENABLE_HUSH_FAST |
| 6214 | i = G.count_SIGCHLD; |
| 6215 | #endif |
| 6216 | childpid = waitpid(-1, &status, attributes); |
| 6217 | if (childpid <= 0) { |
| 6218 | if (childpid && errno != ECHILD) |
| 6219 | bb_perror_msg("waitpid"); |
| 6220 | #if ENABLE_HUSH_FAST |
| 6221 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 6222 | G.we_have_children = (childpid == 0); |
| 6223 | G.handled_SIGCHLD = i; |
| 6224 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6225 | } |
| 6226 | #endif |
| 6227 | break; |
| 6228 | } |
| 6229 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 6230 | |
| 6231 | #if DEBUG_JOBS |
| 6232 | if (WIFSTOPPED(status)) |
| 6233 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 6234 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 6235 | if (WIFSIGNALED(status)) |
| 6236 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 6237 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 6238 | if (WIFEXITED(status)) |
| 6239 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 6240 | childpid, WEXITSTATUS(status)); |
| 6241 | #endif |
| 6242 | /* Were we asked to wait for fg pipe? */ |
| 6243 | if (fg_pipe) { |
| 6244 | for (i = 0; i < fg_pipe->num_cmds; i++) { |
| 6245 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 6246 | if (fg_pipe->cmds[i].pid != childpid) |
| 6247 | continue; |
| 6248 | if (dead) { |
| 6249 | fg_pipe->cmds[i].pid = 0; |
| 6250 | fg_pipe->alive_cmds--; |
| 6251 | if (i == fg_pipe->num_cmds - 1) { |
| 6252 | /* last process gives overall exitstatus */ |
| 6253 | rcode = WEXITSTATUS(status); |
| 6254 | /* bash prints killer signal's name for *last* |
| 6255 | * process in pipe (prints just newline for SIGINT). |
| 6256 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 6257 | */ |
| 6258 | if (WIFSIGNALED(status)) { |
| 6259 | int sig = WTERMSIG(status); |
| 6260 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
| 6261 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 6262 | * Maybe we need to use sig | 128? */ |
| 6263 | rcode = sig + 128; |
| 6264 | } |
| 6265 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
| 6266 | } |
| 6267 | } else { |
| 6268 | fg_pipe->cmds[i].is_stopped = 1; |
| 6269 | fg_pipe->stopped_cmds++; |
| 6270 | } |
| 6271 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 6272 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
| 6273 | if (fg_pipe->alive_cmds - fg_pipe->stopped_cmds <= 0) { |
| 6274 | /* All processes in fg pipe have exited or stopped */ |
| 6275 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 6276 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 6277 | * and "killall -STOP cat" */ |
| 6278 | if (G_interactive_fd) { |
| 6279 | #if ENABLE_HUSH_JOB |
| 6280 | if (fg_pipe->alive_cmds) |
| 6281 | insert_bg_job(fg_pipe); |
| 6282 | #endif |
| 6283 | return rcode; |
| 6284 | } |
| 6285 | if (!fg_pipe->alive_cmds) |
| 6286 | return rcode; |
| 6287 | } |
| 6288 | /* There are still running processes in the fg pipe */ |
| 6289 | goto wait_more; /* do waitpid again */ |
| 6290 | } |
| 6291 | /* it wasnt fg_pipe, look for process in bg pipes */ |
| 6292 | } |
| 6293 | |
| 6294 | #if ENABLE_HUSH_JOB |
| 6295 | /* We asked to wait for bg or orphaned children */ |
| 6296 | /* No need to remember exitcode in this case */ |
| 6297 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6298 | for (i = 0; i < pi->num_cmds; i++) { |
| 6299 | if (pi->cmds[i].pid == childpid) |
| 6300 | goto found_pi_and_prognum; |
| 6301 | } |
| 6302 | } |
| 6303 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 6304 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 6305 | continue; /* do waitpid again */ |
| 6306 | |
| 6307 | found_pi_and_prognum: |
| 6308 | if (dead) { |
| 6309 | /* child exited */ |
| 6310 | pi->cmds[i].pid = 0; |
| 6311 | pi->alive_cmds--; |
| 6312 | if (!pi->alive_cmds) { |
| 6313 | if (G_interactive_fd) |
| 6314 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 6315 | "Done", pi->cmdtext); |
| 6316 | delete_finished_bg_job(pi); |
| 6317 | } |
| 6318 | } else { |
| 6319 | /* child stopped */ |
| 6320 | pi->cmds[i].is_stopped = 1; |
| 6321 | pi->stopped_cmds++; |
| 6322 | } |
| 6323 | #endif |
| 6324 | } /* while (waitpid succeeds)... */ |
| 6325 | |
| 6326 | return rcode; |
| 6327 | } |
| 6328 | |
| 6329 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6330 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6331 | { |
| 6332 | pid_t p; |
| 6333 | int rcode = checkjobs(fg_pipe); |
| 6334 | if (G_saved_tty_pgrp) { |
| 6335 | /* Job finished, move the shell to the foreground */ |
| 6336 | p = getpgrp(); /* our process group id */ |
| 6337 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 6338 | tcsetpgrp(G_interactive_fd, p); |
| 6339 | } |
| 6340 | return rcode; |
| 6341 | } |
| 6342 | #endif |
| 6343 | |
| 6344 | /* Start all the jobs, but don't wait for anything to finish. |
| 6345 | * See checkjobs(). |
| 6346 | * |
| 6347 | * Return code is normally -1, when the caller has to wait for children |
| 6348 | * to finish to determine the exit status of the pipe. If the pipe |
| 6349 | * is a simple builtin command, however, the action is done by the |
| 6350 | * time run_pipe returns, and the exit code is provided as the |
| 6351 | * return value. |
| 6352 | * |
| 6353 | * Returns -1 only if started some children. IOW: we have to |
| 6354 | * mask out retvals of builtins etc with 0xff! |
| 6355 | * |
| 6356 | * The only case when we do not need to [v]fork is when the pipe |
| 6357 | * is single, non-backgrounded, non-subshell command. Examples: |
| 6358 | * cmd ; ... { list } ; ... |
| 6359 | * cmd && ... { list } && ... |
| 6360 | * cmd || ... { list } || ... |
| 6361 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 6362 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 6363 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 6364 | * |
| 6365 | * Cases when we must fork: |
| 6366 | * non-single: cmd | cmd |
| 6367 | * backgrounded: cmd & { list } & |
| 6368 | * subshell: ( list ) [&] |
| 6369 | */ |
| 6370 | #if !ENABLE_HUSH_MODE_X |
| 6371 | #define redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel, char argv_expanded) \ |
| 6372 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 6373 | #endif |
| 6374 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 6375 | struct variable **old_vars_p, |
| 6376 | struct command *command, |
| 6377 | int squirrel[3], |
| 6378 | char **argv_expanded) |
| 6379 | { |
| 6380 | /* setup_redirects acts on file descriptors, not FILEs. |
| 6381 | * This is perfect for work that comes after exec(). |
| 6382 | * Is it really safe for inline use? Experimentally, |
| 6383 | * things seem to work. */ |
| 6384 | int rcode = setup_redirects(command, squirrel); |
| 6385 | if (rcode == 0) { |
| 6386 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 6387 | *new_env_p = new_env; |
| 6388 | dump_cmd_in_x_mode(new_env); |
| 6389 | dump_cmd_in_x_mode(argv_expanded); |
| 6390 | if (old_vars_p) |
| 6391 | *old_vars_p = set_vars_and_save_old(new_env); |
| 6392 | } |
| 6393 | return rcode; |
| 6394 | } |
| 6395 | static NOINLINE int run_pipe(struct pipe *pi) |
| 6396 | { |
| 6397 | static const char *const null_ptr = NULL; |
| 6398 | |
| 6399 | int cmd_no; |
| 6400 | int next_infd; |
| 6401 | struct command *command; |
| 6402 | char **argv_expanded; |
| 6403 | char **argv; |
| 6404 | /* it is not always needed, but we aim to smaller code */ |
| 6405 | int squirrel[] = { -1, -1, -1 }; |
| 6406 | int rcode; |
| 6407 | |
| 6408 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 6409 | debug_enter(); |
| 6410 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6411 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 6412 | * Result should be 3 lines: q w e, qwe, q w e |
| 6413 | */ |
| 6414 | G.ifs = get_local_var_value("IFS"); |
| 6415 | if (!G.ifs) |
| 6416 | G.ifs = defifs; |
| 6417 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6418 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 6419 | pi->stopped_cmds = 0; |
| 6420 | command = &pi->cmds[0]; |
| 6421 | argv_expanded = NULL; |
| 6422 | |
| 6423 | if (pi->num_cmds != 1 |
| 6424 | || pi->followup == PIPE_BG |
| 6425 | || command->cmd_type == CMD_SUBSHELL |
| 6426 | ) { |
| 6427 | goto must_fork; |
| 6428 | } |
| 6429 | |
| 6430 | pi->alive_cmds = 1; |
| 6431 | |
| 6432 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 6433 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 6434 | |
| 6435 | if (command->group) { |
| 6436 | #if ENABLE_HUSH_FUNCTIONS |
| 6437 | if (command->cmd_type == CMD_FUNCDEF) { |
| 6438 | /* "executing" func () { list } */ |
| 6439 | struct function *funcp; |
| 6440 | |
| 6441 | funcp = new_function(command->argv[0]); |
| 6442 | /* funcp->name is already set to argv[0] */ |
| 6443 | funcp->body = command->group; |
| 6444 | # if !BB_MMU |
| 6445 | funcp->body_as_string = command->group_as_string; |
| 6446 | command->group_as_string = NULL; |
| 6447 | # endif |
| 6448 | command->group = NULL; |
| 6449 | command->argv[0] = NULL; |
| 6450 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 6451 | funcp->parent_cmd = command; |
| 6452 | command->child_func = funcp; |
| 6453 | |
| 6454 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 6455 | debug_leave(); |
| 6456 | return EXIT_SUCCESS; |
| 6457 | } |
| 6458 | #endif |
| 6459 | /* { list } */ |
| 6460 | debug_printf("non-subshell group\n"); |
| 6461 | rcode = 1; /* exitcode if redir failed */ |
| 6462 | if (setup_redirects(command, squirrel) == 0) { |
| 6463 | debug_printf_exec(": run_list\n"); |
| 6464 | rcode = run_list(command->group) & 0xff; |
| 6465 | } |
| 6466 | restore_redirects(squirrel); |
| 6467 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6468 | debug_leave(); |
| 6469 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6470 | return rcode; |
| 6471 | } |
| 6472 | |
| 6473 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 6474 | { |
| 6475 | const struct built_in_command *x; |
| 6476 | #if ENABLE_HUSH_FUNCTIONS |
| 6477 | const struct function *funcp; |
| 6478 | #else |
| 6479 | enum { funcp = 0 }; |
| 6480 | #endif |
| 6481 | char **new_env = NULL; |
| 6482 | struct variable *old_vars = NULL; |
| 6483 | |
| 6484 | if (argv[command->assignment_cnt] == NULL) { |
| 6485 | /* Assignments, but no command */ |
| 6486 | /* Ensure redirects take effect (that is, create files). |
| 6487 | * Try "a=t >file" */ |
| 6488 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 6489 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 6490 | /* Set shell variables */ |
| 6491 | if (new_env) { |
| 6492 | argv = new_env; |
| 6493 | while (*argv) { |
| 6494 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6495 | /* Do we need to flag set_local_var() errors? |
| 6496 | * "assignment to readonly var" and "putenv error" |
| 6497 | */ |
| 6498 | argv++; |
| 6499 | } |
| 6500 | } |
| 6501 | /* Redirect error sets $? to 1. Otherwise, |
| 6502 | * if evaluating assignment value set $?, retain it. |
| 6503 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 6504 | if (rcode == 0) |
| 6505 | rcode = G.last_exitcode; |
| 6506 | /* Exit, _skipping_ variable restoring code: */ |
| 6507 | goto clean_up_and_ret0; |
| 6508 | |
| 6509 | #else /* Older, bigger, but more correct code */ |
| 6510 | |
| 6511 | rcode = setup_redirects(command, squirrel); |
| 6512 | restore_redirects(squirrel); |
| 6513 | /* Set shell variables */ |
| 6514 | if (G_x_mode) |
| 6515 | bb_putchar_stderr('+'); |
| 6516 | while (*argv) { |
| 6517 | char *p = expand_string_to_string(*argv); |
| 6518 | if (G_x_mode) |
| 6519 | fprintf(stderr, " %s", p); |
| 6520 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 6521 | *argv, p); |
| 6522 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6523 | /* Do we need to flag set_local_var() errors? |
| 6524 | * "assignment to readonly var" and "putenv error" |
| 6525 | */ |
| 6526 | argv++; |
| 6527 | } |
| 6528 | if (G_x_mode) |
| 6529 | bb_putchar_stderr('\n'); |
| 6530 | /* Redirect error sets $? to 1. Otherwise, |
| 6531 | * if evaluating assignment value set $?, retain it. |
| 6532 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 6533 | if (rcode == 0) |
| 6534 | rcode = G.last_exitcode; |
| 6535 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6536 | debug_leave(); |
| 6537 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6538 | return rcode; |
| 6539 | #endif |
| 6540 | } |
| 6541 | |
| 6542 | /* Expand the rest into (possibly) many strings each */ |
| 6543 | if (0) {} |
| 6544 | #if ENABLE_HUSH_BASH_COMPAT |
| 6545 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
| 6546 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 6547 | } |
| 6548 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6549 | else { |
| 6550 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 6551 | } |
| 6552 | |
| 6553 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 6554 | if (!argv_expanded[0]) { |
| 6555 | free(argv_expanded); |
| 6556 | debug_leave(); |
| 6557 | return G.last_exitcode; |
| 6558 | } |
| 6559 | |
| 6560 | x = find_builtin(argv_expanded[0]); |
| 6561 | #if ENABLE_HUSH_FUNCTIONS |
| 6562 | funcp = NULL; |
| 6563 | if (!x) |
| 6564 | funcp = find_function(argv_expanded[0]); |
| 6565 | #endif |
| 6566 | if (x || funcp) { |
| 6567 | if (!funcp) { |
| 6568 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 6569 | debug_printf("exec with redirects only\n"); |
| 6570 | rcode = setup_redirects(command, NULL); |
| 6571 | goto clean_up_and_ret1; |
| 6572 | } |
| 6573 | } |
| 6574 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 6575 | if (rcode == 0) { |
| 6576 | if (!funcp) { |
| 6577 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 6578 | x->b_cmd, argv_expanded[1]); |
| 6579 | rcode = x->b_function(argv_expanded) & 0xff; |
| 6580 | fflush_all(); |
| 6581 | } |
| 6582 | #if ENABLE_HUSH_FUNCTIONS |
| 6583 | else { |
| 6584 | # if ENABLE_HUSH_LOCAL |
| 6585 | struct variable **sv; |
| 6586 | sv = G.shadowed_vars_pp; |
| 6587 | G.shadowed_vars_pp = &old_vars; |
| 6588 | # endif |
| 6589 | debug_printf_exec(": function '%s' '%s'...\n", |
| 6590 | funcp->name, argv_expanded[1]); |
| 6591 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 6592 | # if ENABLE_HUSH_LOCAL |
| 6593 | G.shadowed_vars_pp = sv; |
| 6594 | # endif |
| 6595 | } |
| 6596 | #endif |
| 6597 | } |
| 6598 | clean_up_and_ret: |
| 6599 | unset_vars(new_env); |
| 6600 | add_vars(old_vars); |
| 6601 | /* clean_up_and_ret0: */ |
| 6602 | restore_redirects(squirrel); |
| 6603 | clean_up_and_ret1: |
| 6604 | free(argv_expanded); |
| 6605 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6606 | debug_leave(); |
| 6607 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 6608 | return rcode; |
| 6609 | } |
| 6610 | |
| 6611 | if (ENABLE_FEATURE_SH_STANDALONE) { |
| 6612 | int n = find_applet_by_name(argv_expanded[0]); |
| 6613 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 6614 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 6615 | if (rcode == 0) { |
| 6616 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 6617 | argv_expanded[0], argv_expanded[1]); |
| 6618 | rcode = run_nofork_applet(n, argv_expanded); |
| 6619 | } |
| 6620 | goto clean_up_and_ret; |
| 6621 | } |
| 6622 | } |
| 6623 | /* It is neither builtin nor applet. We must fork. */ |
| 6624 | } |
| 6625 | |
| 6626 | must_fork: |
| 6627 | /* NB: argv_expanded may already be created, and that |
| 6628 | * might include `cmd` runs! Do not rerun it! We *must* |
| 6629 | * use argv_expanded if it's non-NULL */ |
| 6630 | |
| 6631 | /* Going to fork a child per each pipe member */ |
| 6632 | pi->alive_cmds = 0; |
| 6633 | next_infd = 0; |
| 6634 | |
| 6635 | cmd_no = 0; |
| 6636 | while (cmd_no < pi->num_cmds) { |
| 6637 | struct fd_pair pipefds; |
| 6638 | #if !BB_MMU |
| 6639 | volatile nommu_save_t nommu_save; |
| 6640 | nommu_save.new_env = NULL; |
| 6641 | nommu_save.old_vars = NULL; |
| 6642 | nommu_save.argv = NULL; |
| 6643 | nommu_save.argv_from_re_execing = NULL; |
| 6644 | #endif |
| 6645 | command = &pi->cmds[cmd_no]; |
| 6646 | cmd_no++; |
| 6647 | if (command->argv) { |
| 6648 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 6649 | command->argv[0], command->argv[1]); |
| 6650 | } else { |
| 6651 | debug_printf_exec(": pipe member with no argv\n"); |
| 6652 | } |
| 6653 | |
| 6654 | /* pipes are inserted between pairs of commands */ |
| 6655 | pipefds.rd = 0; |
| 6656 | pipefds.wr = 1; |
| 6657 | if (cmd_no < pi->num_cmds) |
| 6658 | xpiped_pair(pipefds); |
| 6659 | |
| 6660 | command->pid = BB_MMU ? fork() : vfork(); |
| 6661 | if (!command->pid) { /* child */ |
| 6662 | #if ENABLE_HUSH_JOB |
| 6663 | disable_restore_tty_pgrp_on_exit(); |
| 6664 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6665 | |
| 6666 | /* Every child adds itself to new process group |
| 6667 | * with pgid == pid_of_first_child_in_pipe */ |
| 6668 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 6669 | pid_t pgrp; |
| 6670 | pgrp = pi->pgrp; |
| 6671 | if (pgrp < 0) /* true for 1st process only */ |
| 6672 | pgrp = getpid(); |
| 6673 | if (setpgid(0, pgrp) == 0 |
| 6674 | && pi->followup != PIPE_BG |
| 6675 | && G_saved_tty_pgrp /* we have ctty */ |
| 6676 | ) { |
| 6677 | /* We do it in *every* child, not just first, |
| 6678 | * to avoid races */ |
| 6679 | tcsetpgrp(G_interactive_fd, pgrp); |
| 6680 | } |
| 6681 | } |
| 6682 | #endif |
| 6683 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 6684 | /* 1st cmd in backgrounded pipe |
| 6685 | * should have its stdin /dev/null'ed */ |
| 6686 | close(0); |
| 6687 | if (open(bb_dev_null, O_RDONLY)) |
| 6688 | xopen("/", O_RDONLY); |
| 6689 | } else { |
| 6690 | xmove_fd(next_infd, 0); |
| 6691 | } |
| 6692 | xmove_fd(pipefds.wr, 1); |
| 6693 | if (pipefds.rd > 1) |
| 6694 | close(pipefds.rd); |
| 6695 | /* Like bash, explicit redirects override pipes, |
| 6696 | * and the pipe fd is available for dup'ing. */ |
| 6697 | if (setup_redirects(command, NULL)) |
| 6698 | _exit(1); |
| 6699 | |
| 6700 | /* Restore default handlers just prior to exec */ |
| 6701 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 6702 | |
| 6703 | /* Stores to nommu_save list of env vars putenv'ed |
| 6704 | * (NOMMU, on MMU we don't need that) */ |
| 6705 | /* cast away volatility... */ |
| 6706 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 6707 | /* pseudo_exec() does not return */ |
| 6708 | } |
| 6709 | |
| 6710 | /* parent or error */ |
| 6711 | #if ENABLE_HUSH_FAST |
| 6712 | G.count_SIGCHLD++; |
| 6713 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6714 | #endif |
| 6715 | enable_restore_tty_pgrp_on_exit(); |
| 6716 | #if !BB_MMU |
| 6717 | /* Clean up after vforked child */ |
| 6718 | free(nommu_save.argv); |
| 6719 | free(nommu_save.argv_from_re_execing); |
| 6720 | unset_vars(nommu_save.new_env); |
| 6721 | add_vars(nommu_save.old_vars); |
| 6722 | #endif |
| 6723 | free(argv_expanded); |
| 6724 | argv_expanded = NULL; |
| 6725 | if (command->pid < 0) { /* [v]fork failed */ |
| 6726 | /* Clearly indicate, was it fork or vfork */ |
| 6727 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 6728 | } else { |
| 6729 | pi->alive_cmds++; |
| 6730 | #if ENABLE_HUSH_JOB |
| 6731 | /* Second and next children need to know pid of first one */ |
| 6732 | if (pi->pgrp < 0) |
| 6733 | pi->pgrp = command->pid; |
| 6734 | #endif |
| 6735 | } |
| 6736 | |
| 6737 | if (cmd_no > 1) |
| 6738 | close(next_infd); |
| 6739 | if (cmd_no < pi->num_cmds) |
| 6740 | close(pipefds.wr); |
| 6741 | /* Pass read (output) pipe end to next iteration */ |
| 6742 | next_infd = pipefds.rd; |
| 6743 | } |
| 6744 | |
| 6745 | if (!pi->alive_cmds) { |
| 6746 | debug_leave(); |
| 6747 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 6748 | return 1; |
| 6749 | } |
| 6750 | |
| 6751 | debug_leave(); |
| 6752 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 6753 | return -1; |
| 6754 | } |
| 6755 | |
| 6756 | #ifndef debug_print_tree |
| 6757 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 6758 | { |
| 6759 | static const char *const PIPE[] = { |
| 6760 | [PIPE_SEQ] = "SEQ", |
| 6761 | [PIPE_AND] = "AND", |
| 6762 | [PIPE_OR ] = "OR" , |
| 6763 | [PIPE_BG ] = "BG" , |
| 6764 | }; |
| 6765 | static const char *RES[] = { |
| 6766 | [RES_NONE ] = "NONE" , |
| 6767 | # if ENABLE_HUSH_IF |
| 6768 | [RES_IF ] = "IF" , |
| 6769 | [RES_THEN ] = "THEN" , |
| 6770 | [RES_ELIF ] = "ELIF" , |
| 6771 | [RES_ELSE ] = "ELSE" , |
| 6772 | [RES_FI ] = "FI" , |
| 6773 | # endif |
| 6774 | # if ENABLE_HUSH_LOOPS |
| 6775 | [RES_FOR ] = "FOR" , |
| 6776 | [RES_WHILE] = "WHILE", |
| 6777 | [RES_UNTIL] = "UNTIL", |
| 6778 | [RES_DO ] = "DO" , |
| 6779 | [RES_DONE ] = "DONE" , |
| 6780 | # endif |
| 6781 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 6782 | [RES_IN ] = "IN" , |
| 6783 | # endif |
| 6784 | # if ENABLE_HUSH_CASE |
| 6785 | [RES_CASE ] = "CASE" , |
| 6786 | [RES_CASE_IN ] = "CASE_IN" , |
| 6787 | [RES_MATCH] = "MATCH", |
| 6788 | [RES_CASE_BODY] = "CASE_BODY", |
| 6789 | [RES_ESAC ] = "ESAC" , |
| 6790 | # endif |
| 6791 | [RES_XXXX ] = "XXXX" , |
| 6792 | [RES_SNTX ] = "SNTX" , |
| 6793 | }; |
| 6794 | static const char *const CMDTYPE[] = { |
| 6795 | "{}", |
| 6796 | "()", |
| 6797 | "[noglob]", |
| 6798 | # if ENABLE_HUSH_FUNCTIONS |
| 6799 | "func()", |
| 6800 | # endif |
| 6801 | }; |
| 6802 | |
| 6803 | int pin, prn; |
| 6804 | |
| 6805 | pin = 0; |
| 6806 | while (pi) { |
| 6807 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 6808 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 6809 | prn = 0; |
| 6810 | while (prn < pi->num_cmds) { |
| 6811 | struct command *command = &pi->cmds[prn]; |
| 6812 | char **argv = command->argv; |
| 6813 | |
| 6814 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
| 6815 | lvl*2, "", prn, |
| 6816 | command->assignment_cnt); |
| 6817 | if (command->group) { |
| 6818 | fprintf(stderr, " group %s: (argv=%p)%s%s\n", |
| 6819 | CMDTYPE[command->cmd_type], |
| 6820 | argv |
| 6821 | # if !BB_MMU |
| 6822 | , " group_as_string:", command->group_as_string |
| 6823 | # else |
| 6824 | , "", "" |
| 6825 | # endif |
| 6826 | ); |
| 6827 | debug_print_tree(command->group, lvl+1); |
| 6828 | prn++; |
| 6829 | continue; |
| 6830 | } |
| 6831 | if (argv) while (*argv) { |
| 6832 | fprintf(stderr, " '%s'", *argv); |
| 6833 | argv++; |
| 6834 | } |
| 6835 | fprintf(stderr, "\n"); |
| 6836 | prn++; |
| 6837 | } |
| 6838 | pi = pi->next; |
| 6839 | pin++; |
| 6840 | } |
| 6841 | } |
| 6842 | #endif /* debug_print_tree */ |
| 6843 | |
| 6844 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 6845 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 6846 | static int run_list(struct pipe *pi) |
| 6847 | { |
| 6848 | #if ENABLE_HUSH_CASE |
| 6849 | char *case_word = NULL; |
| 6850 | #endif |
| 6851 | #if ENABLE_HUSH_LOOPS |
| 6852 | struct pipe *loop_top = NULL; |
| 6853 | char **for_lcur = NULL; |
| 6854 | char **for_list = NULL; |
| 6855 | #endif |
| 6856 | smallint last_followup; |
| 6857 | smalluint rcode; |
| 6858 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 6859 | smalluint cond_code = 0; |
| 6860 | #else |
| 6861 | enum { cond_code = 0 }; |
| 6862 | #endif |
| 6863 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 6864 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6865 | smallint last_rword; /* ditto */ |
| 6866 | #endif |
| 6867 | |
| 6868 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 6869 | debug_enter(); |
| 6870 | |
| 6871 | #if ENABLE_HUSH_LOOPS |
| 6872 | /* Check syntax for "for" */ |
| 6873 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 6874 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 6875 | continue; |
| 6876 | /* current word is FOR or IN (BOLD in comments below) */ |
| 6877 | if (cpipe->next == NULL) { |
| 6878 | syntax_error("malformed for"); |
| 6879 | debug_leave(); |
| 6880 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 6881 | return 1; |
| 6882 | } |
| 6883 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 6884 | if (cpipe->next->res_word == RES_DO) |
| 6885 | continue; |
| 6886 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 6887 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 6888 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 6889 | ) { |
| 6890 | syntax_error("malformed for"); |
| 6891 | debug_leave(); |
| 6892 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 6893 | return 1; |
| 6894 | } |
| 6895 | } |
| 6896 | #endif |
| 6897 | |
| 6898 | /* Past this point, all code paths should jump to ret: label |
| 6899 | * in order to return, no direct "return" statements please. |
| 6900 | * This helps to ensure that no memory is leaked. */ |
| 6901 | |
| 6902 | #if ENABLE_HUSH_JOB |
| 6903 | G.run_list_level++; |
| 6904 | #endif |
| 6905 | |
| 6906 | #if HAS_KEYWORDS |
| 6907 | rword = RES_NONE; |
| 6908 | last_rword = RES_XXXX; |
| 6909 | #endif |
| 6910 | last_followup = PIPE_SEQ; |
| 6911 | rcode = G.last_exitcode; |
| 6912 | |
| 6913 | /* Go through list of pipes, (maybe) executing them. */ |
| 6914 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
| 6915 | if (G.flag_SIGINT) |
| 6916 | break; |
| 6917 | |
| 6918 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 6919 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 6920 | rword, cond_code, last_rword); |
| 6921 | #if ENABLE_HUSH_LOOPS |
| 6922 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 6923 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 6924 | ) { |
| 6925 | /* start of a loop: remember where loop starts */ |
| 6926 | loop_top = pi; |
| 6927 | G.depth_of_loop++; |
| 6928 | } |
| 6929 | #endif |
| 6930 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 6931 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 6932 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 6933 | || (rcode != 0 && last_followup == PIPE_AND) |
| 6934 | ) { |
| 6935 | /* It is "<true> || CMD" or "<false> && CMD" |
| 6936 | * and we should not execute CMD */ |
| 6937 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 6938 | last_followup = pi->followup; |
| 6939 | continue; |
| 6940 | } |
| 6941 | } |
| 6942 | last_followup = pi->followup; |
| 6943 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 6944 | #if ENABLE_HUSH_IF |
| 6945 | if (cond_code) { |
| 6946 | if (rword == RES_THEN) { |
| 6947 | /* if false; then ... fi has exitcode 0! */ |
| 6948 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 6949 | /* "if <false> THEN cmd": skip cmd */ |
| 6950 | continue; |
| 6951 | } |
| 6952 | } else { |
| 6953 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 6954 | /* "if <true> then ... ELSE/ELIF cmd": |
| 6955 | * skip cmd and all following ones */ |
| 6956 | break; |
| 6957 | } |
| 6958 | } |
| 6959 | #endif |
| 6960 | #if ENABLE_HUSH_LOOPS |
| 6961 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 6962 | if (!for_lcur) { |
| 6963 | /* first loop through for */ |
| 6964 | |
| 6965 | static const char encoded_dollar_at[] ALIGN1 = { |
| 6966 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 6967 | }; /* encoded representation of "$@" */ |
| 6968 | static const char *const encoded_dollar_at_argv[] = { |
| 6969 | encoded_dollar_at, NULL |
| 6970 | }; /* argv list with one element: "$@" */ |
| 6971 | char **vals; |
| 6972 | |
| 6973 | vals = (char**)encoded_dollar_at_argv; |
| 6974 | if (pi->next->res_word == RES_IN) { |
| 6975 | /* if no variable values after "in" we skip "for" */ |
| 6976 | if (!pi->next->cmds[0].argv) { |
| 6977 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 6978 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 6979 | break; |
| 6980 | } |
| 6981 | vals = pi->next->cmds[0].argv; |
| 6982 | } /* else: "for var; do..." -> assume "$@" list */ |
| 6983 | /* create list of variable values */ |
| 6984 | debug_print_strings("for_list made from", vals); |
| 6985 | for_list = expand_strvec_to_strvec(vals); |
| 6986 | for_lcur = for_list; |
| 6987 | debug_print_strings("for_list", for_list); |
| 6988 | } |
| 6989 | if (!*for_lcur) { |
| 6990 | /* "for" loop is over, clean up */ |
| 6991 | free(for_list); |
| 6992 | for_list = NULL; |
| 6993 | for_lcur = NULL; |
| 6994 | break; |
| 6995 | } |
| 6996 | /* Insert next value from for_lcur */ |
| 6997 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 6998 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6999 | continue; |
| 7000 | } |
| 7001 | if (rword == RES_IN) { |
| 7002 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7003 | } |
| 7004 | if (rword == RES_DONE) { |
| 7005 | continue; /* "done" has no cmds too */ |
| 7006 | } |
| 7007 | #endif |
| 7008 | #if ENABLE_HUSH_CASE |
| 7009 | if (rword == RES_CASE) { |
| 7010 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7011 | continue; |
| 7012 | } |
| 7013 | if (rword == RES_MATCH) { |
| 7014 | char **argv; |
| 7015 | |
| 7016 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7017 | break; |
| 7018 | /* all prev words didn't match, does this one match? */ |
| 7019 | argv = pi->cmds->argv; |
| 7020 | while (*argv) { |
| 7021 | char *pattern = expand_string_to_string(*argv); |
| 7022 | /* TODO: which FNM_xxx flags to use? */ |
| 7023 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7024 | free(pattern); |
| 7025 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 7026 | free(case_word); /* make future "word)" stop */ |
| 7027 | case_word = NULL; |
| 7028 | break; |
| 7029 | } |
| 7030 | argv++; |
| 7031 | } |
| 7032 | continue; |
| 7033 | } |
| 7034 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
| 7035 | if (cond_code != 0) |
| 7036 | continue; /* not matched yet, skip this pipe */ |
| 7037 | } |
| 7038 | #endif |
| 7039 | /* Just pressing <enter> in shell should check for jobs. |
| 7040 | * OTOH, in non-interactive shell this is useless |
| 7041 | * and only leads to extra job checks */ |
| 7042 | if (pi->num_cmds == 0) { |
| 7043 | if (G_interactive_fd) |
| 7044 | goto check_jobs_and_continue; |
| 7045 | continue; |
| 7046 | } |
| 7047 | |
| 7048 | /* After analyzing all keywords and conditions, we decided |
| 7049 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7050 | * after run_pipe to collect any background children, |
| 7051 | * even if list execution is to be stopped. */ |
| 7052 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
| 7053 | { |
| 7054 | int r; |
| 7055 | #if ENABLE_HUSH_LOOPS |
| 7056 | G.flag_break_continue = 0; |
| 7057 | #endif |
| 7058 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 7059 | if (r != -1) { |
| 7060 | /* We ran a builtin, function, or group. |
| 7061 | * rcode is already known |
| 7062 | * and we don't need to wait for anything. */ |
| 7063 | G.last_exitcode = rcode; |
| 7064 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 7065 | check_and_run_traps(0); |
| 7066 | #if ENABLE_HUSH_LOOPS |
| 7067 | /* Was it "break" or "continue"? */ |
| 7068 | if (G.flag_break_continue) { |
| 7069 | smallint fbc = G.flag_break_continue; |
| 7070 | /* We might fall into outer *loop*, |
| 7071 | * don't want to break it too */ |
| 7072 | if (loop_top) { |
| 7073 | G.depth_break_continue--; |
| 7074 | if (G.depth_break_continue == 0) |
| 7075 | G.flag_break_continue = 0; |
| 7076 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7077 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 7078 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
| 7079 | goto check_jobs_and_break; |
| 7080 | /* "continue": simulate end of loop */ |
| 7081 | rword = RES_DONE; |
| 7082 | continue; |
| 7083 | } |
| 7084 | #endif |
| 7085 | #if ENABLE_HUSH_FUNCTIONS |
| 7086 | if (G.flag_return_in_progress == 1) { |
| 7087 | /* same as "goto check_jobs_and_break" */ |
| 7088 | checkjobs(NULL); |
| 7089 | break; |
| 7090 | } |
| 7091 | #endif |
| 7092 | } else if (pi->followup == PIPE_BG) { |
| 7093 | /* What does bash do with attempts to background builtins? */ |
| 7094 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7095 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7096 | * I'm NOT treating inner &'s as jobs */ |
| 7097 | check_and_run_traps(0); |
| 7098 | #if ENABLE_HUSH_JOB |
| 7099 | if (G.run_list_level == 1) |
| 7100 | insert_bg_job(pi); |
| 7101 | #endif |
| 7102 | /* Last command's pid goes to $! */ |
| 7103 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 7104 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7105 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 7106 | } else { |
| 7107 | #if ENABLE_HUSH_JOB |
| 7108 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7109 | /* Waits for completion, then fg's main shell */ |
| 7110 | rcode = checkjobs_and_fg_shell(pi); |
| 7111 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
| 7112 | check_and_run_traps(0); |
| 7113 | } else |
| 7114 | #endif |
| 7115 | { /* This one just waits for completion */ |
| 7116 | rcode = checkjobs(pi); |
| 7117 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 7118 | check_and_run_traps(0); |
| 7119 | } |
| 7120 | G.last_exitcode = rcode; |
| 7121 | } |
| 7122 | } |
| 7123 | |
| 7124 | /* Analyze how result affects subsequent commands */ |
| 7125 | #if ENABLE_HUSH_IF |
| 7126 | if (rword == RES_IF || rword == RES_ELIF) |
| 7127 | cond_code = rcode; |
| 7128 | #endif |
| 7129 | #if ENABLE_HUSH_LOOPS |
| 7130 | /* Beware of "while false; true; do ..."! */ |
| 7131 | if (pi->next && pi->next->res_word == RES_DO) { |
| 7132 | if (rword == RES_WHILE) { |
| 7133 | if (rcode) { |
| 7134 | /* "while false; do...done" - exitcode 0 */ |
| 7135 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7136 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 7137 | goto check_jobs_and_break; |
| 7138 | } |
| 7139 | } |
| 7140 | if (rword == RES_UNTIL) { |
| 7141 | if (!rcode) { |
| 7142 | debug_printf_exec(": until expr is true: breaking\n"); |
| 7143 | check_jobs_and_break: |
| 7144 | checkjobs(NULL); |
| 7145 | break; |
| 7146 | } |
| 7147 | } |
| 7148 | } |
| 7149 | #endif |
| 7150 | |
| 7151 | check_jobs_and_continue: |
| 7152 | checkjobs(NULL); |
| 7153 | } /* for (pi) */ |
| 7154 | |
| 7155 | #if ENABLE_HUSH_JOB |
| 7156 | G.run_list_level--; |
| 7157 | #endif |
| 7158 | #if ENABLE_HUSH_LOOPS |
| 7159 | if (loop_top) |
| 7160 | G.depth_of_loop--; |
| 7161 | free(for_list); |
| 7162 | #endif |
| 7163 | #if ENABLE_HUSH_CASE |
| 7164 | free(case_word); |
| 7165 | #endif |
| 7166 | debug_leave(); |
| 7167 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 7168 | return rcode; |
| 7169 | } |
| 7170 | |
| 7171 | /* Select which version we will use */ |
| 7172 | static int run_and_free_list(struct pipe *pi) |
| 7173 | { |
| 7174 | int rcode = 0; |
| 7175 | debug_printf_exec("run_and_free_list entered\n"); |
| 7176 | if (!G.n_mode) { |
| 7177 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 7178 | rcode = run_list(pi); |
| 7179 | } |
| 7180 | /* free_pipe_list has the side effect of clearing memory. |
| 7181 | * In the long run that function can be merged with run_list, |
| 7182 | * but doing that now would hobble the debugging effort. */ |
| 7183 | free_pipe_list(pi); |
| 7184 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 7185 | return rcode; |
| 7186 | } |
| 7187 | |
| 7188 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7189 | /* Called a few times only (or even once if "sh -c") */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7190 | static void init_sigmasks(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7191 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7192 | unsigned sig; |
| 7193 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7194 | sigset_t old_blocked_set; |
| 7195 | |
| 7196 | if (!G.inherited_set_is_saved) { |
| 7197 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 7198 | G.inherited_set = G.blocked_set; |
| 7199 | } |
| 7200 | old_blocked_set = G.blocked_set; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7201 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7202 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7203 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 7204 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7205 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7206 | mask |= SPECIAL_JOB_SIGS; |
| 7207 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7208 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7209 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7210 | sig = 0; |
| 7211 | while (mask) { |
| 7212 | if (mask & 1) |
| 7213 | sigaddset(&G.blocked_set, sig); |
| 7214 | mask >>= 1; |
| 7215 | sig++; |
| 7216 | } |
| 7217 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7218 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7219 | if (memcmp(&old_blocked_set, &G.blocked_set, sizeof(old_blocked_set)) != 0) |
| 7220 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7221 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7222 | /* POSIX allows shell to re-enable SIGCHLD |
| 7223 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7224 | #if ENABLE_HUSH_FAST |
| 7225 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7226 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7227 | signal(SIGCHLD, SIGCHLD_handler); |
| 7228 | #else |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7229 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7230 | signal(SIGCHLD, SIG_DFL); |
| 7231 | #endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7232 | |
| 7233 | G.inherited_set_is_saved = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | #if ENABLE_HUSH_JOB |
| 7237 | /* helper */ |
| 7238 | static void maybe_set_to_sigexit(int sig) |
| 7239 | { |
| 7240 | void (*handler)(int); |
| 7241 | /* non_DFL_mask'ed signals are, well, masked, |
| 7242 | * no need to set handler for them. |
| 7243 | */ |
| 7244 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 7245 | handler = signal(sig, sigexit); |
| 7246 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 7247 | signal(sig, handler); |
| 7248 | } |
| 7249 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7250 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7251 | static void set_fatal_handlers(void) |
| 7252 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 7253 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7254 | if (HUSH_DEBUG) { |
| 7255 | maybe_set_to_sigexit(SIGILL ); |
| 7256 | maybe_set_to_sigexit(SIGFPE ); |
| 7257 | maybe_set_to_sigexit(SIGBUS ); |
| 7258 | maybe_set_to_sigexit(SIGSEGV); |
| 7259 | maybe_set_to_sigexit(SIGTRAP); |
| 7260 | } /* else: hush is perfect. what SEGV? */ |
| 7261 | maybe_set_to_sigexit(SIGABRT); |
| 7262 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7263 | maybe_set_to_sigexit(SIGPIPE); |
| 7264 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7265 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7266 | * if we aren't interactive... but in this case |
| 7267 | * 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] | 7268 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7269 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 7270 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 7271 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7272 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7273 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7274 | static int set_mode(const char cstate, const char mode) |
| 7275 | { |
| 7276 | int state = (cstate == '-' ? 1 : 0); |
| 7277 | switch (mode) { |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 7278 | case 'n': G.n_mode = state; break; |
| 7279 | case 'x': IF_HUSH_MODE_X(G_x_mode = state;) break; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7280 | default: return EXIT_FAILURE; |
| 7281 | } |
| 7282 | return EXIT_SUCCESS; |
| 7283 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7284 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7285 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7286 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7287 | { |
| 7288 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7289 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7290 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7291 | struct variable *cur_var; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7292 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7293 | INIT_G(); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 7294 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7295 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7296 | #if !BB_MMU |
| 7297 | G.argv0_for_re_execing = argv[0]; |
| 7298 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7299 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 7300 | G.shell_ver.flg_export = 1; |
| 7301 | G.shell_ver.flg_read_only = 1; |
| 7302 | /* Code which handles ${var/P/R} needs writable values for all variables, |
| 7303 | * therefore we xstrdup: */ |
| 7304 | G.shell_ver.varstr = xstrdup(hush_version_str), |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7305 | G.top_var = &G.shell_ver; |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7306 | /* Create shell local variables from the values |
| 7307 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7308 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7309 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7310 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7311 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7312 | if (e) while (*e) { |
| 7313 | char *value = strchr(*e, '='); |
| 7314 | if (value) { /* paranoia */ |
| 7315 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7316 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7317 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7318 | cur_var->max_len = strlen(*e); |
| 7319 | cur_var->flg_export = 1; |
| 7320 | } |
| 7321 | e++; |
| 7322 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7323 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
| 7324 | debug_printf_env("putenv '%s'\n", G.shell_ver.varstr); |
| 7325 | putenv(G.shell_ver.varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7326 | |
| 7327 | /* Export PWD */ |
| 7328 | set_pwd_var(/*exp:*/ 1); |
| 7329 | /* bash also exports SHLVL and _, |
| 7330 | * and sets (but doesn't export) the following variables: |
| 7331 | * BASH=/bin/bash |
| 7332 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7333 | * BASH_VERSION='3.2.0(1)-release' |
| 7334 | * HOSTTYPE=i386 |
| 7335 | * MACHTYPE=i386-pc-linux-gnu |
| 7336 | * OSTYPE=linux-gnu |
| 7337 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7338 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7339 | * EUID=<NNNNN> |
| 7340 | * UID=<NNNNN> |
| 7341 | * GROUPS=() |
| 7342 | * LINES=<NNN> |
| 7343 | * COLUMNS=<NNN> |
| 7344 | * BASH_ARGC=() |
| 7345 | * BASH_ARGV=() |
| 7346 | * BASH_LINENO=() |
| 7347 | * BASH_SOURCE=() |
| 7348 | * DIRSTACK=() |
| 7349 | * PIPESTATUS=([0]="0") |
| 7350 | * HISTFILE=/<xxx>/.bash_history |
| 7351 | * HISTFILESIZE=500 |
| 7352 | * HISTSIZE=500 |
| 7353 | * MAILCHECK=60 |
| 7354 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7355 | * SHELL=/bin/bash |
| 7356 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7357 | * TERM=dumb |
| 7358 | * OPTERR=1 |
| 7359 | * OPTIND=1 |
| 7360 | * IFS=$' \t\n' |
| 7361 | * PS1='\s-\v\$ ' |
| 7362 | * PS2='> ' |
| 7363 | * PS4='+ ' |
| 7364 | */ |
| 7365 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7366 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7367 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7368 | #endif |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7369 | G.global_argc = argc; |
| 7370 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7371 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7372 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7373 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7374 | if (setjmp(die_jmp)) { |
| 7375 | /* xfunc has failed! die die die */ |
| 7376 | /* no EXIT traps, this is an escape hatch! */ |
| 7377 | G.exiting = 1; |
| 7378 | hush_exit(xfunc_error_retval); |
| 7379 | } |
| 7380 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7381 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7382 | * init_sigmasks() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7383 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7384 | * If we later decide that we are interactive, we run init_sigmasks() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7385 | * in order to intercept (more) signals. |
| 7386 | */ |
| 7387 | |
| 7388 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7389 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7390 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7391 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 7392 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7393 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7394 | "<:$:R:V:" |
| 7395 | # if ENABLE_HUSH_FUNCTIONS |
| 7396 | "F:" |
| 7397 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7398 | #endif |
| 7399 | ); |
| 7400 | if (opt <= 0) |
| 7401 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7402 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7403 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7404 | /* Possibilities: |
| 7405 | * sh ... -c 'script' |
| 7406 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7407 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7408 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7409 | * "" needs to be replaced with NULL |
| 7410 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7411 | * Note: the form without ARG0 never happens: |
| 7412 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7413 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7414 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7415 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7416 | G.root_ppid = getppid(); |
| 7417 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7418 | G.global_argv = argv + optind; |
| 7419 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7420 | if (builtin_argc) { |
| 7421 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7422 | const struct built_in_command *x; |
| 7423 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7424 | init_sigmasks(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7425 | x = find_builtin(optarg); |
| 7426 | if (x) { /* paranoia */ |
| 7427 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7428 | G.global_argv += builtin_argc; |
| 7429 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7430 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7431 | } |
| 7432 | goto final_return; |
| 7433 | } |
| 7434 | if (!G.global_argv[0]) { |
| 7435 | /* -c 'script' (no params): prevent empty $0 */ |
| 7436 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7437 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7438 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7439 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7440 | init_sigmasks(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7441 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7442 | goto final_return; |
| 7443 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7444 | /* Well, we cannot just declare interactiveness, |
| 7445 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7446 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7447 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7448 | case 's': |
| 7449 | /* "-s" means "read from stdin", but this is how we always |
| 7450 | * operate, so simply do nothing here. */ |
| 7451 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7452 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7453 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7454 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7455 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7456 | case '$': { |
| 7457 | unsigned long long empty_trap_mask; |
| 7458 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7459 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7460 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7461 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7462 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7463 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7464 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7465 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7466 | optarg++; |
| 7467 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7468 | optarg++; |
| 7469 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7470 | if (empty_trap_mask != 0) { |
| 7471 | int sig; |
| 7472 | init_sigmasks(); |
| 7473 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7474 | for (sig = 1; sig < NSIG; sig++) { |
| 7475 | if (empty_trap_mask & (1LL << sig)) { |
| 7476 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7477 | sigaddset(&G.blocked_set, sig); |
| 7478 | } |
| 7479 | } |
| 7480 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7481 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7482 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7483 | optarg++; |
| 7484 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7485 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7486 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7487 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7488 | case 'R': |
| 7489 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7490 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7491 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7492 | # if ENABLE_HUSH_FUNCTIONS |
| 7493 | case 'F': { |
| 7494 | struct function *funcp = new_function(optarg); |
| 7495 | /* funcp->name is already set to optarg */ |
| 7496 | /* funcp->body is set to NULL. It's a special case. */ |
| 7497 | funcp->body_as_string = argv[optind]; |
| 7498 | optind++; |
| 7499 | break; |
| 7500 | } |
| 7501 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7502 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7503 | case 'n': |
| 7504 | case 'x': |
Denys Vlasenko | 889550b | 2010-07-14 19:01:25 +0200 | [diff] [blame] | 7505 | if (set_mode('-', opt) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7506 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7507 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7508 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7509 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7510 | " or: sh -c command [args]...\n\n"); |
| 7511 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7512 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7513 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7514 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7515 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7516 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7517 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7518 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7519 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7520 | G.root_ppid = getppid(); |
| 7521 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7522 | |
| 7523 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7524 | if (argv[0] && argv[0][0] == '-') { |
| 7525 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7526 | debug_printf("sourcing /etc/profile\n"); |
| 7527 | input = fopen_for_read("/etc/profile"); |
| 7528 | if (input != NULL) { |
| 7529 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7530 | init_sigmasks(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7531 | parse_and_run_file(input); |
| 7532 | fclose(input); |
| 7533 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7534 | /* bash: after sourcing /etc/profile, |
| 7535 | * tries to source (in the given order): |
| 7536 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7537 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7538 | * bash also sources ~/.bash_logout on exit. |
| 7539 | * If called as sh, skips .bash_XXX files. |
| 7540 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7543 | if (argv[optind]) { |
| 7544 | FILE *input; |
| 7545 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7546 | * "bash <script>" (which is never interactive (unless -i?)) |
| 7547 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7548 | * If called as sh, does the same but with $ENV. |
| 7549 | */ |
| 7550 | debug_printf("running script '%s'\n", argv[optind]); |
| 7551 | G.global_argv = argv + optind; |
| 7552 | G.global_argc = argc - optind; |
| 7553 | input = xfopen_for_read(argv[optind]); |
| 7554 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7555 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7556 | parse_and_run_file(input); |
| 7557 | #if ENABLE_FEATURE_CLEAN_UP |
| 7558 | fclose(input); |
| 7559 | #endif |
| 7560 | goto final_return; |
| 7561 | } |
| 7562 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7563 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7564 | * NB: don't forget to (re)run init_sigmasks() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7565 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7566 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7567 | /* A shell is interactive if the '-i' flag was given, |
| 7568 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 7569 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7570 | * no arguments remaining or the -s flag given |
| 7571 | * standard input is a terminal |
| 7572 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7573 | * Refer to Posix.2, the description of the 'sh' utility. |
| 7574 | */ |
| 7575 | #if ENABLE_HUSH_JOB |
| 7576 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7577 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 7578 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 7579 | if (G_saved_tty_pgrp < 0) |
| 7580 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7581 | |
| 7582 | /* try to dup stdin to high fd#, >= 255 */ |
| 7583 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7584 | if (G_interactive_fd < 0) { |
| 7585 | /* try to dup to any fd */ |
| 7586 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7587 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7588 | /* give up */ |
| 7589 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7590 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7591 | } |
| 7592 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7593 | // TODO: track & disallow any attempts of user |
| 7594 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7595 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7596 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7597 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7598 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7599 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7600 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7601 | /* If we were run as 'hush &', sleep until we are |
| 7602 | * in the foreground (tty pgrp == our pgrp). |
| 7603 | * If we get started under a job aware app (like bash), |
| 7604 | * make sure we are now in charge so we don't fight over |
| 7605 | * who gets the foreground */ |
| 7606 | while (1) { |
| 7607 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7608 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 7609 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7610 | break; |
| 7611 | /* send TTIN to ourself (should stop us) */ |
| 7612 | kill(- shell_pgrp, SIGTTIN); |
| 7613 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7614 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7615 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7616 | /* Block some signals */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7617 | init_sigmasks(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7618 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7619 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7620 | /* Set other signals to restore saved_tty_pgrp */ |
| 7621 | set_fatal_handlers(); |
| 7622 | /* Put ourselves in our own process group |
| 7623 | * (bash, too, does this only if ctty is available) */ |
| 7624 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 7625 | /* Grab control of the terminal */ |
| 7626 | tcsetpgrp(G_interactive_fd, getpid()); |
| 7627 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 7628 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 7629 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 7630 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7631 | } else { |
| 7632 | init_sigmasks(); |
| 7633 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7634 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7635 | /* No job control compiled in, only prompt/line editing */ |
| 7636 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7637 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7638 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7639 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7640 | G_interactive_fd = dup(STDIN_FILENO); |
| 7641 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7642 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7643 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7644 | } |
| 7645 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7646 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7647 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7648 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7649 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7650 | #else |
| 7651 | /* We have interactiveness code disabled */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7652 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7653 | #endif |
| 7654 | /* bash: |
| 7655 | * if interactive but not a login shell, sources ~/.bashrc |
| 7656 | * (--norc turns this off, --rcfile <file> overrides) |
| 7657 | */ |
| 7658 | |
| 7659 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7660 | /* note: ash and hush share this string */ |
| 7661 | printf("\n\n%s %s\n" |
| 7662 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7663 | "\n", |
| 7664 | bb_banner, |
| 7665 | "hush - the humble shell" |
| 7666 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7667 | } |
| 7668 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7669 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7670 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7671 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7672 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7673 | if (G.cwd != bb_msg_unknown) |
| 7674 | free((char*)G.cwd); |
| 7675 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7676 | while (cur_var) { |
| 7677 | struct variable *tmp = cur_var; |
| 7678 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7679 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7680 | cur_var = cur_var->next; |
| 7681 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 7682 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7683 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7684 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7685 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7686 | |
| 7687 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7688 | #if ENABLE_MSH |
| 7689 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7690 | int msh_main(int argc, char **argv) |
| 7691 | { |
| 7692 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7693 | return hush_main(argc, argv); |
| 7694 | } |
| 7695 | #endif |
| 7696 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7697 | |
| 7698 | /* |
| 7699 | * Built-ins |
| 7700 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7701 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7702 | { |
| 7703 | return 0; |
| 7704 | } |
| 7705 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7706 | 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] | 7707 | { |
| 7708 | int argc = 0; |
| 7709 | while (*argv) { |
| 7710 | argc++; |
| 7711 | argv++; |
| 7712 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7713 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7714 | } |
| 7715 | |
| 7716 | static int FAST_FUNC builtin_test(char **argv) |
| 7717 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7718 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7719 | } |
| 7720 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7721 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7722 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7723 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7724 | } |
| 7725 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7726 | #if ENABLE_PRINTF |
| 7727 | static int FAST_FUNC builtin_printf(char **argv) |
| 7728 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7729 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7730 | } |
| 7731 | #endif |
| 7732 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7733 | static char **skip_dash_dash(char **argv) |
| 7734 | { |
| 7735 | argv++; |
| 7736 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 7737 | argv++; |
| 7738 | return argv; |
| 7739 | } |
| 7740 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7741 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7742 | { |
| 7743 | int rcode = EXIT_SUCCESS; |
| 7744 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7745 | argv = skip_dash_dash(argv); |
| 7746 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7747 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7748 | /* bash: |
| 7749 | * eval "echo Hi; done" ("done" is syntax error): |
| 7750 | * "echo Hi" will not execute too. |
| 7751 | */ |
| 7752 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7753 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7754 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7755 | } |
| 7756 | return rcode; |
| 7757 | } |
| 7758 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7759 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7760 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7761 | const char *newdir; |
| 7762 | |
| 7763 | argv = skip_dash_dash(argv); |
| 7764 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7765 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7766 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7767 | * bash says "bash: cd: HOME not set" and does nothing |
| 7768 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7769 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7770 | const char *home = get_local_var_value("HOME"); |
| 7771 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7772 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7773 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7774 | /* Mimic bash message exactly */ |
| 7775 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7776 | return EXIT_FAILURE; |
| 7777 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7778 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7779 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7780 | * set it again, but do not export. bash does the same. |
| 7781 | */ |
| 7782 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7783 | return EXIT_SUCCESS; |
| 7784 | } |
| 7785 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7786 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7787 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7788 | argv = skip_dash_dash(argv); |
| 7789 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7790 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7791 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7792 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7793 | * tty pgrp then, only top-level shell process does that */ |
| 7794 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7795 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7796 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7797 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 7798 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 7799 | * and tcsetpgrp, and this is inherently racy. |
| 7800 | */ |
| 7801 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7802 | } |
| 7803 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7804 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7805 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 7806 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7807 | |
| 7808 | /* interactive bash: |
| 7809 | * # trap "echo EEE" EXIT |
| 7810 | * # exit |
| 7811 | * exit |
| 7812 | * There are stopped jobs. |
| 7813 | * (if there are _stopped_ jobs, running ones don't count) |
| 7814 | * # exit |
| 7815 | * exit |
| 7816 | # EEE (then bash exits) |
| 7817 | * |
| 7818 | * we can use G.exiting = -1 as indicator "last cmd was exit" |
| 7819 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 7820 | |
| 7821 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7822 | argv = skip_dash_dash(argv); |
| 7823 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7824 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7825 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 7826 | xfunc_error_retval = 255; |
| 7827 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7828 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7829 | } |
| 7830 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7831 | static void print_escaped(const char *s) |
| 7832 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7833 | if (*s == '\'') |
| 7834 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7835 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7836 | const char *p = strchrnul(s, '\''); |
| 7837 | /* print 'xxxx', possibly just '' */ |
| 7838 | printf("'%.*s'", (int)(p - s), s); |
| 7839 | if (*p == '\0') |
| 7840 | break; |
| 7841 | s = p; |
| 7842 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7843 | /* s points to '; print "'''...'''" */ |
| 7844 | putchar('"'); |
| 7845 | do putchar('\''); while (*++s == '\''); |
| 7846 | putchar('"'); |
| 7847 | } while (*s); |
| 7848 | } |
| 7849 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7850 | #if !ENABLE_HUSH_LOCAL |
| 7851 | #define helper_export_local(argv, exp, lvl) \ |
| 7852 | helper_export_local(argv, exp) |
| 7853 | #endif |
| 7854 | static void helper_export_local(char **argv, int exp, int lvl) |
| 7855 | { |
| 7856 | do { |
| 7857 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 7858 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7859 | |
| 7860 | /* So far we do not check that name is valid (TODO?) */ |
| 7861 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 7862 | if (*name_end == '\0') { |
| 7863 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7864 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 7865 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 7866 | var = vpp ? *vpp : NULL; |
| 7867 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7868 | if (exp == -1) { /* unexporting? */ |
| 7869 | /* export -n NAME (without =VALUE) */ |
| 7870 | if (var) { |
| 7871 | var->flg_export = 0; |
| 7872 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 7873 | unsetenv(name); |
| 7874 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 7875 | continue; |
| 7876 | } |
| 7877 | if (exp == 1) { /* exporting? */ |
| 7878 | /* export NAME (without =VALUE) */ |
| 7879 | if (var) { |
| 7880 | var->flg_export = 1; |
| 7881 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 7882 | putenv(var->varstr); |
| 7883 | continue; |
| 7884 | } |
| 7885 | } |
| 7886 | /* Exporting non-existing variable. |
| 7887 | * bash does not put it in environment, |
| 7888 | * but remembers that it is exported, |
| 7889 | * and does put it in env when it is set later. |
| 7890 | * We just set it to "" and export. */ |
| 7891 | /* Or, it's "local NAME" (without =VALUE). |
| 7892 | * bash sets the value to "". */ |
| 7893 | name = xasprintf("%s=", name); |
| 7894 | } else { |
| 7895 | /* (Un)exporting/making local NAME=VALUE */ |
| 7896 | name = xstrdup(name); |
| 7897 | } |
| 7898 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 7899 | } while (*++argv); |
| 7900 | } |
| 7901 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7902 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7903 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 7904 | unsigned opt_unexport; |
| 7905 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 7906 | #if ENABLE_HUSH_EXPORT_N |
| 7907 | /* "!": do not abort on errors */ |
| 7908 | opt_unexport = getopt32(argv, "!n"); |
| 7909 | if (opt_unexport == (uint32_t)-1) |
| 7910 | return EXIT_FAILURE; |
| 7911 | argv += optind; |
| 7912 | #else |
| 7913 | opt_unexport = 0; |
| 7914 | argv++; |
| 7915 | #endif |
| 7916 | |
| 7917 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7918 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7919 | if (e) { |
| 7920 | while (*e) { |
| 7921 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7922 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7923 | #else |
| 7924 | /* ash emits: export VAR='VAL' |
| 7925 | * bash: declare -x VAR="VAL" |
| 7926 | * we follow ash example */ |
| 7927 | const char *s = *e++; |
| 7928 | const char *p = strchr(s, '='); |
| 7929 | |
| 7930 | if (!p) /* wtf? take next variable */ |
| 7931 | continue; |
| 7932 | /* export var= */ |
| 7933 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7934 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7935 | putchar('\n'); |
| 7936 | #endif |
| 7937 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7938 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7939 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7940 | return EXIT_SUCCESS; |
| 7941 | } |
| 7942 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7943 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7944 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7945 | return EXIT_SUCCESS; |
| 7946 | } |
| 7947 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7948 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7949 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7950 | { |
| 7951 | if (G.func_nest_level == 0) { |
| 7952 | bb_error_msg("%s: not in a function", argv[0]); |
| 7953 | return EXIT_FAILURE; /* bash compat */ |
| 7954 | } |
| 7955 | helper_export_local(argv, 0, G.func_nest_level); |
| 7956 | return EXIT_SUCCESS; |
| 7957 | } |
| 7958 | #endif |
| 7959 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7960 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7961 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7962 | int sig; |
| 7963 | char *new_cmd; |
| 7964 | |
| 7965 | if (!G.traps) |
| 7966 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7967 | |
| 7968 | argv++; |
| 7969 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7970 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7971 | /* No args: print all trapped */ |
| 7972 | for (i = 0; i < NSIG; ++i) { |
| 7973 | if (G.traps[i]) { |
| 7974 | printf("trap -- "); |
| 7975 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 7976 | /* note: bash adds "SIG", but only if invoked |
| 7977 | * as "bash". If called as "sh", or if set -o posix, |
| 7978 | * then it prints short signal names. |
| 7979 | * We are printing short names: */ |
| 7980 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7981 | } |
| 7982 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 7983 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7984 | return EXIT_SUCCESS; |
| 7985 | } |
| 7986 | |
| 7987 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 7988 | /* If first arg is a number: reset all specified signals */ |
| 7989 | sig = bb_strtou(*argv, NULL, 10); |
| 7990 | if (errno == 0) { |
| 7991 | int ret; |
| 7992 | process_sig_list: |
| 7993 | ret = EXIT_SUCCESS; |
| 7994 | while (*argv) { |
| 7995 | sig = get_signum(*argv++); |
| 7996 | if (sig < 0 || sig >= NSIG) { |
| 7997 | ret = EXIT_FAILURE; |
| 7998 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 7999 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8000 | continue; |
| 8001 | } |
| 8002 | |
| 8003 | free(G.traps[sig]); |
| 8004 | G.traps[sig] = xstrdup(new_cmd); |
| 8005 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8006 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8007 | get_signame(sig), sig, G.traps[sig]); |
| 8008 | |
| 8009 | /* There is no signal for 0 (EXIT) */ |
| 8010 | if (sig == 0) |
| 8011 | continue; |
| 8012 | |
| 8013 | if (new_cmd) { |
| 8014 | sigaddset(&G.blocked_set, sig); |
| 8015 | } else { |
| 8016 | /* There was a trap handler, we are removing it |
| 8017 | * (if sig has non-DFL handling, |
| 8018 | * we don't need to do anything) */ |
| 8019 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 8020 | continue; |
| 8021 | sigdelset(&G.blocked_set, sig); |
| 8022 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8023 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8024 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8025 | return ret; |
| 8026 | } |
| 8027 | |
| 8028 | if (!argv[1]) { /* no second arg */ |
| 8029 | bb_error_msg("trap: invalid arguments"); |
| 8030 | return EXIT_FAILURE; |
| 8031 | } |
| 8032 | |
| 8033 | /* First arg is "-": reset all specified to default */ |
| 8034 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 8035 | /* Everything else: set arg as signal handler |
| 8036 | * (includes "" case, which ignores signal) */ |
| 8037 | if (argv[0][0] == '-') { |
| 8038 | if (argv[0][1] == '\0') { /* "-" */ |
| 8039 | /* new_cmd remains NULL: "reset these sigs" */ |
| 8040 | goto reset_traps; |
| 8041 | } |
| 8042 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 8043 | argv++; |
| 8044 | } |
| 8045 | /* else: "-something", no special meaning */ |
| 8046 | } |
| 8047 | new_cmd = *argv; |
| 8048 | reset_traps: |
| 8049 | argv++; |
| 8050 | goto process_sig_list; |
| 8051 | } |
| 8052 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8053 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8054 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8055 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8056 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8057 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8058 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8059 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 8060 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8061 | |
| 8062 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8063 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8064 | type = "an alias";*/ |
| 8065 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8066 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8067 | type = "a function"; |
| 8068 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8069 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8070 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8071 | else if ((path = find_in_path(*argv)) != NULL) |
| 8072 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8073 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8074 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8075 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8076 | continue; |
| 8077 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8078 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8079 | printf("%s is %s\n", *argv, type); |
| 8080 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8081 | } |
| 8082 | |
| 8083 | return ret; |
| 8084 | } |
| 8085 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8086 | #if ENABLE_HUSH_JOB |
| 8087 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8088 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8089 | { |
| 8090 | int i, jobnum; |
| 8091 | struct pipe *pi; |
| 8092 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8093 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8094 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8095 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8096 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 8097 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8098 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8099 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8100 | goto found; |
| 8101 | } |
| 8102 | } |
| 8103 | bb_error_msg("%s: no current job", argv[0]); |
| 8104 | return EXIT_FAILURE; |
| 8105 | } |
| 8106 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 8107 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 8108 | return EXIT_FAILURE; |
| 8109 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8110 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8111 | if (pi->jobid == jobnum) { |
| 8112 | goto found; |
| 8113 | } |
| 8114 | } |
| 8115 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 8116 | return EXIT_FAILURE; |
| 8117 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 8118 | /* TODO: bash prints a string representation |
| 8119 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8120 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8121 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8122 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8123 | } |
| 8124 | |
| 8125 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8126 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 8127 | for (i = 0; i < pi->num_cmds; i++) { |
| 8128 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 8129 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8130 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8131 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8132 | |
| 8133 | i = kill(- pi->pgrp, SIGCONT); |
| 8134 | if (i < 0) { |
| 8135 | if (errno == ESRCH) { |
| 8136 | delete_finished_bg_job(pi); |
| 8137 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8138 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8139 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8140 | } |
| 8141 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8142 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8143 | remove_bg_job(pi); |
| 8144 | return checkjobs_and_fg_shell(pi); |
| 8145 | } |
| 8146 | return EXIT_SUCCESS; |
| 8147 | } |
| 8148 | #endif |
| 8149 | |
| 8150 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8151 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8152 | { |
| 8153 | const struct built_in_command *x; |
| 8154 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8155 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8156 | "Built-in commands:\n" |
| 8157 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8158 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8159 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8160 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8161 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8162 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8163 | return EXIT_SUCCESS; |
| 8164 | } |
| 8165 | #endif |
| 8166 | |
| 8167 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8168 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8169 | { |
| 8170 | struct pipe *job; |
| 8171 | const char *status_string; |
| 8172 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8173 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8174 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8175 | status_string = "Stopped"; |
| 8176 | else |
| 8177 | status_string = "Running"; |
| 8178 | |
| 8179 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8180 | } |
| 8181 | return EXIT_SUCCESS; |
| 8182 | } |
| 8183 | #endif |
| 8184 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8185 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8186 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8187 | { |
| 8188 | void *p; |
| 8189 | unsigned long l; |
| 8190 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8191 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8192 | /* Optional. Reduces probability of false positives */ |
| 8193 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8194 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8195 | /* Crude attempt to find where "free memory" starts, |
| 8196 | * sans fragmentation. */ |
| 8197 | p = malloc(240); |
| 8198 | l = (unsigned long)p; |
| 8199 | free(p); |
| 8200 | p = malloc(3400); |
| 8201 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8202 | free(p); |
| 8203 | |
| 8204 | if (!G.memleak_value) |
| 8205 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8206 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8207 | l -= G.memleak_value; |
| 8208 | if ((long)l < 0) |
| 8209 | l = 0; |
| 8210 | l /= 1024; |
| 8211 | if (l > 127) |
| 8212 | l = 127; |
| 8213 | |
| 8214 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8215 | return l; |
| 8216 | } |
| 8217 | #endif |
| 8218 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8219 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8220 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8221 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8222 | return EXIT_SUCCESS; |
| 8223 | } |
| 8224 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8225 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8226 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8227 | const char *r; |
| 8228 | char *opt_n = NULL; |
| 8229 | char *opt_p = NULL; |
| 8230 | char *opt_t = NULL; |
| 8231 | char *opt_u = NULL; |
| 8232 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8233 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8234 | /* "!": do not abort on errors. |
| 8235 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8236 | */ |
| 8237 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8238 | if (read_flags == (uint32_t)-1) |
| 8239 | return EXIT_FAILURE; |
| 8240 | argv += optind; |
| 8241 | |
| 8242 | r = shell_builtin_read(set_local_var_from_halves, |
| 8243 | argv, |
| 8244 | get_local_var_value("IFS"), /* can be NULL */ |
| 8245 | read_flags, |
| 8246 | opt_n, |
| 8247 | opt_p, |
| 8248 | opt_t, |
| 8249 | opt_u |
| 8250 | ); |
| 8251 | |
| 8252 | if ((uintptr_t)r > 1) { |
| 8253 | bb_error_msg("%s", r); |
| 8254 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8255 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8256 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8257 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8258 | } |
| 8259 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8260 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8261 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8262 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8263 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8264 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8265 | * set -- [argument...] |
| 8266 | * set -o |
| 8267 | * set +o |
| 8268 | * Implementations shall support the options in both their hyphen and |
| 8269 | * plus-sign forms. These options can also be specified as options to sh. |
| 8270 | * Examples: |
| 8271 | * Write out all variables and their values: set |
| 8272 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8273 | * Turn on the -x and -v options: set -xv |
| 8274 | * Unset all positional parameters: set -- |
| 8275 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8276 | * Set the positional parameters to the expansion of x, even if x expands |
| 8277 | * with a leading '-' or '+': set -- $x |
| 8278 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8279 | * 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] | 8280 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8281 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8282 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8283 | int n; |
| 8284 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8285 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8286 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8287 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8288 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8289 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8290 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8291 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8292 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8293 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8294 | do { |
| 8295 | if (!strcmp(arg, "--")) { |
| 8296 | ++argv; |
| 8297 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8298 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8299 | if (arg[0] != '+' && arg[0] != '-') |
| 8300 | break; |
| 8301 | for (n = 1; arg[n]; ++n) |
| 8302 | if (set_mode(arg[0], arg[n])) |
| 8303 | goto error; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8304 | } while ((arg = *++argv) != NULL); |
| 8305 | /* Now argv[0] is 1st argument */ |
| 8306 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8307 | if (arg == NULL) |
| 8308 | return EXIT_SUCCESS; |
| 8309 | set_argv: |
| 8310 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8311 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8312 | g_argv = G.global_argv; |
| 8313 | if (G.global_args_malloced) { |
| 8314 | pp = g_argv; |
| 8315 | while (*++pp) |
| 8316 | free(*pp); |
| 8317 | g_argv[1] = NULL; |
| 8318 | } else { |
| 8319 | G.global_args_malloced = 1; |
| 8320 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8321 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8322 | g_argv = pp; |
| 8323 | } |
| 8324 | /* This realloc's G.global_argv */ |
| 8325 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8326 | |
| 8327 | n = 1; |
| 8328 | while (*++pp) |
| 8329 | n++; |
| 8330 | G.global_argc = n; |
| 8331 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8332 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8333 | |
| 8334 | /* Nothing known, so abort */ |
| 8335 | error: |
| 8336 | bb_error_msg("set: %s: invalid option", arg); |
| 8337 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8338 | } |
| 8339 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8340 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8341 | { |
| 8342 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8343 | argv = skip_dash_dash(argv); |
| 8344 | if (argv[0]) { |
| 8345 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8346 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8347 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8348 | if (G.global_args_malloced) { |
| 8349 | int m = 1; |
| 8350 | while (m <= n) |
| 8351 | free(G.global_argv[m++]); |
| 8352 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8353 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8354 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8355 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8356 | return EXIT_SUCCESS; |
| 8357 | } |
| 8358 | return EXIT_FAILURE; |
| 8359 | } |
| 8360 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8361 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8362 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8363 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8364 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8365 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8366 | #if ENABLE_HUSH_FUNCTIONS |
| 8367 | smallint sv_flg; |
| 8368 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8369 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8370 | argv = skip_dash_dash(argv); |
| 8371 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8372 | if (!filename) { |
| 8373 | /* bash says: "bash: .: filename argument required" */ |
| 8374 | return 2; /* bash compat */ |
| 8375 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8376 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8377 | if (!strchr(filename, '/')) { |
| 8378 | arg_path = find_in_path(filename); |
| 8379 | if (arg_path) |
| 8380 | filename = arg_path; |
| 8381 | } |
| 8382 | input = fopen_or_warn(filename, "r"); |
| 8383 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8384 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8385 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8386 | return EXIT_FAILURE; |
| 8387 | } |
| 8388 | close_on_exec_on(fileno(input)); |
| 8389 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8390 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8391 | sv_flg = G.flag_return_in_progress; |
| 8392 | /* "we are inside sourced file, ok to use return" */ |
| 8393 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8394 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8395 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8396 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8397 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8398 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8399 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8400 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8401 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8402 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8403 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8404 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8405 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8406 | } |
| 8407 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8408 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8409 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8410 | int rc; |
| 8411 | mode_t mask; |
| 8412 | |
| 8413 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8414 | argv = skip_dash_dash(argv); |
| 8415 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8416 | mode_t old_mask = mask; |
| 8417 | |
| 8418 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8419 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8420 | mask ^= 0777; |
| 8421 | if (rc == 0) { |
| 8422 | mask = old_mask; |
| 8423 | /* bash messages: |
| 8424 | * bash: umask: 'q': invalid symbolic mode operator |
| 8425 | * bash: umask: 999: octal number out of range |
| 8426 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8427 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8428 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8429 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8430 | rc = 1; |
| 8431 | /* Mimic bash */ |
| 8432 | printf("%04o\n", (unsigned) mask); |
| 8433 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8434 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8435 | umask(mask); |
| 8436 | |
| 8437 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8438 | } |
| 8439 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8440 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8441 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8442 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8443 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8444 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8445 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8446 | /* "!": do not abort on errors */ |
| 8447 | /* "+": stop at 1st non-option */ |
| 8448 | opts = getopt32(argv, "!+vf"); |
| 8449 | if (opts == (unsigned)-1) |
| 8450 | return EXIT_FAILURE; |
| 8451 | if (opts == 3) { |
| 8452 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8453 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8454 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8455 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8456 | |
| 8457 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8458 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8459 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8460 | if (unset_local_var(*argv)) { |
| 8461 | /* unset <nonexistent_var> doesn't fail. |
| 8462 | * Error is when one tries to unset RO var. |
| 8463 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8464 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8465 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8466 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8467 | #if ENABLE_HUSH_FUNCTIONS |
| 8468 | else { |
| 8469 | unset_func(*argv); |
| 8470 | } |
| 8471 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8472 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8473 | } |
| 8474 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8475 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8476 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8477 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8478 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8479 | { |
| 8480 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8481 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8482 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8483 | argv = skip_dash_dash(argv); |
| 8484 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8485 | /* Don't care about wait results */ |
| 8486 | /* Note 1: must wait until there are no more children */ |
| 8487 | /* Note 2: must be interruptible */ |
| 8488 | /* Examples: |
| 8489 | * $ sleep 3 & sleep 6 & wait |
| 8490 | * [1] 30934 sleep 3 |
| 8491 | * [2] 30935 sleep 6 |
| 8492 | * [1] Done sleep 3 |
| 8493 | * [2] Done sleep 6 |
| 8494 | * $ sleep 3 & sleep 6 & wait |
| 8495 | * [1] 30936 sleep 3 |
| 8496 | * [2] 30937 sleep 6 |
| 8497 | * [1] Done sleep 3 |
| 8498 | * ^C <-- after ~4 sec from keyboard |
| 8499 | * $ |
| 8500 | */ |
| 8501 | sigaddset(&G.blocked_set, SIGCHLD); |
| 8502 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8503 | while (1) { |
| 8504 | checkjobs(NULL); |
| 8505 | if (errno == ECHILD) |
| 8506 | break; |
| 8507 | /* Wait for SIGCHLD or any other signal of interest */ |
| 8508 | /* sigtimedwait with infinite timeout: */ |
| 8509 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 8510 | if (sig > 0) { |
| 8511 | sig = check_and_run_traps(sig); |
| 8512 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 8513 | ret = 128 + sig; |
| 8514 | break; |
| 8515 | } |
| 8516 | } |
| 8517 | } |
| 8518 | sigdelset(&G.blocked_set, SIGCHLD); |
| 8519 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8520 | return ret; |
| 8521 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8522 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8523 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8524 | while (*argv) { |
| 8525 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 8526 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8527 | /* mimic bash message */ |
| 8528 | 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] | 8529 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8530 | } |
| 8531 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8532 | if (WIFSIGNALED(status)) |
| 8533 | ret = 128 + WTERMSIG(status); |
| 8534 | else if (WIFEXITED(status)) |
| 8535 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8536 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8537 | ret = EXIT_FAILURE; |
| 8538 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8539 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8540 | ret = 127; |
| 8541 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8542 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
| 8545 | return ret; |
| 8546 | } |
| 8547 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8548 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 8549 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 8550 | { |
| 8551 | if (argv[1]) { |
| 8552 | def = bb_strtou(argv[1], NULL, 10); |
| 8553 | if (errno || def < def_min || argv[2]) { |
| 8554 | bb_error_msg("%s: bad arguments", argv[0]); |
| 8555 | def = UINT_MAX; |
| 8556 | } |
| 8557 | } |
| 8558 | return def; |
| 8559 | } |
| 8560 | #endif |
| 8561 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8562 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8563 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8564 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8565 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8566 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8567 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 8568 | return EXIT_SUCCESS; /* bash compat */ |
| 8569 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8570 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8571 | |
| 8572 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 8573 | if (depth == UINT_MAX) |
| 8574 | G.flag_break_continue = BC_BREAK; |
| 8575 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8576 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8577 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8578 | return EXIT_SUCCESS; |
| 8579 | } |
| 8580 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8581 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8582 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8583 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 8584 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8585 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8586 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8587 | |
| 8588 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8589 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8590 | { |
| 8591 | int rc; |
| 8592 | |
| 8593 | if (G.flag_return_in_progress != -1) { |
| 8594 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 8595 | return EXIT_FAILURE; /* bash compat */ |
| 8596 | } |
| 8597 | |
| 8598 | G.flag_return_in_progress = 1; |
| 8599 | |
| 8600 | /* bash: |
| 8601 | * out of range: wraps around at 256, does not error out |
| 8602 | * non-numeric param: |
| 8603 | * f() { false; return qwe; }; f; echo $? |
| 8604 | * bash: return: qwe: numeric argument required <== we do this |
| 8605 | * 255 <== we also do this |
| 8606 | */ |
| 8607 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 8608 | return rc; |
| 8609 | } |
| 8610 | #endif |