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 | * |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 12 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 13 | * Credits: |
| 14 | * The parser routines proper are all original material, first |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 15 | * written Dec 2000 and Jan 2001 by Larry Doolittle. The |
| 16 | * execution engine, the builtins, and much of the underlying |
| 17 | * support has been adapted from busybox-0.49pre's lash, which is |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 19 | * written by Erik Andersen <andersen@codepoet.org>. That, in turn, |
| 20 | * is based in part on ladsh.c, by Michael K. Johnson and Erik W. |
| 21 | * Troan, which they placed in the public domain. I don't know |
| 22 | * how much of the Johnson/Troan code has survived the repeated |
| 23 | * rewrites. |
| 24 | * |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 25 | * Other credits: |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 26 | * o_addchr derived from similar w_addchar function in glibc-2.2. |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 27 | * parse_redirect, redirect_opt_num, and big chunks of main |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 28 | * and many builtins derived from contributions by Erik Andersen. |
| 29 | * Miscellaneous bugfixes from Matt Kraai. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 30 | * |
| 31 | * There are two big (and related) architecture differences between |
| 32 | * this parser and the lash parser. One is that this version is |
| 33 | * actually designed from the ground up to understand nearly all |
| 34 | * of the Bourne grammar. The second, consequential change is that |
| 35 | * the parser and input reader have been turned inside out. Now, |
| 36 | * the parser is in control, and asks for input as needed. The old |
| 37 | * way had the input reader in control, and it asked for parsing to |
| 38 | * take place as needed. The new way makes it much easier to properly |
| 39 | * handle the recursion implicit in the various substitutions, especially |
| 40 | * across continuation lines. |
| 41 | * |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 42 | * TODOs: |
| 43 | * grep for "TODO" and fix (some of them are easy) |
| 44 | * special variables (done: PWD, PPID, RANDOM) |
| 45 | * tilde expansion |
Eric Andersen | 78a7c99 | 2001-05-15 16:30:25 +0000 | [diff] [blame] | 46 | * aliases |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 47 | * follow IFS rules more precisely, including update semantics |
| 48 | * builtins mandated by standards we don't support: |
| 49 | * [un]alias, command, fc, getopts, newgrp, readonly, times |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 50 | * make complex ${var%...} constructs support optional |
| 51 | * make here documents optional |
Mike Frysinger | 25a6ca0 | 2009-03-28 13:59:26 +0000 | [diff] [blame] | 52 | * |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 53 | * Bash compat TODO: |
| 54 | * redirection of stdout+stderr: &> and >& |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 55 | * reserved words: function select |
| 56 | * advanced test: [[ ]] |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 57 | * process substitution: <(list) and >(list) |
| 58 | * =~: regex operator |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 59 | * let EXPR [EXPR...] |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 60 | * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION) |
| 61 | * If the last arg evaluates to 0, let returns 1; 0 otherwise. |
| 62 | * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used) |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 63 | * ((EXPR)) |
Denys Vlasenko | 349ef96 | 2010-05-21 15:46:24 +0200 | [diff] [blame] | 64 | * The EXPR is evaluated according to ARITHMETIC EVALUATION. |
| 65 | * This is exactly equivalent to let "EXPR". |
Denys Vlasenko | adc0e20 | 2010-05-17 18:56:58 +0200 | [diff] [blame] | 66 | * $[EXPR]: synonym for $((EXPR)) |
Denys Vlasenko | bbecd74 | 2010-10-03 17:22:52 +0200 | [diff] [blame] | 67 | * |
| 68 | * Won't do: |
| 69 | * In bash, export builtin is special, its arguments are assignments |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 70 | * and therefore expansion of them should be "one-word" expansion: |
| 71 | * $ export i=`echo 'a b'` # export has one arg: "i=a b" |
| 72 | * compare with: |
| 73 | * $ ls i=`echo 'a b'` # ls has two args: "i=a" and "b" |
| 74 | * ls: cannot access i=a: No such file or directory |
| 75 | * ls: cannot access b: No such file or directory |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 76 | * Note1: same applies to local builtin. |
Denys Vlasenko | 0821801 | 2009-06-03 14:43:56 +0200 | [diff] [blame] | 77 | * Note2: bash 3.2.33(1) does this only if export word itself |
| 78 | * is not quoted: |
| 79 | * $ export i=`echo 'aaa bbb'`; echo "$i" |
| 80 | * aaa bbb |
| 81 | * $ "export" i=`echo 'aaa bbb'`; echo "$i" |
| 82 | * aaa |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 83 | */ |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 84 | #include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ |
Denys Vlasenko | 8da415e | 2010-12-05 01:30:14 +0100 | [diff] [blame^] | 85 | #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \ |
| 86 | || defined(__APPLE__) \ |
| 87 | ) |
| 88 | # include <malloc.h> /* for malloc_trim */ |
| 89 | #endif |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 90 | #include <glob.h> |
| 91 | /* #include <dmalloc.h> */ |
| 92 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 93 | # include <fnmatch.h> |
Denis Vlasenko | be709c2 | 2008-07-28 00:01:16 +0000 | [diff] [blame] | 94 | #endif |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 95 | |
| 96 | #include "shell_common.h" |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 97 | #include "math.h" |
Mike Frysinger | a4f331d | 2009-04-07 06:03:22 +0000 | [diff] [blame] | 98 | #include "match.h" |
Denys Vlasenko | cbe0b7f | 2009-10-09 22:00:58 +0200 | [diff] [blame] | 99 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 100 | # include "random.h" |
Denys Vlasenko | 76ace25 | 2009-10-12 15:25:01 +0200 | [diff] [blame] | 101 | #else |
| 102 | # define CLEAR_RANDOM_T(rnd) ((void)0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 103 | #endif |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 104 | #ifndef PIPE_BUF |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 105 | # define PIPE_BUF 4096 /* amount of buffering in a pipe */ |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 106 | #endif |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 107 | |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 108 | //applet:IF_HUSH(APPLET(hush, _BB_DIR_BIN, _BB_SUID_DROP)) |
| 109 | //applet:IF_MSH(APPLET(msh, _BB_DIR_BIN, _BB_SUID_DROP)) |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 110 | //applet:IF_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, _BB_DIR_BIN, _BB_SUID_DROP, sh)) |
| 111 | //applet:IF_FEATURE_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, _BB_DIR_BIN, _BB_SUID_DROP, bash)) |
| 112 | |
| 113 | //kbuild:lib-$(CONFIG_HUSH) += hush.o match.o shell_common.o |
| 114 | //kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o |
| 115 | |
| 116 | //config:config HUSH |
| 117 | //config: bool "hush" |
| 118 | //config: default y |
| 119 | //config: help |
Denys Vlasenko | 771f199 | 2010-07-16 14:31:34 +0200 | [diff] [blame] | 120 | //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] | 121 | //config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, |
| 122 | //config: case/esac. Redirections, here documents, $((arithmetic)) |
| 123 | //config: and functions are supported. |
| 124 | //config: |
| 125 | //config: It will compile and work on no-mmu systems. |
| 126 | //config: |
Denys Vlasenko | e2069fb | 2010-10-04 00:01:47 +0200 | [diff] [blame] | 127 | //config: It does not handle select, aliases, tilde expansion, |
| 128 | //config: &>file and >&file redirection of stdout+stderr. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 129 | //config: |
| 130 | //config:config HUSH_BASH_COMPAT |
| 131 | //config: bool "bash-compatible extensions" |
| 132 | //config: default y |
| 133 | //config: depends on HUSH |
| 134 | //config: help |
| 135 | //config: Enable bash-compatible extensions. |
| 136 | //config: |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 137 | //config:config HUSH_BRACE_EXPANSION |
| 138 | //config: bool "Brace expansion" |
| 139 | //config: default y |
| 140 | //config: depends on HUSH_BASH_COMPAT |
| 141 | //config: help |
| 142 | //config: Enable {abc,def} extension. |
| 143 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 144 | //config:config HUSH_HELP |
| 145 | //config: bool "help builtin" |
| 146 | //config: default y |
| 147 | //config: depends on HUSH |
| 148 | //config: help |
| 149 | //config: Enable help builtin in hush. Code size + ~1 kbyte. |
| 150 | //config: |
| 151 | //config:config HUSH_INTERACTIVE |
| 152 | //config: bool "Interactive mode" |
| 153 | //config: default y |
| 154 | //config: depends on HUSH |
| 155 | //config: help |
| 156 | //config: Enable interactive mode (prompt and command editing). |
| 157 | //config: Without this, hush simply reads and executes commands |
| 158 | //config: from stdin just like a shell script from a file. |
| 159 | //config: No prompt, no PS1/PS2 magic shell variables. |
| 160 | //config: |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 161 | //config:config HUSH_SAVEHISTORY |
| 162 | //config: bool "Save command history to .hush_history" |
| 163 | //config: default y |
| 164 | //config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY |
| 165 | //config: help |
| 166 | //config: Enable history saving in hush. |
| 167 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 168 | //config:config HUSH_JOB |
| 169 | //config: bool "Job control" |
| 170 | //config: default y |
| 171 | //config: depends on HUSH_INTERACTIVE |
| 172 | //config: help |
| 173 | //config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current |
| 174 | //config: command (not entire shell), fg/bg builtins work. Without this option, |
| 175 | //config: "cmd &" still works by simply spawning a process and immediately |
| 176 | //config: prompting for next command (or executing next command in a script), |
| 177 | //config: but no separate process group is formed. |
| 178 | //config: |
| 179 | //config:config HUSH_TICK |
| 180 | //config: bool "Process substitution" |
| 181 | //config: default y |
| 182 | //config: depends on HUSH |
| 183 | //config: help |
| 184 | //config: Enable process substitution `command` and $(command) in hush. |
| 185 | //config: |
| 186 | //config:config HUSH_IF |
| 187 | //config: bool "Support if/then/elif/else/fi" |
| 188 | //config: default y |
| 189 | //config: depends on HUSH |
| 190 | //config: help |
| 191 | //config: Enable if/then/elif/else/fi in hush. |
| 192 | //config: |
| 193 | //config:config HUSH_LOOPS |
| 194 | //config: bool "Support for, while and until loops" |
| 195 | //config: default y |
| 196 | //config: depends on HUSH |
| 197 | //config: help |
| 198 | //config: Enable for, while and until loops in hush. |
| 199 | //config: |
| 200 | //config:config HUSH_CASE |
| 201 | //config: bool "Support case ... esac statement" |
| 202 | //config: default y |
| 203 | //config: depends on HUSH |
| 204 | //config: help |
| 205 | //config: Enable case ... esac statement in hush. +400 bytes. |
| 206 | //config: |
| 207 | //config:config HUSH_FUNCTIONS |
| 208 | //config: bool "Support funcname() { commands; } syntax" |
| 209 | //config: default y |
| 210 | //config: depends on HUSH |
| 211 | //config: help |
| 212 | //config: Enable support for shell functions in hush. +800 bytes. |
| 213 | //config: |
| 214 | //config:config HUSH_LOCAL |
| 215 | //config: bool "Support local builtin" |
| 216 | //config: default y |
| 217 | //config: depends on HUSH_FUNCTIONS |
| 218 | //config: help |
| 219 | //config: Enable support for local variables in functions. |
| 220 | //config: |
| 221 | //config:config HUSH_RANDOM_SUPPORT |
| 222 | //config: bool "Pseudorandom generator and $RANDOM variable" |
| 223 | //config: default y |
| 224 | //config: depends on HUSH |
| 225 | //config: help |
| 226 | //config: Enable pseudorandom generator and dynamic variable "$RANDOM". |
| 227 | //config: Each read of "$RANDOM" will generate a new pseudorandom value. |
| 228 | //config: |
| 229 | //config:config HUSH_EXPORT_N |
| 230 | //config: bool "Support 'export -n' option" |
| 231 | //config: default y |
| 232 | //config: depends on HUSH |
| 233 | //config: help |
| 234 | //config: export -n unexports variables. It is a bash extension. |
| 235 | //config: |
| 236 | //config:config HUSH_MODE_X |
| 237 | //config: bool "Support 'hush -x' option and 'set -x' command" |
| 238 | //config: default y |
| 239 | //config: depends on HUSH |
| 240 | //config: help |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 241 | //config: This instructs hush to print commands before execution. |
| 242 | //config: Adds ~300 bytes. |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 243 | //config: |
Denys Vlasenko | 6adf2aa | 2010-07-16 19:26:38 +0200 | [diff] [blame] | 244 | //config:config MSH |
| 245 | //config: bool "msh (deprecated: aliased to hush)" |
| 246 | //config: default n |
| 247 | //config: select HUSH |
| 248 | //config: help |
| 249 | //config: msh is deprecated and will be removed, please migrate to hush. |
| 250 | //config: |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 251 | |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 252 | /* -i (interactive) and -s (read stdin) are also accepted, |
| 253 | * but currently do nothing, therefore aren't shown in help. |
| 254 | * NOMMU-specific options are not meant to be used by users, |
| 255 | * therefore we don't show them either. |
| 256 | */ |
| 257 | //usage:#define hush_trivial_usage |
| 258 | //usage: "[-nx] [-c SCRIPT]" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 259 | //usage:#define hush_full_usage "" |
Dan Fandrich | 89ca2f9 | 2010-11-28 01:54:39 +0100 | [diff] [blame] | 260 | //usage:#define msh_trivial_usage hush_trivial_usage |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 261 | //usage:#define msh_full_usage "" |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 262 | //usage:#define sh_trivial_usage NOUSAGE_STR |
| 263 | //usage:#define sh_full_usage "" |
| 264 | //usage:#define bash_trivial_usage NOUSAGE_STR |
| 265 | //usage:#define bash_full_usage "" |
Denys Vlasenko | 202a2d1 | 2010-07-16 12:36:14 +0200 | [diff] [blame] | 266 | |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 267 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 268 | /* Build knobs */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 269 | #define LEAK_HUNTING 0 |
| 270 | #define BUILD_AS_NOMMU 0 |
| 271 | /* Enable/disable sanity checks. Ok to enable in production, |
| 272 | * only adds a bit of bloat. Set to >1 to get non-production level verbosity. |
| 273 | * Keeping 1 for now even in released versions. |
| 274 | */ |
| 275 | #define HUSH_DEBUG 1 |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 276 | /* Slightly bigger (+200 bytes), but faster hush. |
| 277 | * So far it only enables a trick with counting SIGCHLDs and forks, |
| 278 | * which allows us to do fewer waitpid's. |
| 279 | * (we can detect a case where neither forks were done nor SIGCHLDs happened |
| 280 | * and therefore waitpid will return the same result as last time) |
| 281 | */ |
| 282 | #define ENABLE_HUSH_FAST 0 |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 283 | /* TODO: implement simplified code for users which do not need ${var%...} ops |
| 284 | * So far ${var%...} ops are always enabled: |
| 285 | */ |
| 286 | #define ENABLE_HUSH_DOLLAR_OPS 1 |
Denis Vlasenko | 1943aec | 2009-04-09 14:15:57 +0000 | [diff] [blame] | 287 | |
| 288 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 289 | #if BUILD_AS_NOMMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 290 | # undef BB_MMU |
| 291 | # undef USE_FOR_NOMMU |
| 292 | # undef USE_FOR_MMU |
| 293 | # define BB_MMU 0 |
| 294 | # define USE_FOR_NOMMU(...) __VA_ARGS__ |
| 295 | # define USE_FOR_MMU(...) |
| 296 | #endif |
| 297 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame] | 298 | #include "NUM_APPLETS.h" |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 299 | #if NUM_APPLETS == 1 |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 300 | /* STANDALONE does not make sense, and won't compile */ |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 301 | # undef CONFIG_FEATURE_SH_STANDALONE |
| 302 | # undef ENABLE_FEATURE_SH_STANDALONE |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 303 | # undef IF_FEATURE_SH_STANDALONE |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 304 | # undef IF_NOT_FEATURE_SH_STANDALONE |
| 305 | # define ENABLE_FEATURE_SH_STANDALONE 0 |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 306 | # define IF_FEATURE_SH_STANDALONE(...) |
| 307 | # define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__ |
Denis Vlasenko | 61befda | 2008-11-25 01:36:03 +0000 | [diff] [blame] | 308 | #endif |
| 309 | |
Denis Vlasenko | 05743d7 | 2008-02-10 12:10:08 +0000 | [diff] [blame] | 310 | #if !ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 311 | # undef ENABLE_FEATURE_EDITING |
| 312 | # define ENABLE_FEATURE_EDITING 0 |
| 313 | # undef ENABLE_FEATURE_EDITING_FANCY_PROMPT |
| 314 | # define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0 |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 315 | #endif |
| 316 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 317 | /* Do we support ANY keywords? */ |
| 318 | #if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 319 | # define HAS_KEYWORDS 1 |
| 320 | # define IF_HAS_KEYWORDS(...) __VA_ARGS__ |
| 321 | # define IF_HAS_NO_KEYWORDS(...) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 322 | #else |
Denis Vlasenko | ce4acbb | 2009-04-10 23:23:41 +0000 | [diff] [blame] | 323 | # define HAS_KEYWORDS 0 |
| 324 | # define IF_HAS_KEYWORDS(...) |
| 325 | # define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 326 | #endif |
Denis Vlasenko | 8412d79 | 2007-10-01 09:59:47 +0000 | [diff] [blame] | 327 | |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 328 | /* If you comment out one of these below, it will be #defined later |
| 329 | * to perform debug printfs to stderr: */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 330 | #define debug_printf(...) do {} while (0) |
Denis Vlasenko | 400c5b6 | 2007-05-04 13:07:27 +0000 | [diff] [blame] | 331 | /* Finer-grained debug switches */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 332 | #define debug_printf_parse(...) do {} while (0) |
| 333 | #define debug_print_tree(a, b) do {} while (0) |
| 334 | #define debug_printf_exec(...) do {} while (0) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 335 | #define debug_printf_env(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 336 | #define debug_printf_jobs(...) do {} while (0) |
| 337 | #define debug_printf_expand(...) do {} while (0) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 338 | #define debug_printf_varexp(...) do {} while (0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 339 | #define debug_printf_glob(...) do {} while (0) |
| 340 | #define debug_printf_list(...) do {} while (0) |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 341 | #define debug_printf_subst(...) do {} while (0) |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 342 | #define debug_printf_clean(...) do {} while (0) |
Denis Vlasenko | d01ff13 | 2007-05-02 21:40:23 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 344 | #define ERR_PTR ((void*)(long)1) |
| 345 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 346 | #define JOB_STATUS_FORMAT "[%d] %-22s %.40s\n" |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 347 | |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 348 | #define _SPECIAL_VARS_STR "_*@$!?#" |
| 349 | #define SPECIAL_VARS_STR ("_*@$!?#" + 1) |
| 350 | #define NUMERIC_SPECVARS_STR ("_*@$!?#" + 3) |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 351 | #if ENABLE_HUSH_BASH_COMPAT |
| 352 | /* Support / and // replace ops */ |
| 353 | /* Note that // is stored as \ in "encoded" string representation */ |
| 354 | # define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?" |
| 355 | # define VAR_SUBST_OPS ("\\/%#:-=+?" + 1) |
| 356 | # define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5) |
| 357 | #else |
| 358 | # define VAR_ENCODED_SUBST_OPS "%#:-=+?" |
| 359 | # define VAR_SUBST_OPS "%#:-=+?" |
| 360 | # define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3) |
| 361 | #endif |
Denys Vlasenko | e85248a | 2010-05-22 06:20:26 +0200 | [diff] [blame] | 362 | |
| 363 | #define SPECIAL_VAR_SYMBOL 3 |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 364 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 365 | struct variable; |
| 366 | |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 367 | static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER; |
| 368 | |
| 369 | /* This supports saving pointers malloced in vfork child, |
Denis Vlasenko | c376db3 | 2009-04-15 21:49:48 +0000 | [diff] [blame] | 370 | * to be freed in the parent. |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 371 | */ |
| 372 | #if !BB_MMU |
| 373 | typedef struct nommu_save_t { |
| 374 | char **new_env; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 375 | struct variable *old_vars; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 376 | char **argv; |
Denis Vlasenko | 27014ed | 2009-04-15 21:48:23 +0000 | [diff] [blame] | 377 | char **argv_from_re_execing; |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 378 | } nommu_save_t; |
| 379 | #endif |
| 380 | |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 381 | enum { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 382 | RES_NONE = 0, |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 383 | #if ENABLE_HUSH_IF |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 384 | RES_IF , |
| 385 | RES_THEN , |
| 386 | RES_ELIF , |
| 387 | RES_ELSE , |
| 388 | RES_FI , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 389 | #endif |
| 390 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 391 | RES_FOR , |
| 392 | RES_WHILE , |
| 393 | RES_UNTIL , |
| 394 | RES_DO , |
| 395 | RES_DONE , |
Denis Vlasenko | d91afa3 | 2008-07-29 11:10:01 +0000 | [diff] [blame] | 396 | #endif |
| 397 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 398 | RES_IN , |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 399 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 400 | #if ENABLE_HUSH_CASE |
| 401 | RES_CASE , |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 402 | /* three pseudo-keywords support contrived "case" syntax: */ |
| 403 | RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */ |
| 404 | RES_MATCH , /* "word)" */ |
| 405 | RES_CASE_BODY, /* "this command is inside CASE" */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 406 | RES_ESAC , |
| 407 | #endif |
| 408 | RES_XXXX , |
| 409 | RES_SNTX |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 410 | }; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 411 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 412 | typedef struct o_string { |
| 413 | char *data; |
| 414 | int length; /* position where data is appended */ |
| 415 | int maxlen; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 416 | int o_expflags; |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 417 | /* At least some part of the string was inside '' or "", |
| 418 | * possibly empty one: word"", wo''rd etc. */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 419 | smallint has_quoted_part; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 420 | smallint has_empty_slot; |
| 421 | smallint o_assignment; /* 0:maybe, 1:yes, 2:no */ |
| 422 | } o_string; |
| 423 | enum { |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 424 | EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */ |
| 425 | EXP_FLAG_GLOB = 0x2, |
| 426 | /* Protect newly added chars against globbing |
| 427 | * by prepending \ to *, ?, [, \ */ |
| 428 | EXP_FLAG_ESC_GLOB_CHARS = 0x1, |
| 429 | }; |
| 430 | enum { |
| 431 | MAYBE_ASSIGNMENT = 0, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 432 | DEFINITELY_ASSIGNMENT = 1, |
Denys Vlasenko | 0e13b40 | 2010-09-21 12:35:39 +0200 | [diff] [blame] | 433 | NOT_ASSIGNMENT = 2, |
| 434 | /* Not an assigment, but next word may be: "if v=xyz cmd;" */ |
| 435 | WORD_IS_KEYWORD = 3, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 436 | }; |
| 437 | /* Used for initialization: o_string foo = NULL_O_STRING; */ |
| 438 | #define NULL_O_STRING { NULL } |
| 439 | |
| 440 | /* I can almost use ordinary FILE*. Is open_memstream() universally |
| 441 | * available? Where is it documented? */ |
| 442 | typedef struct in_str { |
| 443 | const char *p; |
| 444 | /* eof_flag=1: last char in ->p is really an EOF */ |
| 445 | char eof_flag; /* meaningless if ->p == NULL */ |
| 446 | char peek_buf[2]; |
| 447 | #if ENABLE_HUSH_INTERACTIVE |
| 448 | smallint promptme; |
| 449 | smallint promptmode; /* 0: PS1, 1: PS2 */ |
| 450 | #endif |
| 451 | FILE *file; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 452 | int (*get) (struct in_str *) FAST_FUNC; |
| 453 | int (*peek) (struct in_str *) FAST_FUNC; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 454 | } in_str; |
| 455 | #define i_getch(input) ((input)->get(input)) |
| 456 | #define i_peek(input) ((input)->peek(input)) |
| 457 | |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 458 | /* The descrip member of this structure is only used to make |
| 459 | * debugging output pretty */ |
| 460 | static const struct { |
| 461 | int mode; |
| 462 | signed char default_fd; |
| 463 | char descrip[3]; |
| 464 | } redir_table[] = { |
| 465 | { O_RDONLY, 0, "<" }, |
| 466 | { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" }, |
| 467 | { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" }, |
| 468 | { O_CREAT|O_RDWR, 1, "<>" }, |
| 469 | { O_RDONLY, 0, "<<" }, |
| 470 | /* Should not be needed. Bogus default_fd helps in debugging */ |
| 471 | /* { O_RDONLY, 77, "<<" }, */ |
| 472 | }; |
| 473 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 474 | struct redir_struct { |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 475 | struct redir_struct *next; |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 476 | char *rd_filename; /* filename */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 477 | int rd_fd; /* fd to redirect */ |
| 478 | /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */ |
| 479 | int rd_dup; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 480 | smallint rd_type; /* (enum redir_type) */ |
| 481 | /* note: for heredocs, rd_filename contains heredoc delimiter, |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 482 | * and subsequently heredoc itself; and rd_dup is a bitmask: |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 483 | * bit 0: do we need to trim leading tabs? |
| 484 | * bit 1: is heredoc quoted (<<'delim' syntax) ? |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 485 | */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 486 | }; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 487 | typedef enum redir_type { |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 488 | REDIRECT_INPUT = 0, |
| 489 | REDIRECT_OVERWRITE = 1, |
| 490 | REDIRECT_APPEND = 2, |
| 491 | REDIRECT_IO = 3, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 492 | REDIRECT_HEREDOC = 4, |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 493 | REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 494 | |
| 495 | REDIRFD_CLOSE = -3, |
| 496 | REDIRFD_SYNTAX_ERR = -2, |
Denis Vlasenko | 835fcfd | 2009-04-10 13:51:56 +0000 | [diff] [blame] | 497 | REDIRFD_TO_FILE = -1, |
| 498 | /* otherwise, rd_fd is redirected to rd_dup */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 499 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 500 | HEREDOC_SKIPTABS = 1, |
| 501 | HEREDOC_QUOTED = 2, |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 502 | } redir_type; |
| 503 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 504 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 505 | struct command { |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 506 | pid_t pid; /* 0 if exited */ |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 507 | int assignment_cnt; /* how many argv[i] are assignments? */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 508 | smallint is_stopped; /* is the command currently running? */ |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 509 | smallint cmd_type; /* CMD_xxx */ |
| 510 | #define CMD_NORMAL 0 |
| 511 | #define CMD_SUBSHELL 1 |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 512 | #if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | d383b49 | 2010-09-06 10:22:13 +0200 | [diff] [blame] | 513 | /* used for "[[ EXPR ]]" */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 514 | # define CMD_SINGLEWORD_NOGLOB 2 |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 515 | #endif |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 516 | #if ENABLE_HUSH_FUNCTIONS |
| 517 | # define CMD_FUNCDEF 3 |
| 518 | #endif |
| 519 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 520 | smalluint cmd_exitcode; |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 521 | /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */ |
| 522 | struct pipe *group; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 523 | #if !BB_MMU |
| 524 | char *group_as_string; |
| 525 | #endif |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 526 | #if ENABLE_HUSH_FUNCTIONS |
| 527 | struct function *child_func; |
| 528 | /* This field is used to prevent a bug here: |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 529 | * while...do f1() {a;}; f1; f1() {b;}; f1; done |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 530 | * When we execute "f1() {a;}" cmd, we create new function and clear |
| 531 | * cmd->group, cmd->group_as_string, cmd->argv[0]. |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 532 | * When we execute "f1() {b;}", we notice that f1 exists, |
| 533 | * and that its "parent cmd" struct is still "alive", |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 534 | * we put those fields back into cmd->xxx |
| 535 | * (struct function has ->parent_cmd ptr to facilitate that). |
| 536 | * When we loop back, we can execute "f1() {a;}" again and set f1 correctly. |
| 537 | * Without this trick, loop would execute a;b;b;b;... |
| 538 | * instead of correct sequence a;b;a;b;... |
| 539 | * When command is freed, it severs the link |
| 540 | * (sets ->child_func->parent_cmd to NULL). |
| 541 | */ |
| 542 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 543 | char **argv; /* command name and arguments */ |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 544 | /* argv vector may contain variable references (^Cvar^C, ^C0^C etc) |
| 545 | * and on execution these are substituted with their values. |
| 546 | * Substitution can make _several_ words out of one argv[n]! |
| 547 | * Example: argv[0]=='.^C*^C.' here: echo .$*. |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 548 | * References of the form ^C`cmd arg^C are `cmd arg` substitutions. |
Denis Vlasenko | 03eb8bf | 2007-05-14 16:19:34 +0000 | [diff] [blame] | 549 | */ |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 550 | struct redir_struct *redirects; /* I/O redirections */ |
| 551 | }; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 552 | /* Is there anything in this command at all? */ |
| 553 | #define IS_NULL_CMD(cmd) \ |
| 554 | (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects) |
| 555 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 556 | struct pipe { |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 557 | struct pipe *next; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 558 | int num_cmds; /* total number of commands in pipe */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 559 | int alive_cmds; /* number of commands running (not exited) */ |
| 560 | int stopped_cmds; /* number of commands alive, but stopped */ |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 561 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 562 | int jobid; /* job number */ |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 563 | pid_t pgrp; /* process group ID for the job */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 564 | char *cmdtext; /* name of job */ |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 565 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 566 | struct command *cmds; /* array of commands in pipe */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 567 | smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 568 | IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */ |
| 569 | IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 570 | }; |
Denis Vlasenko | a2b11e3 | 2009-04-06 14:11:13 +0000 | [diff] [blame] | 571 | typedef enum pipe_style { |
| 572 | PIPE_SEQ = 1, |
| 573 | PIPE_AND = 2, |
| 574 | PIPE_OR = 3, |
| 575 | PIPE_BG = 4, |
| 576 | } pipe_style; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 577 | /* Is there anything in this pipe at all? */ |
| 578 | #define IS_NULL_PIPE(pi) \ |
| 579 | ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 580 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 581 | /* This holds pointers to the various results of parsing */ |
| 582 | struct parse_context { |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 583 | /* linked list of pipes */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 584 | struct pipe *list_head; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 585 | /* last pipe (being constructed right now) */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 586 | struct pipe *pipe; |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 587 | /* last command in pipe (being constructed right now) */ |
| 588 | struct command *command; |
| 589 | /* last redirect in command->redirects list */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 590 | struct redir_struct *pending_redirect; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 591 | #if !BB_MMU |
| 592 | o_string as_string; |
| 593 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 594 | #if HAS_KEYWORDS |
| 595 | smallint ctx_res_w; |
| 596 | smallint ctx_inverted; /* "! cmd | cmd" */ |
| 597 | #if ENABLE_HUSH_CASE |
| 598 | smallint ctx_dsemicolon; /* ";;" seen */ |
| 599 | #endif |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 600 | /* bitmask of FLAG_xxx, for figuring out valid reserved words */ |
| 601 | int old_flag; |
| 602 | /* group we are enclosed in: |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 603 | * example: "if pipe1; pipe2; then pipe3; fi" |
| 604 | * when we see "if" or "then", we malloc and copy current context, |
| 605 | * and make ->stack point to it. then we parse pipeN. |
| 606 | * when closing "then" / fi" / whatever is found, |
| 607 | * we move list_head into ->stack->command->group, |
| 608 | * copy ->stack into current context, and delete ->stack. |
| 609 | * (parsing of { list } and ( list ) doesn't use this method) |
Denis Vlasenko | f9f7429 | 2009-04-03 00:07:05 +0000 | [diff] [blame] | 610 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 611 | struct parse_context *stack; |
| 612 | #endif |
| 613 | }; |
| 614 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 615 | /* On program start, environ points to initial environment. |
| 616 | * putenv adds new pointers into it, unsetenv removes them. |
| 617 | * Neither of these (de)allocates the strings. |
| 618 | * setenv allocates new strings in malloc space and does putenv, |
| 619 | * and thus setenv is unusable (leaky) for shell's purposes */ |
| 620 | #define setenv(...) setenv_is_leaky_dont_use() |
| 621 | struct variable { |
| 622 | struct variable *next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 623 | char *varstr; /* points to "name=" portion */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 624 | #if ENABLE_HUSH_LOCAL |
| 625 | unsigned func_nest_level; |
| 626 | #endif |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 627 | int max_len; /* if > 0, name is part of initial env; else name is malloced */ |
| 628 | smallint flg_export; /* putenv should be done on this var */ |
Denis Vlasenko | 219e88d | 2007-05-21 10:18:23 +0000 | [diff] [blame] | 629 | smallint flg_read_only; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 630 | }; |
| 631 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 632 | enum { |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 633 | BC_BREAK = 1, |
| 634 | BC_CONTINUE = 2, |
| 635 | }; |
| 636 | |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 637 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 638 | struct function { |
| 639 | struct function *next; |
| 640 | char *name; |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 641 | struct command *parent_cmd; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 642 | struct pipe *body; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 643 | # if !BB_MMU |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 644 | char *body_as_string; |
Denys Vlasenko | c1947f1 | 2009-10-23 01:30:26 +0200 | [diff] [blame] | 645 | # endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 646 | }; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 647 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 648 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 649 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 650 | /* set -/+o OPT support. (TODO: make it optional) |
| 651 | * bash supports the following opts: |
| 652 | * allexport off |
| 653 | * braceexpand on |
| 654 | * emacs on |
| 655 | * errexit off |
| 656 | * errtrace off |
| 657 | * functrace off |
| 658 | * hashall on |
| 659 | * histexpand off |
| 660 | * history on |
| 661 | * ignoreeof off |
| 662 | * interactive-comments on |
| 663 | * keyword off |
| 664 | * monitor on |
| 665 | * noclobber off |
| 666 | * noexec off |
| 667 | * noglob off |
| 668 | * nolog off |
| 669 | * notify off |
| 670 | * nounset off |
| 671 | * onecmd off |
| 672 | * physical off |
| 673 | * pipefail off |
| 674 | * posix off |
| 675 | * privileged off |
| 676 | * verbose off |
| 677 | * vi off |
| 678 | * xtrace off |
| 679 | */ |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 680 | static const char o_opt_strings[] ALIGN1 = |
| 681 | "pipefail\0" |
| 682 | "noexec\0" |
| 683 | #if ENABLE_HUSH_MODE_X |
| 684 | "xtrace\0" |
| 685 | #endif |
| 686 | ; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 687 | enum { |
| 688 | OPT_O_PIPEFAIL, |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 689 | OPT_O_NOEXEC, |
| 690 | #if ENABLE_HUSH_MODE_X |
| 691 | OPT_O_XTRACE, |
| 692 | #endif |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 693 | NUM_OPT_O |
| 694 | }; |
| 695 | |
| 696 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 697 | /* "Globals" within this file */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 698 | /* Sorted roughly by size (smaller offsets == smaller code) */ |
| 699 | struct globals { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 700 | /* interactive_fd != 0 means we are an interactive shell. |
| 701 | * If we are, then saved_tty_pgrp can also be != 0, meaning |
| 702 | * that controlling tty is available. With saved_tty_pgrp == 0, |
| 703 | * job control still works, but terminal signals |
| 704 | * (^C, ^Z, ^Y, ^\) won't work at all, and background |
| 705 | * process groups can only be created with "cmd &". |
| 706 | * With saved_tty_pgrp != 0, hush will use tcsetpgrp() |
| 707 | * to give tty to the foreground process group, |
| 708 | * and will take it back when the group is stopped (^Z) |
| 709 | * or killed (^C). |
| 710 | */ |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 711 | #if ENABLE_HUSH_INTERACTIVE |
| 712 | /* 'interactive_fd' is a fd# open to ctty, if we have one |
| 713 | * _AND_ if we decided to act interactively */ |
| 714 | int interactive_fd; |
| 715 | const char *PS1; |
| 716 | const char *PS2; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 717 | # define G_interactive_fd (G.interactive_fd) |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 718 | #else |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 719 | # define G_interactive_fd 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 720 | #endif |
| 721 | #if ENABLE_FEATURE_EDITING |
| 722 | line_input_t *line_input_state; |
| 723 | #endif |
Denis Vlasenko | cc3f20b | 2008-06-23 22:31:52 +0000 | [diff] [blame] | 724 | pid_t root_pid; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 725 | pid_t root_ppid; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 726 | pid_t last_bg_pid; |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 727 | #if ENABLE_HUSH_RANDOM_SUPPORT |
| 728 | random_t random_gen; |
| 729 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 730 | #if ENABLE_HUSH_JOB |
| 731 | int run_list_level; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 732 | int last_jobid; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 733 | pid_t saved_tty_pgrp; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 734 | struct pipe *job_list; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 735 | # define G_saved_tty_pgrp (G.saved_tty_pgrp) |
| 736 | #else |
| 737 | # define G_saved_tty_pgrp 0 |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 738 | #endif |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 739 | char o_opt[NUM_OPT_O]; |
Denys Vlasenko | 57542eb | 2010-11-28 03:59:30 +0100 | [diff] [blame] | 740 | #if ENABLE_HUSH_MODE_X |
| 741 | # define G_x_mode (G.o_opt[OPT_O_XTRACE]) |
| 742 | #else |
| 743 | # define G_x_mode 0 |
| 744 | #endif |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 745 | smallint flag_SIGINT; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 746 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 747 | smallint flag_break_continue; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 748 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 749 | #if ENABLE_HUSH_FUNCTIONS |
| 750 | /* 0: outside of a function (or sourced file) |
| 751 | * -1: inside of a function, ok to use return builtin |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 752 | * 1: return is invoked, skip all till end of func |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 753 | */ |
| 754 | smallint flag_return_in_progress; |
| 755 | #endif |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 756 | smallint exiting; /* used to prevent EXIT trap recursion */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 757 | /* These four support $?, $#, and $1 */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 758 | smalluint last_exitcode; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 759 | /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */ |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 760 | smalluint global_args_malloced; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 761 | smalluint inherited_set_is_saved; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 762 | /* how many non-NULL argv's we have. NB: $# + 1 */ |
| 763 | int global_argc; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 764 | char **global_argv; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 765 | #if !BB_MMU |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 766 | char *argv0_for_re_execing; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 767 | #endif |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 768 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 6a2d40f | 2008-07-28 23:07:06 +0000 | [diff] [blame] | 769 | unsigned depth_break_continue; |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 770 | unsigned depth_of_loop; |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 771 | #endif |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 772 | const char *ifs; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 773 | const char *cwd; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 774 | struct variable *top_var; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 775 | char **expanded_assignments; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 776 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 777 | struct function *top_func; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 778 | # if ENABLE_HUSH_LOCAL |
| 779 | struct variable **shadowed_vars_pp; |
| 780 | unsigned func_nest_level; |
| 781 | # endif |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 782 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 783 | /* Signal and trap handling */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 784 | #if ENABLE_HUSH_FAST |
| 785 | unsigned count_SIGCHLD; |
| 786 | unsigned handled_SIGCHLD; |
Denys Vlasenko | e2df5f4 | 2009-05-26 14:34:10 +0200 | [diff] [blame] | 787 | smallint we_have_children; |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 788 | #endif |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 789 | /* which signals have non-DFL handler (even with no traps set)? */ |
| 790 | unsigned non_DFL_mask; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 791 | char **traps; /* char *traps[NSIG] */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 792 | sigset_t blocked_set; |
| 793 | sigset_t inherited_set; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 794 | #if HUSH_DEBUG |
| 795 | unsigned long memleak_value; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 796 | int debug_indent; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 797 | #endif |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 798 | 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] | 799 | }; |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 800 | #define G (*ptr_to_globals) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 801 | /* Not #defining name to G.name - this quickly gets unwieldy |
| 802 | * (too many defines). Also, I actually prefer to see when a variable |
| 803 | * is global, thus "G." prefix is a useful hint */ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 804 | #define INIT_G() do { \ |
| 805 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 806 | } while (0) |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 807 | |
| 808 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 809 | /* Function prototypes for builtins */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 810 | static int builtin_cd(char **argv) FAST_FUNC; |
| 811 | static int builtin_echo(char **argv) FAST_FUNC; |
| 812 | static int builtin_eval(char **argv) FAST_FUNC; |
| 813 | static int builtin_exec(char **argv) FAST_FUNC; |
| 814 | static int builtin_exit(char **argv) FAST_FUNC; |
| 815 | static int builtin_export(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 816 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 817 | static int builtin_fg_bg(char **argv) FAST_FUNC; |
| 818 | static int builtin_jobs(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 819 | #endif |
| 820 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 821 | static int builtin_help(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 822 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 823 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 824 | static int builtin_local(char **argv) FAST_FUNC; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 825 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 826 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 827 | static int builtin_memleak(char **argv) FAST_FUNC; |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 828 | #endif |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 829 | #if ENABLE_PRINTF |
| 830 | static int builtin_printf(char **argv) FAST_FUNC; |
| 831 | #endif |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 832 | static int builtin_pwd(char **argv) FAST_FUNC; |
| 833 | static int builtin_read(char **argv) FAST_FUNC; |
| 834 | static int builtin_set(char **argv) FAST_FUNC; |
| 835 | static int builtin_shift(char **argv) FAST_FUNC; |
| 836 | static int builtin_source(char **argv) FAST_FUNC; |
| 837 | static int builtin_test(char **argv) FAST_FUNC; |
| 838 | static int builtin_trap(char **argv) FAST_FUNC; |
| 839 | static int builtin_type(char **argv) FAST_FUNC; |
| 840 | static int builtin_true(char **argv) FAST_FUNC; |
| 841 | static int builtin_umask(char **argv) FAST_FUNC; |
| 842 | static int builtin_unset(char **argv) FAST_FUNC; |
| 843 | static int builtin_wait(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 844 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 845 | static int builtin_break(char **argv) FAST_FUNC; |
| 846 | static int builtin_continue(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 847 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 848 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 849 | static int builtin_return(char **argv) FAST_FUNC; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 850 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 851 | |
| 852 | /* Table of built-in functions. They can be forked or not, depending on |
| 853 | * context: within pipes, they fork. As simple commands, they do not. |
| 854 | * When used in non-forking context, they can change global variables |
| 855 | * in the parent shell process. If forked, of course they cannot. |
| 856 | * For example, 'unset foo | whatever' will parse and run, but foo will |
| 857 | * still be set at the end. */ |
| 858 | struct built_in_command { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 859 | const char *b_cmd; |
| 860 | int (*b_function)(char **argv) FAST_FUNC; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 861 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 862 | const char *b_descr; |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 863 | # define BLTIN(cmd, func, help) { cmd, func, help } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 864 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 865 | # define BLTIN(cmd, func, help) { cmd, func } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 866 | #endif |
| 867 | }; |
| 868 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 869 | static const struct built_in_command bltins1[] = { |
| 870 | BLTIN("." , builtin_source , "Run commands in a file"), |
| 871 | BLTIN(":" , builtin_true , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 872 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 873 | BLTIN("bg" , builtin_fg_bg , "Resume a job in the background"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 874 | #endif |
| 875 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 876 | BLTIN("break" , builtin_break , "Exit from a loop"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 877 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 878 | BLTIN("cd" , builtin_cd , "Change directory"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 879 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 880 | BLTIN("continue" , builtin_continue, "Start new loop iteration"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 881 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 882 | BLTIN("eval" , builtin_eval , "Construct and run shell command"), |
| 883 | BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"), |
| 884 | BLTIN("exit" , builtin_exit , "Exit"), |
| 885 | BLTIN("export" , builtin_export , "Set environment variables"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 886 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 887 | BLTIN("fg" , builtin_fg_bg , "Bring job into the foreground"), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 888 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 889 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 890 | BLTIN("help" , builtin_help , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 891 | #endif |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 892 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 893 | BLTIN("jobs" , builtin_jobs , "List jobs"), |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 894 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 895 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 896 | BLTIN("local" , builtin_local , "Set local variables"), |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 897 | #endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 898 | #if HUSH_DEBUG |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 899 | BLTIN("memleak" , builtin_memleak , NULL), |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 900 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 901 | BLTIN("read" , builtin_read , "Input into variable"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 902 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 903 | BLTIN("return" , builtin_return , "Return from a function"), |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 904 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 905 | BLTIN("set" , builtin_set , "Set/unset positional parameters"), |
| 906 | BLTIN("shift" , builtin_shift , "Shift positional parameters"), |
Denys Vlasenko | 82731b4 | 2010-05-17 17:49:52 +0200 | [diff] [blame] | 907 | #if ENABLE_HUSH_BASH_COMPAT |
| 908 | BLTIN("source" , builtin_source , "Run commands in a file"), |
| 909 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 910 | BLTIN("trap" , builtin_trap , "Trap signals"), |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 911 | BLTIN("type" , builtin_type , "Show command type"), |
Denys Vlasenko | f3c742f | 2010-03-06 20:12:00 +0100 | [diff] [blame] | 912 | BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 913 | BLTIN("umask" , builtin_umask , "Set file creation mask"), |
| 914 | BLTIN("unset" , builtin_unset , "Unset variables"), |
| 915 | BLTIN("wait" , builtin_wait , "Wait for process"), |
| 916 | }; |
| 917 | /* For now, echo and test are unconditionally enabled. |
| 918 | * Maybe make it configurable? */ |
| 919 | static const struct built_in_command bltins2[] = { |
| 920 | BLTIN("[" , builtin_test , NULL), |
| 921 | BLTIN("echo" , builtin_echo , NULL), |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 922 | #if ENABLE_PRINTF |
| 923 | BLTIN("printf" , builtin_printf , NULL), |
| 924 | #endif |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 925 | BLTIN("pwd" , builtin_pwd , NULL), |
| 926 | BLTIN("test" , builtin_test , NULL), |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 927 | }; |
| 928 | |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 929 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 930 | /* Debug printouts. |
| 931 | */ |
| 932 | #if HUSH_DEBUG |
| 933 | /* prevent disasters with G.debug_indent < 0 */ |
| 934 | # define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") |
| 935 | # define debug_enter() (G.debug_indent++) |
| 936 | # define debug_leave() (G.debug_indent--) |
| 937 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 938 | # define indent() ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 939 | # define debug_enter() ((void)0) |
| 940 | # define debug_leave() ((void)0) |
| 941 | #endif |
| 942 | |
| 943 | #ifndef debug_printf |
| 944 | # define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 945 | #endif |
| 946 | |
| 947 | #ifndef debug_printf_parse |
| 948 | # define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 949 | #endif |
| 950 | |
| 951 | #ifndef debug_printf_exec |
| 952 | #define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 953 | #endif |
| 954 | |
| 955 | #ifndef debug_printf_env |
| 956 | # define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 957 | #endif |
| 958 | |
| 959 | #ifndef debug_printf_jobs |
| 960 | # define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 961 | # define DEBUG_JOBS 1 |
| 962 | #else |
| 963 | # define DEBUG_JOBS 0 |
| 964 | #endif |
| 965 | |
| 966 | #ifndef debug_printf_expand |
| 967 | # define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 968 | # define DEBUG_EXPAND 1 |
| 969 | #else |
| 970 | # define DEBUG_EXPAND 0 |
| 971 | #endif |
| 972 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 973 | #ifndef debug_printf_varexp |
| 974 | # define debug_printf_varexp(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 975 | #endif |
| 976 | |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 977 | #ifndef debug_printf_glob |
| 978 | # define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 979 | # define DEBUG_GLOB 1 |
| 980 | #else |
| 981 | # define DEBUG_GLOB 0 |
| 982 | #endif |
| 983 | |
| 984 | #ifndef debug_printf_list |
| 985 | # define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 986 | #endif |
| 987 | |
| 988 | #ifndef debug_printf_subst |
| 989 | # define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 990 | #endif |
| 991 | |
| 992 | #ifndef debug_printf_clean |
| 993 | # define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) |
| 994 | # define DEBUG_CLEAN 1 |
| 995 | #else |
| 996 | # define DEBUG_CLEAN 0 |
| 997 | #endif |
| 998 | |
| 999 | #if DEBUG_EXPAND |
| 1000 | static void debug_print_strings(const char *prefix, char **vv) |
| 1001 | { |
| 1002 | indent(); |
| 1003 | fprintf(stderr, "%s:\n", prefix); |
| 1004 | while (*vv) |
| 1005 | fprintf(stderr, " '%s'\n", *vv++); |
| 1006 | } |
| 1007 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1008 | # define debug_print_strings(prefix, vv) ((void)0) |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 1009 | #endif |
| 1010 | |
| 1011 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1012 | /* Leak hunting. Use hush_leaktool.sh for post-processing. |
| 1013 | */ |
| 1014 | #if LEAK_HUNTING |
| 1015 | static void *xxmalloc(int lineno, size_t size) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1016 | { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1017 | void *ptr = xmalloc((size + 0xff) & ~0xff); |
| 1018 | fdprintf(2, "line %d: malloc %p\n", lineno, ptr); |
| 1019 | return ptr; |
| 1020 | } |
| 1021 | static void *xxrealloc(int lineno, void *ptr, size_t size) |
| 1022 | { |
| 1023 | ptr = xrealloc(ptr, (size + 0xff) & ~0xff); |
| 1024 | fdprintf(2, "line %d: realloc %p\n", lineno, ptr); |
| 1025 | return ptr; |
| 1026 | } |
| 1027 | static char *xxstrdup(int lineno, const char *str) |
| 1028 | { |
| 1029 | char *ptr = xstrdup(str); |
| 1030 | fdprintf(2, "line %d: strdup %p\n", lineno, ptr); |
| 1031 | return ptr; |
| 1032 | } |
| 1033 | static void xxfree(void *ptr) |
| 1034 | { |
| 1035 | fdprintf(2, "free %p\n", ptr); |
| 1036 | free(ptr); |
| 1037 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1038 | # define xmalloc(s) xxmalloc(__LINE__, s) |
| 1039 | # define xrealloc(p, s) xxrealloc(__LINE__, p, s) |
| 1040 | # define xstrdup(s) xxstrdup(__LINE__, s) |
| 1041 | # define free(p) xxfree(p) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1042 | #endif |
| 1043 | |
| 1044 | |
| 1045 | /* Syntax and runtime errors. They always abort scripts. |
| 1046 | * In interactive use they usually discard unparsed and/or unexecuted commands |
| 1047 | * and return to the prompt. |
| 1048 | * HUSH_DEBUG >= 2 prints line number in this file where it was detected. |
| 1049 | */ |
| 1050 | #if HUSH_DEBUG < 2 |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1051 | # define die_if_script(lineno, ...) die_if_script(__VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1052 | # define syntax_error(lineno, msg) syntax_error(msg) |
| 1053 | # define syntax_error_at(lineno, msg) syntax_error_at(msg) |
| 1054 | # define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch) |
| 1055 | # define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s) |
| 1056 | # define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1057 | #endif |
| 1058 | |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1059 | static void die_if_script(unsigned lineno, const char *fmt, ...) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1060 | { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1061 | va_list p; |
| 1062 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1063 | #if HUSH_DEBUG >= 2 |
| 1064 | bb_error_msg("hush.c:%u", lineno); |
| 1065 | #endif |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1066 | va_start(p, fmt); |
| 1067 | bb_verror_msg(fmt, p, NULL); |
| 1068 | va_end(p); |
| 1069 | if (!G_interactive_fd) |
| 1070 | xfunc_die(); |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1071 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1072 | |
| 1073 | static void syntax_error(unsigned lineno, const char *msg) |
| 1074 | { |
| 1075 | if (msg) |
| 1076 | die_if_script(lineno, "syntax error: %s", msg); |
| 1077 | else |
| 1078 | die_if_script(lineno, "syntax error", NULL); |
| 1079 | } |
| 1080 | |
| 1081 | static void syntax_error_at(unsigned lineno, const char *msg) |
| 1082 | { |
| 1083 | die_if_script(lineno, "syntax error at '%s'", msg); |
| 1084 | } |
| 1085 | |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1086 | static void syntax_error_unterm_str(unsigned lineno, const char *s) |
| 1087 | { |
| 1088 | die_if_script(lineno, "syntax error: unterminated %s", s); |
| 1089 | } |
| 1090 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1091 | /* It so happens that all such cases are totally fatal |
| 1092 | * even if shell is interactive: EOF while looking for closing |
| 1093 | * delimiter. There is nowhere to read stuff from after that, |
| 1094 | * it's EOF! The only choice is to terminate. |
| 1095 | */ |
| 1096 | static void syntax_error_unterm_ch(unsigned lineno, char ch) NORETURN; |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1097 | static void syntax_error_unterm_ch(unsigned lineno, char ch) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1098 | { |
Mike Frysinger | 6a46ab8 | 2009-06-01 14:14:36 -0400 | [diff] [blame] | 1099 | char msg[2] = { ch, '\0' }; |
| 1100 | syntax_error_unterm_str(lineno, msg); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 1101 | xfunc_die(); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1104 | static void syntax_error_unexpected_ch(unsigned lineno, int ch) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1105 | { |
| 1106 | char msg[2]; |
| 1107 | msg[0] = ch; |
| 1108 | msg[1] = '\0'; |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 1109 | die_if_script(lineno, "syntax error: unexpected %s", ch == EOF ? "EOF" : msg); |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1112 | #if HUSH_DEBUG < 2 |
| 1113 | # undef die_if_script |
| 1114 | # undef syntax_error |
| 1115 | # undef syntax_error_at |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 1116 | # undef syntax_error_unterm_ch |
| 1117 | # undef syntax_error_unterm_str |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1118 | # undef syntax_error_unexpected_ch |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1119 | #else |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 1120 | # define die_if_script(...) die_if_script(__LINE__, __VA_ARGS__) |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 1121 | # define syntax_error(msg) syntax_error(__LINE__, msg) |
| 1122 | # define syntax_error_at(msg) syntax_error_at(__LINE__, msg) |
| 1123 | # define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch) |
| 1124 | # define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s) |
| 1125 | # define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 1126 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 1127 | |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1128 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1129 | #if ENABLE_HUSH_INTERACTIVE |
| 1130 | static void cmdedit_update_prompt(void); |
| 1131 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1132 | # define cmdedit_update_prompt() ((void)0) |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1133 | #endif |
| 1134 | |
| 1135 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1136 | /* Utility functions |
| 1137 | */ |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1138 | /* Replace each \x with x in place, return ptr past NUL. */ |
| 1139 | static char *unbackslash(char *src) |
| 1140 | { |
Denys Vlasenko | 7188540 | 2009-09-24 01:44:13 +0200 | [diff] [blame] | 1141 | char *dst = src = strchrnul(src, '\\'); |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 1142 | while (1) { |
| 1143 | if (*src == '\\') |
| 1144 | src++; |
| 1145 | if ((*dst++ = *src++) == '\0') |
| 1146 | break; |
| 1147 | } |
| 1148 | return dst; |
| 1149 | } |
| 1150 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1151 | 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] | 1152 | { |
| 1153 | int i; |
| 1154 | unsigned count1; |
| 1155 | unsigned count2; |
| 1156 | char **v; |
| 1157 | |
| 1158 | v = strings; |
| 1159 | count1 = 0; |
| 1160 | if (v) { |
| 1161 | while (*v) { |
| 1162 | count1++; |
| 1163 | v++; |
| 1164 | } |
| 1165 | } |
| 1166 | count2 = 0; |
| 1167 | v = add; |
| 1168 | while (*v) { |
| 1169 | count2++; |
| 1170 | v++; |
| 1171 | } |
| 1172 | v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*)); |
| 1173 | v[count1 + count2] = NULL; |
| 1174 | i = count2; |
| 1175 | while (--i >= 0) |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1176 | v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1177 | return v; |
| 1178 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1179 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1180 | static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup) |
| 1181 | { |
| 1182 | char **ptr = add_strings_to_strings(strings, add, need_to_dup); |
| 1183 | fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr); |
| 1184 | return ptr; |
| 1185 | } |
| 1186 | #define add_strings_to_strings(strings, add, need_to_dup) \ |
| 1187 | xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup) |
| 1188 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1189 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1190 | /* Note: takes ownership of "add" ptr (it is not strdup'ed) */ |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1191 | static char **add_string_to_strings(char **strings, char *add) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1192 | { |
| 1193 | char *v[2]; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1194 | v[0] = add; |
| 1195 | v[1] = NULL; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 1196 | return add_strings_to_strings(strings, v, /*dup:*/ 0); |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 1197 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 1198 | #if LEAK_HUNTING |
Denis Vlasenko | cc90f44 | 2009-04-08 16:40:34 +0000 | [diff] [blame] | 1199 | static char **xx_add_string_to_strings(int lineno, char **strings, char *add) |
| 1200 | { |
| 1201 | char **ptr = add_string_to_strings(strings, add); |
| 1202 | fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr); |
| 1203 | return ptr; |
| 1204 | } |
| 1205 | #define add_string_to_strings(strings, add) \ |
| 1206 | xx_add_string_to_strings(__LINE__, strings, add) |
| 1207 | #endif |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1208 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1209 | static void free_strings(char **strings) |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1210 | { |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1211 | char **v; |
| 1212 | |
| 1213 | if (!strings) |
| 1214 | return; |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1215 | v = strings; |
| 1216 | while (*v) { |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1217 | free(*v); |
| 1218 | v++; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1219 | } |
Denis Vlasenko | afd7a8d | 2008-10-09 16:29:44 +0000 | [diff] [blame] | 1220 | free(strings); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Denis Vlasenko | 76d5041 | 2008-06-10 16:19:39 +0000 | [diff] [blame] | 1223 | |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 1224 | /* Helpers for setting new $n and restoring them back |
| 1225 | */ |
| 1226 | typedef struct save_arg_t { |
| 1227 | char *sv_argv0; |
| 1228 | char **sv_g_argv; |
| 1229 | int sv_g_argc; |
| 1230 | smallint sv_g_malloced; |
| 1231 | } save_arg_t; |
| 1232 | |
| 1233 | static void save_and_replace_G_args(save_arg_t *sv, char **argv) |
| 1234 | { |
| 1235 | int n; |
| 1236 | |
| 1237 | sv->sv_argv0 = argv[0]; |
| 1238 | sv->sv_g_argv = G.global_argv; |
| 1239 | sv->sv_g_argc = G.global_argc; |
| 1240 | sv->sv_g_malloced = G.global_args_malloced; |
| 1241 | |
| 1242 | argv[0] = G.global_argv[0]; /* retain $0 */ |
| 1243 | G.global_argv = argv; |
| 1244 | G.global_args_malloced = 0; |
| 1245 | |
| 1246 | n = 1; |
| 1247 | while (*++argv) |
| 1248 | n++; |
| 1249 | G.global_argc = n; |
| 1250 | } |
| 1251 | |
| 1252 | static void restore_G_args(save_arg_t *sv, char **argv) |
| 1253 | { |
| 1254 | char **pp; |
| 1255 | |
| 1256 | if (G.global_args_malloced) { |
| 1257 | /* someone ran "set -- arg1 arg2 ...", undo */ |
| 1258 | pp = G.global_argv; |
| 1259 | while (*++pp) /* note: does not free $0 */ |
| 1260 | free(*pp); |
| 1261 | free(G.global_argv); |
| 1262 | } |
| 1263 | argv[0] = sv->sv_argv0; |
| 1264 | G.global_argv = sv->sv_g_argv; |
| 1265 | G.global_argc = sv->sv_g_argc; |
| 1266 | G.global_args_malloced = sv->sv_g_malloced; |
| 1267 | } |
| 1268 | |
| 1269 | |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1270 | /* Basic theory of signal handling in shell |
| 1271 | * ======================================== |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1272 | * This does not describe what hush does, rather, it is current understanding |
| 1273 | * what it _should_ do. If it doesn't, it's a bug. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1274 | * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap |
| 1275 | * |
| 1276 | * Signals are handled only after each pipe ("cmd | cmd | cmd" thing) |
| 1277 | * is finished or backgrounded. It is the same in interactive and |
| 1278 | * non-interactive shells, and is the same regardless of whether |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1279 | * 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] | 1280 | * ^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] | 1281 | * backgrounds (i.e. stops) or kills all members of currently running |
| 1282 | * pipe. |
| 1283 | * |
| 1284 | * Wait builtin in interruptible by signals for which user trap is set |
| 1285 | * or by SIGINT in interactive shell. |
| 1286 | * |
| 1287 | * Trap handlers will execute even within trap handlers. (right?) |
| 1288 | * |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1289 | * User trap handlers are forgotten when subshell ("(cmd)") is entered, |
| 1290 | * except for handlers set to '' (empty string). |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1291 | * |
| 1292 | * If job control is off, backgrounded commands ("cmd &") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1293 | * have SIGINT, SIGQUIT set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1294 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1295 | * Commands which are run in command substitution ("`cmd`") |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1296 | * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1297 | * |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 1298 | * Ordinary commands have signals set to SIG_IGN/DFL as inherited |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1299 | * by the shell from its parent. |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1300 | * |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 1301 | * Signals which differ from SIG_DFL action |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1302 | * (note: child (i.e., [v]forked) shell is not an interactive shell): |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1303 | * |
| 1304 | * SIGQUIT: ignore |
| 1305 | * SIGTERM (interactive): ignore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1306 | * SIGHUP (interactive): |
| 1307 | * send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1308 | * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore |
Denis Vlasenko | c4ada79 | 2009-04-15 23:29:00 +0000 | [diff] [blame] | 1309 | * Note that ^Z is handled not by trapping SIGTSTP, but by seeing |
| 1310 | * that all pipe members are stopped. Try this in bash: |
| 1311 | * while :; do :; done - ^Z does not background it |
| 1312 | * (while :; do :; done) - ^Z backgrounds it |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1313 | * SIGINT (interactive): wait for last pipe, ignore the rest |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1314 | * of the command line, show prompt. NB: ^C does not send SIGINT |
| 1315 | * to interactive shell while shell is waiting for a pipe, |
| 1316 | * since shell is bg'ed (is not in foreground process group). |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1317 | * Example 1: this waits 5 sec, but does not execute ls: |
| 1318 | * "echo $$; sleep 5; ls -l" + "kill -INT <pid>" |
| 1319 | * Example 2: this does not wait and does not execute ls: |
| 1320 | * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>" |
| 1321 | * Example 3: this does not wait 5 sec, but executes ls: |
| 1322 | * "sleep 5; ls -l" + press ^C |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1323 | * |
| 1324 | * (What happens to signals which are IGN on shell start?) |
| 1325 | * (What happens with signal mask on shell start?) |
| 1326 | * |
| 1327 | * Implementation in hush |
| 1328 | * ====================== |
| 1329 | * We use in-kernel pending signal mask to determine which signals were sent. |
| 1330 | * We block all signals which we don't want to take action immediately, |
| 1331 | * i.e. we block all signals which need to have special handling as described |
| 1332 | * above, and all signals which have traps set. |
| 1333 | * After each pipe execution, we extract any pending signals via sigtimedwait() |
| 1334 | * and act on them. |
| 1335 | * |
| 1336 | * unsigned non_DFL_mask: a mask of such "special" signals |
| 1337 | * sigset_t blocked_set: current blocked signal set |
| 1338 | * |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1339 | * "trap - SIGxxx": |
Denis Vlasenko | 552433b | 2009-04-04 19:29:21 +0000 | [diff] [blame] | 1340 | * 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] | 1341 | * "trap 'cmd' SIGxxx": |
| 1342 | * set bit in blocked_set (even if 'cmd' is '') |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1343 | * after [v]fork, if we plan to be a shell: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1344 | * unblock signals with special interactive handling |
| 1345 | * (child shell is not interactive), |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 1346 | * unset all traps except '' (note: regardless of child shell's type - {}, (), etc) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1347 | * after [v]fork, if we plan to exec: |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 1348 | * 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] | 1349 | * 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] | 1350 | * |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1351 | * 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] | 1352 | * are to count SIGCHLDs |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1353 | * and to restore tty pgrp on signal-induced exit. |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1354 | * |
Denys Vlasenko | 67f7186 | 2009-09-25 14:21:06 +0200 | [diff] [blame] | 1355 | * Note 2 (compat): |
Denys Vlasenko | 4ea0ca8 | 2009-09-25 12:58:37 +0200 | [diff] [blame] | 1356 | * Standard says "When a subshell is entered, traps that are not being ignored |
| 1357 | * 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] | 1358 | * 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] | 1359 | */ |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1360 | enum { |
| 1361 | SPECIAL_INTERACTIVE_SIGS = 0 |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1362 | | (1 << SIGTERM) |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1363 | | (1 << SIGINT) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1364 | | (1 << SIGHUP) |
| 1365 | , |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1366 | SPECIAL_JOB_SIGS = 0 |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1367 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 1368 | | (1 << SIGTTIN) |
| 1369 | | (1 << SIGTTOU) |
| 1370 | | (1 << SIGTSTP) |
| 1371 | #endif |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 1372 | }; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1373 | |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1374 | #if ENABLE_HUSH_FAST |
| 1375 | static void SIGCHLD_handler(int sig UNUSED_PARAM) |
| 1376 | { |
| 1377 | G.count_SIGCHLD++; |
| 1378 | //bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1379 | } |
| 1380 | #endif |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1381 | |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 1382 | #if ENABLE_HUSH_JOB |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1383 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1384 | /* 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] | 1385 | # define disable_restore_tty_pgrp_on_exit() (die_sleep = 0) |
Denis Vlasenko | 25af86f | 2009-04-07 13:29:27 +0000 | [diff] [blame] | 1386 | /* After [v]fork, in parent: restore tty pgrp on xfunc death */ |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1387 | # define enable_restore_tty_pgrp_on_exit() (die_sleep = -1) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1388 | |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1389 | /* Restores tty foreground process group, and exits. |
| 1390 | * May be called as signal handler for fatal signal |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1391 | * (will resend signal to itself, producing correct exit state) |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1392 | * or called directly with -EXITCODE. |
| 1393 | * We also call it if xfunc is exiting. */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1394 | static void sigexit(int sig) NORETURN; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1395 | static void sigexit(int sig) |
| 1396 | { |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1397 | /* Disable all signals: job control, SIGPIPE, etc. */ |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 1398 | sigprocmask_allsigs(SIG_BLOCK); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1399 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1400 | /* Careful: we can end up here after [v]fork. Do not restore |
Denis Vlasenko | 7b830e7 | 2009-03-31 13:05:32 +0000 | [diff] [blame] | 1401 | * tty pgrp then, only top-level shell process does that */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 1402 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 1403 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1404 | |
| 1405 | /* Not a signal, just exit */ |
| 1406 | if (sig <= 0) |
| 1407 | _exit(- sig); |
| 1408 | |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 1409 | kill_myself_with_sig(sig); /* does not return */ |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 1410 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1411 | #else |
| 1412 | |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1413 | # define disable_restore_tty_pgrp_on_exit() ((void)0) |
| 1414 | # define enable_restore_tty_pgrp_on_exit() ((void)0) |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 1415 | |
Denis Vlasenko | e0755e5 | 2009-04-03 21:16:45 +0000 | [diff] [blame] | 1416 | #endif |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1417 | |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1418 | /* Restores tty foreground process group, and exits. */ |
| 1419 | static void hush_exit(int exitcode) NORETURN; |
| 1420 | static void hush_exit(int exitcode) |
| 1421 | { |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 1422 | if (G.exiting <= 0 && G.traps && G.traps[0] && G.traps[0][0]) { |
| 1423 | /* Prevent recursion: |
| 1424 | * trap "echo Hi; exit" EXIT; exit |
| 1425 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1426 | char *argv[3]; |
| 1427 | /* argv[0] is unused */ |
| 1428 | argv[1] = G.traps[0]; |
| 1429 | argv[2] = NULL; |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1430 | G.exiting = 1; /* prevent EXIT trap recursion */ |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 1431 | /* Note: G.traps[0] is not cleared! |
Denys Vlasenko | de8c3f6 | 2010-09-12 16:13:44 +0200 | [diff] [blame] | 1432 | * "trap" will still show it, if executed |
| 1433 | * in the handler */ |
| 1434 | builtin_eval(argv); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 1435 | } |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1436 | |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1437 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1438 | fflush_all(); |
Denis Vlasenko | abedaac | 2009-03-31 12:03:40 +0000 | [diff] [blame] | 1439 | sigexit(- (exitcode & 0xff)); |
| 1440 | #else |
| 1441 | exit(exitcode); |
| 1442 | #endif |
Mike Frysinger | 9f8128f | 2009-03-29 23:49:37 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1445 | |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1446 | static int check_and_run_traps(int sig) |
| 1447 | { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 1448 | /* I want it in rodata, not in bss. |
| 1449 | * gcc 4.2.1 puts it in rodata only if it has { 0, 0 } |
| 1450 | * initializer. But other compilers may still use bss. |
| 1451 | * TODO: find more portable solution. |
| 1452 | */ |
| 1453 | static const struct timespec zero_timespec = { 0, 0 }; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1454 | smalluint save_rcode; |
| 1455 | int last_sig = 0; |
| 1456 | |
| 1457 | if (sig) |
| 1458 | goto jump_in; |
| 1459 | while (1) { |
| 1460 | sig = sigtimedwait(&G.blocked_set, NULL, &zero_timespec); |
| 1461 | if (sig <= 0) |
| 1462 | break; |
| 1463 | jump_in: |
| 1464 | last_sig = sig; |
| 1465 | if (G.traps && G.traps[sig]) { |
| 1466 | if (G.traps[sig][0]) { |
| 1467 | /* We have user-defined handler */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1468 | char *argv[3]; |
| 1469 | /* argv[0] is unused */ |
| 1470 | argv[1] = G.traps[sig]; |
| 1471 | argv[2] = NULL; |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1472 | save_rcode = G.last_exitcode; |
| 1473 | builtin_eval(argv); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1474 | G.last_exitcode = save_rcode; |
| 1475 | } /* else: "" trap, ignoring signal */ |
| 1476 | continue; |
| 1477 | } |
| 1478 | /* not a trap: special action */ |
| 1479 | switch (sig) { |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 1480 | #if ENABLE_HUSH_FAST |
| 1481 | case SIGCHLD: |
| 1482 | G.count_SIGCHLD++; |
| 1483 | //bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 1484 | break; |
| 1485 | #endif |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1486 | case SIGINT: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 1487 | /* Builtin was ^C'ed, make it look prettier: */ |
| 1488 | bb_putchar('\n'); |
| 1489 | G.flag_SIGINT = 1; |
| 1490 | break; |
| 1491 | #if ENABLE_HUSH_JOB |
| 1492 | case SIGHUP: { |
| 1493 | struct pipe *job; |
| 1494 | /* bash is observed to signal whole process groups, |
| 1495 | * not individual processes */ |
| 1496 | for (job = G.job_list; job; job = job->next) { |
| 1497 | if (job->pgrp <= 0) |
| 1498 | continue; |
| 1499 | debug_printf_exec("HUPing pgrp %d\n", job->pgrp); |
| 1500 | if (kill(- job->pgrp, SIGHUP) == 0) |
| 1501 | kill(- job->pgrp, SIGCONT); |
| 1502 | } |
| 1503 | sigexit(SIGHUP); |
| 1504 | } |
| 1505 | #endif |
| 1506 | default: /* ignored: */ |
| 1507 | /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */ |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | return last_sig; |
| 1512 | } |
| 1513 | |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 1514 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1515 | static const char *get_cwd(int force) |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1516 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1517 | if (force || G.cwd == NULL) { |
| 1518 | /* xrealloc_getcwd_or_warn(arg) calls free(arg), |
| 1519 | * we must not try to free(bb_msg_unknown) */ |
| 1520 | if (G.cwd == bb_msg_unknown) |
| 1521 | G.cwd = NULL; |
| 1522 | G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd); |
| 1523 | if (!G.cwd) |
| 1524 | G.cwd = bb_msg_unknown; |
| 1525 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 1526 | return G.cwd; |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Denis Vlasenko | 8350686 | 2007-11-23 13:11:42 +0000 | [diff] [blame] | 1529 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1530 | /* |
| 1531 | * Shell and environment variable support |
| 1532 | */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1533 | 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] | 1534 | { |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1535 | struct variable **pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1536 | struct variable *cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1537 | |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1538 | pp = &G.top_var; |
| 1539 | while ((cur = *pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1540 | if (strncmp(cur->varstr, name, len) == 0 && cur->varstr[len] == '=') |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1541 | return pp; |
| 1542 | pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1543 | } |
| 1544 | return NULL; |
| 1545 | } |
| 1546 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1547 | static const char* FAST_FUNC get_local_var_value(const char *name) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1548 | { |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1549 | struct variable **vpp; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1550 | unsigned len = strlen(name); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1551 | |
| 1552 | if (G.expanded_assignments) { |
| 1553 | char **cpp = G.expanded_assignments; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1554 | while (*cpp) { |
| 1555 | char *cp = *cpp; |
| 1556 | if (strncmp(cp, name, len) == 0 && cp[len] == '=') |
| 1557 | return cp + len + 1; |
| 1558 | cpp++; |
| 1559 | } |
| 1560 | } |
| 1561 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1562 | vpp = get_ptr_to_local_var(name, len); |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1563 | if (vpp) |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1564 | return (*vpp)->varstr + len + 1; |
Denys Vlasenko | 2908223 | 2010-07-16 13:52:32 +0200 | [diff] [blame] | 1565 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 1566 | if (strcmp(name, "PPID") == 0) |
| 1567 | return utoa(G.root_ppid); |
| 1568 | // bash compat: UID? EUID? |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1569 | #if ENABLE_HUSH_RANDOM_SUPPORT |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1570 | if (strcmp(name, "RANDOM") == 0) |
Denys Vlasenko | 20b3d14 | 2009-10-09 20:59:39 +0200 | [diff] [blame] | 1571 | return utoa(next_random(&G.random_gen)); |
| 1572 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1573 | return NULL; |
| 1574 | } |
| 1575 | |
| 1576 | /* str holds "NAME=VAL" and is expected to be malloced. |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1577 | * We take ownership of it. |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1578 | * flg_export: |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1579 | * 0: do not change export flag |
| 1580 | * (if creating new variable, flag will be 0) |
| 1581 | * 1: set export flag and putenv the variable |
| 1582 | * -1: clear export flag and unsetenv the variable |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1583 | * flg_read_only is set only when we handle -R var=val |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1584 | */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1585 | #if !BB_MMU && ENABLE_HUSH_LOCAL |
| 1586 | /* all params are used */ |
| 1587 | #elif BB_MMU && ENABLE_HUSH_LOCAL |
| 1588 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1589 | set_local_var(str, flg_export, local_lvl) |
| 1590 | #elif BB_MMU && !ENABLE_HUSH_LOCAL |
| 1591 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1592 | set_local_var(str, flg_export) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1593 | #elif !BB_MMU && !ENABLE_HUSH_LOCAL |
| 1594 | #define set_local_var(str, flg_export, local_lvl, flg_read_only) \ |
| 1595 | set_local_var(str, flg_export, flg_read_only) |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1596 | #endif |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1597 | 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] | 1598 | { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1599 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1600 | struct variable *cur; |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1601 | char *eq_sign; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1602 | int name_len; |
| 1603 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1604 | eq_sign = strchr(str, '='); |
| 1605 | if (!eq_sign) { /* not expected to ever happen? */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1606 | free(str); |
| 1607 | return -1; |
| 1608 | } |
| 1609 | |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1610 | name_len = eq_sign - str + 1; /* including '=' */ |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1611 | var_pp = &G.top_var; |
| 1612 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1613 | if (strncmp(cur->varstr, str, name_len) != 0) { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1614 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1615 | continue; |
| 1616 | } |
| 1617 | /* We found an existing var with this name */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1618 | if (cur->flg_read_only) { |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1619 | #if !BB_MMU |
| 1620 | if (!flg_read_only) |
| 1621 | #endif |
| 1622 | bb_error_msg("%s: readonly variable", str); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1623 | free(str); |
| 1624 | return -1; |
| 1625 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1626 | if (flg_export == -1) { // "&& cur->flg_export" ? |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1627 | debug_printf_env("%s: unsetenv '%s'\n", __func__, str); |
| 1628 | *eq_sign = '\0'; |
| 1629 | unsetenv(str); |
| 1630 | *eq_sign = '='; |
| 1631 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1632 | #if ENABLE_HUSH_LOCAL |
| 1633 | if (cur->func_nest_level < local_lvl) { |
| 1634 | /* New variable is declared as local, |
| 1635 | * and existing one is global, or local |
| 1636 | * from enclosing function. |
| 1637 | * Remove and save old one: */ |
| 1638 | *var_pp = cur->next; |
| 1639 | cur->next = *G.shadowed_vars_pp; |
| 1640 | *G.shadowed_vars_pp = cur; |
| 1641 | /* bash 3.2.33(1) and exported vars: |
| 1642 | * # export z=z |
| 1643 | * # f() { local z=a; env | grep ^z; } |
| 1644 | * # f |
| 1645 | * z=a |
| 1646 | * # env | grep ^z |
| 1647 | * z=z |
| 1648 | */ |
| 1649 | if (cur->flg_export) |
| 1650 | flg_export = 1; |
| 1651 | break; |
| 1652 | } |
| 1653 | #endif |
Denis Vlasenko | 950bd72 | 2009-04-21 11:23:56 +0000 | [diff] [blame] | 1654 | if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1655 | free_and_exp: |
| 1656 | free(str); |
| 1657 | goto exp; |
| 1658 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1659 | if (cur->max_len != 0) { |
| 1660 | if (cur->max_len >= strlen(str)) { |
| 1661 | /* This one is from startup env, reuse space */ |
| 1662 | strcpy(cur->varstr, str); |
| 1663 | goto free_and_exp; |
| 1664 | } |
| 1665 | } else { |
| 1666 | /* max_len == 0 signifies "malloced" var, which we can |
| 1667 | * (and has to) free */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1668 | free(cur->varstr); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1669 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1670 | cur->max_len = 0; |
| 1671 | goto set_str_and_exp; |
| 1672 | } |
| 1673 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1674 | /* Not found - create new variable struct */ |
| 1675 | cur = xzalloc(sizeof(*cur)); |
| 1676 | #if ENABLE_HUSH_LOCAL |
| 1677 | cur->func_nest_level = local_lvl; |
| 1678 | #endif |
| 1679 | cur->next = *var_pp; |
| 1680 | *var_pp = cur; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1681 | |
| 1682 | set_str_and_exp: |
| 1683 | cur->varstr = str; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1684 | #if !BB_MMU |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 1685 | cur->flg_read_only = flg_read_only; |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 1686 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1687 | exp: |
Mike Frysinger | 6379bb4 | 2009-03-28 18:55:03 +0000 | [diff] [blame] | 1688 | if (flg_export == 1) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1689 | cur->flg_export = 1; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1690 | if (name_len == 4 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1691 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1692 | if (cur->flg_export) { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 1693 | if (flg_export == -1) { |
| 1694 | cur->flg_export = 0; |
| 1695 | /* unsetenv was already done */ |
| 1696 | } else { |
| 1697 | debug_printf_env("%s: putenv '%s'\n", __func__, cur->varstr); |
| 1698 | return putenv(cur->varstr); |
| 1699 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1700 | } |
| 1701 | return 0; |
| 1702 | } |
| 1703 | |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1704 | /* Used at startup and after each cd */ |
| 1705 | static void set_pwd_var(int exp) |
| 1706 | { |
| 1707 | set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), |
| 1708 | /*exp:*/ exp, /*lvl:*/ 0, /*ro:*/ 0); |
| 1709 | } |
| 1710 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1711 | static int unset_local_var_len(const char *name, int name_len) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1712 | { |
| 1713 | struct variable *cur; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1714 | struct variable **var_pp; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1715 | |
| 1716 | if (!name) |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1717 | return EXIT_SUCCESS; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1718 | var_pp = &G.top_var; |
| 1719 | while ((cur = *var_pp) != NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1720 | if (strncmp(cur->varstr, name, name_len) == 0 && cur->varstr[name_len] == '=') { |
| 1721 | if (cur->flg_read_only) { |
| 1722 | bb_error_msg("%s: readonly variable", name); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1723 | return EXIT_FAILURE; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1724 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1725 | *var_pp = cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1726 | debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr); |
| 1727 | bb_unsetenv(cur->varstr); |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1728 | if (name_len == 3 && cur->varstr[0] == 'P' && cur->varstr[1] == 'S') |
| 1729 | cmdedit_update_prompt(); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1730 | if (!cur->max_len) |
| 1731 | free(cur->varstr); |
| 1732 | free(cur); |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1733 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1734 | } |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1735 | var_pp = &cur->next; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1736 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 1737 | return EXIT_SUCCESS; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1740 | static int unset_local_var(const char *name) |
| 1741 | { |
| 1742 | return unset_local_var_len(name, strlen(name)); |
| 1743 | } |
| 1744 | |
| 1745 | static void unset_vars(char **strings) |
| 1746 | { |
| 1747 | char **v; |
| 1748 | |
| 1749 | if (!strings) |
| 1750 | return; |
| 1751 | v = strings; |
| 1752 | while (*v) { |
| 1753 | const char *eq = strchrnul(*v, '='); |
| 1754 | unset_local_var_len(*v, (int)(eq - *v)); |
| 1755 | v++; |
| 1756 | } |
| 1757 | free(strings); |
| 1758 | } |
| 1759 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1760 | 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] | 1761 | { |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1762 | char *var = xasprintf("%s=%s", name, val); |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 1763 | set_local_var(var, /*flags:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 1764 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1765 | |
Denis Vlasenko | b29eb6e | 2009-04-02 13:46:27 +0000 | [diff] [blame] | 1766 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1767 | /* |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1768 | * Helpers for "var1=val1 var2=val2 cmd" feature |
| 1769 | */ |
| 1770 | static void add_vars(struct variable *var) |
| 1771 | { |
| 1772 | struct variable *next; |
| 1773 | |
| 1774 | while (var) { |
| 1775 | next = var->next; |
| 1776 | var->next = G.top_var; |
| 1777 | G.top_var = var; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1778 | if (var->flg_export) { |
| 1779 | debug_printf_env("%s: restoring exported '%s'\n", __func__, var->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1780 | putenv(var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1781 | } else { |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1782 | debug_printf_env("%s: restoring variable '%s'\n", __func__, var->varstr); |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1783 | } |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1784 | var = next; |
| 1785 | } |
| 1786 | } |
| 1787 | |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1788 | static struct variable *set_vars_and_save_old(char **strings) |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1789 | { |
| 1790 | char **s; |
| 1791 | struct variable *old = NULL; |
| 1792 | |
| 1793 | if (!strings) |
| 1794 | return old; |
| 1795 | s = strings; |
| 1796 | while (*s) { |
| 1797 | struct variable *var_p; |
| 1798 | struct variable **var_pp; |
| 1799 | char *eq; |
| 1800 | |
| 1801 | eq = strchr(*s, '='); |
| 1802 | if (eq) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 1803 | var_pp = get_ptr_to_local_var(*s, eq - *s); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1804 | if (var_pp) { |
| 1805 | /* Remove variable from global linked list */ |
| 1806 | var_p = *var_pp; |
Denys Vlasenko | acdc49c | 2009-05-04 01:58:10 +0200 | [diff] [blame] | 1807 | debug_printf_env("%s: removing '%s'\n", __func__, var_p->varstr); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1808 | *var_pp = var_p->next; |
| 1809 | /* Add it to returned list */ |
| 1810 | var_p->next = old; |
| 1811 | old = var_p; |
| 1812 | } |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 1813 | set_local_var(*s, /*exp:*/ 1, /*lvl:*/ 0, /*ro:*/ 0); |
Denys Vlasenko | cb6ff25 | 2009-05-04 00:14:30 +0200 | [diff] [blame] | 1814 | } |
| 1815 | s++; |
| 1816 | } |
| 1817 | return old; |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | /* |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1822 | * in_str support |
| 1823 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1824 | static int FAST_FUNC static_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1825 | { |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1826 | int ch = *i->p; |
| 1827 | if (ch != '\0') { |
| 1828 | i->p++; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1829 | return ch; |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1830 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 1831 | return EOF; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1834 | static int FAST_FUNC static_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1835 | { |
| 1836 | return *i->p; |
| 1837 | } |
| 1838 | |
| 1839 | #if ENABLE_HUSH_INTERACTIVE |
| 1840 | |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1841 | static void cmdedit_update_prompt(void) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1842 | { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1843 | if (ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1844 | G.PS1 = get_local_var_value("PS1"); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1845 | if (G.PS1 == NULL) |
| 1846 | G.PS1 = "\\w \\$ "; |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1847 | G.PS2 = get_local_var_value("PS2"); |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1848 | } else { |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1849 | G.PS1 = NULL; |
Denys Vlasenko | 690ad24 | 2009-04-30 21:24:24 +0200 | [diff] [blame] | 1850 | } |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 1851 | if (G.PS2 == NULL) |
| 1852 | G.PS2 = "> "; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1853 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1854 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 1855 | static const char *setup_prompt_string(int promptmode) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1856 | { |
| 1857 | const char *prompt_str; |
| 1858 | debug_printf("setup_prompt_string %d ", promptmode); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1859 | if (!ENABLE_FEATURE_EDITING_FANCY_PROMPT) { |
| 1860 | /* Set up the prompt */ |
| 1861 | if (promptmode == 0) { /* PS1 */ |
| 1862 | free((char*)G.PS1); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 1863 | /* bash uses $PWD value, even if it is set by user. |
| 1864 | * It uses current dir only if PWD is unset. |
| 1865 | * We always use current dir. */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 1866 | G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#'); |
Mike Frysinger | ec2c655 | 2009-03-28 12:24:44 +0000 | [diff] [blame] | 1867 | prompt_str = G.PS1; |
| 1868 | } else |
| 1869 | prompt_str = G.PS2; |
| 1870 | } else |
| 1871 | prompt_str = (promptmode == 0) ? G.PS1 : G.PS2; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1872 | debug_printf("result '%s'\n", prompt_str); |
| 1873 | return prompt_str; |
| 1874 | } |
| 1875 | |
| 1876 | static void get_user_input(struct in_str *i) |
| 1877 | { |
| 1878 | int r; |
| 1879 | const char *prompt_str; |
| 1880 | |
| 1881 | prompt_str = setup_prompt_string(i->promptmode); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1882 | # if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1883 | /* Enable command line editing only while a command line |
| 1884 | * is actually being read */ |
| 1885 | do { |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1886 | G.flag_SIGINT = 0; |
| 1887 | /* buglet: SIGINT will not make new prompt to appear _at once_, |
| 1888 | * only after <Enter>. (^C will work) */ |
Denys Vlasenko | aaa22d2 | 2009-10-19 16:34:39 +0200 | [diff] [blame] | 1889 | 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] | 1890 | /* catch *SIGINT* etc (^C is handled by read_line_input) */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1891 | check_and_run_traps(0); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1892 | } while (r == 0 || G.flag_SIGINT); /* repeat if ^C or SIGINT */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1893 | i->eof_flag = (r < 0); |
| 1894 | if (i->eof_flag) { /* EOF/error detected */ |
| 1895 | G.user_input_buf[0] = EOF; /* yes, it will be truncated, it's ok */ |
| 1896 | G.user_input_buf[1] = '\0'; |
| 1897 | } |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1898 | # else |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1899 | do { |
| 1900 | G.flag_SIGINT = 0; |
| 1901 | fputs(prompt_str, stdout); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1902 | fflush_all(); |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1903 | G.user_input_buf[0] = r = fgetc(i->file); |
| 1904 | /*G.user_input_buf[1] = '\0'; - already is and never changed */ |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 1905 | //do we need check_and_run_traps(0)? (maybe only if stdin) |
Denis Vlasenko | 422cd7c | 2009-03-31 12:41:52 +0000 | [diff] [blame] | 1906 | } while (G.flag_SIGINT); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1907 | i->eof_flag = (r == EOF); |
Denys Vlasenko | 8391c48 | 2010-05-22 17:50:43 +0200 | [diff] [blame] | 1908 | # endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1909 | i->p = G.user_input_buf; |
| 1910 | } |
| 1911 | |
| 1912 | #endif /* INTERACTIVE */ |
| 1913 | |
| 1914 | /* This is the magic location that prints prompts |
| 1915 | * and gets data back from the user */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1916 | static int FAST_FUNC file_get(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1917 | { |
| 1918 | int ch; |
| 1919 | |
| 1920 | /* If there is data waiting, eat it up */ |
| 1921 | if (i->p && *i->p) { |
| 1922 | #if ENABLE_HUSH_INTERACTIVE |
| 1923 | take_cached: |
| 1924 | #endif |
| 1925 | ch = *i->p++; |
| 1926 | if (i->eof_flag && !*i->p) |
| 1927 | ch = EOF; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1928 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1929 | } else { |
| 1930 | /* need to double check i->file because we might be doing something |
| 1931 | * more complicated by now, like sourcing or substituting. */ |
| 1932 | #if ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 1933 | if (G_interactive_fd && i->promptme && i->file == stdin) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1934 | do { |
| 1935 | get_user_input(i); |
| 1936 | } while (!*i->p); /* need non-empty line */ |
| 1937 | i->promptmode = 1; /* PS2 */ |
| 1938 | i->promptme = 0; |
| 1939 | goto take_cached; |
| 1940 | } |
| 1941 | #endif |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1942 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1943 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1944 | debug_printf("file_get: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1945 | #if ENABLE_HUSH_INTERACTIVE |
| 1946 | if (ch == '\n') |
| 1947 | i->promptme = 1; |
| 1948 | #endif |
| 1949 | return ch; |
| 1950 | } |
| 1951 | |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1952 | /* All callers guarantee this routine will never |
| 1953 | * be used right after a newline, so prompting is not needed. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1954 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 1955 | static int FAST_FUNC file_peek(struct in_str *i) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1956 | { |
| 1957 | int ch; |
| 1958 | if (i->p && *i->p) { |
| 1959 | if (i->eof_flag && !i->p[1]) |
| 1960 | return EOF; |
| 1961 | return *i->p; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1962 | /* note: ch is never NUL */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1963 | } |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1964 | do ch = fgetc(i->file); while (ch == '\0'); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1965 | i->eof_flag = (ch == EOF); |
| 1966 | i->peek_buf[0] = ch; |
| 1967 | i->peek_buf[1] = '\0'; |
| 1968 | i->p = i->peek_buf; |
Denis Vlasenko | 913a201 | 2009-04-05 22:17:04 +0000 | [diff] [blame] | 1969 | debug_printf("file_peek: got '%c' %d\n", ch, ch); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 1970 | return ch; |
| 1971 | } |
| 1972 | |
| 1973 | static void setup_file_in_str(struct in_str *i, FILE *f) |
| 1974 | { |
| 1975 | i->peek = file_peek; |
| 1976 | i->get = file_get; |
| 1977 | #if ENABLE_HUSH_INTERACTIVE |
| 1978 | i->promptme = 1; |
| 1979 | i->promptmode = 0; /* PS1 */ |
| 1980 | #endif |
| 1981 | i->file = f; |
| 1982 | i->p = NULL; |
| 1983 | } |
| 1984 | |
| 1985 | static void setup_string_in_str(struct in_str *i, const char *s) |
| 1986 | { |
| 1987 | i->peek = static_peek; |
| 1988 | i->get = static_get; |
| 1989 | #if ENABLE_HUSH_INTERACTIVE |
| 1990 | i->promptme = 1; |
| 1991 | i->promptmode = 0; /* PS1 */ |
| 1992 | #endif |
| 1993 | i->p = s; |
| 1994 | i->eof_flag = 0; |
| 1995 | } |
| 1996 | |
| 1997 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 1998 | /* |
| 1999 | * o_string support |
| 2000 | */ |
| 2001 | #define B_CHUNK (32 * sizeof(char*)) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2002 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 2003 | static void o_reset_to_empty_unquoted(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2004 | { |
| 2005 | o->length = 0; |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2006 | o->has_quoted_part = 0; |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2007 | if (o->data) |
| 2008 | o->data[0] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2011 | static void o_free(o_string *o) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2012 | { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 2013 | free(o->data); |
Denis Vlasenko | d65ea39 | 2007-10-01 10:02:25 +0000 | [diff] [blame] | 2014 | memset(o, 0, sizeof(*o)); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2017 | static ALWAYS_INLINE void o_free_unsafe(o_string *o) |
| 2018 | { |
| 2019 | free(o->data); |
| 2020 | } |
| 2021 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2022 | static void o_grow_by(o_string *o, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2023 | { |
| 2024 | if (o->length + len > o->maxlen) { |
| 2025 | o->maxlen += (2*len > B_CHUNK ? 2*len : B_CHUNK); |
| 2026 | o->data = xrealloc(o->data, 1 + o->maxlen); |
| 2027 | } |
| 2028 | } |
| 2029 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2030 | static void o_addchr(o_string *o, int ch) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2031 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2032 | debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o); |
| 2033 | o_grow_by(o, 1); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2034 | o->data[o->length] = ch; |
| 2035 | o->length++; |
| 2036 | o->data[o->length] = '\0'; |
| 2037 | } |
| 2038 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2039 | static void o_addblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2040 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2041 | o_grow_by(o, len); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2042 | memcpy(&o->data[o->length], str, len); |
| 2043 | o->length += len; |
| 2044 | o->data[o->length] = '\0'; |
| 2045 | } |
| 2046 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2047 | static void o_addstr(o_string *o, const char *str) |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2048 | { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2049 | o_addblock(o, str, strlen(str)); |
| 2050 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 2051 | |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 2052 | #if !BB_MMU |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 2053 | static void nommu_addchr(o_string *o, int ch) |
| 2054 | { |
| 2055 | if (o) |
| 2056 | o_addchr(o, ch); |
| 2057 | } |
| 2058 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2059 | # define nommu_addchr(o, str) ((void)0) |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2060 | #endif |
| 2061 | |
| 2062 | static void o_addstr_with_NUL(o_string *o, const char *str) |
| 2063 | { |
| 2064 | o_addblock(o, str, strlen(str) + 1); |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2067 | /* |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2068 | * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side. |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2069 | * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v. |
| 2070 | * Apparently, on unquoted $v bash still does globbing |
| 2071 | * ("v='*.txt'; echo $v" prints all .txt files), |
| 2072 | * but NOT brace expansion! Thus, there should be TWO independent |
| 2073 | * quoting mechanisms on $v expansion side: one protects |
| 2074 | * $v from brace expansion, and other additionally protects "$v" against globbing. |
| 2075 | * We have only second one. |
| 2076 | */ |
| 2077 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2078 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2079 | # define MAYBE_BRACES "{}" |
| 2080 | #else |
| 2081 | # define MAYBE_BRACES "" |
| 2082 | #endif |
| 2083 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2084 | /* My analysis of quoting semantics tells me that state information |
| 2085 | * is associated with a destination, not a source. |
| 2086 | */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2087 | static void o_addqchr(o_string *o, int ch) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2088 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2089 | int sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2090 | char *found = strchr("*?[\\" MAYBE_BRACES, ch); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2091 | if (found) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2092 | sz++; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 2093 | o_grow_by(o, sz); |
| 2094 | if (found) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2095 | o->data[o->length] = '\\'; |
| 2096 | o->length++; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2097 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2098 | o->data[o->length] = ch; |
| 2099 | o->length++; |
| 2100 | o->data[o->length] = '\0'; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2103 | static void o_addQchr(o_string *o, int ch) |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2104 | { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2105 | int sz = 1; |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2106 | if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) |
| 2107 | && strchr("*?[\\" MAYBE_BRACES, ch) |
| 2108 | ) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2109 | sz++; |
| 2110 | o->data[o->length] = '\\'; |
| 2111 | o->length++; |
| 2112 | } |
| 2113 | o_grow_by(o, sz); |
| 2114 | o->data[o->length] = ch; |
| 2115 | o->length++; |
| 2116 | o->data[o->length] = '\0'; |
| 2117 | } |
| 2118 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2119 | static void o_addqblock(o_string *o, const char *str, int len) |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2120 | { |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2121 | while (len) { |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2122 | char ch; |
| 2123 | int sz; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2124 | int ordinary_cnt = strcspn(str, "*?[\\" MAYBE_BRACES); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2125 | if (ordinary_cnt > len) /* paranoia */ |
| 2126 | ordinary_cnt = len; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2127 | o_addblock(o, str, ordinary_cnt); |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2128 | if (ordinary_cnt == len) |
| 2129 | return; |
| 2130 | str += ordinary_cnt; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2131 | len -= ordinary_cnt + 1; /* we are processing + 1 char below */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2132 | |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2133 | ch = *str++; |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2134 | sz = 1; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2135 | if (ch) { /* it is necessarily one of "*?[\\" MAYBE_BRACES */ |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2136 | sz++; |
| 2137 | o->data[o->length] = '\\'; |
| 2138 | o->length++; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2139 | } |
Denis Vlasenko | 7e3d33b | 2008-06-12 13:31:04 +0000 | [diff] [blame] | 2140 | o_grow_by(o, sz); |
| 2141 | o->data[o->length] = ch; |
| 2142 | o->length++; |
| 2143 | o->data[o->length] = '\0'; |
Denis Vlasenko | 87f40ba | 2008-06-10 22:39:37 +0000 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2147 | static void o_addQblock(o_string *o, const char *str, int len) |
| 2148 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2149 | if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2150 | o_addblock(o, str, len); |
| 2151 | return; |
| 2152 | } |
| 2153 | o_addqblock(o, str, len); |
| 2154 | } |
| 2155 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2156 | static void o_addQstr(o_string *o, const char *str) |
| 2157 | { |
| 2158 | o_addQblock(o, str, strlen(str)); |
| 2159 | } |
| 2160 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2161 | /* A special kind of o_string for $VAR and `cmd` expansion. |
| 2162 | * 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] | 2163 | * increments. Actual string data starts at the next multiple of 16 * (char*). |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2164 | * list[i] contains an INDEX (int!) into this string data. |
| 2165 | * It means that if list[] needs to grow, data needs to be moved higher up |
| 2166 | * but list[i]'s need not be modified. |
| 2167 | * NB: remembering how many list[i]'s you have there is crucial. |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2168 | * o_finalize_list() operation post-processes this structure - calculates |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2169 | * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well. |
| 2170 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2171 | #if DEBUG_EXPAND || DEBUG_GLOB |
| 2172 | static void debug_print_list(const char *prefix, o_string *o, int n) |
| 2173 | { |
| 2174 | char **list = (char**)o->data; |
| 2175 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2176 | int i = 0; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2177 | |
| 2178 | indent(); |
Denys Vlasenko | e298ce6 | 2010-09-04 19:52:44 +0200 | [diff] [blame] | 2179 | 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] | 2180 | prefix, list, n, string_start, o->length, o->maxlen, |
| 2181 | !!(o->o_expflags & EXP_FLAG_GLOB), |
| 2182 | o->has_quoted_part, |
| 2183 | !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2184 | while (i < n) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2185 | indent(); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2186 | fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], |
| 2187 | o->data + (int)list[i] + string_start, |
| 2188 | o->data + (int)list[i] + string_start); |
| 2189 | i++; |
| 2190 | } |
| 2191 | if (n) { |
| 2192 | const char *p = o->data + (int)list[n - 1] + string_start; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2193 | indent(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 2194 | 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] | 2195 | } |
| 2196 | } |
| 2197 | #else |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 2198 | # define debug_print_list(prefix, o, n) ((void)0) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2199 | #endif |
| 2200 | |
| 2201 | /* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value |
| 2202 | * in list[n] so that it points past last stored byte so far. |
| 2203 | * It returns n+1. */ |
| 2204 | static int o_save_ptr_helper(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2205 | { |
| 2206 | char **list = (char**)o->data; |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2207 | int string_start; |
| 2208 | int string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2209 | |
| 2210 | if (!o->has_empty_slot) { |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2211 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2212 | string_len = o->length - string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2213 | if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */ |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2214 | 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] | 2215 | /* list[n] points to string_start, make space for 16 more pointers */ |
| 2216 | o->maxlen += 0x10 * sizeof(list[0]); |
| 2217 | o->data = xrealloc(o->data, o->maxlen + 1); |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 2218 | list = (char**)o->data; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2219 | memmove(list + n + 0x10, list + n, string_len); |
| 2220 | o->length += 0x10 * sizeof(list[0]); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2221 | } else { |
| 2222 | debug_printf_list("list[%d]=%d string_start=%d\n", |
| 2223 | n, string_len, string_start); |
| 2224 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2225 | } else { |
| 2226 | /* We have empty slot at list[n], reuse without growth */ |
Denis Vlasenko | 895bea2 | 2008-06-10 18:06:24 +0000 | [diff] [blame] | 2227 | string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */ |
| 2228 | string_len = o->length - string_start; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 2229 | debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n", |
| 2230 | n, string_len, string_start); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2231 | o->has_empty_slot = 0; |
| 2232 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2233 | list[n] = (char*)(uintptr_t)string_len; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2234 | return n + 1; |
| 2235 | } |
| 2236 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2237 | /* "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] | 2238 | static int o_get_last_ptr(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2239 | { |
| 2240 | char **list = (char**)o->data; |
| 2241 | int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2242 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2243 | return ((int)(uintptr_t)list[n-1]) + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 2246 | #if ENABLE_HUSH_BRACE_EXPANSION |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2247 | /* There in a GNU extension, GLOB_BRACE, but it is not usable: |
| 2248 | * first, it processes even {a} (no commas), second, |
| 2249 | * 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] | 2250 | * existing files. Need to re-implement it. |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2251 | */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2252 | |
| 2253 | /* Helper */ |
| 2254 | static int glob_needed(const char *s) |
| 2255 | { |
| 2256 | while (*s) { |
| 2257 | if (*s == '\\') { |
| 2258 | if (!s[1]) |
| 2259 | return 0; |
| 2260 | s += 2; |
| 2261 | continue; |
| 2262 | } |
| 2263 | if (*s == '*' || *s == '[' || *s == '?' || *s == '{') |
| 2264 | return 1; |
| 2265 | s++; |
| 2266 | } |
| 2267 | return 0; |
| 2268 | } |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2269 | /* Return pointer to next closing brace or to comma */ |
| 2270 | static const char *next_brace_sub(const char *cp) |
| 2271 | { |
| 2272 | unsigned depth = 0; |
| 2273 | cp++; |
| 2274 | while (*cp != '\0') { |
| 2275 | if (*cp == '\\') { |
| 2276 | if (*++cp == '\0') |
| 2277 | break; |
| 2278 | cp++; |
| 2279 | continue; |
Denys Vlasenko | 3581c62 | 2010-01-25 13:39:24 +0100 | [diff] [blame] | 2280 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2281 | if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0)) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2282 | break; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2283 | if (*cp++ == '{') |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2284 | depth++; |
| 2285 | } |
| 2286 | |
| 2287 | return *cp != '\0' ? cp : NULL; |
| 2288 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2289 | /* Recursive brace globber. Note: may garble pattern[]. */ |
| 2290 | static int glob_brace(char *pattern, o_string *o, int n) |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2291 | { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2292 | char *new_pattern_buf; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2293 | const char *begin; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2294 | const char *next; |
| 2295 | const char *rest; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2296 | const char *p; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2297 | size_t rest_len; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2298 | |
| 2299 | debug_printf_glob("glob_brace('%s')\n", pattern); |
| 2300 | |
| 2301 | begin = pattern; |
| 2302 | while (1) { |
| 2303 | if (*begin == '\0') |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2304 | goto simple_glob; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2305 | if (*begin == '{') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2306 | /* Find the first sub-pattern and at the same time |
| 2307 | * find the rest after the closing brace */ |
| 2308 | next = next_brace_sub(begin); |
| 2309 | if (next == NULL) { |
| 2310 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2311 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2312 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2313 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2314 | /* "{abc}" with no commas - illegal |
| 2315 | * brace expr, disregard and skip it */ |
| 2316 | begin = next + 1; |
| 2317 | continue; |
| 2318 | } |
| 2319 | break; |
| 2320 | } |
| 2321 | if (*begin == '\\' && begin[1] != '\0') |
| 2322 | begin++; |
| 2323 | begin++; |
| 2324 | } |
| 2325 | debug_printf_glob("begin:%s\n", begin); |
| 2326 | debug_printf_glob("next:%s\n", next); |
| 2327 | |
| 2328 | /* Now find the end of the whole brace expression */ |
| 2329 | rest = next; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2330 | while (*rest != '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2331 | rest = next_brace_sub(rest); |
| 2332 | if (rest == NULL) { |
| 2333 | /* An illegal expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2334 | goto simple_glob; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2335 | } |
| 2336 | debug_printf_glob("rest:%s\n", rest); |
| 2337 | } |
| 2338 | rest_len = strlen(++rest) + 1; |
| 2339 | |
| 2340 | /* We are sure the brace expression is well-formed */ |
| 2341 | |
| 2342 | /* Allocate working buffer large enough for our work */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2343 | new_pattern_buf = xmalloc(strlen(pattern)); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2344 | |
| 2345 | /* We have a brace expression. BEGIN points to the opening {, |
| 2346 | * NEXT points past the terminator of the first element, and REST |
| 2347 | * points past the final }. We will accumulate result names from |
| 2348 | * recursive runs for each brace alternative in the buffer using |
| 2349 | * GLOB_APPEND. */ |
| 2350 | |
| 2351 | p = begin + 1; |
| 2352 | while (1) { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2353 | /* Construct the new glob expression */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2354 | memcpy( |
| 2355 | mempcpy( |
| 2356 | mempcpy(new_pattern_buf, |
| 2357 | /* We know the prefix for all sub-patterns */ |
| 2358 | pattern, begin - pattern), |
| 2359 | p, next - p), |
| 2360 | rest, rest_len); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2361 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2362 | /* Note: glob_brace() may garble new_pattern_buf[]. |
| 2363 | * That's why we re-copy prefix every time (1st memcpy above). |
| 2364 | */ |
| 2365 | n = glob_brace(new_pattern_buf, o, n); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 2366 | if (*next == '}') { |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2367 | /* We saw the last entry */ |
| 2368 | break; |
| 2369 | } |
| 2370 | p = next + 1; |
| 2371 | next = next_brace_sub(next); |
| 2372 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2373 | free(new_pattern_buf); |
| 2374 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2375 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2376 | simple_glob: |
| 2377 | { |
| 2378 | int gr; |
| 2379 | glob_t globdata; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2380 | |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2381 | memset(&globdata, 0, sizeof(globdata)); |
| 2382 | gr = glob(pattern, 0, NULL, &globdata); |
| 2383 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
| 2384 | if (gr != 0) { |
| 2385 | if (gr == GLOB_NOMATCH) { |
| 2386 | globfree(&globdata); |
| 2387 | /* NB: garbles parameter */ |
| 2388 | unbackslash(pattern); |
| 2389 | o_addstr_with_NUL(o, pattern); |
| 2390 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2391 | return o_save_ptr_helper(o, n); |
| 2392 | } |
| 2393 | if (gr == GLOB_NOSPACE) |
| 2394 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
| 2395 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2396 | * but we didn't specify it. Paranoia again. */ |
| 2397 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
| 2398 | } |
| 2399 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2400 | char **argv = globdata.gl_pathv; |
| 2401 | while (1) { |
| 2402 | o_addstr_with_NUL(o, *argv); |
| 2403 | n = o_save_ptr_helper(o, n); |
| 2404 | argv++; |
| 2405 | if (!*argv) |
| 2406 | break; |
| 2407 | } |
| 2408 | } |
| 2409 | globfree(&globdata); |
| 2410 | } |
| 2411 | return n; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2412 | } |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2413 | /* Performs globbing on last list[], |
| 2414 | * saving each result as a new list[]. |
| 2415 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2416 | static int perform_glob(o_string *o, int n) |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2417 | { |
| 2418 | char *pattern, *copy; |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2419 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2420 | 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] | 2421 | if (!o->data) |
| 2422 | return o_save_ptr_helper(o, n); |
| 2423 | pattern = o->data + o_get_last_ptr(o, n); |
| 2424 | debug_printf_glob("glob pattern '%s'\n", pattern); |
| 2425 | if (!glob_needed(pattern)) { |
| 2426 | /* unbackslash last string in o in place, fix length */ |
| 2427 | o->length = unbackslash(pattern) - o->data; |
| 2428 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
| 2429 | return o_save_ptr_helper(o, n); |
| 2430 | } |
| 2431 | |
| 2432 | copy = xstrdup(pattern); |
| 2433 | /* "forget" pattern in o */ |
| 2434 | o->length = pattern - o->data; |
| 2435 | n = glob_brace(copy, o, n); |
| 2436 | free(copy); |
| 2437 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2438 | debug_print_list("perform_glob returning", o, n); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2439 | return n; |
| 2440 | } |
| 2441 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2442 | #else /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2443 | |
| 2444 | /* Helper */ |
| 2445 | static int glob_needed(const char *s) |
| 2446 | { |
| 2447 | while (*s) { |
| 2448 | if (*s == '\\') { |
| 2449 | if (!s[1]) |
| 2450 | return 0; |
| 2451 | s += 2; |
| 2452 | continue; |
| 2453 | } |
| 2454 | if (*s == '*' || *s == '[' || *s == '?') |
| 2455 | return 1; |
| 2456 | s++; |
| 2457 | } |
| 2458 | return 0; |
| 2459 | } |
| 2460 | /* Performs globbing on last list[], |
| 2461 | * saving each result as a new list[]. |
| 2462 | */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2463 | static int perform_glob(o_string *o, int n) |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2464 | { |
| 2465 | glob_t globdata; |
| 2466 | int gr; |
| 2467 | char *pattern; |
| 2468 | |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2469 | 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] | 2470 | if (!o->data) |
| 2471 | return o_save_ptr_helper(o, n); |
| 2472 | pattern = o->data + o_get_last_ptr(o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2473 | debug_printf_glob("glob pattern '%s'\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2474 | if (!glob_needed(pattern)) { |
| 2475 | literal: |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2476 | /* unbackslash last string in o in place, fix length */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2477 | o->length = unbackslash(pattern) - o->data; |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2478 | debug_printf_glob("glob pattern '%s' is literal\n", pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2479 | return o_save_ptr_helper(o, n); |
| 2480 | } |
| 2481 | |
| 2482 | memset(&globdata, 0, sizeof(globdata)); |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2483 | /* Can't use GLOB_NOCHECK: it does not unescape the string. |
| 2484 | * If we glob "*.\*" and don't find anything, we need |
| 2485 | * to fall back to using literal "*.*", but GLOB_NOCHECK |
| 2486 | * will return "*.\*"! |
| 2487 | */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2488 | gr = glob(pattern, 0, NULL, &globdata); |
| 2489 | debug_printf_glob("glob('%s'):%d\n", pattern, gr); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2490 | if (gr != 0) { |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2491 | if (gr == GLOB_NOMATCH) { |
| 2492 | globfree(&globdata); |
| 2493 | goto literal; |
| 2494 | } |
| 2495 | if (gr == GLOB_NOSPACE) |
| 2496 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Denys Vlasenko | 5b2db97 | 2009-11-16 05:49:36 +0100 | [diff] [blame] | 2497 | /* GLOB_ABORTED? Only happens with GLOB_ERR flag, |
| 2498 | * but we didn't specify it. Paranoia again. */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2499 | bb_error_msg_and_die("glob error %d on '%s'", gr, pattern); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2500 | } |
| 2501 | if (globdata.gl_pathv && globdata.gl_pathv[0]) { |
| 2502 | char **argv = globdata.gl_pathv; |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2503 | /* "forget" pattern in o */ |
| 2504 | o->length = pattern - o->data; |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2505 | while (1) { |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2506 | o_addstr_with_NUL(o, *argv); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2507 | n = o_save_ptr_helper(o, n); |
| 2508 | argv++; |
| 2509 | if (!*argv) |
| 2510 | break; |
| 2511 | } |
| 2512 | } |
| 2513 | globfree(&globdata); |
| 2514 | if (DEBUG_GLOB) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2515 | debug_print_list("perform_glob returning", o, n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2516 | return n; |
| 2517 | } |
| 2518 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 2519 | #endif /* !HUSH_BRACE_EXPANSION */ |
Denys Vlasenko | f3e2818 | 2009-11-17 03:35:31 +0100 | [diff] [blame] | 2520 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2521 | /* 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] | 2522 | * Otherwise, just finish current list[] and start new */ |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2523 | static int o_save_ptr(o_string *o, int n) |
| 2524 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 2525 | if (o->o_expflags & EXP_FLAG_GLOB) { |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2526 | /* If o->has_empty_slot, list[n] was already globbed |
| 2527 | * (if it was requested back then when it was filled) |
| 2528 | * so don't do that again! */ |
| 2529 | if (!o->has_empty_slot) |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 2530 | return perform_glob(o, n); /* o_save_ptr_helper is inside */ |
Denis Vlasenko | a8b6dff | 2009-03-20 12:05:14 +0000 | [diff] [blame] | 2531 | } |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2532 | return o_save_ptr_helper(o, n); |
| 2533 | } |
| 2534 | |
| 2535 | /* "Please convert list[n] to real char* ptrs, and NULL terminate it." */ |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 2536 | static char **o_finalize_list(o_string *o, int n) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2537 | { |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2538 | char **list; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2539 | int string_start; |
| 2540 | |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2541 | n = o_save_ptr(o, n); /* force growth for list[n] if necessary */ |
| 2542 | if (DEBUG_EXPAND) |
| 2543 | debug_print_list("finalized", o, n); |
Denis Vlasenko | 30c9cc5 | 2008-06-17 07:24:29 +0000 | [diff] [blame] | 2544 | debug_printf_expand("finalized n:%d\n", n); |
Denis Vlasenko | b61e13d | 2008-06-17 05:11:43 +0000 | [diff] [blame] | 2545 | list = (char**)o->data; |
| 2546 | string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]); |
| 2547 | list[--n] = NULL; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2548 | while (n) { |
| 2549 | n--; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2550 | list[n] = o->data + (int)(uintptr_t)list[n] + string_start; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 2551 | } |
| 2552 | return list; |
| 2553 | } |
| 2554 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2555 | static void free_pipe_list(struct pipe *pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2556 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2557 | /* Returns pi->next - next pipe in the list */ |
| 2558 | static struct pipe *free_pipe(struct pipe *pi) |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2559 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2560 | struct pipe *next; |
| 2561 | int i; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2562 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2563 | debug_printf_clean("free_pipe (pid %d)\n", getpid()); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2564 | for (i = 0; i < pi->num_cmds; i++) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2565 | struct command *command; |
| 2566 | struct redir_struct *r, *rnext; |
| 2567 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2568 | command = &pi->cmds[i]; |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2569 | debug_printf_clean(" command %d:\n", i); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2570 | if (command->argv) { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2571 | if (DEBUG_CLEAN) { |
| 2572 | int a; |
| 2573 | char **p; |
| 2574 | for (a = 0, p = command->argv; *p; a++, p++) { |
| 2575 | debug_printf_clean(" argv[%d] = %s\n", a, *p); |
| 2576 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2577 | } |
| 2578 | free_strings(command->argv); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2579 | //command->argv = NULL; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2580 | } |
| 2581 | /* not "else if": on syntax error, we may have both! */ |
| 2582 | if (command->group) { |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2583 | debug_printf_clean(" begin group (cmd_type:%d)\n", |
| 2584 | command->cmd_type); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2585 | free_pipe_list(command->group); |
| 2586 | debug_printf_clean(" end group\n"); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2587 | //command->group = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2588 | } |
Denis Vlasenko | ed05521 | 2009-04-11 10:37:10 +0000 | [diff] [blame] | 2589 | /* else is crucial here. |
| 2590 | * If group != NULL, child_func is meaningless */ |
| 2591 | #if ENABLE_HUSH_FUNCTIONS |
| 2592 | else if (command->child_func) { |
| 2593 | debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func); |
| 2594 | command->child_func->parent_cmd = NULL; |
| 2595 | } |
| 2596 | #endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2597 | #if !BB_MMU |
| 2598 | free(command->group_as_string); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2599 | //command->group_as_string = NULL; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2600 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2601 | for (r = command->redirects; r; r = rnext) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2602 | debug_printf_clean(" redirect %d%s", |
| 2603 | r->rd_fd, redir_table[r->rd_type].descrip); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2604 | /* guard against the case >$FOO, where foo is unset or blank */ |
| 2605 | if (r->rd_filename) { |
| 2606 | debug_printf_clean(" fname:'%s'\n", r->rd_filename); |
| 2607 | free(r->rd_filename); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2608 | //r->rd_filename = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2609 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 2610 | debug_printf_clean(" rd_dup:%d\n", r->rd_dup); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2611 | rnext = r->next; |
| 2612 | free(r); |
| 2613 | } |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2614 | //command->redirects = NULL; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2615 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2616 | 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] | 2617 | //pi->cmds = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2618 | #if ENABLE_HUSH_JOB |
| 2619 | free(pi->cmdtext); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2620 | //pi->cmdtext = NULL; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2621 | #endif |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2622 | |
| 2623 | next = pi->next; |
| 2624 | free(pi); |
| 2625 | return next; |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 2626 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2627 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2628 | static void free_pipe_list(struct pipe *pi) |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2629 | { |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2630 | while (pi) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2631 | #if HAS_KEYWORDS |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2632 | debug_printf_clean("pipe reserved word %d\n", pi->res_word); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2633 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 2634 | debug_printf_clean("pipe followup code %d\n", pi->followup); |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 2635 | pi = free_pipe(pi); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2636 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 2640 | /*** Parsing routines ***/ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 2641 | |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 2642 | #ifndef debug_print_tree |
| 2643 | static void debug_print_tree(struct pipe *pi, int lvl) |
| 2644 | { |
| 2645 | static const char *const PIPE[] = { |
| 2646 | [PIPE_SEQ] = "SEQ", |
| 2647 | [PIPE_AND] = "AND", |
| 2648 | [PIPE_OR ] = "OR" , |
| 2649 | [PIPE_BG ] = "BG" , |
| 2650 | }; |
| 2651 | static const char *RES[] = { |
| 2652 | [RES_NONE ] = "NONE" , |
| 2653 | # if ENABLE_HUSH_IF |
| 2654 | [RES_IF ] = "IF" , |
| 2655 | [RES_THEN ] = "THEN" , |
| 2656 | [RES_ELIF ] = "ELIF" , |
| 2657 | [RES_ELSE ] = "ELSE" , |
| 2658 | [RES_FI ] = "FI" , |
| 2659 | # endif |
| 2660 | # if ENABLE_HUSH_LOOPS |
| 2661 | [RES_FOR ] = "FOR" , |
| 2662 | [RES_WHILE] = "WHILE", |
| 2663 | [RES_UNTIL] = "UNTIL", |
| 2664 | [RES_DO ] = "DO" , |
| 2665 | [RES_DONE ] = "DONE" , |
| 2666 | # endif |
| 2667 | # if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE |
| 2668 | [RES_IN ] = "IN" , |
| 2669 | # endif |
| 2670 | # if ENABLE_HUSH_CASE |
| 2671 | [RES_CASE ] = "CASE" , |
| 2672 | [RES_CASE_IN ] = "CASE_IN" , |
| 2673 | [RES_MATCH] = "MATCH", |
| 2674 | [RES_CASE_BODY] = "CASE_BODY", |
| 2675 | [RES_ESAC ] = "ESAC" , |
| 2676 | # endif |
| 2677 | [RES_XXXX ] = "XXXX" , |
| 2678 | [RES_SNTX ] = "SNTX" , |
| 2679 | }; |
| 2680 | static const char *const CMDTYPE[] = { |
| 2681 | "{}", |
| 2682 | "()", |
| 2683 | "[noglob]", |
| 2684 | # if ENABLE_HUSH_FUNCTIONS |
| 2685 | "func()", |
| 2686 | # endif |
| 2687 | }; |
| 2688 | |
| 2689 | int pin, prn; |
| 2690 | |
| 2691 | pin = 0; |
| 2692 | while (pi) { |
| 2693 | fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", |
| 2694 | pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); |
| 2695 | prn = 0; |
| 2696 | while (prn < pi->num_cmds) { |
| 2697 | struct command *command = &pi->cmds[prn]; |
| 2698 | char **argv = command->argv; |
| 2699 | |
| 2700 | fprintf(stderr, "%*s cmd %d assignment_cnt:%d", |
| 2701 | lvl*2, "", prn, |
| 2702 | command->assignment_cnt); |
| 2703 | if (command->group) { |
| 2704 | fprintf(stderr, " group %s: (argv=%p)%s%s\n", |
| 2705 | CMDTYPE[command->cmd_type], |
| 2706 | argv |
| 2707 | # if !BB_MMU |
| 2708 | , " group_as_string:", command->group_as_string |
| 2709 | # else |
| 2710 | , "", "" |
| 2711 | # endif |
| 2712 | ); |
| 2713 | debug_print_tree(command->group, lvl+1); |
| 2714 | prn++; |
| 2715 | continue; |
| 2716 | } |
| 2717 | if (argv) while (*argv) { |
| 2718 | fprintf(stderr, " '%s'", *argv); |
| 2719 | argv++; |
| 2720 | } |
| 2721 | fprintf(stderr, "\n"); |
| 2722 | prn++; |
| 2723 | } |
| 2724 | pi = pi->next; |
| 2725 | pin++; |
| 2726 | } |
| 2727 | } |
| 2728 | #endif /* debug_print_tree */ |
| 2729 | |
Denis Vlasenko | ac678ec | 2007-04-16 22:32:04 +0000 | [diff] [blame] | 2730 | static struct pipe *new_pipe(void) |
| 2731 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2732 | struct pipe *pi; |
Denis Vlasenko | 3ac0e00 | 2007-04-28 16:45:22 +0000 | [diff] [blame] | 2733 | pi = xzalloc(sizeof(struct pipe)); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2734 | /*pi->followup = 0; - deliberately invalid value */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2735 | /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2736 | return pi; |
| 2737 | } |
| 2738 | |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2739 | /* Command (member of a pipe) is complete, or we start a new pipe |
| 2740 | * if ctx->command is NULL. |
| 2741 | * No errors possible here. |
| 2742 | */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2743 | static int done_command(struct parse_context *ctx) |
| 2744 | { |
| 2745 | /* The command is really already in the pipe structure, so |
| 2746 | * advance the pipe counter and make a new, null command. */ |
| 2747 | struct pipe *pi = ctx->pipe; |
| 2748 | struct command *command = ctx->command; |
| 2749 | |
| 2750 | if (command) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2751 | if (IS_NULL_CMD(command)) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2752 | 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] | 2753 | goto clear_and_ret; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2754 | } |
| 2755 | pi->num_cmds++; |
| 2756 | debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2757 | //debug_print_tree(ctx->list_head, 20); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2758 | } else { |
| 2759 | debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds); |
| 2760 | } |
| 2761 | |
| 2762 | /* Only real trickiness here is that the uncommitted |
| 2763 | * command structure is not counted in pi->num_cmds. */ |
| 2764 | pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1)); |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2765 | ctx->command = command = &pi->cmds[pi->num_cmds]; |
| 2766 | clear_and_ret: |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2767 | memset(command, 0, sizeof(*command)); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2768 | return pi->num_cmds; /* used only for 0/nonzero check */ |
| 2769 | } |
| 2770 | |
| 2771 | static void done_pipe(struct parse_context *ctx, pipe_style type) |
| 2772 | { |
| 2773 | int not_null; |
| 2774 | |
| 2775 | debug_printf_parse("done_pipe entered, followup %d\n", type); |
| 2776 | /* Close previous command */ |
| 2777 | not_null = done_command(ctx); |
| 2778 | ctx->pipe->followup = type; |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2779 | #if HAS_KEYWORDS |
| 2780 | ctx->pipe->pi_inverted = ctx->ctx_inverted; |
| 2781 | ctx->ctx_inverted = 0; |
| 2782 | ctx->pipe->res_word = ctx->ctx_res_w; |
| 2783 | #endif |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2784 | |
| 2785 | /* Without this check, even just <enter> on command line generates |
| 2786 | * tree of three NOPs (!). Which is harmless but annoying. |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2787 | * IOW: it is safe to do it unconditionally. */ |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2788 | if (not_null |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2789 | #if ENABLE_HUSH_IF |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2790 | || ctx->ctx_res_w == RES_FI |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2791 | #endif |
| 2792 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2793 | || ctx->ctx_res_w == RES_DONE |
| 2794 | || ctx->ctx_res_w == RES_FOR |
| 2795 | || ctx->ctx_res_w == RES_IN |
Denis Vlasenko | 7f95937 | 2009-04-14 08:06:59 +0000 | [diff] [blame] | 2796 | #endif |
| 2797 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2798 | || ctx->ctx_res_w == RES_ESAC |
| 2799 | #endif |
| 2800 | ) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2801 | struct pipe *new_p; |
| 2802 | debug_printf_parse("done_pipe: adding new pipe: " |
| 2803 | "not_null:%d ctx->ctx_res_w:%d\n", |
| 2804 | not_null, ctx->ctx_res_w); |
| 2805 | new_p = new_pipe(); |
| 2806 | ctx->pipe->next = new_p; |
| 2807 | ctx->pipe = new_p; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2808 | /* RES_THEN, RES_DO etc are "sticky" - |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 2809 | * they remain set for pipes inside if/while. |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2810 | * This is used to control execution. |
| 2811 | * RES_FOR and RES_IN are NOT sticky (needed to support |
| 2812 | * cases where variable or value happens to match a keyword): |
| 2813 | */ |
| 2814 | #if ENABLE_HUSH_LOOPS |
| 2815 | if (ctx->ctx_res_w == RES_FOR |
| 2816 | || ctx->ctx_res_w == RES_IN) |
| 2817 | ctx->ctx_res_w = RES_NONE; |
| 2818 | #endif |
| 2819 | #if ENABLE_HUSH_CASE |
| 2820 | if (ctx->ctx_res_w == RES_MATCH) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2821 | ctx->ctx_res_w = RES_CASE_BODY; |
| 2822 | if (ctx->ctx_res_w == RES_CASE) |
| 2823 | ctx->ctx_res_w = RES_CASE_IN; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2824 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2825 | ctx->command = NULL; /* trick done_command below */ |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2826 | /* Create the memory for command, roughly: |
| 2827 | * ctx->pipe->cmds = new struct command; |
| 2828 | * ctx->command = &ctx->pipe->cmds[0]; |
| 2829 | */ |
| 2830 | done_command(ctx); |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 2831 | //debug_print_tree(ctx->list_head, 10); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 2832 | } |
| 2833 | debug_printf_parse("done_pipe return\n"); |
| 2834 | } |
| 2835 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2836 | static void initialize_context(struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2837 | { |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2838 | memset(ctx, 0, sizeof(*ctx)); |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2839 | ctx->pipe = ctx->list_head = new_pipe(); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 2840 | /* Create the memory for command, roughly: |
| 2841 | * ctx->pipe->cmds = new struct command; |
| 2842 | * ctx->command = &ctx->pipe->cmds[0]; |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2843 | */ |
| 2844 | done_command(ctx); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2847 | /* If a reserved word is found and processed, parse context is modified |
| 2848 | * and 1 is returned. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2849 | */ |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 2850 | #if HAS_KEYWORDS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2851 | struct reserved_combo { |
| 2852 | char literal[6]; |
| 2853 | unsigned char res; |
| 2854 | unsigned char assignment_flag; |
| 2855 | int flag; |
| 2856 | }; |
| 2857 | enum { |
| 2858 | FLAG_END = (1 << RES_NONE ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2859 | # if ENABLE_HUSH_IF |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2860 | FLAG_IF = (1 << RES_IF ), |
| 2861 | FLAG_THEN = (1 << RES_THEN ), |
| 2862 | FLAG_ELIF = (1 << RES_ELIF ), |
| 2863 | FLAG_ELSE = (1 << RES_ELSE ), |
| 2864 | FLAG_FI = (1 << RES_FI ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2865 | # endif |
| 2866 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2867 | FLAG_FOR = (1 << RES_FOR ), |
| 2868 | FLAG_WHILE = (1 << RES_WHILE), |
| 2869 | FLAG_UNTIL = (1 << RES_UNTIL), |
| 2870 | FLAG_DO = (1 << RES_DO ), |
| 2871 | FLAG_DONE = (1 << RES_DONE ), |
| 2872 | FLAG_IN = (1 << RES_IN ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2873 | # endif |
| 2874 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2875 | FLAG_MATCH = (1 << RES_MATCH), |
| 2876 | FLAG_ESAC = (1 << RES_ESAC ), |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2877 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2878 | FLAG_START = (1 << RES_XXXX ), |
| 2879 | }; |
| 2880 | |
| 2881 | static const struct reserved_combo* match_reserved_word(o_string *word) |
| 2882 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2883 | /* Mostly a list of accepted follow-up reserved words. |
| 2884 | * FLAG_END means we are done with the sequence, and are ready |
| 2885 | * to turn the compound list into a command. |
| 2886 | * FLAG_START means the word must start a new compound list. |
| 2887 | */ |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2888 | static const struct reserved_combo reserved_list[] = { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2889 | # if ENABLE_HUSH_IF |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2890 | { "!", RES_NONE, NOT_ASSIGNMENT , 0 }, |
| 2891 | { "if", RES_IF, WORD_IS_KEYWORD, FLAG_THEN | FLAG_START }, |
| 2892 | { "then", RES_THEN, WORD_IS_KEYWORD, FLAG_ELIF | FLAG_ELSE | FLAG_FI }, |
| 2893 | { "elif", RES_ELIF, WORD_IS_KEYWORD, FLAG_THEN }, |
| 2894 | { "else", RES_ELSE, WORD_IS_KEYWORD, FLAG_FI }, |
| 2895 | { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2896 | # endif |
| 2897 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2898 | { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START }, |
| 2899 | { "while", RES_WHILE, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2900 | { "until", RES_UNTIL, WORD_IS_KEYWORD, FLAG_DO | FLAG_START }, |
| 2901 | { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO }, |
| 2902 | { "do", RES_DO, WORD_IS_KEYWORD, FLAG_DONE }, |
| 2903 | { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2904 | # endif |
| 2905 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2906 | { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START }, |
| 2907 | { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END }, |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2908 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2909 | }; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2910 | const struct reserved_combo *r; |
| 2911 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 2912 | for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) { |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2913 | if (strcmp(word->data, r->literal) == 0) |
| 2914 | return r; |
| 2915 | } |
| 2916 | return NULL; |
| 2917 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2918 | /* Return 0: not a keyword, 1: keyword |
| 2919 | */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2920 | static int reserved_word(o_string *word, struct parse_context *ctx) |
| 2921 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2922 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2923 | static const struct reserved_combo reserved_match = { |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 2924 | "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 2925 | }; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2926 | # endif |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2927 | const struct reserved_combo *r; |
Denis Vlasenko | c72c1ed | 2007-01-30 22:31:26 +0000 | [diff] [blame] | 2928 | |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 2929 | if (word->has_quoted_part) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2930 | return 0; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2931 | r = match_reserved_word(word); |
| 2932 | if (!r) |
| 2933 | return 0; |
| 2934 | |
| 2935 | 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] | 2936 | # if ENABLE_HUSH_CASE |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2937 | if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) { |
| 2938 | /* "case word IN ..." - IN part starts first MATCH part */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2939 | r = &reserved_match; |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 2940 | } else |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2941 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2942 | if (r->flag == 0) { /* '!' */ |
| 2943 | if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */ |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 2944 | syntax_error("! ! command"); |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2945 | ctx->ctx_res_w = RES_SNTX; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2946 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2947 | ctx->ctx_inverted = 1; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 2948 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2949 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2950 | if (r->flag & FLAG_START) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2951 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2952 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2953 | old = xmalloc(sizeof(*old)); |
| 2954 | debug_printf_parse("push stack %p\n", old); |
| 2955 | *old = *ctx; /* physical copy */ |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2956 | initialize_context(ctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2957 | ctx->stack = old; |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2958 | } 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] | 2959 | syntax_error_at(word->data); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2960 | ctx->ctx_res_w = RES_SNTX; |
| 2961 | return 1; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2962 | } else { |
| 2963 | /* "{...} fi" is ok. "{...} if" is not |
| 2964 | * Example: |
| 2965 | * if { echo foo; } then { echo bar; } fi */ |
| 2966 | if (ctx->command->group) |
| 2967 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2968 | } |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2969 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2970 | ctx->ctx_res_w = r->res; |
| 2971 | ctx->old_flag = r->flag; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2972 | word->o_assignment = r->assignment_flag; |
| 2973 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2974 | if (ctx->old_flag & FLAG_END) { |
| 2975 | struct parse_context *old; |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 2976 | |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2977 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2978 | debug_printf_parse("pop stack %p\n", ctx->stack); |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2979 | old = ctx->stack; |
| 2980 | old->command->group = ctx->list_head; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 2981 | old->command->cmd_type = CMD_NORMAL; |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2982 | # if !BB_MMU |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 2983 | o_addstr(&old->as_string, ctx->as_string.data); |
| 2984 | o_free_unsafe(&ctx->as_string); |
| 2985 | old->command->group_as_string = xstrdup(old->as_string.data); |
| 2986 | debug_printf_parse("pop, remembering as:'%s'\n", |
| 2987 | old->command->group_as_string); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2988 | # endif |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2989 | *ctx = *old; /* physical copy */ |
| 2990 | free(old); |
| 2991 | } |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 2992 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2993 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 2994 | #endif /* HAS_KEYWORDS */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 2995 | |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 2996 | /* Word is complete, look at it and update parsing context. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 2997 | * Normal return is 0. Syntax errors return 1. |
| 2998 | * Note: on return, word is reset, but not o_free'd! |
| 2999 | */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3000 | static int done_word(o_string *word, struct parse_context *ctx) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3001 | { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3002 | struct command *command = ctx->command; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3003 | |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3004 | debug_printf_parse("done_word entered: '%s' %p\n", word->data, command); |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3005 | if (word->length == 0 && !word->has_quoted_part) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3006 | debug_printf_parse("done_word return 0: true null, ignored\n"); |
| 3007 | return 0; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3008 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 3009 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3010 | if (ctx->pending_redirect) { |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3011 | /* We do not glob in e.g. >*.tmp case. bash seems to glob here |
| 3012 | * only if run as "bash", not "sh" */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3013 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3014 | * "2.7 Redirection |
| 3015 | * ...the word that follows the redirection operator |
| 3016 | * shall be subjected to tilde expansion, parameter expansion, |
| 3017 | * command substitution, arithmetic expansion, and quote |
| 3018 | * removal. Pathname expansion shall not be performed |
| 3019 | * on the word by a non-interactive shell; an interactive |
| 3020 | * shell may perform it, but shall do so only when |
| 3021 | * the expansion would result in one word." |
| 3022 | */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3023 | ctx->pending_redirect->rd_filename = xstrdup(word->data); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3024 | /* Cater for >\file case: |
| 3025 | * >\a creates file a; >\\a, >"\a", >"\\a" create file \a |
| 3026 | * Same with heredocs: |
| 3027 | * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H |
| 3028 | */ |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3029 | if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) { |
| 3030 | unbackslash(ctx->pending_redirect->rd_filename); |
| 3031 | /* Is it <<"HEREDOC"? */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3032 | if (word->has_quoted_part) { |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3033 | ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED; |
| 3034 | } |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3035 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3036 | debug_printf_parse("word stored in rd_filename: '%s'\n", word->data); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3037 | ctx->pending_redirect = NULL; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3038 | } else { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3039 | /* If this word wasn't an assignment, next ones definitely |
| 3040 | * can't be assignments. Even if they look like ones. */ |
| 3041 | if (word->o_assignment != DEFINITELY_ASSIGNMENT |
| 3042 | && word->o_assignment != WORD_IS_KEYWORD |
| 3043 | ) { |
| 3044 | word->o_assignment = NOT_ASSIGNMENT; |
| 3045 | } else { |
| 3046 | if (word->o_assignment == DEFINITELY_ASSIGNMENT) |
| 3047 | command->assignment_cnt++; |
| 3048 | word->o_assignment = MAYBE_ASSIGNMENT; |
| 3049 | } |
| 3050 | |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3051 | #if HAS_KEYWORDS |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3052 | # if ENABLE_HUSH_CASE |
Denis Vlasenko | 757361f | 2008-07-14 08:26:47 +0000 | [diff] [blame] | 3053 | if (ctx->ctx_dsemicolon |
| 3054 | && strcmp(word->data, "esac") != 0 /* not "... pattern) cmd;; esac" */ |
| 3055 | ) { |
Denis Vlasenko | 395ae45 | 2008-07-14 06:29:38 +0000 | [diff] [blame] | 3056 | /* already done when ctx_dsemicolon was set to 1: */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3057 | /* ctx->ctx_res_w = RES_MATCH; */ |
| 3058 | ctx->ctx_dsemicolon = 0; |
| 3059 | } else |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3060 | # endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3061 | if (!command->argv /* if it's the first word... */ |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3062 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3063 | && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */ |
| 3064 | && ctx->ctx_res_w != RES_IN |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3065 | # endif |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 3066 | # if ENABLE_HUSH_CASE |
| 3067 | && ctx->ctx_res_w != RES_CASE |
| 3068 | # endif |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3069 | ) { |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3070 | debug_printf_parse("checking '%s' for reserved-ness\n", word->data); |
Denis Vlasenko | a844200 | 2008-06-14 11:00:17 +0000 | [diff] [blame] | 3071 | if (reserved_word(word, ctx)) { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3072 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3073 | debug_printf_parse("done_word return %d\n", |
| 3074 | (ctx->ctx_res_w == RES_SNTX)); |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3075 | return (ctx->ctx_res_w == RES_SNTX); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3076 | } |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3077 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3078 | if (strcmp(word->data, "[[") == 0) { |
| 3079 | command->cmd_type = CMD_SINGLEWORD_NOGLOB; |
| 3080 | } |
| 3081 | /* fall through */ |
Denys Vlasenko | 9ca656b | 2009-06-10 13:39:35 +0200 | [diff] [blame] | 3082 | # endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3083 | } |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 3084 | #endif |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3085 | if (command->group) { |
| 3086 | /* "{ echo foo; } echo bar" - bad */ |
| 3087 | syntax_error_at(word->data); |
| 3088 | debug_printf_parse("done_word return 1: syntax error, " |
| 3089 | "groups and arglists don't mix\n"); |
| 3090 | return 1; |
| 3091 | } |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3092 | if (word->has_quoted_part |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 3093 | /* optimization: and if it's ("" or '') or ($v... or `cmd`...): */ |
| 3094 | && (word->data[0] == '\0' || word->data[0] == SPECIAL_VAR_SYMBOL) |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3095 | /* (otherwise it's known to be not empty and is already safe) */ |
Denis Vlasenko | ab876cd | 2008-06-18 16:29:32 +0000 | [diff] [blame] | 3096 | ) { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3097 | /* exclude "$@" - it can expand to no word despite "" */ |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3098 | char *p = word->data; |
| 3099 | while (p[0] == SPECIAL_VAR_SYMBOL |
| 3100 | && (p[1] & 0x7f) == '@' |
| 3101 | && p[2] == SPECIAL_VAR_SYMBOL |
| 3102 | ) { |
| 3103 | p += 3; |
| 3104 | } |
| 3105 | if (p == word->data || p[0] != '\0') { |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3106 | /* saw no "$@", or not only "$@" but some |
| 3107 | * real text is there too */ |
| 3108 | /* insert "empty variable" reference, this makes |
Denis Vlasenko | afdcd12 | 2008-07-05 17:40:04 +0000 | [diff] [blame] | 3109 | * e.g. "", $empty"" etc to not disappear */ |
| 3110 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3111 | o_addchr(word, SPECIAL_VAR_SYMBOL); |
| 3112 | } |
Denis Vlasenko | c1c63b6 | 2008-06-18 09:20:35 +0000 | [diff] [blame] | 3113 | } |
Denis Vlasenko | 22d10a0 | 2008-10-13 08:53:43 +0000 | [diff] [blame] | 3114 | command->argv = add_string_to_strings(command->argv, xstrdup(word->data)); |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3115 | debug_print_strings("word appended to argv", command->argv); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3116 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3117 | |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3118 | #if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3119 | if (ctx->ctx_res_w == RES_FOR) { |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3120 | if (word->has_quoted_part |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3121 | || !is_well_formed_var_name(command->argv[0], '\0') |
| 3122 | ) { |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3123 | /* bash says just "not a valid identifier" */ |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3124 | syntax_error("not a valid identifier in for"); |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3125 | return 1; |
| 3126 | } |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3127 | /* Force FOR to have just one word (variable name) */ |
| 3128 | /* NB: basically, this makes hush see "for v in ..." |
| 3129 | * syntax as if it is "for v; in ...". FOR and IN become |
| 3130 | * two pipe structs in parse tree. */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 3131 | done_pipe(ctx, PIPE_SEQ); |
Denis Vlasenko | 733e3fb | 2008-07-06 10:01:13 +0000 | [diff] [blame] | 3132 | } |
Denis Vlasenko | 0681033 | 2007-05-21 23:30:54 +0000 | [diff] [blame] | 3133 | #endif |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 3134 | #if ENABLE_HUSH_CASE |
| 3135 | /* Force CASE to have just one word */ |
| 3136 | if (ctx->ctx_res_w == RES_CASE) { |
| 3137 | done_pipe(ctx, PIPE_SEQ); |
| 3138 | } |
| 3139 | #endif |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3140 | |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3141 | o_reset_to_empty_unquoted(word); |
Denis Vlasenko | 1fd1ea4 | 2009-04-10 12:03:20 +0000 | [diff] [blame] | 3142 | |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3143 | debug_printf_parse("done_word return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3144 | return 0; |
| 3145 | } |
| 3146 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3147 | |
| 3148 | /* Peek ahead in the input to find out if we have a "&n" construct, |
| 3149 | * as in "2>&1", that represents duplicating a file descriptor. |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3150 | * Return: |
| 3151 | * REDIRFD_CLOSE if >&- "close fd" construct is seen, |
| 3152 | * REDIRFD_SYNTAX_ERR if syntax error, |
| 3153 | * REDIRFD_TO_FILE if no & was seen, |
| 3154 | * or the number found. |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3155 | */ |
| 3156 | #if BB_MMU |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3157 | #define parse_redir_right_fd(as_string, input) \ |
| 3158 | parse_redir_right_fd(input) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3159 | #endif |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3160 | 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] | 3161 | { |
| 3162 | int ch, d, ok; |
| 3163 | |
| 3164 | ch = i_peek(input); |
| 3165 | if (ch != '&') |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3166 | return REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3167 | |
| 3168 | ch = i_getch(input); /* get the & */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3169 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3170 | ch = i_peek(input); |
| 3171 | if (ch == '-') { |
| 3172 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3173 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3174 | return REDIRFD_CLOSE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3175 | } |
| 3176 | d = 0; |
| 3177 | ok = 0; |
| 3178 | while (ch != EOF && isdigit(ch)) { |
| 3179 | d = d*10 + (ch-'0'); |
| 3180 | ok = 1; |
| 3181 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3182 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3183 | ch = i_peek(input); |
| 3184 | } |
| 3185 | if (ok) return d; |
| 3186 | |
| 3187 | //TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2) |
| 3188 | |
| 3189 | bb_error_msg("ambiguous redirect"); |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3190 | return REDIRFD_SYNTAX_ERR; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3193 | /* Return code is 0 normal, 1 if a syntax error is detected |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3194 | */ |
| 3195 | static int parse_redirect(struct parse_context *ctx, |
| 3196 | int fd, |
| 3197 | redir_type style, |
| 3198 | struct in_str *input) |
| 3199 | { |
| 3200 | struct command *command = ctx->command; |
| 3201 | struct redir_struct *redir; |
| 3202 | struct redir_struct **redirp; |
| 3203 | int dup_num; |
| 3204 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3205 | dup_num = REDIRFD_TO_FILE; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3206 | if (style != REDIRECT_HEREDOC) { |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3207 | /* Check for a '>&1' type redirect */ |
| 3208 | dup_num = parse_redir_right_fd(&ctx->as_string, input); |
| 3209 | if (dup_num == REDIRFD_SYNTAX_ERR) |
| 3210 | return 1; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3211 | } else { |
| 3212 | int ch = i_peek(input); |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3213 | dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3214 | if (dup_num) { /* <<-... */ |
| 3215 | ch = i_getch(input); |
| 3216 | nommu_addchr(&ctx->as_string, ch); |
| 3217 | ch = i_peek(input); |
| 3218 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3219 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3220 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3221 | if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3222 | int ch = i_peek(input); |
| 3223 | if (ch == '|') { |
| 3224 | /* >|FILE redirect ("clobbering" >). |
| 3225 | * Since we do not support "set -o noclobber" yet, |
| 3226 | * >| and > are the same for now. Just eat |. |
| 3227 | */ |
| 3228 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3229 | nommu_addchr(&ctx->as_string, ch); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | /* Create a new redir_struct and append it to the linked list */ |
| 3234 | redirp = &command->redirects; |
| 3235 | while ((redir = *redirp) != NULL) { |
| 3236 | redirp = &(redir->next); |
| 3237 | } |
| 3238 | *redirp = redir = xzalloc(sizeof(*redir)); |
| 3239 | /* redir->next = NULL; */ |
| 3240 | /* redir->rd_filename = NULL; */ |
| 3241 | redir->rd_type = style; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3242 | redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3243 | |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3244 | debug_printf_parse("redirect type %d %s\n", redir->rd_fd, |
| 3245 | redir_table[style].descrip); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3246 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3247 | redir->rd_dup = dup_num; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 3248 | if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3249 | /* Erik had a check here that the file descriptor in question |
| 3250 | * is legit; I postpone that to "run time" |
| 3251 | * A "-" representation of "close me" shows up as a -3 here */ |
Denis Vlasenko | 02d6f1a | 2009-04-07 19:56:55 +0000 | [diff] [blame] | 3252 | debug_printf_parse("duplicating redirect '%d>&%d'\n", |
| 3253 | redir->rd_fd, redir->rd_dup); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3254 | } else { |
| 3255 | /* Set ctx->pending_redirect, so we know what to do at the |
| 3256 | * end of the next parsed word. */ |
| 3257 | ctx->pending_redirect = redir; |
| 3258 | } |
| 3259 | return 0; |
| 3260 | } |
| 3261 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3262 | /* If a redirect is immediately preceded by a number, that number is |
| 3263 | * supposed to tell which file descriptor to redirect. This routine |
| 3264 | * looks for such preceding numbers. In an ideal world this routine |
| 3265 | * needs to handle all the following classes of redirects... |
| 3266 | * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo |
| 3267 | * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo |
| 3268 | * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo |
| 3269 | * 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] | 3270 | * |
| 3271 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |
| 3272 | * "2.7 Redirection |
| 3273 | * ... If n is quoted, the number shall not be recognized as part of |
| 3274 | * the redirection expression. For example: |
| 3275 | * echo \2>a |
| 3276 | * writes the character 2 into file a" |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3277 | * 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] | 3278 | * |
| 3279 | * A -1 return means no valid number was found, |
| 3280 | * the caller should use the appropriate default for this redirection. |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3281 | */ |
| 3282 | static int redirect_opt_num(o_string *o) |
| 3283 | { |
| 3284 | int num; |
| 3285 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3286 | if (o->data == NULL) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 3287 | return -1; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3288 | num = bb_strtou(o->data, NULL, 10); |
| 3289 | if (errno || num < 0) |
| 3290 | return -1; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3291 | o_reset_to_empty_unquoted(o); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3292 | return num; |
| 3293 | } |
| 3294 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3295 | #if BB_MMU |
| 3296 | #define fetch_till_str(as_string, input, word, skip_tabs) \ |
| 3297 | fetch_till_str(input, word, skip_tabs) |
| 3298 | #endif |
| 3299 | static char *fetch_till_str(o_string *as_string, |
| 3300 | struct in_str *input, |
| 3301 | const char *word, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3302 | int heredoc_flags) |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3303 | { |
| 3304 | o_string heredoc = NULL_O_STRING; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3305 | unsigned past_EOL; |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3306 | int prev = 0; /* not \ */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3307 | int ch; |
| 3308 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3309 | goto jump_in; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3310 | while (1) { |
| 3311 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3312 | if (ch != EOF) |
| 3313 | nommu_addchr(as_string, ch); |
| 3314 | if ((ch == '\n' || ch == EOF) |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3315 | && ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') |
| 3316 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3317 | if (strcmp(heredoc.data + past_EOL, word) == 0) { |
| 3318 | heredoc.data[past_EOL] = '\0'; |
| 3319 | debug_printf_parse("parsed heredoc '%s'\n", heredoc.data); |
| 3320 | return heredoc.data; |
| 3321 | } |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3322 | while (ch == '\n') { |
| 3323 | o_addchr(&heredoc, ch); |
| 3324 | prev = ch; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3325 | jump_in: |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3326 | past_EOL = heredoc.length; |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3327 | do { |
| 3328 | ch = i_getch(input); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3329 | if (ch != EOF) |
| 3330 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3331 | } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t'); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3332 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3333 | } |
| 3334 | if (ch == EOF) { |
| 3335 | o_free_unsafe(&heredoc); |
| 3336 | return NULL; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3337 | } |
| 3338 | o_addchr(&heredoc, ch); |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 3339 | nommu_addchr(as_string, ch); |
Denys Vlasenko | c3adfac | 2010-09-06 11:46:03 +0200 | [diff] [blame] | 3340 | if (prev == '\\' && ch == '\\') |
| 3341 | /* Correctly handle foo\\<eol> (not a line cont.) */ |
| 3342 | prev = 0; /* not \ */ |
| 3343 | else |
| 3344 | prev = ch; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3348 | /* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs |
| 3349 | * and load them all. There should be exactly heredoc_cnt of them. |
| 3350 | */ |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3351 | static int fetch_heredocs(int heredoc_cnt, struct parse_context *ctx, struct in_str *input) |
| 3352 | { |
| 3353 | struct pipe *pi = ctx->list_head; |
| 3354 | |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3355 | while (pi && heredoc_cnt) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3356 | int i; |
| 3357 | struct command *cmd = pi->cmds; |
| 3358 | |
| 3359 | debug_printf_parse("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n", |
| 3360 | pi->num_cmds, |
| 3361 | cmd->argv ? cmd->argv[0] : "NONE"); |
| 3362 | for (i = 0; i < pi->num_cmds; i++) { |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3363 | struct redir_struct *redir = cmd->redirects; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3364 | |
| 3365 | debug_printf_parse("fetch_heredocs: %d cmd argv0:'%s'\n", |
| 3366 | i, cmd->argv ? cmd->argv[0] : "NONE"); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3367 | while (redir) { |
| 3368 | if (redir->rd_type == REDIRECT_HEREDOC) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3369 | char *p; |
| 3370 | |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3371 | redir->rd_type = REDIRECT_HEREDOC2; |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 3372 | /* redir->rd_dup is (ab)used to indicate <<- */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3373 | p = fetch_till_str(&ctx->as_string, input, |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 3374 | redir->rd_filename, redir->rd_dup); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3375 | if (!p) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3376 | syntax_error("unexpected EOF in here document"); |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3377 | return 1; |
| 3378 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3379 | free(redir->rd_filename); |
| 3380 | redir->rd_filename = p; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3381 | heredoc_cnt--; |
| 3382 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3383 | redir = redir->next; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3384 | } |
| 3385 | cmd++; |
| 3386 | } |
| 3387 | pi = pi->next; |
| 3388 | } |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3389 | #if 0 |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3390 | /* Should be 0. If it isn't, it's a parse error */ |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3391 | if (heredoc_cnt) |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3392 | bb_error_msg_and_die("heredoc BUG 2"); |
| 3393 | #endif |
| 3394 | return 0; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 3398 | static int run_list(struct pipe *pi); |
| 3399 | #if BB_MMU |
| 3400 | #define parse_stream(pstring, input, end_trigger) \ |
| 3401 | parse_stream(input, end_trigger) |
| 3402 | #endif |
| 3403 | static struct pipe *parse_stream(char **pstring, |
| 3404 | struct in_str *input, |
| 3405 | int end_trigger); |
Denis Vlasenko | ba7cf26 | 2007-05-25 14:34:30 +0000 | [diff] [blame] | 3406 | |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3407 | |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 3408 | #if !ENABLE_HUSH_FUNCTIONS |
| 3409 | #define parse_group(dest, ctx, input, ch) \ |
| 3410 | parse_group(ctx, input, ch) |
| 3411 | #endif |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3412 | static int parse_group(o_string *dest, struct parse_context *ctx, |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3413 | struct in_str *input, int ch) |
| 3414 | { |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3415 | /* dest contains characters seen prior to ( or {. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3416 | * Typically it's empty, but for function defs, |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3417 | * it contains function name (without '()'). */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3418 | struct pipe *pipe_list; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3419 | int endch; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3420 | struct command *command = ctx->command; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3421 | |
| 3422 | debug_printf_parse("parse_group entered\n"); |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3423 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3424 | if (ch == '(' && !dest->has_quoted_part) { |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3425 | if (dest->length) |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3426 | if (done_word(dest, ctx)) |
| 3427 | return 1; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3428 | if (!command->argv) |
| 3429 | goto skip; /* (... */ |
| 3430 | if (command->argv[1]) { /* word word ... (... */ |
| 3431 | syntax_error_unexpected_ch('('); |
| 3432 | return 1; |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3433 | } |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3434 | /* it is "word(..." or "word (..." */ |
| 3435 | do |
| 3436 | ch = i_getch(input); |
| 3437 | while (ch == ' ' || ch == '\t'); |
| 3438 | if (ch != ')') { |
| 3439 | syntax_error_unexpected_ch(ch); |
| 3440 | return 1; |
| 3441 | } |
| 3442 | nommu_addchr(&ctx->as_string, ch); |
| 3443 | do |
| 3444 | ch = i_getch(input); |
| 3445 | while (ch == ' ' || ch == '\t' || ch == '\n'); |
| 3446 | if (ch != '{') { |
| 3447 | syntax_error_unexpected_ch(ch); |
| 3448 | return 1; |
| 3449 | } |
| 3450 | nommu_addchr(&ctx->as_string, ch); |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3451 | command->cmd_type = CMD_FUNCDEF; |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 3452 | goto skip; |
Denis Vlasenko | 371de4a | 2008-10-14 12:43:13 +0000 | [diff] [blame] | 3453 | } |
| 3454 | #endif |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3455 | |
| 3456 | #if 0 /* Prevented by caller */ |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3457 | if (command->argv /* word [word]{... */ |
| 3458 | || dest->length /* word{... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 3459 | || dest->has_quoted_part /* ""{... */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3460 | ) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3461 | syntax_error(NULL); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3462 | debug_printf_parse("parse_group return 1: " |
| 3463 | "syntax error, groups and arglists don't mix\n"); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 3464 | return 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3465 | } |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 3466 | #endif |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3467 | |
| 3468 | #if ENABLE_HUSH_FUNCTIONS |
| 3469 | skip: |
| 3470 | #endif |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3471 | endch = '}'; |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 3472 | if (ch == '(') { |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 3473 | endch = ')'; |
Denys Vlasenko | 9d617c4 | 2009-06-09 18:40:52 +0200 | [diff] [blame] | 3474 | command->cmd_type = CMD_SUBSHELL; |
Denis Vlasenko | f8c1f02 | 2009-04-17 11:55:42 +0000 | [diff] [blame] | 3475 | } else { |
| 3476 | /* bash does not allow "{echo...", requires whitespace */ |
| 3477 | ch = i_getch(input); |
| 3478 | if (ch != ' ' && ch != '\t' && ch != '\n') { |
| 3479 | syntax_error_unexpected_ch(ch); |
| 3480 | return 1; |
| 3481 | } |
| 3482 | nommu_addchr(&ctx->as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3483 | } |
Denis Vlasenko | b7d8c0d | 2009-04-10 19:05:43 +0000 | [diff] [blame] | 3484 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3485 | { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3486 | #if BB_MMU |
| 3487 | # define as_string NULL |
| 3488 | #else |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3489 | char *as_string = NULL; |
| 3490 | #endif |
| 3491 | pipe_list = parse_stream(&as_string, input, endch); |
| 3492 | #if !BB_MMU |
| 3493 | if (as_string) |
| 3494 | o_addstr(&ctx->as_string, as_string); |
| 3495 | #endif |
| 3496 | /* empty ()/{} or parse error? */ |
| 3497 | if (!pipe_list || pipe_list == ERR_PTR) { |
Denis Vlasenko | bb92951 | 2009-04-16 10:59:40 +0000 | [diff] [blame] | 3498 | /* parse_stream already emitted error msg */ |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3499 | if (!BB_MMU) |
| 3500 | free(as_string); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3501 | debug_printf_parse("parse_group return 1: " |
| 3502 | "parse_stream returned %p\n", pipe_list); |
| 3503 | return 1; |
| 3504 | } |
| 3505 | command->group = pipe_list; |
| 3506 | #if !BB_MMU |
| 3507 | as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */ |
| 3508 | command->group_as_string = as_string; |
| 3509 | debug_printf_parse("end of group, remembering as:'%s'\n", |
| 3510 | command->group_as_string); |
| 3511 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3512 | #undef as_string |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 3513 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3514 | debug_printf_parse("parse_group return 0\n"); |
| 3515 | return 0; |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 3516 | /* command remains "open", available for possible redirects */ |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3519 | #if ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3520 | /* Subroutines for copying $(...) and `...` things */ |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3521 | static void add_till_backquote(o_string *dest, struct in_str *input, int in_dquote); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3522 | /* '...' */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3523 | 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] | 3524 | { |
| 3525 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3526 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3527 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3528 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3529 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3530 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3531 | if (ch == '\'') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3532 | return; |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3533 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3534 | } |
| 3535 | } |
| 3536 | /* "...\"...`..`...." - do we need to handle "...$(..)..." too? */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3537 | 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] | 3538 | { |
| 3539 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3540 | int ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3541 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3542 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3543 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3544 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3545 | if (ch == '"') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3546 | return; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3547 | if (ch == '\\') { /* \x. Copy both chars. */ |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3548 | o_addchr(dest, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3549 | ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3550 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3551 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3552 | if (ch == '`') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3553 | add_till_backquote(dest, input, /*in_dquote:*/ 1); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3554 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3555 | continue; |
| 3556 | } |
Denis Vlasenko | 5703c22 | 2008-06-15 11:49:42 +0000 | [diff] [blame] | 3557 | //if (ch == '$') ... |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3558 | } |
| 3559 | } |
| 3560 | /* Process `cmd` - copy contents until "`" is seen. Complicated by |
| 3561 | * \` quoting. |
| 3562 | * "Within the backquoted style of command substitution, backslash |
| 3563 | * shall retain its literal meaning, except when followed by: '$', '`', or '\'. |
| 3564 | * The search for the matching backquote shall be satisfied by the first |
| 3565 | * backquote found without a preceding backslash; during this search, |
| 3566 | * if a non-escaped backquote is encountered within a shell comment, |
| 3567 | * a here-document, an embedded command substitution of the $(command) |
| 3568 | * form, or a quoted string, undefined results occur. A single-quoted |
| 3569 | * or double-quoted string that begins, but does not end, within the |
| 3570 | * "`...`" sequence produces undefined results." |
| 3571 | * Example Output |
| 3572 | * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST |
| 3573 | */ |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3574 | static void add_till_backquote(o_string *dest, struct in_str *input, int in_dquote) |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3575 | { |
| 3576 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3577 | int ch = i_getch(input); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3578 | if (ch == '`') |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3579 | return; |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3580 | if (ch == '\\') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3581 | /* \x. Copy both unless it is \`, \$, \\ and maybe \" */ |
| 3582 | ch = i_getch(input); |
| 3583 | if (ch != '`' |
| 3584 | && ch != '$' |
| 3585 | && ch != '\\' |
| 3586 | && (!in_dquote || ch != '"') |
| 3587 | ) { |
| 3588 | o_addchr(dest, '\\'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3589 | } |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3590 | } |
| 3591 | if (ch == EOF) { |
| 3592 | syntax_error_unterm_ch('`'); |
| 3593 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3594 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3595 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3596 | } |
| 3597 | } |
| 3598 | /* Process $(cmd) - copy contents until ")" is seen. Complicated by |
| 3599 | * quoting and nested ()s. |
| 3600 | * "With the $(command) style of command substitution, all characters |
| 3601 | * following the open parenthesis to the matching closing parenthesis |
| 3602 | * constitute the command. Any valid shell script can be used for command, |
| 3603 | * except a script consisting solely of redirections which produces |
| 3604 | * unspecified results." |
| 3605 | * Example Output |
| 3606 | * echo $(echo '(TEST)' BEST) (TEST) BEST |
| 3607 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 3608 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3609 | * |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3610 | * Also adapted to eat ${var%...} and $((...)) constructs, since ... part |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3611 | * can contain arbitrary constructs, just like $(cmd). |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3612 | * In bash compat mode, it needs to also be able to stop on ':' or '/' |
| 3613 | * for ${var:N[:M]} and ${var/P[/R]} parsing. |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3614 | */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3615 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3616 | 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] | 3617 | { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3618 | int ch; |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3619 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3620 | # if ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3621 | char end_char2 = end_ch >> 8; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3622 | # endif |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3623 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1); |
| 3624 | |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3625 | while (1) { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3626 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3627 | if (ch == EOF) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3628 | syntax_error_unterm_ch(end_ch); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3629 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3630 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3631 | if (ch == end_ch IF_HUSH_BASH_COMPAT( || ch == end_char2)) { |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3632 | if (!dbl) |
| 3633 | break; |
| 3634 | /* we look for closing )) of $((EXPR)) */ |
| 3635 | if (i_peek(input) == end_ch) { |
| 3636 | i_getch(input); /* eat second ')' */ |
| 3637 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3638 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3639 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3640 | o_addchr(dest, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3641 | if (ch == '(' || ch == '{') { |
| 3642 | ch = (ch == '(' ? ')' : '}'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3643 | add_till_closing_bracket(dest, input, ch); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3644 | o_addchr(dest, ch); |
| 3645 | continue; |
| 3646 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3647 | if (ch == '\'') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3648 | add_till_single_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3649 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3650 | continue; |
| 3651 | } |
| 3652 | if (ch == '"') { |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3653 | add_till_double_quote(dest, input); |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3654 | o_addchr(dest, ch); |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3655 | continue; |
| 3656 | } |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3657 | if (ch == '`') { |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3658 | add_till_backquote(dest, input, /*in_dquote:*/ 0); |
Denys Vlasenko | a6ad397 | 2010-05-22 00:26:06 +0200 | [diff] [blame] | 3659 | o_addchr(dest, ch); |
| 3660 | continue; |
| 3661 | } |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3662 | if (ch == '\\') { |
| 3663 | /* \x. Copy verbatim. Important for \(, \) */ |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3664 | ch = i_getch(input); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3665 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3666 | syntax_error_unterm_ch(')'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3667 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 3668 | } |
Denis Vlasenko | 82dfec3 | 2008-06-16 12:47:11 +0000 | [diff] [blame] | 3669 | o_addchr(dest, ch); |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3670 | continue; |
| 3671 | } |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3672 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3673 | return ch; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3674 | } |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3675 | #endif /* ENABLE_HUSH_TICK || ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_DOLLAR_OPS */ |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 3676 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 3677 | /* Return code: 0 for OK, 1 for syntax error */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3678 | #if BB_MMU |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3679 | #define parse_dollar(as_string, dest, input, quote_mask) \ |
| 3680 | parse_dollar(dest, input, quote_mask) |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3681 | #define as_string NULL |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3682 | #endif |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3683 | static int parse_dollar(o_string *as_string, |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3684 | o_string *dest, |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3685 | struct in_str *input, unsigned char quote_mask) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3686 | { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3687 | int ch = i_peek(input); /* first character after the $ */ |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3688 | |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3689 | debug_printf_parse("parse_dollar entered: ch='%c'\n", ch); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3690 | if (isalpha(ch)) { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3691 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3692 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d498131 | 2008-07-31 10:34:48 +0000 | [diff] [blame] | 3693 | make_var: |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3694 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3695 | while (1) { |
Denis Vlasenko | e0a3367 | 2007-05-10 23:06:55 +0000 | [diff] [blame] | 3696 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3697 | o_addchr(dest, ch | quote_mask); |
Denis Vlasenko | 1f4cf51 | 2007-05-16 10:39:24 +0000 | [diff] [blame] | 3698 | quote_mask = 0; |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3699 | ch = i_peek(input); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3700 | if (!isalnum(ch) && ch != '_') |
| 3701 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3702 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3703 | nommu_addchr(as_string, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3704 | } |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3705 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3706 | } else if (isdigit(ch)) { |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3707 | make_one_char_var: |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3708 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3709 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3710 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | 602d13c | 2007-05-13 18:34:53 +0000 | [diff] [blame] | 3711 | debug_printf_parse(": '%c'\n", ch); |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 3712 | o_addchr(dest, ch | quote_mask); |
| 3713 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3714 | } else switch (ch) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3715 | case '$': /* pid */ |
| 3716 | case '!': /* last bg pid */ |
| 3717 | case '?': /* last exit code */ |
| 3718 | case '#': /* number of args */ |
| 3719 | case '*': /* args */ |
| 3720 | case '@': /* args */ |
| 3721 | goto make_one_char_var; |
| 3722 | case '{': { |
Mike Frysinger | ef3e7fd | 2009-06-01 14:13:39 -0400 | [diff] [blame] | 3723 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3724 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3725 | ch = i_getch(input); /* eat '{' */ |
| 3726 | nommu_addchr(as_string, ch); |
| 3727 | |
| 3728 | ch = i_getch(input); /* first char after '{' */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3729 | /* It should be ${?}, or ${#var}, |
| 3730 | * or even ${?+subst} - operator acting on a special variable, |
| 3731 | * or the beginning of variable name. |
| 3732 | */ |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3733 | if (ch == EOF |
| 3734 | || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */ |
| 3735 | ) { |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3736 | bad_dollar_syntax: |
| 3737 | syntax_error_unterm_str("${name}"); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3738 | debug_printf_parse("parse_dollar return 1: unterminated ${name}\n"); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3739 | return 1; |
| 3740 | } |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3741 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3742 | ch |= quote_mask; |
| 3743 | |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3744 | /* 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] | 3745 | * However, this regresses some of our testsuite cases |
| 3746 | * which check invalid constructs like ${%}. |
| 3747 | * Oh well... let's check that the var name part is fine... */ |
| 3748 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3749 | while (1) { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3750 | unsigned pos; |
| 3751 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3752 | o_addchr(dest, ch); |
| 3753 | debug_printf_parse(": '%c'\n", ch); |
| 3754 | |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3755 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3756 | nommu_addchr(as_string, ch); |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3757 | if (ch == '}') |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3758 | break; |
Mike Frysinger | 98c5264 | 2009-04-02 10:02:37 +0000 | [diff] [blame] | 3759 | |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3760 | if (!isalnum(ch) && ch != '_') { |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3761 | unsigned end_ch; |
| 3762 | unsigned char last_ch; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3763 | /* handle parameter expansions |
| 3764 | * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02 |
| 3765 | */ |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3766 | if (!strchr(VAR_SUBST_OPS, ch)) /* ${var<bad_char>... */ |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3767 | goto bad_dollar_syntax; |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3768 | |
| 3769 | /* Eat everything until closing '}' (or ':') */ |
| 3770 | end_ch = '}'; |
| 3771 | if (ENABLE_HUSH_BASH_COMPAT |
| 3772 | && ch == ':' |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3773 | && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input)) |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3774 | ) { |
| 3775 | /* It's ${var:N[:M]} thing */ |
| 3776 | end_ch = '}' * 0x100 + ':'; |
| 3777 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3778 | if (ENABLE_HUSH_BASH_COMPAT |
| 3779 | && ch == '/' |
| 3780 | ) { |
| 3781 | /* It's ${var/[/]pattern[/repl]} thing */ |
| 3782 | if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */ |
| 3783 | i_getch(input); |
| 3784 | nommu_addchr(as_string, '/'); |
| 3785 | ch = '\\'; |
| 3786 | } |
| 3787 | end_ch = '}' * 0x100 + '/'; |
| 3788 | } |
| 3789 | o_addchr(dest, ch); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3790 | again: |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3791 | if (!BB_MMU) |
| 3792 | pos = dest->length; |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3793 | #if ENABLE_HUSH_DOLLAR_OPS |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3794 | last_ch = add_till_closing_bracket(dest, input, end_ch); |
Denys Vlasenko | 9297dbc | 2010-07-05 21:37:12 +0200 | [diff] [blame] | 3795 | #else |
| 3796 | #error Simple code to only allow ${var} is not implemented |
| 3797 | #endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3798 | if (as_string) { |
| 3799 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3800 | o_addchr(as_string, last_ch); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3801 | } |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3802 | |
| 3803 | if (ENABLE_HUSH_BASH_COMPAT && (end_ch & 0xff00)) { |
| 3804 | /* close the first block: */ |
| 3805 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3806 | /* while parsing N from ${var:N[:M]} |
| 3807 | * or pattern from ${var/[/]pattern[/repl]} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3808 | if ((end_ch & 0xff) == last_ch) { |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3809 | /* got ':' or '/'- parse the rest */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3810 | end_ch = '}'; |
| 3811 | goto again; |
| 3812 | } |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 3813 | /* got '}' */ |
| 3814 | if (end_ch == '}' * 0x100 + ':') { |
| 3815 | /* it's ${var:N} - emulate :999999999 */ |
| 3816 | o_addstr(dest, "999999999"); |
| 3817 | } /* else: it's ${var/[/]pattern} */ |
Denys Vlasenko | 1e811b1 | 2010-05-22 03:12:29 +0200 | [diff] [blame] | 3818 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3819 | break; |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3820 | } |
Denys Vlasenko | 7436950 | 2010-05-21 19:52:01 +0200 | [diff] [blame] | 3821 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3822 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3823 | break; |
| 3824 | } |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 3825 | #if ENABLE_SH_MATH_SUPPORT || ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3826 | case '(': { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3827 | unsigned pos; |
| 3828 | |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3829 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3830 | nommu_addchr(as_string, ch); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3831 | # if ENABLE_SH_MATH_SUPPORT |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3832 | if (i_peek(input) == '(') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3833 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3834 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3835 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3836 | o_addchr(dest, /*quote_mask |*/ '+'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3837 | if (!BB_MMU) |
| 3838 | pos = dest->length; |
| 3839 | add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3840 | if (as_string) { |
| 3841 | o_addstr(as_string, dest->data + pos); |
| 3842 | o_addchr(as_string, ')'); |
| 3843 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3844 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3845 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3846 | break; |
Denis Vlasenko | 76db5ad | 2008-06-12 12:58:20 +0000 | [diff] [blame] | 3847 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3848 | # endif |
| 3849 | # if ENABLE_HUSH_TICK |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3850 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3851 | o_addchr(dest, quote_mask | '`'); |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3852 | if (!BB_MMU) |
| 3853 | pos = dest->length; |
| 3854 | add_till_closing_bracket(dest, input, ')'); |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 3855 | if (as_string) { |
| 3856 | o_addstr(as_string, dest->data + pos); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 3857 | o_addchr(as_string, ')'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 3858 | } |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3859 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3860 | # endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3861 | break; |
| 3862 | } |
Denis Vlasenko | d85a5df | 2009-04-05 08:43:57 +0000 | [diff] [blame] | 3863 | #endif |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3864 | case '_': |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 3865 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3866 | nommu_addchr(as_string, ch); |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 3867 | ch = i_peek(input); |
| 3868 | if (isalnum(ch)) { /* it's $_name or $_123 */ |
| 3869 | ch = '_'; |
| 3870 | goto make_var; |
| 3871 | } |
| 3872 | /* else: it's $_ */ |
Denys Vlasenko | 69b1cef | 2009-09-21 10:21:44 +0200 | [diff] [blame] | 3873 | /* TODO: $_ and $-: */ |
| 3874 | /* $_ Shell or shell script name; or last argument of last command |
| 3875 | * (if last command wasn't a pipe; if it was, bash sets $_ to ""); |
| 3876 | * 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] | 3877 | /* $- Option flags set by set builtin or shell options (-i etc) */ |
| 3878 | default: |
| 3879 | o_addQchr(dest, '$'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3880 | } |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3881 | debug_printf_parse("parse_dollar return 0\n"); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3882 | return 0; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3883 | #undef as_string |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3886 | #if BB_MMU |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3887 | # if ENABLE_HUSH_BASH_COMPAT |
| 3888 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 3889 | encode_string(dest, input, dquote_end, process_bkslash) |
| 3890 | # else |
| 3891 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 3892 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 3893 | encode_string(dest, input, dquote_end) |
| 3894 | # endif |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3895 | #define as_string NULL |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3896 | |
| 3897 | #else /* !MMU */ |
| 3898 | |
| 3899 | # if ENABLE_HUSH_BASH_COMPAT |
| 3900 | /* all parameters are needed, no macro tricks */ |
| 3901 | # else |
| 3902 | #define encode_string(as_string, dest, input, dquote_end, process_bkslash) \ |
| 3903 | encode_string(as_string, dest, input, dquote_end) |
| 3904 | # endif |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3905 | #endif |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3906 | static int encode_string(o_string *as_string, |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3907 | o_string *dest, |
| 3908 | struct in_str *input, |
Denys Vlasenko | 14e289b | 2010-09-10 10:15:18 +0200 | [diff] [blame] | 3909 | int dquote_end, |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3910 | int process_bkslash) |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3911 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3912 | #if !ENABLE_HUSH_BASH_COMPAT |
| 3913 | const int process_bkslash = 1; |
| 3914 | #endif |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 3915 | int ch; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3916 | int next; |
| 3917 | |
| 3918 | again: |
| 3919 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 3920 | if (ch != EOF) |
| 3921 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3922 | if (ch == dquote_end) { /* may be only '"' or EOF */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3923 | debug_printf_parse("encode_string return 0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3924 | return 0; |
| 3925 | } |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 3926 | /* note: can't move it above ch == dquote_end check! */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3927 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 3928 | syntax_error_unterm_ch('"'); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3929 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3930 | } |
| 3931 | next = '\0'; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3932 | if (ch != '\n') { |
| 3933 | next = i_peek(input); |
| 3934 | } |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 3935 | debug_printf_parse("\" ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 3936 | ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3937 | if (process_bkslash && ch == '\\') { |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3938 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 3939 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 3940 | xfunc_die(); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3941 | } |
| 3942 | /* bash: |
| 3943 | * "The backslash retains its special meaning [in "..."] |
| 3944 | * only when followed by one of the following characters: |
| 3945 | * $, `, ", \, or <newline>. A double quote may be quoted |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 3946 | * within double quotes by preceding it with a backslash." |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3947 | * NB: in (unquoted) heredoc, above does not apply to ", |
| 3948 | * therefore we check for it by "next == dquote_end" cond. |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3949 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3950 | if (next == dquote_end || strchr("$`\\\n", next)) { |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 3951 | ch = i_getch(input); /* eat next */ |
| 3952 | if (ch == '\n') |
| 3953 | goto again; /* skip \<newline> */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 3954 | } /* else: ch remains == '\\', and we double it below: */ |
| 3955 | o_addqchr(dest, ch); /* \c if c is a glob char, else just c */ |
Denys Vlasenko | 850b15b | 2010-09-09 12:58:19 +0200 | [diff] [blame] | 3956 | nommu_addchr(as_string, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3957 | goto again; |
| 3958 | } |
| 3959 | if (ch == '$') { |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 3960 | if (parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80) != 0) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 3961 | debug_printf_parse("encode_string return 1: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 3962 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3963 | return 1; |
| 3964 | } |
| 3965 | goto again; |
| 3966 | } |
| 3967 | #if ENABLE_HUSH_TICK |
| 3968 | if (ch == '`') { |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3969 | //unsigned pos = dest->length; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3970 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3971 | o_addchr(dest, 0x80 | '`'); |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 3972 | add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"'); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3973 | o_addchr(dest, SPECIAL_VAR_SYMBOL); |
| 3974 | //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos); |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 3975 | goto again; |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3976 | } |
| 3977 | #endif |
Denis Vlasenko | f328e00 | 2009-04-02 16:55:38 +0000 | [diff] [blame] | 3978 | o_addQchr(dest, ch); |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3979 | goto again; |
Denys Vlasenko | ddc62f6 | 2010-05-22 00:53:32 +0200 | [diff] [blame] | 3980 | #undef as_string |
Denis Vlasenko | 2f1d394 | 2009-04-02 16:31:29 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3983 | /* |
| 3984 | * Scan input until EOF or end_trigger char. |
| 3985 | * Return a list of pipes to execute, or NULL on EOF |
| 3986 | * or if end_trigger character is met. |
| 3987 | * On syntax error, exit is shell is not interactive, |
| 3988 | * reset parsing machinery and start parsing anew, |
| 3989 | * or return ERR_PTR. |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 3990 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 3991 | static struct pipe *parse_stream(char **pstring, |
| 3992 | struct in_str *input, |
| 3993 | int end_trigger) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3994 | { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 3995 | struct parse_context ctx; |
| 3996 | o_string dest = NULL_O_STRING; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 3997 | int heredoc_cnt; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 3998 | |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 3999 | /* 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] | 4000 | * found. When recursing, quote state is passed in via dest->o_expflags. |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4001 | */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4002 | debug_printf_parse("parse_stream entered, end_trigger='%c'\n", |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 4003 | end_trigger ? end_trigger : 'X'); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4004 | debug_enter(); |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4005 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 4006 | /* If very first arg is "" or '', dest.data may end up NULL. |
| 4007 | * Preventing this: */ |
| 4008 | o_addchr(&dest, '\0'); |
| 4009 | dest.length = 0; |
| 4010 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4011 | /* We used to separate words on $IFS here. This was wrong. |
| 4012 | * $IFS is used only for word splitting when $var is expanded, |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4013 | * here we should use blank chars as separators, not $IFS |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4014 | */ |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4015 | |
| 4016 | reset: /* we come back here only on syntax errors in interactive shell */ |
| 4017 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4018 | #if ENABLE_HUSH_INTERACTIVE |
| 4019 | input->promptmode = 0; /* PS1 */ |
| 4020 | #endif |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4021 | if (MAYBE_ASSIGNMENT != 0) |
| 4022 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4023 | initialize_context(&ctx); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4024 | heredoc_cnt = 0; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4025 | while (1) { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4026 | const char *is_blank; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4027 | const char *is_special; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4028 | int ch; |
| 4029 | int next; |
| 4030 | int redir_fd; |
| 4031 | redir_type redir_style; |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4032 | |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4033 | ch = i_getch(input); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4034 | debug_printf_parse(": ch=%c (%d) escape=%d\n", |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 4035 | ch, ch, !!(dest.o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4036 | if (ch == EOF) { |
| 4037 | struct pipe *pi; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4038 | |
| 4039 | if (heredoc_cnt) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4040 | syntax_error_unterm_str("here document"); |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4041 | goto parse_error; |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4042 | } |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4043 | /* end_trigger == '}' case errors out earlier, |
| 4044 | * checking only ')' */ |
| 4045 | if (end_trigger == ')') { |
| 4046 | syntax_error_unterm_ch('('); /* exits */ |
| 4047 | /* goto parse_error; */ |
| 4048 | } |
| 4049 | |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4050 | if (done_word(&dest, &ctx)) { |
Denys Vlasenko | b1cfc45 | 2009-05-02 17:18:34 +0200 | [diff] [blame] | 4051 | goto parse_error; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4052 | } |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4053 | o_free(&dest); |
| 4054 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4055 | pi = ctx.list_head; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4056 | /* If we got nothing... */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4057 | /* (this makes bare "&" cmd a no-op. |
| 4058 | * bash says: "syntax error near unexpected token '&'") */ |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4059 | if (pi->num_cmds == 0 |
| 4060 | IF_HAS_KEYWORDS( && pi->res_word == RES_NONE) |
| 4061 | ) { |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4062 | free_pipe_list(pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4063 | pi = NULL; |
| 4064 | } |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4065 | #if !BB_MMU |
| 4066 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4067 | if (pstring) |
| 4068 | *pstring = ctx.as_string.data; |
| 4069 | else |
| 4070 | o_free_unsafe(&ctx.as_string); |
| 4071 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4072 | debug_leave(); |
| 4073 | debug_printf_parse("parse_stream return %p\n", pi); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4074 | return pi; |
Denis Vlasenko | 1a73586 | 2007-05-23 00:32:25 +0000 | [diff] [blame] | 4075 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4076 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4077 | |
| 4078 | next = '\0'; |
| 4079 | if (ch != '\n') |
| 4080 | next = i_peek(input); |
| 4081 | |
| 4082 | is_special = "{}<>;&|()#'" /* special outside of "str" */ |
| 4083 | "\\$\"" IF_HUSH_TICK("`"); /* always special */ |
| 4084 | /* Are { and } special here? */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4085 | if (ctx.command->argv /* word [word]{... - non-special */ |
| 4086 | || dest.length /* word{... - non-special */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4087 | || dest.has_quoted_part /* ""{... - non-special */ |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4088 | || (next != ';' /* }; - special */ |
| 4089 | && next != ')' /* }) - special */ |
| 4090 | && next != '&' /* }& and }&& ... - special */ |
| 4091 | && next != '|' /* }|| ... - special */ |
| 4092 | && !strchr(defifs, next) /* {word - non-special */ |
Denys Vlasenko | 3227d3f | 2010-05-17 09:49:47 +0200 | [diff] [blame] | 4093 | ) |
Denys Vlasenko | d8389ad | 2009-11-16 03:18:46 +0100 | [diff] [blame] | 4094 | ) { |
| 4095 | /* They are not special, skip "{}" */ |
| 4096 | is_special += 2; |
| 4097 | } |
| 4098 | is_special = strchr(is_special, ch); |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4099 | is_blank = strchr(defifs, ch); |
Denis Vlasenko | 6da69cd | 2009-04-04 12:12:58 +0000 | [diff] [blame] | 4100 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4101 | if (!is_special && !is_blank) { /* ordinary char */ |
Denis Vlasenko | bf25fbc | 2009-04-19 13:57:51 +0000 | [diff] [blame] | 4102 | ordinary_char: |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4103 | o_addQchr(&dest, ch); |
| 4104 | if ((dest.o_assignment == MAYBE_ASSIGNMENT |
| 4105 | || dest.o_assignment == WORD_IS_KEYWORD) |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4106 | && ch == '=' |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4107 | && is_well_formed_var_name(dest.data, '=') |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4108 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4109 | dest.o_assignment = DEFINITELY_ASSIGNMENT; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4110 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4111 | continue; |
| 4112 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4113 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4114 | if (is_blank) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4115 | if (done_word(&dest, &ctx)) { |
| 4116 | goto parse_error; |
Eric Andersen | aac75e5 | 2001-04-30 18:18:45 +0000 | [diff] [blame] | 4117 | } |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4118 | if (ch == '\n') { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4119 | /* Is this a case when newline is simply ignored? |
| 4120 | * Some examples: |
| 4121 | * "cmd | <newline> cmd ..." |
| 4122 | * "case ... in <newline> word) ..." |
| 4123 | */ |
| 4124 | if (IS_NULL_CMD(ctx.command) |
| 4125 | && dest.length == 0 && !dest.has_quoted_part |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4126 | ) { |
| 4127 | continue; |
| 4128 | } |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4129 | /* Treat newline as a command separator. */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4130 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4131 | debug_printf_parse("heredoc_cnt:%d\n", heredoc_cnt); |
| 4132 | if (heredoc_cnt) { |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4133 | if (fetch_heredocs(heredoc_cnt, &ctx, input)) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4134 | goto parse_error; |
Denis Vlasenko | 3dfb035 | 2009-04-08 09:29:14 +0000 | [diff] [blame] | 4135 | } |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4136 | heredoc_cnt = 0; |
| 4137 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4138 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4139 | ch = ';'; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4140 | /* note: if (is_blank) continue; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4141 | * will still trigger for us */ |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4142 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4143 | } |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4144 | |
| 4145 | /* "cmd}" or "cmd }..." without semicolon or &: |
| 4146 | * } is an ordinary char in this case, even inside { cmd; } |
| 4147 | * Pathological example: { ""}; } should exec "}" cmd |
| 4148 | */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4149 | if (ch == '}') { |
| 4150 | if (!IS_NULL_CMD(ctx.command) /* cmd } */ |
| 4151 | || dest.length != 0 /* word} */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4152 | || dest.has_quoted_part /* ""} */ |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4153 | ) { |
| 4154 | goto ordinary_char; |
| 4155 | } |
| 4156 | if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */ |
| 4157 | goto skip_end_trigger; |
| 4158 | /* else: } does terminate a group */ |
Denis Vlasenko | 9f8d938 | 2009-04-19 14:03:11 +0000 | [diff] [blame] | 4159 | } |
| 4160 | |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4161 | if (end_trigger && end_trigger == ch |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4162 | && (ch != ';' || heredoc_cnt == 0) |
| 4163 | #if ENABLE_HUSH_CASE |
| 4164 | && (ch != ')' |
| 4165 | || ctx.ctx_res_w != RES_MATCH |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4166 | || (!dest.has_quoted_part && strcmp(dest.data, "esac") == 0) |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4167 | ) |
| 4168 | #endif |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4169 | ) { |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4170 | if (heredoc_cnt) { |
| 4171 | /* This is technically valid: |
| 4172 | * { cat <<HERE; }; echo Ok |
| 4173 | * heredoc |
| 4174 | * heredoc |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4175 | * HERE |
| 4176 | * but we don't support this. |
| 4177 | * We require heredoc to be in enclosing {}/(), |
| 4178 | * if any. |
| 4179 | */ |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4180 | syntax_error_unterm_str("here document"); |
Denis Vlasenko | 6c9be7f | 2009-04-07 02:29:51 +0000 | [diff] [blame] | 4181 | goto parse_error; |
| 4182 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4183 | if (done_word(&dest, &ctx)) { |
| 4184 | goto parse_error; |
| 4185 | } |
| 4186 | done_pipe(&ctx, PIPE_SEQ); |
| 4187 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Denis Vlasenko | 240c255 | 2009-04-03 03:45:05 +0000 | [diff] [blame] | 4188 | /* Do we sit outside of any if's, loops or case's? */ |
Denis Vlasenko | 3718168 | 2009-04-03 03:19:15 +0000 | [diff] [blame] | 4189 | if (!HAS_KEYWORDS |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4190 | 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] | 4191 | ) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4192 | o_free(&dest); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4193 | #if !BB_MMU |
| 4194 | debug_printf_parse("as_string '%s'\n", ctx.as_string.data); |
| 4195 | if (pstring) |
| 4196 | *pstring = ctx.as_string.data; |
| 4197 | else |
| 4198 | o_free_unsafe(&ctx.as_string); |
| 4199 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4200 | debug_leave(); |
| 4201 | debug_printf_parse("parse_stream return %p: " |
| 4202 | "end_trigger char found\n", |
| 4203 | ctx.list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4204 | return ctx.list_head; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4205 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4206 | } |
Denis Vlasenko | dcd78c4 | 2009-04-19 23:07:51 +0000 | [diff] [blame] | 4207 | skip_end_trigger: |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4208 | if (is_blank) |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4209 | continue; |
Denis Vlasenko | 55789c6 | 2008-06-18 16:30:42 +0000 | [diff] [blame] | 4210 | |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4211 | /* Catch <, > before deciding whether this word is |
| 4212 | * an assignment. a=1 2>z b=2: b=2 is still assignment */ |
| 4213 | switch (ch) { |
| 4214 | case '>': |
| 4215 | redir_fd = redirect_opt_num(&dest); |
| 4216 | if (done_word(&dest, &ctx)) { |
| 4217 | goto parse_error; |
| 4218 | } |
| 4219 | redir_style = REDIRECT_OVERWRITE; |
| 4220 | if (next == '>') { |
| 4221 | redir_style = REDIRECT_APPEND; |
| 4222 | ch = i_getch(input); |
| 4223 | nommu_addchr(&ctx.as_string, ch); |
| 4224 | } |
| 4225 | #if 0 |
| 4226 | else if (next == '(') { |
| 4227 | syntax_error(">(process) not supported"); |
| 4228 | goto parse_error; |
| 4229 | } |
| 4230 | #endif |
| 4231 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4232 | goto parse_error; |
| 4233 | continue; /* back to top of while (1) */ |
| 4234 | case '<': |
| 4235 | redir_fd = redirect_opt_num(&dest); |
| 4236 | if (done_word(&dest, &ctx)) { |
| 4237 | goto parse_error; |
| 4238 | } |
| 4239 | redir_style = REDIRECT_INPUT; |
| 4240 | if (next == '<') { |
| 4241 | redir_style = REDIRECT_HEREDOC; |
| 4242 | heredoc_cnt++; |
| 4243 | debug_printf_parse("++heredoc_cnt=%d\n", heredoc_cnt); |
| 4244 | ch = i_getch(input); |
| 4245 | nommu_addchr(&ctx.as_string, ch); |
| 4246 | } else if (next == '>') { |
| 4247 | redir_style = REDIRECT_IO; |
| 4248 | ch = i_getch(input); |
| 4249 | nommu_addchr(&ctx.as_string, ch); |
| 4250 | } |
| 4251 | #if 0 |
| 4252 | else if (next == '(') { |
| 4253 | syntax_error("<(process) not supported"); |
| 4254 | goto parse_error; |
| 4255 | } |
| 4256 | #endif |
| 4257 | if (parse_redirect(&ctx, redir_fd, redir_style, input)) |
| 4258 | goto parse_error; |
| 4259 | continue; /* back to top of while (1) */ |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4260 | case '#': |
| 4261 | if (dest.length == 0 && !dest.has_quoted_part) { |
| 4262 | /* skip "#comment" */ |
| 4263 | while (1) { |
| 4264 | ch = i_peek(input); |
| 4265 | if (ch == EOF || ch == '\n') |
| 4266 | break; |
| 4267 | i_getch(input); |
| 4268 | /* note: we do not add it to &ctx.as_string */ |
| 4269 | } |
| 4270 | nommu_addchr(&ctx.as_string, '\n'); |
| 4271 | continue; /* back to top of while (1) */ |
| 4272 | } |
| 4273 | break; |
| 4274 | case '\\': |
| 4275 | if (next == '\n') { |
| 4276 | /* It's "\<newline>" */ |
| 4277 | #if !BB_MMU |
| 4278 | /* Remove trailing '\' from ctx.as_string */ |
| 4279 | ctx.as_string.data[--ctx.as_string.length] = '\0'; |
| 4280 | #endif |
| 4281 | ch = i_getch(input); /* eat it */ |
| 4282 | continue; /* back to top of while (1) */ |
| 4283 | } |
| 4284 | break; |
Denis Vlasenko | c96865f | 2009-04-10 00:20:58 +0000 | [diff] [blame] | 4285 | } |
| 4286 | |
| 4287 | if (dest.o_assignment == MAYBE_ASSIGNMENT |
| 4288 | /* check that we are not in word in "a=1 2>word b=1": */ |
| 4289 | && !ctx.pending_redirect |
| 4290 | ) { |
| 4291 | /* ch is a special char and thus this word |
| 4292 | * cannot be an assignment */ |
| 4293 | dest.o_assignment = NOT_ASSIGNMENT; |
| 4294 | } |
| 4295 | |
Denys Vlasenko | cbfe6ad | 2009-08-12 19:47:44 +0200 | [diff] [blame] | 4296 | /* Note: nommu_addchr(&ctx.as_string, ch) is already done */ |
| 4297 | |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4298 | switch (ch) { |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4299 | case '#': /* non-comment #: "echo a#b" etc */ |
| 4300 | o_addQchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4301 | break; |
| 4302 | case '\\': |
| 4303 | if (next == EOF) { |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 4304 | syntax_error("\\<eof>"); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4305 | xfunc_die(); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4306 | } |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4307 | ch = i_getch(input); |
Denys Vlasenko | 7b4c0fd | 2010-11-22 17:58:14 +0100 | [diff] [blame] | 4308 | /* note: ch != '\n' (that case does not reach this place) */ |
| 4309 | o_addchr(&dest, '\\'); |
| 4310 | /*nommu_addchr(&ctx.as_string, '\\'); - already done */ |
| 4311 | o_addchr(&dest, ch); |
| 4312 | nommu_addchr(&ctx.as_string, ch); |
| 4313 | /* Example: echo Hello \2>file |
| 4314 | * we need to know that word 2 is quoted */ |
| 4315 | dest.has_quoted_part = 1; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4316 | break; |
| 4317 | case '$': |
Denys Vlasenko | 101a4e3 | 2010-09-09 14:04:57 +0200 | [diff] [blame] | 4318 | if (parse_dollar(&ctx.as_string, &dest, input, /*quote_mask:*/ 0) != 0) { |
Denis Vlasenko | a24c8ca | 2009-04-04 15:24:40 +0000 | [diff] [blame] | 4319 | debug_printf_parse("parse_stream parse error: " |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4320 | "parse_dollar returned non-0\n"); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4321 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4322 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4323 | break; |
| 4324 | case '\'': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4325 | dest.has_quoted_part = 1; |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4326 | while (1) { |
Denis Vlasenko | 46ccdcb | 2008-06-10 18:05:12 +0000 | [diff] [blame] | 4327 | ch = i_getch(input); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4328 | if (ch == EOF) { |
Denis Vlasenko | d68ae08 | 2009-04-09 20:41:34 +0000 | [diff] [blame] | 4329 | syntax_error_unterm_ch('\''); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 4330 | /*xfunc_die(); - redundant */ |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4331 | } |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4332 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4333 | if (ch == '\'') |
Denis Vlasenko | 0c886c6 | 2007-01-30 22:30:09 +0000 | [diff] [blame] | 4334 | break; |
Denys Vlasenko | e640cb4 | 2009-05-28 16:49:11 +0200 | [diff] [blame] | 4335 | o_addqchr(&dest, ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4336 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4337 | break; |
| 4338 | case '"': |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4339 | dest.has_quoted_part = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4340 | if (dest.o_assignment == NOT_ASSIGNMENT) |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4341 | dest.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4342 | if (encode_string(&ctx.as_string, &dest, input, '"', /*process_bkslash:*/ 1)) |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4343 | goto parse_error; |
Denys Vlasenko | 5b6210c | 2010-09-09 13:32:21 +0200 | [diff] [blame] | 4344 | dest.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4345 | break; |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4346 | #if ENABLE_HUSH_TICK |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4347 | case '`': { |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4348 | unsigned pos; |
| 4349 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4350 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4351 | o_addchr(&dest, '`'); |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4352 | pos = dest.length; |
Denys Vlasenko | acd5bc8 | 2010-09-12 15:05:39 +0200 | [diff] [blame] | 4353 | add_till_backquote(&dest, input, /*in_dquote:*/ 0); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4354 | # if !BB_MMU |
Denis Vlasenko | 5c090a9 | 2009-04-08 21:51:33 +0000 | [diff] [blame] | 4355 | o_addstr(&ctx.as_string, dest.data + pos); |
| 4356 | o_addchr(&ctx.as_string, '`'); |
Denys Vlasenko | 2e48d53 | 2010-05-22 17:30:39 +0200 | [diff] [blame] | 4357 | # endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4358 | o_addchr(&dest, SPECIAL_VAR_SYMBOL); |
| 4359 | //debug_printf_subst("SUBST RES3 '%s'\n", dest.data + pos); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4360 | break; |
Denis Vlasenko | 7b4f3f1 | 2008-06-10 18:04:32 +0000 | [diff] [blame] | 4361 | } |
Denis Vlasenko | 14b5dd9 | 2007-05-20 21:51:38 +0000 | [diff] [blame] | 4362 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4363 | case ';': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4364 | #if ENABLE_HUSH_CASE |
| 4365 | case_semi: |
| 4366 | #endif |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4367 | if (done_word(&dest, &ctx)) { |
| 4368 | goto parse_error; |
| 4369 | } |
| 4370 | done_pipe(&ctx, PIPE_SEQ); |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4371 | #if ENABLE_HUSH_CASE |
| 4372 | /* Eat multiple semicolons, detect |
| 4373 | * whether it means something special */ |
| 4374 | while (1) { |
| 4375 | ch = i_peek(input); |
| 4376 | if (ch != ';') |
| 4377 | break; |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4378 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4379 | nommu_addchr(&ctx.as_string, ch); |
Denys Vlasenko | e9bda90 | 2009-05-23 16:50:07 +0200 | [diff] [blame] | 4380 | if (ctx.ctx_res_w == RES_CASE_BODY) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4381 | ctx.ctx_dsemicolon = 1; |
| 4382 | ctx.ctx_res_w = RES_MATCH; |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4383 | break; |
| 4384 | } |
| 4385 | } |
| 4386 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4387 | new_cmd: |
| 4388 | /* We just finished a cmd. New one may start |
| 4389 | * with an assignment */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4390 | dest.o_assignment = MAYBE_ASSIGNMENT; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4391 | break; |
| 4392 | case '&': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4393 | if (done_word(&dest, &ctx)) { |
| 4394 | goto parse_error; |
| 4395 | } |
Denis Vlasenko | bb81c58 | 2007-01-30 22:32:09 +0000 | [diff] [blame] | 4396 | if (next == '&') { |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4397 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4398 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4399 | done_pipe(&ctx, PIPE_AND); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4400 | } else { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4401 | done_pipe(&ctx, PIPE_BG); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4402 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4403 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4404 | case '|': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4405 | if (done_word(&dest, &ctx)) { |
| 4406 | goto parse_error; |
| 4407 | } |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4408 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4409 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4410 | break; /* we are in case's "word | word)" */ |
Denis Vlasenko | fbeeb32 | 2008-07-31 00:17:01 +0000 | [diff] [blame] | 4411 | #endif |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4412 | if (next == '|') { /* || */ |
Denis Vlasenko | 609f2ab | 2009-04-04 23:15:14 +0000 | [diff] [blame] | 4413 | ch = i_getch(input); |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 4414 | nommu_addchr(&ctx.as_string, ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4415 | done_pipe(&ctx, PIPE_OR); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4416 | } else { |
| 4417 | /* we could pick up a file descriptor choice here |
| 4418 | * with redirect_opt_num(), but bash doesn't do it. |
| 4419 | * "echo foo 2| cat" yields "foo 2". */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4420 | done_command(&ctx); |
Denys Vlasenko | b70cef7 | 2010-01-12 13:45:45 +0100 | [diff] [blame] | 4421 | #if !BB_MMU |
| 4422 | o_reset_to_empty_unquoted(&ctx.as_string); |
| 4423 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4424 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4425 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4426 | case '(': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4427 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | f173607 | 2008-07-31 10:09:26 +0000 | [diff] [blame] | 4428 | /* "case... in [(]word)..." - skip '(' */ |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4429 | if (ctx.ctx_res_w == RES_MATCH |
| 4430 | && ctx.command->argv == NULL /* not (word|(... */ |
| 4431 | && dest.length == 0 /* not word(... */ |
Denys Vlasenko | 38292b6 | 2010-09-05 14:49:40 +0200 | [diff] [blame] | 4432 | && dest.has_quoted_part == 0 /* not ""(... */ |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4433 | ) { |
| 4434 | continue; |
| 4435 | } |
| 4436 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4437 | case '{': |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4438 | if (parse_group(&dest, &ctx, input, ch) != 0) { |
| 4439 | goto parse_error; |
Denis Vlasenko | e725bfe | 2007-05-03 22:45:39 +0000 | [diff] [blame] | 4440 | } |
Denis Vlasenko | 2b576b8 | 2008-08-04 00:46:07 +0000 | [diff] [blame] | 4441 | goto new_cmd; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4442 | case ')': |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4443 | #if ENABLE_HUSH_CASE |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4444 | if (ctx.ctx_res_w == RES_MATCH) |
Denis Vlasenko | 17f02e7 | 2008-07-14 04:32:29 +0000 | [diff] [blame] | 4445 | goto case_semi; |
| 4446 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4447 | case '}': |
Denis Vlasenko | c373527 | 2008-10-09 12:58:26 +0000 | [diff] [blame] | 4448 | /* proper use of this character is caught by end_trigger: |
| 4449 | * if we see {, we call parse_group(..., end_trigger='}') |
| 4450 | * and it will match } earlier (not here). */ |
Denis Vlasenko | c0ea329 | 2009-04-10 21:22:02 +0000 | [diff] [blame] | 4451 | syntax_error_unexpected_ch(ch); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4452 | goto parse_error; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4453 | default: |
Denis Vlasenko | 5ec6132 | 2008-06-24 00:50:07 +0000 | [diff] [blame] | 4454 | if (HUSH_DEBUG) |
Denis Vlasenko | 90e485c | 2007-05-23 15:22:50 +0000 | [diff] [blame] | 4455 | bb_error_msg_and_die("BUG: unexpected %c\n", ch); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4456 | } |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 4457 | } /* while (1) */ |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4458 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4459 | parse_error: |
| 4460 | { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4461 | struct parse_context *pctx; |
| 4462 | IF_HAS_KEYWORDS(struct parse_context *p2;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4463 | |
| 4464 | /* Clean up allocated tree. |
Denys Vlasenko | 764b2f0 | 2009-06-07 16:05:04 +0200 | [diff] [blame] | 4465 | * Sample for finding leaks on syntax error recovery path. |
| 4466 | * Run it from interactive shell, watch pmap `pidof hush`. |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4467 | * while if false; then false; fi; do break; fi |
Denis Vlasenko | cc4c693 | 2009-04-05 07:38:48 +0000 | [diff] [blame] | 4468 | * Samples to catch leaks at execution: |
| 4469 | * while if (true | {true;}); then echo ok; fi; do break; done |
| 4470 | * 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] | 4471 | */ |
| 4472 | pctx = &ctx; |
| 4473 | do { |
| 4474 | /* Update pipe/command counts, |
| 4475 | * otherwise freeing may miss some */ |
| 4476 | done_pipe(pctx, PIPE_SEQ); |
| 4477 | debug_printf_clean("freeing list %p from ctx %p\n", |
| 4478 | pctx->list_head, pctx); |
| 4479 | debug_print_tree(pctx->list_head, 0); |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4480 | free_pipe_list(pctx->list_head); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4481 | debug_printf_clean("freed list %p\n", pctx->list_head); |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4482 | #if !BB_MMU |
| 4483 | o_free_unsafe(&pctx->as_string); |
| 4484 | #endif |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4485 | IF_HAS_KEYWORDS(p2 = pctx->stack;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4486 | if (pctx != &ctx) { |
| 4487 | free(pctx); |
| 4488 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 4489 | IF_HAS_KEYWORDS(pctx = p2;) |
| 4490 | } while (HAS_KEYWORDS && pctx); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4491 | /* Free text, clear all dest fields */ |
| 4492 | o_free(&dest); |
| 4493 | /* If we are not in top-level parse, we return, |
| 4494 | * our caller will propagate error. |
| 4495 | */ |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4496 | if (end_trigger != ';') { |
| 4497 | #if !BB_MMU |
| 4498 | if (pstring) |
| 4499 | *pstring = NULL; |
| 4500 | #endif |
Denis Vlasenko | 0701dca | 2009-04-11 10:38:47 +0000 | [diff] [blame] | 4501 | debug_leave(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4502 | return ERR_PTR; |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 4503 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4504 | /* Discard cached input, force prompt */ |
| 4505 | input->p = NULL; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 4506 | IF_HUSH_INTERACTIVE(input->promptme = 1;) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 4507 | goto reset; |
Denis Vlasenko | 027e3fd | 2009-04-02 22:50:40 +0000 | [diff] [blame] | 4508 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4511 | |
| 4512 | /*** Execution routines ***/ |
| 4513 | |
| 4514 | /* Expansion can recurse, need forward decls: */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4515 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4516 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4517 | #define expand_string_to_string(str, do_unbackslash) \ |
| 4518 | expand_string_to_string(str) |
| 4519 | #endif |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4520 | static char *expand_string_to_string(const char *str, int do_unbackslash); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4521 | #if ENABLE_HUSH_TICK |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4522 | static int process_command_subs(o_string *dest, const char *s); |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 4523 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4524 | |
| 4525 | /* expand_strvec_to_strvec() takes a list of strings, expands |
| 4526 | * all variable references within and returns a pointer to |
| 4527 | * a list of expanded strings, possibly with larger number |
| 4528 | * of strings. (Think VAR="a b"; echo $VAR). |
| 4529 | * This new list is allocated as a single malloc block. |
| 4530 | * NULL-terminated list of char* pointers is at the beginning of it, |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4531 | * followed by strings themselves. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4532 | * Caller can deallocate entire list by single free(list). */ |
| 4533 | |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4534 | /* A horde of its helpers come first: */ |
| 4535 | |
| 4536 | static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len) |
| 4537 | { |
| 4538 | while (--len >= 0) { |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4539 | char c = *str++; |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4540 | |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4541 | #if ENABLE_HUSH_BRACE_EXPANSION |
| 4542 | if (c == '{' || c == '}') { |
| 4543 | /* { -> \{, } -> \} */ |
| 4544 | o_addchr(o, '\\'); |
Denys Vlasenko | 957f79f | 2010-10-03 17:15:50 +0200 | [diff] [blame] | 4545 | /* And now we want to add { or } and continue: |
| 4546 | * o_addchr(o, c); |
| 4547 | * continue; |
| 4548 | * luckily, just falling throught achieves this. |
| 4549 | */ |
Denys Vlasenko | 9e80022 | 2010-10-03 14:28:04 +0200 | [diff] [blame] | 4550 | } |
| 4551 | #endif |
| 4552 | o_addchr(o, c); |
| 4553 | if (c == '\\') { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4554 | /* \z -> \\\z; \<eol> -> \\<eol> */ |
| 4555 | o_addchr(o, '\\'); |
| 4556 | if (len) { |
| 4557 | len--; |
| 4558 | o_addchr(o, '\\'); |
| 4559 | o_addchr(o, *str++); |
| 4560 | } |
| 4561 | } |
| 4562 | } |
| 4563 | } |
| 4564 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4565 | /* Store given string, finalizing the word and starting new one whenever |
| 4566 | * we encounter IFS char(s). This is used for expanding variable values. |
| 4567 | * End-of-string does NOT finalize word: think about 'echo -$VAR-' */ |
| 4568 | static int expand_on_ifs(o_string *output, int n, const char *str) |
| 4569 | { |
| 4570 | while (1) { |
| 4571 | int word_len = strcspn(str, G.ifs); |
| 4572 | if (word_len) { |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4573 | if (!(output->o_expflags & EXP_FLAG_GLOB)) { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4574 | o_addblock(output, str, word_len); |
Denys Vlasenko | 238081f | 2010-10-03 14:26:26 +0200 | [diff] [blame] | 4575 | } else { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4576 | /* Protect backslashes against globbing up :) |
Denys Vlasenko | a769e02 | 2010-09-10 10:12:34 +0200 | [diff] [blame] | 4577 | * Example: "v='\*'; echo b$v" prints "b\*" |
| 4578 | * (and does not try to glob on "*") |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4579 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4580 | o_addblock_duplicate_backslash(output, str, word_len); |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4581 | /*/ Why can't we do it easier? */ |
| 4582 | /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */ |
| 4583 | /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */ |
| 4584 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4585 | str += word_len; |
| 4586 | } |
| 4587 | if (!*str) /* EOL - do not finalize word */ |
| 4588 | break; |
| 4589 | o_addchr(output, '\0'); |
| 4590 | debug_print_list("expand_on_ifs", output, n); |
| 4591 | n = o_save_ptr(output, n); |
| 4592 | str += strspn(str, G.ifs); /* skip ifs chars */ |
| 4593 | } |
| 4594 | debug_print_list("expand_on_ifs[1]", output, n); |
| 4595 | return n; |
| 4596 | } |
| 4597 | |
| 4598 | /* Helper to expand $((...)) and heredoc body. These act as if |
| 4599 | * they are in double quotes, with the exception that they are not :). |
| 4600 | * Just the rules are similar: "expand only $var and `cmd`" |
| 4601 | * |
| 4602 | * Returns malloced string. |
| 4603 | * As an optimization, we return NULL if expansion is not needed. |
| 4604 | */ |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4605 | #if !ENABLE_HUSH_BASH_COMPAT |
| 4606 | /* only ${var/pattern/repl} (its pattern part) needs additional mode */ |
| 4607 | #define encode_then_expand_string(str, process_bkslash, do_unbackslash) \ |
| 4608 | encode_then_expand_string(str) |
| 4609 | #endif |
| 4610 | static char *encode_then_expand_string(const char *str, int process_bkslash, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4611 | { |
| 4612 | char *exp_str; |
| 4613 | struct in_str input; |
| 4614 | o_string dest = NULL_O_STRING; |
| 4615 | |
| 4616 | if (!strchr(str, '$') |
Denys Vlasenko | 77b32cc | 2010-09-06 11:27:32 +0200 | [diff] [blame] | 4617 | && !strchr(str, '\\') |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4618 | #if ENABLE_HUSH_TICK |
| 4619 | && !strchr(str, '`') |
| 4620 | #endif |
| 4621 | ) { |
| 4622 | return NULL; |
| 4623 | } |
| 4624 | |
| 4625 | /* We need to expand. Example: |
| 4626 | * echo $(($a + `echo 1`)) $((1 + $((2)) )) |
| 4627 | */ |
| 4628 | setup_string_in_str(&input, str); |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4629 | encode_string(NULL, &dest, &input, EOF, process_bkslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4630 | //bb_error_msg("'%s' -> '%s'", str, dest.data); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 4631 | exp_str = expand_string_to_string(dest.data, /*unbackslash:*/ do_unbackslash); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4632 | //bb_error_msg("'%s' -> '%s'", dest.data, exp_str); |
| 4633 | o_free_unsafe(&dest); |
| 4634 | return exp_str; |
| 4635 | } |
| 4636 | |
| 4637 | #if ENABLE_SH_MATH_SUPPORT |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4638 | static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4639 | { |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4640 | arith_state_t math_state; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4641 | arith_t res; |
| 4642 | char *exp_str; |
| 4643 | |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4644 | math_state.lookupvar = get_local_var_value; |
| 4645 | math_state.setvar = set_local_var_from_halves; |
| 4646 | //math_state.endofname = endofname; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4647 | exp_str = encode_then_expand_string(arg, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | 06d44d7 | 2010-09-13 12:49:03 +0200 | [diff] [blame] | 4648 | res = arith(&math_state, exp_str ? exp_str : arg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4649 | free(exp_str); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4650 | if (errmsg_p) |
| 4651 | *errmsg_p = math_state.errmsg; |
| 4652 | if (math_state.errmsg) |
| 4653 | die_if_script(math_state.errmsg); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4654 | return res; |
| 4655 | } |
| 4656 | #endif |
| 4657 | |
| 4658 | #if ENABLE_HUSH_BASH_COMPAT |
| 4659 | /* ${var/[/]pattern[/repl]} helpers */ |
| 4660 | static char *strstr_pattern(char *val, const char *pattern, int *size) |
| 4661 | { |
| 4662 | while (1) { |
| 4663 | char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF); |
| 4664 | debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end); |
| 4665 | if (end) { |
| 4666 | *size = end - val; |
| 4667 | return val; |
| 4668 | } |
| 4669 | if (*val == '\0') |
| 4670 | return NULL; |
| 4671 | /* Optimization: if "*pat" did not match the start of "string", |
| 4672 | * we know that "tring", "ring" etc will not match too: |
| 4673 | */ |
| 4674 | if (pattern[0] == '*') |
| 4675 | return NULL; |
| 4676 | val++; |
| 4677 | } |
| 4678 | } |
| 4679 | static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op) |
| 4680 | { |
| 4681 | char *result = NULL; |
| 4682 | unsigned res_len = 0; |
| 4683 | unsigned repl_len = strlen(repl); |
| 4684 | |
| 4685 | while (1) { |
| 4686 | int size; |
| 4687 | char *s = strstr_pattern(val, pattern, &size); |
| 4688 | if (!s) |
| 4689 | break; |
| 4690 | |
| 4691 | result = xrealloc(result, res_len + (s - val) + repl_len + 1); |
| 4692 | memcpy(result + res_len, val, s - val); |
| 4693 | res_len += s - val; |
| 4694 | strcpy(result + res_len, repl); |
| 4695 | res_len += repl_len; |
| 4696 | debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); |
| 4697 | |
| 4698 | val = s + size; |
| 4699 | if (exp_op == '/') |
| 4700 | break; |
| 4701 | } |
| 4702 | if (val[0] && result) { |
| 4703 | result = xrealloc(result, res_len + strlen(val) + 1); |
| 4704 | strcpy(result + res_len, val); |
| 4705 | debug_printf_varexp("val:'%s' result:'%s'\n", val, result); |
| 4706 | } |
| 4707 | debug_printf_varexp("result:'%s'\n", result); |
| 4708 | return result; |
| 4709 | } |
| 4710 | #endif |
| 4711 | |
| 4712 | /* Helper: |
| 4713 | * Handles <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct. |
| 4714 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4715 | static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, char **pp) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4716 | { |
| 4717 | const char *val = NULL; |
| 4718 | char *to_be_freed = NULL; |
| 4719 | char *p = *pp; |
| 4720 | char *var; |
| 4721 | char first_char; |
| 4722 | char exp_op; |
| 4723 | char exp_save = exp_save; /* for compiler */ |
| 4724 | char *exp_saveptr; /* points to expansion operator */ |
| 4725 | char *exp_word = exp_word; /* for compiler */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4726 | char arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4727 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4728 | *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4729 | var = arg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4730 | exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4731 | arg0 = arg[0]; |
| 4732 | first_char = arg[0] = arg0 & 0x7f; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4733 | exp_op = 0; |
| 4734 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4735 | if (first_char == '#' /* ${#... */ |
| 4736 | && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ |
| 4737 | ) { |
| 4738 | /* It must be length operator: ${#var} */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4739 | var++; |
| 4740 | exp_op = 'L'; |
| 4741 | } else { |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4742 | /* Maybe handle parameter expansion */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4743 | if (exp_saveptr /* if 2nd char is one of expansion operators */ |
| 4744 | && strchr(NUMERIC_SPECVARS_STR, first_char) /* 1st char is special variable */ |
| 4745 | ) { |
| 4746 | /* ${?:0}, ${#[:]%0} etc */ |
| 4747 | exp_saveptr = var + 1; |
| 4748 | } else { |
| 4749 | /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */ |
| 4750 | exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS); |
| 4751 | } |
| 4752 | exp_op = exp_save = *exp_saveptr; |
| 4753 | if (exp_op) { |
| 4754 | exp_word = exp_saveptr + 1; |
| 4755 | if (exp_op == ':') { |
| 4756 | exp_op = *exp_word++; |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4757 | //TODO: try ${var:} and ${var:bogus} in non-bash config |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4758 | if (ENABLE_HUSH_BASH_COMPAT |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4759 | && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op)) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4760 | ) { |
| 4761 | /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */ |
| 4762 | exp_op = ':'; |
| 4763 | exp_word--; |
| 4764 | } |
| 4765 | } |
| 4766 | *exp_saveptr = '\0'; |
| 4767 | } /* else: it's not an expansion op, but bare ${var} */ |
| 4768 | } |
| 4769 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4770 | /* Look up the variable in question */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4771 | if (isdigit(var[0])) { |
Denys Vlasenko | 77a7b55 | 2010-09-09 12:40:03 +0200 | [diff] [blame] | 4772 | /* parse_dollar should have vetted var for us */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4773 | int n = xatoi_positive(var); |
| 4774 | if (n < G.global_argc) |
| 4775 | val = G.global_argv[n]; |
| 4776 | /* else val remains NULL: $N with too big N */ |
| 4777 | } else { |
| 4778 | switch (var[0]) { |
| 4779 | case '$': /* pid */ |
| 4780 | val = utoa(G.root_pid); |
| 4781 | break; |
| 4782 | case '!': /* bg pid */ |
| 4783 | val = G.last_bg_pid ? utoa(G.last_bg_pid) : ""; |
| 4784 | break; |
| 4785 | case '?': /* exitcode */ |
| 4786 | val = utoa(G.last_exitcode); |
| 4787 | break; |
| 4788 | case '#': /* argc */ |
| 4789 | val = utoa(G.global_argc ? G.global_argc-1 : 0); |
| 4790 | break; |
| 4791 | default: |
| 4792 | val = get_local_var_value(var); |
| 4793 | } |
| 4794 | } |
| 4795 | |
| 4796 | /* Handle any expansions */ |
| 4797 | if (exp_op == 'L') { |
| 4798 | debug_printf_expand("expand: length(%s)=", val); |
| 4799 | val = utoa(val ? strlen(val) : 0); |
| 4800 | debug_printf_expand("%s\n", val); |
| 4801 | } else if (exp_op) { |
| 4802 | if (exp_op == '%' || exp_op == '#') { |
| 4803 | /* Standard-mandated substring removal ops: |
| 4804 | * ${parameter%word} - remove smallest suffix pattern |
| 4805 | * ${parameter%%word} - remove largest suffix pattern |
| 4806 | * ${parameter#word} - remove smallest prefix pattern |
| 4807 | * ${parameter##word} - remove largest prefix pattern |
| 4808 | * |
| 4809 | * Word is expanded to produce a glob pattern. |
| 4810 | * Then var's value is matched to it and matching part removed. |
| 4811 | */ |
| 4812 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4813 | char *t; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4814 | char *exp_exp_word; |
| 4815 | char *loc; |
| 4816 | unsigned scan_flags = pick_scan(exp_op, *exp_word); |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 4817 | if (exp_op == *exp_word) /* ## or %% */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4818 | exp_word++; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4819 | exp_exp_word = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4820 | if (exp_exp_word) |
| 4821 | exp_word = exp_exp_word; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4822 | /* HACK ALERT. We depend here on the fact that |
| 4823 | * G.global_argv and results of utoa and get_local_var_value |
| 4824 | * are actually in writable memory: |
| 4825 | * scan_and_match momentarily stores NULs there. */ |
| 4826 | t = (char*)val; |
| 4827 | loc = scan_and_match(t, exp_word, scan_flags); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4828 | //bb_error_msg("op:%c str:'%s' pat:'%s' res:'%s'", |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4829 | // exp_op, t, exp_word, loc); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4830 | free(exp_exp_word); |
| 4831 | if (loc) { /* match was found */ |
| 4832 | if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4833 | val = loc; /* take right part */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4834 | else /* %[%] */ |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4835 | val = to_be_freed = xstrndup(val, loc - val); /* left */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | } |
| 4839 | #if ENABLE_HUSH_BASH_COMPAT |
| 4840 | else if (exp_op == '/' || exp_op == '\\') { |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4841 | /* It's ${var/[/]pattern[/repl]} thing. |
| 4842 | * Note that in encoded form it has TWO parts: |
| 4843 | * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4844 | * and if // is used, it is encoded as \: |
| 4845 | * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL> |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4846 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4847 | /* Empty variable always gives nothing: */ |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4848 | // "v=''; echo ${v/*/w}" prints "", not "w" |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4849 | if (val && val[0]) { |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 4850 | /* pattern uses non-standard expansion. |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 4851 | * repl should be unbackslashed and globbed |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 4852 | * by the usual expansion rules: |
| 4853 | * >az; >bz; |
| 4854 | * v='a bz'; echo "${v/a*z/a*z}" prints "a*z" |
| 4855 | * v='a bz'; echo "${v/a*z/\z}" prints "\z" |
| 4856 | * v='a bz'; echo ${v/a*z/a*z} prints "az" |
| 4857 | * v='a bz'; echo ${v/a*z/\z} prints "z" |
| 4858 | * (note that a*z _pattern_ is never globbed!) |
| 4859 | */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4860 | char *pattern, *repl, *t; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4861 | pattern = encode_then_expand_string(exp_word, /*process_bkslash:*/ 0, /*unbackslash:*/ 0); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4862 | if (!pattern) |
| 4863 | pattern = xstrdup(exp_word); |
| 4864 | debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern); |
| 4865 | *p++ = SPECIAL_VAR_SYMBOL; |
| 4866 | exp_word = p; |
| 4867 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4868 | *p = '\0'; |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4869 | repl = encode_then_expand_string(exp_word, /*process_bkslash:*/ arg0 & 0x80, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4870 | debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl); |
| 4871 | /* HACK ALERT. We depend here on the fact that |
| 4872 | * G.global_argv and results of utoa and get_local_var_value |
| 4873 | * are actually in writable memory: |
| 4874 | * replace_pattern momentarily stores NULs there. */ |
| 4875 | t = (char*)val; |
| 4876 | to_be_freed = replace_pattern(t, |
| 4877 | pattern, |
| 4878 | (repl ? repl : exp_word), |
| 4879 | exp_op); |
| 4880 | if (to_be_freed) /* at least one replace happened */ |
| 4881 | val = to_be_freed; |
| 4882 | free(pattern); |
| 4883 | free(repl); |
| 4884 | } |
| 4885 | } |
| 4886 | #endif |
| 4887 | else if (exp_op == ':') { |
| 4888 | #if ENABLE_HUSH_BASH_COMPAT && ENABLE_SH_MATH_SUPPORT |
| 4889 | /* It's ${var:N[:M]} bashism. |
| 4890 | * Note that in encoded form it has TWO parts: |
| 4891 | * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL> |
| 4892 | */ |
| 4893 | arith_t beg, len; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4894 | const char *errmsg; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4895 | |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4896 | beg = expand_and_evaluate_arith(exp_word, &errmsg); |
| 4897 | if (errmsg) |
| 4898 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4899 | debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg); |
| 4900 | *p++ = SPECIAL_VAR_SYMBOL; |
| 4901 | exp_word = p; |
| 4902 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 4903 | *p = '\0'; |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4904 | len = expand_and_evaluate_arith(exp_word, &errmsg); |
| 4905 | if (errmsg) |
| 4906 | goto arith_err; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4907 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4908 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4909 | if (beg < 0) /* bash compat */ |
| 4910 | beg = 0; |
| 4911 | debug_printf_varexp("from val:'%s'\n", val); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 4912 | if (len == 0 || !val || beg >= strlen(val)) { |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 4913 | arith_err: |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 4914 | val = NULL; |
| 4915 | } else { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4916 | /* Paranoia. What if user entered 9999999999999 |
| 4917 | * which fits in arith_t but not int? */ |
| 4918 | if (len >= INT_MAX) |
| 4919 | len = INT_MAX; |
| 4920 | val = to_be_freed = xstrndup(val + beg, len); |
| 4921 | } |
| 4922 | debug_printf_varexp("val:'%s'\n", val); |
| 4923 | } else |
| 4924 | #endif |
| 4925 | { |
| 4926 | die_if_script("malformed ${%s:...}", var); |
Denys Vlasenko | b771c65 | 2010-09-13 00:34:26 +0200 | [diff] [blame] | 4927 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4928 | } |
| 4929 | } else { /* one of "-=+?" */ |
| 4930 | /* Standard-mandated substitution ops: |
| 4931 | * ${var?word} - indicate error if unset |
| 4932 | * If var is unset, word (or a message indicating it is unset |
| 4933 | * if word is null) is written to standard error |
| 4934 | * and the shell exits with a non-zero exit status. |
| 4935 | * Otherwise, the value of var is substituted. |
| 4936 | * ${var-word} - use default value |
| 4937 | * If var is unset, word is substituted. |
| 4938 | * ${var=word} - assign and use default value |
| 4939 | * If var is unset, word is assigned to var. |
| 4940 | * In all cases, final value of var is substituted. |
| 4941 | * ${var+word} - use alternative value |
| 4942 | * If var is unset, null is substituted. |
| 4943 | * Otherwise, word is substituted. |
| 4944 | * |
| 4945 | * Word is subjected to tilde expansion, parameter expansion, |
| 4946 | * command substitution, and arithmetic expansion. |
| 4947 | * If word is not needed, it is not expanded. |
| 4948 | * |
| 4949 | * Colon forms (${var:-word}, ${var:=word} etc) do the same, |
| 4950 | * but also treat null var as if it is unset. |
| 4951 | */ |
| 4952 | int use_word = (!val || ((exp_save == ':') && !val[0])); |
| 4953 | if (exp_op == '+') |
| 4954 | use_word = !use_word; |
| 4955 | debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op, |
| 4956 | (exp_save == ':') ? "true" : "false", use_word); |
| 4957 | if (use_word) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 4958 | to_be_freed = encode_then_expand_string(exp_word, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4959 | if (to_be_freed) |
| 4960 | exp_word = to_be_freed; |
| 4961 | if (exp_op == '?') { |
| 4962 | /* mimic bash message */ |
| 4963 | die_if_script("%s: %s", |
| 4964 | var, |
| 4965 | exp_word[0] ? exp_word : "parameter null or not set" |
| 4966 | ); |
| 4967 | //TODO: how interactive bash aborts expansion mid-command? |
| 4968 | } else { |
| 4969 | val = exp_word; |
| 4970 | } |
| 4971 | |
| 4972 | if (exp_op == '=') { |
| 4973 | /* ${var=[word]} or ${var:=[word]} */ |
| 4974 | if (isdigit(var[0]) || var[0] == '#') { |
| 4975 | /* mimic bash message */ |
| 4976 | die_if_script("$%s: cannot assign in this way", var); |
| 4977 | val = NULL; |
| 4978 | } else { |
| 4979 | char *new_var = xasprintf("%s=%s", var, val); |
| 4980 | set_local_var(new_var, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 4981 | } |
| 4982 | } |
| 4983 | } |
| 4984 | } /* one of "-=+?" */ |
| 4985 | |
| 4986 | *exp_saveptr = exp_save; |
| 4987 | } /* if (exp_op) */ |
| 4988 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 4989 | arg[0] = arg0; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 4990 | |
| 4991 | *pp = p; |
| 4992 | *to_be_freed_pp = to_be_freed; |
| 4993 | return val; |
| 4994 | } |
| 4995 | |
| 4996 | /* Expand all variable references in given string, adding words to list[] |
| 4997 | * at n, n+1,... positions. Return updated n (so that list[n] is next one |
| 4998 | * to be filled). This routine is extremely tricky: has to deal with |
| 4999 | * variables/parameters with whitespace, $* and $@, and constructs like |
| 5000 | * 'echo -$*-'. If you play here, you must run testsuite afterwards! */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5001 | 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] | 5002 | { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5003 | /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5004 | * expansion of right-hand side of assignment == 1-element expand. |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5005 | */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5006 | char cant_be_null = 0; /* only bit 0x80 matters */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5007 | char *p; |
| 5008 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5009 | debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg, |
| 5010 | !!(output->o_expflags & EXP_FLAG_SINGLEWORD)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5011 | debug_print_list("expand_vars_to_list", output, n); |
| 5012 | n = o_save_ptr(output, n); |
| 5013 | debug_print_list("expand_vars_to_list[0]", output, n); |
| 5014 | |
| 5015 | while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) { |
| 5016 | char first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5017 | char *to_be_freed = NULL; |
| 5018 | const char *val = NULL; |
| 5019 | #if ENABLE_HUSH_TICK |
| 5020 | o_string subst_result = NULL_O_STRING; |
| 5021 | #endif |
| 5022 | #if ENABLE_SH_MATH_SUPPORT |
| 5023 | char arith_buf[sizeof(arith_t)*3 + 2]; |
| 5024 | #endif |
| 5025 | o_addblock(output, arg, p - arg); |
| 5026 | debug_print_list("expand_vars_to_list[1]", output, n); |
| 5027 | arg = ++p; |
| 5028 | p = strchr(p, SPECIAL_VAR_SYMBOL); |
| 5029 | |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5030 | /* Fetch special var name (if it is indeed one of them) |
| 5031 | * and quote bit, force the bit on if singleword expansion - |
| 5032 | * important for not getting v=$@ expand to many words. */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5033 | first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5034 | |
| 5035 | /* Is this variable quoted and thus expansion can't be null? |
| 5036 | * "$@" is special. Even if quoted, it can still |
| 5037 | * expand to nothing (not even an empty string), |
| 5038 | * thus it is excluded. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5039 | if ((first_ch & 0x7f) != '@') |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5040 | cant_be_null |= first_ch; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5041 | |
| 5042 | switch (first_ch & 0x7f) { |
| 5043 | /* Highest bit in first_ch indicates that var is double-quoted */ |
| 5044 | case '*': |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5045 | case '@': { |
| 5046 | int i; |
| 5047 | if (!G.global_argv[1]) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5048 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5049 | i = 1; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5050 | cant_be_null |= first_ch; /* do it for "$@" _now_, when we know it's not empty */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5051 | if (!(first_ch & 0x80)) { /* unquoted $* or $@ */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5052 | while (G.global_argv[i]) { |
| 5053 | n = expand_on_ifs(output, n, G.global_argv[i]); |
| 5054 | debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1); |
| 5055 | if (G.global_argv[i++][0] && G.global_argv[i]) { |
| 5056 | /* this argv[] is not empty and not last: |
| 5057 | * put terminating NUL, start new word */ |
| 5058 | o_addchr(output, '\0'); |
| 5059 | debug_print_list("expand_vars_to_list[2]", output, n); |
| 5060 | n = o_save_ptr(output, n); |
| 5061 | debug_print_list("expand_vars_to_list[3]", output, n); |
| 5062 | } |
| 5063 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5064 | } else |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5065 | /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....' |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5066 | * and in this case should treat it like '$*' - see 'else...' below */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5067 | if (first_ch == ('@'|0x80) /* quoted $@ */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5068 | && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */ |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5069 | ) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5070 | while (1) { |
| 5071 | o_addQstr(output, G.global_argv[i]); |
| 5072 | if (++i >= G.global_argc) |
| 5073 | break; |
| 5074 | o_addchr(output, '\0'); |
| 5075 | debug_print_list("expand_vars_to_list[4]", output, n); |
| 5076 | n = o_save_ptr(output, n); |
| 5077 | } |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5078 | } else { /* quoted $* (or v="$@" case): add as one word */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5079 | while (1) { |
| 5080 | o_addQstr(output, G.global_argv[i]); |
| 5081 | if (!G.global_argv[++i]) |
| 5082 | break; |
| 5083 | if (G.ifs[0]) |
| 5084 | o_addchr(output, G.ifs[0]); |
| 5085 | } |
| 5086 | } |
| 5087 | break; |
Denys Vlasenko | c49d2d9 | 2010-09-06 10:26:37 +0200 | [diff] [blame] | 5088 | } |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5089 | case SPECIAL_VAR_SYMBOL: /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */ |
| 5090 | /* "Empty variable", used to make "" etc to not disappear */ |
| 5091 | arg++; |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5092 | cant_be_null = 0x80; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5093 | break; |
| 5094 | #if ENABLE_HUSH_TICK |
| 5095 | case '`': /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5096 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5097 | arg++; |
| 5098 | /* Can't just stuff it into output o_string, |
| 5099 | * expanded result may need to be globbed |
| 5100 | * and $IFS-splitted */ |
| 5101 | debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch); |
| 5102 | G.last_exitcode = process_command_subs(&subst_result, arg); |
| 5103 | debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data); |
| 5104 | val = subst_result.data; |
| 5105 | goto store_val; |
| 5106 | #endif |
| 5107 | #if ENABLE_SH_MATH_SUPPORT |
| 5108 | case '+': { /* <SPECIAL_VAR_SYMBOL>+cmd<SPECIAL_VAR_SYMBOL> */ |
| 5109 | arith_t res; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5110 | |
| 5111 | arg++; /* skip '+' */ |
| 5112 | *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */ |
| 5113 | debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch); |
Denys Vlasenko | 063847d | 2010-09-15 13:33:02 +0200 | [diff] [blame] | 5114 | res = expand_and_evaluate_arith(arg, NULL); |
Denys Vlasenko | bed7c81 | 2010-09-16 11:50:46 +0200 | [diff] [blame] | 5115 | debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res); |
| 5116 | sprintf(arith_buf, ARITH_FMT, res); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5117 | val = arith_buf; |
| 5118 | break; |
| 5119 | } |
| 5120 | #endif |
| 5121 | default: |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5122 | val = expand_one_var(&to_be_freed, arg, &p); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5123 | IF_HUSH_TICK(store_val:) |
| 5124 | if (!(first_ch & 0x80)) { /* unquoted $VAR */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5125 | debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val, |
| 5126 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5127 | if (val && val[0]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5128 | n = expand_on_ifs(output, n, val); |
| 5129 | val = NULL; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5130 | } |
| 5131 | } else { /* quoted $VAR, val will be appended below */ |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5132 | debug_printf_expand("quoted '%s', output->o_escape:%d\n", val, |
| 5133 | !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5134 | } |
| 5135 | break; |
| 5136 | |
| 5137 | } /* switch (char after <SPECIAL_VAR_SYMBOL>) */ |
| 5138 | |
| 5139 | if (val && val[0]) { |
| 5140 | o_addQstr(output, val); |
| 5141 | } |
| 5142 | free(to_be_freed); |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5143 | |
| 5144 | /* Restore NULL'ed SPECIAL_VAR_SYMBOL. |
| 5145 | * Do the check to avoid writing to a const string. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5146 | if (*p != SPECIAL_VAR_SYMBOL) |
| 5147 | *p = SPECIAL_VAR_SYMBOL; |
| 5148 | |
| 5149 | #if ENABLE_HUSH_TICK |
| 5150 | o_free(&subst_result); |
| 5151 | #endif |
| 5152 | arg = ++p; |
| 5153 | } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */ |
| 5154 | |
| 5155 | if (arg[0]) { |
| 5156 | debug_print_list("expand_vars_to_list[a]", output, n); |
| 5157 | /* this part is literal, and it was already pre-quoted |
| 5158 | * if needed (much earlier), do not use o_addQstr here! */ |
| 5159 | o_addstr_with_NUL(output, arg); |
| 5160 | debug_print_list("expand_vars_to_list[b]", output, n); |
| 5161 | } else if (output->length == o_get_last_ptr(output, n) /* expansion is empty */ |
Denys Vlasenko | bfc02a7 | 2010-09-09 14:38:46 +0200 | [diff] [blame] | 5162 | && !(cant_be_null & 0x80) /* and all vars were not quoted. */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5163 | ) { |
| 5164 | n--; |
| 5165 | /* allow to reuse list[n] later without re-growth */ |
| 5166 | output->has_empty_slot = 1; |
| 5167 | } else { |
| 5168 | o_addchr(output, '\0'); |
| 5169 | } |
| 5170 | |
| 5171 | return n; |
| 5172 | } |
| 5173 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5174 | static char **expand_variables(char **argv, unsigned expflags) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5175 | { |
| 5176 | int n; |
| 5177 | char **list; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5178 | o_string output = NULL_O_STRING; |
| 5179 | |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5180 | output.o_expflags = expflags; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5181 | |
| 5182 | n = 0; |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5183 | while (*argv) { |
Denys Vlasenko | 95d48f2 | 2010-09-08 13:58:55 +0200 | [diff] [blame] | 5184 | n = expand_vars_to_list(&output, n, *argv); |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5185 | argv++; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5186 | } |
| 5187 | debug_print_list("expand_variables", &output, n); |
| 5188 | |
| 5189 | /* output.data (malloced in one block) gets returned in "list" */ |
| 5190 | list = o_finalize_list(&output, n); |
| 5191 | debug_print_strings("expand_variables[1]", list); |
| 5192 | return list; |
| 5193 | } |
| 5194 | |
| 5195 | static char **expand_strvec_to_strvec(char **argv) |
| 5196 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5197 | return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | #if ENABLE_HUSH_BASH_COMPAT |
| 5201 | static char **expand_strvec_to_strvec_singleword_noglob(char **argv) |
| 5202 | { |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5203 | return expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5204 | } |
| 5205 | #endif |
| 5206 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 5207 | /* Used for expansion of right hand of assignments, |
| 5208 | * $((...)), heredocs, variable espansion parts. |
| 5209 | * |
| 5210 | * NB: should NOT do globbing! |
| 5211 | * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*" |
| 5212 | */ |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5213 | static char *expand_string_to_string(const char *str, int do_unbackslash) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5214 | { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5215 | #if !ENABLE_HUSH_BASH_COMPAT |
| 5216 | const int do_unbackslash = 1; |
| 5217 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5218 | char *argv[2], **list; |
| 5219 | |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5220 | debug_printf_expand("string_to_string<='%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5221 | /* This is generally an optimization, but it also |
| 5222 | * handles "", which otherwise trips over !list[0] check below. |
| 5223 | * (is this ever happens that we actually get str="" here?) |
| 5224 | */ |
| 5225 | if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) { |
| 5226 | //TODO: Can use on strings with \ too, just unbackslash() them? |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5227 | debug_printf_expand("string_to_string(fast)=>'%s'\n", str); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5228 | return xstrdup(str); |
| 5229 | } |
| 5230 | |
| 5231 | argv[0] = (char*)str; |
| 5232 | argv[1] = NULL; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5233 | list = expand_variables(argv, do_unbackslash |
| 5234 | ? EXP_FLAG_ESC_GLOB_CHARS | EXP_FLAG_SINGLEWORD |
| 5235 | : EXP_FLAG_SINGLEWORD |
| 5236 | ); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5237 | if (HUSH_DEBUG) |
| 5238 | if (!list[0] || list[1]) |
| 5239 | bb_error_msg_and_die("BUG in varexp2"); |
| 5240 | /* actually, just move string 2*sizeof(char*) bytes back */ |
| 5241 | overlapping_strcpy((char*)list, list[0]); |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5242 | if (do_unbackslash) |
| 5243 | unbackslash((char*)list); |
| 5244 | debug_printf_expand("string_to_string=>'%s'\n", (char*)list); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5245 | return (char*)list; |
| 5246 | } |
| 5247 | |
| 5248 | /* Used for "eval" builtin */ |
| 5249 | static char* expand_strvec_to_string(char **argv) |
| 5250 | { |
| 5251 | char **list; |
| 5252 | |
Denys Vlasenko | 5b686cb | 2010-09-08 13:44:34 +0200 | [diff] [blame] | 5253 | list = expand_variables(argv, EXP_FLAG_SINGLEWORD); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5254 | /* Convert all NULs to spaces */ |
| 5255 | if (list[0]) { |
| 5256 | int n = 1; |
| 5257 | while (list[n]) { |
| 5258 | if (HUSH_DEBUG) |
| 5259 | if (list[n-1] + strlen(list[n-1]) + 1 != list[n]) |
| 5260 | bb_error_msg_and_die("BUG in varexp3"); |
| 5261 | /* bash uses ' ' regardless of $IFS contents */ |
| 5262 | list[n][-1] = ' '; |
| 5263 | n++; |
| 5264 | } |
| 5265 | } |
| 5266 | overlapping_strcpy((char*)list, list[0]); |
| 5267 | debug_printf_expand("strvec_to_string='%s'\n", (char*)list); |
| 5268 | return (char*)list; |
| 5269 | } |
| 5270 | |
| 5271 | static char **expand_assignments(char **argv, int count) |
| 5272 | { |
| 5273 | int i; |
| 5274 | char **p; |
| 5275 | |
| 5276 | G.expanded_assignments = p = NULL; |
| 5277 | /* Expand assignments into one string each */ |
| 5278 | for (i = 0; i < count; i++) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5279 | G.expanded_assignments = p = add_string_to_strings(p, expand_string_to_string(argv[i], /*unbackslash:*/ 1)); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5280 | } |
| 5281 | G.expanded_assignments = NULL; |
| 5282 | return p; |
| 5283 | } |
| 5284 | |
| 5285 | |
| 5286 | #if BB_MMU |
| 5287 | /* never called */ |
| 5288 | void re_execute_shell(char ***to_free, const char *s, |
| 5289 | char *g_argv0, char **g_argv, |
| 5290 | char **builtin_argv) NORETURN; |
| 5291 | |
| 5292 | static void reset_traps_to_defaults(void) |
| 5293 | { |
| 5294 | /* This function is always called in a child shell |
| 5295 | * after fork (not vfork, NOMMU doesn't use this function). |
| 5296 | */ |
| 5297 | unsigned sig; |
| 5298 | unsigned mask; |
| 5299 | |
| 5300 | /* Child shells are not interactive. |
| 5301 | * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling. |
| 5302 | * Testcase: (while :; do :; done) + ^Z should background. |
| 5303 | * Same goes for SIGTERM, SIGHUP, SIGINT. |
| 5304 | */ |
| 5305 | if (!G.traps && !(G.non_DFL_mask & SPECIAL_INTERACTIVE_SIGS)) |
| 5306 | return; /* already no traps and no SPECIAL_INTERACTIVE_SIGS */ |
| 5307 | |
| 5308 | /* Switching off SPECIAL_INTERACTIVE_SIGS. |
| 5309 | * Stupid. It can be done with *single* &= op, but we can't use |
| 5310 | * the fact that G.blocked_set is implemented as a bitmask |
| 5311 | * in libc... */ |
| 5312 | mask = (SPECIAL_INTERACTIVE_SIGS >> 1); |
| 5313 | sig = 1; |
| 5314 | while (1) { |
| 5315 | if (mask & 1) { |
| 5316 | /* Careful. Only if no trap or trap is not "" */ |
| 5317 | if (!G.traps || !G.traps[sig] || G.traps[sig][0]) |
| 5318 | sigdelset(&G.blocked_set, sig); |
| 5319 | } |
| 5320 | mask >>= 1; |
| 5321 | if (!mask) |
| 5322 | break; |
| 5323 | sig++; |
| 5324 | } |
| 5325 | /* Our homegrown sig mask is saner to work with :) */ |
| 5326 | G.non_DFL_mask &= ~SPECIAL_INTERACTIVE_SIGS; |
| 5327 | |
| 5328 | /* Resetting all traps to default except empty ones */ |
| 5329 | mask = G.non_DFL_mask; |
| 5330 | if (G.traps) for (sig = 0; sig < NSIG; sig++, mask >>= 1) { |
| 5331 | if (!G.traps[sig] || !G.traps[sig][0]) |
| 5332 | continue; |
| 5333 | free(G.traps[sig]); |
| 5334 | G.traps[sig] = NULL; |
| 5335 | /* There is no signal for 0 (EXIT) */ |
| 5336 | if (sig == 0) |
| 5337 | continue; |
| 5338 | /* There was a trap handler, we just removed it. |
| 5339 | * But if sig still has non-DFL handling, |
| 5340 | * we should not unblock the sig. */ |
| 5341 | if (mask & 1) |
| 5342 | continue; |
| 5343 | sigdelset(&G.blocked_set, sig); |
| 5344 | } |
| 5345 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 5346 | } |
| 5347 | |
| 5348 | #else /* !BB_MMU */ |
| 5349 | |
| 5350 | static void re_execute_shell(char ***to_free, const char *s, |
| 5351 | char *g_argv0, char **g_argv, |
| 5352 | char **builtin_argv) NORETURN; |
| 5353 | static void re_execute_shell(char ***to_free, const char *s, |
| 5354 | char *g_argv0, char **g_argv, |
| 5355 | char **builtin_argv) |
| 5356 | { |
| 5357 | # define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x")) |
| 5358 | /* delims + 2 * (number of bytes in printed hex numbers) */ |
| 5359 | char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)]; |
| 5360 | char *heredoc_argv[4]; |
| 5361 | struct variable *cur; |
| 5362 | # if ENABLE_HUSH_FUNCTIONS |
| 5363 | struct function *funcp; |
| 5364 | # endif |
| 5365 | char **argv, **pp; |
| 5366 | unsigned cnt; |
| 5367 | unsigned long long empty_trap_mask; |
| 5368 | |
| 5369 | if (!g_argv0) { /* heredoc */ |
| 5370 | argv = heredoc_argv; |
| 5371 | argv[0] = (char *) G.argv0_for_re_execing; |
| 5372 | argv[1] = (char *) "-<"; |
| 5373 | argv[2] = (char *) s; |
| 5374 | argv[3] = NULL; |
| 5375 | pp = &argv[3]; /* used as pointer to empty environment */ |
| 5376 | goto do_exec; |
| 5377 | } |
| 5378 | |
| 5379 | cnt = 0; |
| 5380 | pp = builtin_argv; |
| 5381 | if (pp) while (*pp++) |
| 5382 | cnt++; |
| 5383 | |
| 5384 | empty_trap_mask = 0; |
| 5385 | if (G.traps) { |
| 5386 | int sig; |
| 5387 | for (sig = 1; sig < NSIG; sig++) { |
| 5388 | if (G.traps[sig] && !G.traps[sig][0]) |
| 5389 | empty_trap_mask |= 1LL << sig; |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | sprintf(param_buf, NOMMU_HACK_FMT |
| 5394 | , (unsigned) G.root_pid |
| 5395 | , (unsigned) G.root_ppid |
| 5396 | , (unsigned) G.last_bg_pid |
| 5397 | , (unsigned) G.last_exitcode |
| 5398 | , cnt |
| 5399 | , empty_trap_mask |
| 5400 | IF_HUSH_LOOPS(, G.depth_of_loop) |
| 5401 | ); |
| 5402 | # undef NOMMU_HACK_FMT |
| 5403 | /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...> |
| 5404 | * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL |
| 5405 | */ |
| 5406 | cnt += 6; |
| 5407 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5408 | if (!cur->flg_export || cur->flg_read_only) |
| 5409 | cnt += 2; |
| 5410 | } |
| 5411 | # if ENABLE_HUSH_FUNCTIONS |
| 5412 | for (funcp = G.top_func; funcp; funcp = funcp->next) |
| 5413 | cnt += 3; |
| 5414 | # endif |
| 5415 | pp = g_argv; |
| 5416 | while (*pp++) |
| 5417 | cnt++; |
| 5418 | *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt); |
| 5419 | *pp++ = (char *) G.argv0_for_re_execing; |
| 5420 | *pp++ = param_buf; |
| 5421 | for (cur = G.top_var; cur; cur = cur->next) { |
| 5422 | if (strcmp(cur->varstr, hush_version_str) == 0) |
| 5423 | continue; |
| 5424 | if (cur->flg_read_only) { |
| 5425 | *pp++ = (char *) "-R"; |
| 5426 | *pp++ = cur->varstr; |
| 5427 | } else if (!cur->flg_export) { |
| 5428 | *pp++ = (char *) "-V"; |
| 5429 | *pp++ = cur->varstr; |
| 5430 | } |
| 5431 | } |
| 5432 | # if ENABLE_HUSH_FUNCTIONS |
| 5433 | for (funcp = G.top_func; funcp; funcp = funcp->next) { |
| 5434 | *pp++ = (char *) "-F"; |
| 5435 | *pp++ = funcp->name; |
| 5436 | *pp++ = funcp->body_as_string; |
| 5437 | } |
| 5438 | # endif |
| 5439 | /* We can pass activated traps here. Say, -Tnn:trap_string |
| 5440 | * |
| 5441 | * However, POSIX says that subshells reset signals with traps |
| 5442 | * to SIG_DFL. |
| 5443 | * I tested bash-3.2 and it not only does that with true subshells |
| 5444 | * of the form ( list ), but with any forked children shells. |
| 5445 | * I set trap "echo W" WINCH; and then tried: |
| 5446 | * |
| 5447 | * { echo 1; sleep 20; echo 2; } & |
| 5448 | * while true; do echo 1; sleep 20; echo 2; break; done & |
| 5449 | * true | { echo 1; sleep 20; echo 2; } | cat |
| 5450 | * |
| 5451 | * In all these cases sending SIGWINCH to the child shell |
| 5452 | * did not run the trap. If I add trap "echo V" WINCH; |
| 5453 | * _inside_ group (just before echo 1), it works. |
| 5454 | * |
| 5455 | * I conclude it means we don't need to pass active traps here. |
| 5456 | * Even if we would use signal handlers instead of signal masking |
| 5457 | * in order to implement trap handling, |
| 5458 | * exec syscall below resets signals to SIG_DFL for us. |
| 5459 | */ |
| 5460 | *pp++ = (char *) "-c"; |
| 5461 | *pp++ = (char *) s; |
| 5462 | if (builtin_argv) { |
| 5463 | while (*++builtin_argv) |
| 5464 | *pp++ = *builtin_argv; |
| 5465 | *pp++ = (char *) ""; |
| 5466 | } |
| 5467 | *pp++ = g_argv0; |
| 5468 | while (*g_argv) |
| 5469 | *pp++ = *g_argv++; |
| 5470 | /* *pp = NULL; - is already there */ |
| 5471 | pp = environ; |
| 5472 | |
| 5473 | do_exec: |
| 5474 | debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s); |
| 5475 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 5476 | execve(bb_busybox_exec_path, argv, pp); |
| 5477 | /* Fallback. Useful for init=/bin/hush usage etc */ |
| 5478 | if (argv[0][0] == '/') |
| 5479 | execve(argv[0], argv, pp); |
| 5480 | xfunc_error_retval = 127; |
| 5481 | bb_error_msg_and_die("can't re-execute the shell"); |
| 5482 | } |
| 5483 | #endif /* !BB_MMU */ |
| 5484 | |
| 5485 | |
| 5486 | static int run_and_free_list(struct pipe *pi); |
| 5487 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5488 | /* Executing from string: eval, sh -c '...' |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5489 | * or from file: /etc/profile, . file, sh <script>, sh (intereactive) |
| 5490 | * end_trigger controls how often we stop parsing |
| 5491 | * NUL: parse all, execute, return |
| 5492 | * ';': parse till ';' or newline, execute, repeat till EOF |
| 5493 | */ |
| 5494 | 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] | 5495 | { |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5496 | /* Why we need empty flag? |
| 5497 | * An obscure corner case "false; ``; echo $?": |
| 5498 | * empty command in `` should still set $? to 0. |
| 5499 | * But we can't just set $? to 0 at the start, |
| 5500 | * this breaks "false; echo `echo $?`" case. |
| 5501 | */ |
| 5502 | bool empty = 1; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5503 | while (1) { |
| 5504 | struct pipe *pipe_list; |
Denis Vlasenko | f8d01d3 | 2008-06-14 17:13:20 +0000 | [diff] [blame] | 5505 | |
Denis Vlasenko | 9aa7d6f | 2009-04-04 22:47:50 +0000 | [diff] [blame] | 5506 | pipe_list = parse_stream(NULL, inp, end_trigger); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5507 | if (!pipe_list) { /* EOF */ |
| 5508 | if (empty) |
| 5509 | G.last_exitcode = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5510 | break; |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5511 | } |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5512 | debug_print_tree(pipe_list, 0); |
| 5513 | debug_printf_exec("parse_and_run_stream: run_and_free_list\n"); |
| 5514 | run_and_free_list(pipe_list); |
Denys Vlasenko | 00243b0 | 2009-11-16 02:00:03 +0100 | [diff] [blame] | 5515 | empty = 0; |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5516 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5519 | static void parse_and_run_string(const char *s) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5520 | { |
| 5521 | struct in_str input; |
| 5522 | setup_string_in_str(&input, s); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5523 | parse_and_run_stream(&input, '\0'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5524 | } |
| 5525 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5526 | static void parse_and_run_file(FILE *f) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5527 | { |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5528 | struct in_str input; |
| 5529 | setup_file_in_str(&input, f); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 5530 | parse_and_run_stream(&input, ';'); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5533 | #if ENABLE_HUSH_TICK |
| 5534 | static FILE *generate_stream_from_string(const char *s, pid_t *pid_p) |
| 5535 | { |
| 5536 | pid_t pid; |
| 5537 | int channel[2]; |
| 5538 | # if !BB_MMU |
| 5539 | char **to_free = NULL; |
| 5540 | # endif |
| 5541 | |
| 5542 | xpipe(channel); |
| 5543 | pid = BB_MMU ? xfork() : xvfork(); |
| 5544 | if (pid == 0) { /* child */ |
| 5545 | disable_restore_tty_pgrp_on_exit(); |
| 5546 | /* Process substitution is not considered to be usual |
| 5547 | * 'command execution'. |
| 5548 | * SUSv3 says ctrl-Z should be ignored, ctrl-C should not. |
| 5549 | */ |
| 5550 | bb_signals(0 |
| 5551 | + (1 << SIGTSTP) |
| 5552 | + (1 << SIGTTIN) |
| 5553 | + (1 << SIGTTOU) |
| 5554 | , SIG_IGN); |
| 5555 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 5556 | close(channel[0]); /* NB: close _first_, then move fd! */ |
| 5557 | xmove_fd(channel[1], 1); |
| 5558 | /* Prevent it from trying to handle ctrl-z etc */ |
| 5559 | IF_HUSH_JOB(G.run_list_level = 1;) |
| 5560 | /* Awful hack for `trap` or $(trap). |
| 5561 | * |
| 5562 | * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html |
| 5563 | * contains an example where "trap" is executed in a subshell: |
| 5564 | * |
| 5565 | * save_traps=$(trap) |
| 5566 | * ... |
| 5567 | * eval "$save_traps" |
| 5568 | * |
| 5569 | * Standard does not say that "trap" in subshell shall print |
| 5570 | * parent shell's traps. It only says that its output |
| 5571 | * must have suitable form, but then, in the above example |
| 5572 | * (which is not supposed to be normative), it implies that. |
| 5573 | * |
| 5574 | * bash (and probably other shell) does implement it |
| 5575 | * (traps are reset to defaults, but "trap" still shows them), |
| 5576 | * but as a result, "trap" logic is hopelessly messed up: |
| 5577 | * |
| 5578 | * # trap |
| 5579 | * trap -- 'echo Ho' SIGWINCH <--- we have a handler |
| 5580 | * # (trap) <--- trap is in subshell - no output (correct, traps are reset) |
| 5581 | * # true | trap <--- trap is in subshell - no output (ditto) |
| 5582 | * # echo `true | trap` <--- in subshell - output (but traps are reset!) |
| 5583 | * trap -- 'echo Ho' SIGWINCH |
| 5584 | * # echo `(trap)` <--- in subshell in subshell - output |
| 5585 | * trap -- 'echo Ho' SIGWINCH |
| 5586 | * # echo `true | (trap)` <--- in subshell in subshell in subshell - output! |
| 5587 | * trap -- 'echo Ho' SIGWINCH |
| 5588 | * |
| 5589 | * The rules when to forget and when to not forget traps |
| 5590 | * get really complex and nonsensical. |
| 5591 | * |
| 5592 | * Our solution: ONLY bare $(trap) or `trap` is special. |
| 5593 | */ |
| 5594 | s = skip_whitespace(s); |
| 5595 | if (strncmp(s, "trap", 4) == 0 |
| 5596 | && skip_whitespace(s + 4)[0] == '\0' |
| 5597 | ) { |
| 5598 | static const char *const argv[] = { NULL, NULL }; |
| 5599 | builtin_trap((char**)argv); |
| 5600 | exit(0); /* not _exit() - we need to fflush */ |
| 5601 | } |
| 5602 | # if BB_MMU |
| 5603 | reset_traps_to_defaults(); |
| 5604 | parse_and_run_string(s); |
| 5605 | _exit(G.last_exitcode); |
| 5606 | # else |
| 5607 | /* We re-execute after vfork on NOMMU. This makes this script safe: |
| 5608 | * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG |
| 5609 | * huge=`cat BIG` # was blocking here forever |
| 5610 | * echo OK |
| 5611 | */ |
| 5612 | re_execute_shell(&to_free, |
| 5613 | s, |
| 5614 | G.global_argv[0], |
| 5615 | G.global_argv + 1, |
| 5616 | NULL); |
| 5617 | # endif |
| 5618 | } |
| 5619 | |
| 5620 | /* parent */ |
| 5621 | *pid_p = pid; |
| 5622 | # if ENABLE_HUSH_FAST |
| 5623 | G.count_SIGCHLD++; |
| 5624 | //bb_error_msg("[%d] fork in generate_stream_from_string:" |
| 5625 | // " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", |
| 5626 | // getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5627 | # endif |
| 5628 | enable_restore_tty_pgrp_on_exit(); |
| 5629 | # if !BB_MMU |
| 5630 | free(to_free); |
| 5631 | # endif |
| 5632 | close(channel[1]); |
| 5633 | close_on_exec_on(channel[0]); |
| 5634 | return xfdopen_for_read(channel[0]); |
| 5635 | } |
| 5636 | |
| 5637 | /* Return code is exit status of the process that is run. */ |
| 5638 | static int process_command_subs(o_string *dest, const char *s) |
| 5639 | { |
| 5640 | FILE *fp; |
| 5641 | struct in_str pipe_str; |
| 5642 | pid_t pid; |
| 5643 | int status, ch, eol_cnt; |
| 5644 | |
| 5645 | fp = generate_stream_from_string(s, &pid); |
| 5646 | |
| 5647 | /* Now send results of command back into original context */ |
| 5648 | setup_file_in_str(&pipe_str, fp); |
| 5649 | eol_cnt = 0; |
| 5650 | while ((ch = i_getch(&pipe_str)) != EOF) { |
| 5651 | if (ch == '\n') { |
| 5652 | eol_cnt++; |
| 5653 | continue; |
| 5654 | } |
| 5655 | while (eol_cnt) { |
| 5656 | o_addchr(dest, '\n'); |
| 5657 | eol_cnt--; |
| 5658 | } |
| 5659 | o_addQchr(dest, ch); |
| 5660 | } |
| 5661 | |
| 5662 | debug_printf("done reading from `cmd` pipe, closing it\n"); |
| 5663 | fclose(fp); |
| 5664 | /* We need to extract exitcode. Test case |
| 5665 | * "true; echo `sleep 1; false` $?" |
| 5666 | * should print 1 */ |
| 5667 | safe_waitpid(pid, &status, 0); |
| 5668 | debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status)); |
| 5669 | return WEXITSTATUS(status); |
| 5670 | } |
| 5671 | #endif /* ENABLE_HUSH_TICK */ |
| 5672 | |
| 5673 | |
| 5674 | static void setup_heredoc(struct redir_struct *redir) |
| 5675 | { |
| 5676 | struct fd_pair pair; |
| 5677 | pid_t pid; |
| 5678 | int len, written; |
| 5679 | /* the _body_ of heredoc (misleading field name) */ |
| 5680 | const char *heredoc = redir->rd_filename; |
| 5681 | char *expanded; |
| 5682 | #if !BB_MMU |
| 5683 | char **to_free; |
| 5684 | #endif |
| 5685 | |
| 5686 | expanded = NULL; |
| 5687 | if (!(redir->rd_dup & HEREDOC_QUOTED)) { |
Denys Vlasenko | d98e5c6 | 2010-09-10 10:44:23 +0200 | [diff] [blame] | 5688 | expanded = encode_then_expand_string(heredoc, /*process_bkslash:*/ 1, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5689 | if (expanded) |
| 5690 | heredoc = expanded; |
| 5691 | } |
| 5692 | len = strlen(heredoc); |
| 5693 | |
| 5694 | close(redir->rd_fd); /* often saves dup2+close in xmove_fd */ |
| 5695 | xpiped_pair(pair); |
| 5696 | xmove_fd(pair.rd, redir->rd_fd); |
| 5697 | |
| 5698 | /* Try writing without forking. Newer kernels have |
| 5699 | * dynamically growing pipes. Must use non-blocking write! */ |
| 5700 | ndelay_on(pair.wr); |
| 5701 | while (1) { |
| 5702 | written = write(pair.wr, heredoc, len); |
| 5703 | if (written <= 0) |
| 5704 | break; |
| 5705 | len -= written; |
| 5706 | if (len == 0) { |
| 5707 | close(pair.wr); |
| 5708 | free(expanded); |
| 5709 | return; |
| 5710 | } |
| 5711 | heredoc += written; |
| 5712 | } |
| 5713 | ndelay_off(pair.wr); |
| 5714 | |
| 5715 | /* Okay, pipe buffer was not big enough */ |
| 5716 | /* Note: we must not create a stray child (bastard? :) |
| 5717 | * for the unsuspecting parent process. Child creates a grandchild |
| 5718 | * and exits before parent execs the process which consumes heredoc |
| 5719 | * (that exec happens after we return from this function) */ |
| 5720 | #if !BB_MMU |
| 5721 | to_free = NULL; |
| 5722 | #endif |
| 5723 | pid = xvfork(); |
| 5724 | if (pid == 0) { |
| 5725 | /* child */ |
| 5726 | disable_restore_tty_pgrp_on_exit(); |
| 5727 | pid = BB_MMU ? xfork() : xvfork(); |
| 5728 | if (pid != 0) |
| 5729 | _exit(0); |
| 5730 | /* grandchild */ |
| 5731 | close(redir->rd_fd); /* read side of the pipe */ |
| 5732 | #if BB_MMU |
| 5733 | full_write(pair.wr, heredoc, len); /* may loop or block */ |
| 5734 | _exit(0); |
| 5735 | #else |
| 5736 | /* Delegate blocking writes to another process */ |
| 5737 | xmove_fd(pair.wr, STDOUT_FILENO); |
| 5738 | re_execute_shell(&to_free, heredoc, NULL, NULL, NULL); |
| 5739 | #endif |
| 5740 | } |
| 5741 | /* parent */ |
| 5742 | #if ENABLE_HUSH_FAST |
| 5743 | G.count_SIGCHLD++; |
| 5744 | //bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 5745 | #endif |
| 5746 | enable_restore_tty_pgrp_on_exit(); |
| 5747 | #if !BB_MMU |
| 5748 | free(to_free); |
| 5749 | #endif |
| 5750 | close(pair.wr); |
| 5751 | free(expanded); |
| 5752 | wait(NULL); /* wait till child has died */ |
| 5753 | } |
| 5754 | |
| 5755 | /* squirrel != NULL means we squirrel away copies of stdin, stdout, |
| 5756 | * and stderr if they are redirected. */ |
| 5757 | static int setup_redirects(struct command *prog, int squirrel[]) |
| 5758 | { |
| 5759 | int openfd, mode; |
| 5760 | struct redir_struct *redir; |
| 5761 | |
| 5762 | for (redir = prog->redirects; redir; redir = redir->next) { |
| 5763 | if (redir->rd_type == REDIRECT_HEREDOC2) { |
| 5764 | /* rd_fd<<HERE case */ |
| 5765 | if (squirrel && redir->rd_fd < 3 |
| 5766 | && squirrel[redir->rd_fd] < 0 |
| 5767 | ) { |
| 5768 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 5769 | } |
| 5770 | /* for REDIRECT_HEREDOC2, rd_filename holds _contents_ |
| 5771 | * of the heredoc */ |
| 5772 | debug_printf_parse("set heredoc '%s'\n", |
| 5773 | redir->rd_filename); |
| 5774 | setup_heredoc(redir); |
| 5775 | continue; |
| 5776 | } |
| 5777 | |
| 5778 | if (redir->rd_dup == REDIRFD_TO_FILE) { |
| 5779 | /* rd_fd<*>file case (<*> is <,>,>>,<>) */ |
| 5780 | char *p; |
| 5781 | if (redir->rd_filename == NULL) { |
| 5782 | /* Something went wrong in the parse. |
| 5783 | * Pretend it didn't happen */ |
| 5784 | bb_error_msg("bug in redirect parse"); |
| 5785 | continue; |
| 5786 | } |
| 5787 | mode = redir_table[redir->rd_type].mode; |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 5788 | p = expand_string_to_string(redir->rd_filename, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5789 | openfd = open_or_warn(p, mode); |
| 5790 | free(p); |
| 5791 | if (openfd < 0) { |
| 5792 | /* this could get lost if stderr has been redirected, but |
| 5793 | * bash and ash both lose it as well (though zsh doesn't!) */ |
| 5794 | //what the above comment tries to say? |
| 5795 | return 1; |
| 5796 | } |
| 5797 | } else { |
| 5798 | /* rd_fd<*>rd_dup or rd_fd<*>- cases */ |
| 5799 | openfd = redir->rd_dup; |
| 5800 | } |
| 5801 | |
| 5802 | if (openfd != redir->rd_fd) { |
| 5803 | if (squirrel && redir->rd_fd < 3 |
| 5804 | && squirrel[redir->rd_fd] < 0 |
| 5805 | ) { |
| 5806 | squirrel[redir->rd_fd] = dup(redir->rd_fd); |
| 5807 | } |
| 5808 | if (openfd == REDIRFD_CLOSE) { |
| 5809 | /* "n>-" means "close me" */ |
| 5810 | close(redir->rd_fd); |
| 5811 | } else { |
| 5812 | xdup2(openfd, redir->rd_fd); |
| 5813 | if (redir->rd_dup == REDIRFD_TO_FILE) |
| 5814 | close(openfd); |
| 5815 | } |
| 5816 | } |
| 5817 | } |
| 5818 | return 0; |
| 5819 | } |
| 5820 | |
| 5821 | static void restore_redirects(int squirrel[]) |
| 5822 | { |
| 5823 | int i, fd; |
| 5824 | for (i = 0; i < 3; i++) { |
| 5825 | fd = squirrel[i]; |
| 5826 | if (fd != -1) { |
| 5827 | /* We simply die on error */ |
| 5828 | xmove_fd(fd, i); |
| 5829 | } |
| 5830 | } |
| 5831 | } |
| 5832 | |
| 5833 | static char *find_in_path(const char *arg) |
| 5834 | { |
| 5835 | char *ret = NULL; |
| 5836 | const char *PATH = get_local_var_value("PATH"); |
| 5837 | |
| 5838 | if (!PATH) |
| 5839 | return NULL; |
| 5840 | |
| 5841 | while (1) { |
| 5842 | const char *end = strchrnul(PATH, ':'); |
| 5843 | int sz = end - PATH; /* must be int! */ |
| 5844 | |
| 5845 | free(ret); |
| 5846 | if (sz != 0) { |
| 5847 | ret = xasprintf("%.*s/%s", sz, PATH, arg); |
| 5848 | } else { |
| 5849 | /* We have xxx::yyyy in $PATH, |
| 5850 | * it means "use current dir" */ |
| 5851 | ret = xstrdup(arg); |
| 5852 | } |
| 5853 | if (access(ret, F_OK) == 0) |
| 5854 | break; |
| 5855 | |
| 5856 | if (*end == '\0') { |
| 5857 | free(ret); |
| 5858 | return NULL; |
| 5859 | } |
| 5860 | PATH = end + 1; |
| 5861 | } |
| 5862 | |
| 5863 | return ret; |
| 5864 | } |
| 5865 | |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5866 | static const struct built_in_command *find_builtin_helper(const char *name, |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5867 | const struct built_in_command *x, |
| 5868 | const struct built_in_command *end) |
| 5869 | { |
| 5870 | while (x != end) { |
| 5871 | if (strcmp(name, x->b_cmd) != 0) { |
| 5872 | x++; |
| 5873 | continue; |
| 5874 | } |
| 5875 | debug_printf_exec("found builtin '%s'\n", name); |
| 5876 | return x; |
| 5877 | } |
| 5878 | return NULL; |
| 5879 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5880 | static const struct built_in_command *find_builtin1(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5881 | { |
| 5882 | return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]); |
| 5883 | } |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 5884 | static const struct built_in_command *find_builtin(const char *name) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 5885 | { |
| 5886 | const struct built_in_command *x = find_builtin1(name); |
| 5887 | if (x) |
| 5888 | return x; |
| 5889 | return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]); |
| 5890 | } |
| 5891 | |
| 5892 | #if ENABLE_HUSH_FUNCTIONS |
| 5893 | static struct function **find_function_slot(const char *name) |
| 5894 | { |
| 5895 | struct function **funcpp = &G.top_func; |
| 5896 | while (*funcpp) { |
| 5897 | if (strcmp(name, (*funcpp)->name) == 0) { |
| 5898 | break; |
| 5899 | } |
| 5900 | funcpp = &(*funcpp)->next; |
| 5901 | } |
| 5902 | return funcpp; |
| 5903 | } |
| 5904 | |
| 5905 | static const struct function *find_function(const char *name) |
| 5906 | { |
| 5907 | const struct function *funcp = *find_function_slot(name); |
| 5908 | if (funcp) |
| 5909 | debug_printf_exec("found function '%s'\n", name); |
| 5910 | return funcp; |
| 5911 | } |
| 5912 | |
| 5913 | /* Note: takes ownership on name ptr */ |
| 5914 | static struct function *new_function(char *name) |
| 5915 | { |
| 5916 | struct function **funcpp = find_function_slot(name); |
| 5917 | struct function *funcp = *funcpp; |
| 5918 | |
| 5919 | if (funcp != NULL) { |
| 5920 | struct command *cmd = funcp->parent_cmd; |
| 5921 | debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd); |
| 5922 | if (!cmd) { |
| 5923 | debug_printf_exec("freeing & replacing function '%s'\n", funcp->name); |
| 5924 | free(funcp->name); |
| 5925 | /* Note: if !funcp->body, do not free body_as_string! |
| 5926 | * This is a special case of "-F name body" function: |
| 5927 | * body_as_string was not malloced! */ |
| 5928 | if (funcp->body) { |
| 5929 | free_pipe_list(funcp->body); |
| 5930 | # if !BB_MMU |
| 5931 | free(funcp->body_as_string); |
| 5932 | # endif |
| 5933 | } |
| 5934 | } else { |
| 5935 | debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name); |
| 5936 | cmd->argv[0] = funcp->name; |
| 5937 | cmd->group = funcp->body; |
| 5938 | # if !BB_MMU |
| 5939 | cmd->group_as_string = funcp->body_as_string; |
| 5940 | # endif |
| 5941 | } |
| 5942 | } else { |
| 5943 | debug_printf_exec("remembering new function '%s'\n", name); |
| 5944 | funcp = *funcpp = xzalloc(sizeof(*funcp)); |
| 5945 | /*funcp->next = NULL;*/ |
| 5946 | } |
| 5947 | |
| 5948 | funcp->name = name; |
| 5949 | return funcp; |
| 5950 | } |
| 5951 | |
| 5952 | static void unset_func(const char *name) |
| 5953 | { |
| 5954 | struct function **funcpp = find_function_slot(name); |
| 5955 | struct function *funcp = *funcpp; |
| 5956 | |
| 5957 | if (funcp != NULL) { |
| 5958 | debug_printf_exec("freeing function '%s'\n", funcp->name); |
| 5959 | *funcpp = funcp->next; |
| 5960 | /* funcp is unlinked now, deleting it. |
| 5961 | * Note: if !funcp->body, the function was created by |
| 5962 | * "-F name body", do not free ->body_as_string |
| 5963 | * and ->name as they were not malloced. */ |
| 5964 | if (funcp->body) { |
| 5965 | free_pipe_list(funcp->body); |
| 5966 | free(funcp->name); |
| 5967 | # if !BB_MMU |
| 5968 | free(funcp->body_as_string); |
| 5969 | # endif |
| 5970 | } |
| 5971 | free(funcp); |
| 5972 | } |
| 5973 | } |
| 5974 | |
| 5975 | # if BB_MMU |
| 5976 | #define exec_function(to_free, funcp, argv) \ |
| 5977 | exec_function(funcp, argv) |
| 5978 | # endif |
| 5979 | static void exec_function(char ***to_free, |
| 5980 | const struct function *funcp, |
| 5981 | char **argv) NORETURN; |
| 5982 | static void exec_function(char ***to_free, |
| 5983 | const struct function *funcp, |
| 5984 | char **argv) |
| 5985 | { |
| 5986 | # if BB_MMU |
| 5987 | int n = 1; |
| 5988 | |
| 5989 | argv[0] = G.global_argv[0]; |
| 5990 | G.global_argv = argv; |
| 5991 | while (*++argv) |
| 5992 | n++; |
| 5993 | G.global_argc = n; |
| 5994 | /* On MMU, funcp->body is always non-NULL */ |
| 5995 | n = run_list(funcp->body); |
| 5996 | fflush_all(); |
| 5997 | _exit(n); |
| 5998 | # else |
| 5999 | re_execute_shell(to_free, |
| 6000 | funcp->body_as_string, |
| 6001 | G.global_argv[0], |
| 6002 | argv + 1, |
| 6003 | NULL); |
| 6004 | # endif |
| 6005 | } |
| 6006 | |
| 6007 | static int run_function(const struct function *funcp, char **argv) |
| 6008 | { |
| 6009 | int rc; |
| 6010 | save_arg_t sv; |
| 6011 | smallint sv_flg; |
| 6012 | |
| 6013 | save_and_replace_G_args(&sv, argv); |
| 6014 | |
| 6015 | /* "we are in function, ok to use return" */ |
| 6016 | sv_flg = G.flag_return_in_progress; |
| 6017 | G.flag_return_in_progress = -1; |
| 6018 | # if ENABLE_HUSH_LOCAL |
| 6019 | G.func_nest_level++; |
| 6020 | # endif |
| 6021 | |
| 6022 | /* On MMU, funcp->body is always non-NULL */ |
| 6023 | # if !BB_MMU |
| 6024 | if (!funcp->body) { |
| 6025 | /* Function defined by -F */ |
| 6026 | parse_and_run_string(funcp->body_as_string); |
| 6027 | rc = G.last_exitcode; |
| 6028 | } else |
| 6029 | # endif |
| 6030 | { |
| 6031 | rc = run_list(funcp->body); |
| 6032 | } |
| 6033 | |
| 6034 | # if ENABLE_HUSH_LOCAL |
| 6035 | { |
| 6036 | struct variable *var; |
| 6037 | struct variable **var_pp; |
| 6038 | |
| 6039 | var_pp = &G.top_var; |
| 6040 | while ((var = *var_pp) != NULL) { |
| 6041 | if (var->func_nest_level < G.func_nest_level) { |
| 6042 | var_pp = &var->next; |
| 6043 | continue; |
| 6044 | } |
| 6045 | /* Unexport */ |
| 6046 | if (var->flg_export) |
| 6047 | bb_unsetenv(var->varstr); |
| 6048 | /* Remove from global list */ |
| 6049 | *var_pp = var->next; |
| 6050 | /* Free */ |
| 6051 | if (!var->max_len) |
| 6052 | free(var->varstr); |
| 6053 | free(var); |
| 6054 | } |
| 6055 | G.func_nest_level--; |
| 6056 | } |
| 6057 | # endif |
| 6058 | G.flag_return_in_progress = sv_flg; |
| 6059 | |
| 6060 | restore_G_args(&sv, argv); |
| 6061 | |
| 6062 | return rc; |
| 6063 | } |
| 6064 | #endif /* ENABLE_HUSH_FUNCTIONS */ |
| 6065 | |
| 6066 | |
| 6067 | #if BB_MMU |
| 6068 | #define exec_builtin(to_free, x, argv) \ |
| 6069 | exec_builtin(x, argv) |
| 6070 | #else |
| 6071 | #define exec_builtin(to_free, x, argv) \ |
| 6072 | exec_builtin(to_free, argv) |
| 6073 | #endif |
| 6074 | static void exec_builtin(char ***to_free, |
| 6075 | const struct built_in_command *x, |
| 6076 | char **argv) NORETURN; |
| 6077 | static void exec_builtin(char ***to_free, |
| 6078 | const struct built_in_command *x, |
| 6079 | char **argv) |
| 6080 | { |
| 6081 | #if BB_MMU |
| 6082 | int rcode = x->b_function(argv); |
| 6083 | fflush_all(); |
| 6084 | _exit(rcode); |
| 6085 | #else |
| 6086 | /* On NOMMU, we must never block! |
| 6087 | * Example: { sleep 99 | read line; } & echo Ok |
| 6088 | */ |
| 6089 | re_execute_shell(to_free, |
| 6090 | argv[0], |
| 6091 | G.global_argv[0], |
| 6092 | G.global_argv + 1, |
| 6093 | argv); |
| 6094 | #endif |
| 6095 | } |
| 6096 | |
| 6097 | |
| 6098 | static void execvp_or_die(char **argv) NORETURN; |
| 6099 | static void execvp_or_die(char **argv) |
| 6100 | { |
| 6101 | debug_printf_exec("execing '%s'\n", argv[0]); |
| 6102 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 6103 | execvp(argv[0], argv); |
| 6104 | bb_perror_msg("can't execute '%s'", argv[0]); |
| 6105 | _exit(127); /* bash compat */ |
| 6106 | } |
| 6107 | |
| 6108 | #if ENABLE_HUSH_MODE_X |
| 6109 | static void dump_cmd_in_x_mode(char **argv) |
| 6110 | { |
| 6111 | if (G_x_mode && argv) { |
| 6112 | /* We want to output the line in one write op */ |
| 6113 | char *buf, *p; |
| 6114 | int len; |
| 6115 | int n; |
| 6116 | |
| 6117 | len = 3; |
| 6118 | n = 0; |
| 6119 | while (argv[n]) |
| 6120 | len += strlen(argv[n++]) + 1; |
| 6121 | buf = xmalloc(len); |
| 6122 | buf[0] = '+'; |
| 6123 | p = buf + 1; |
| 6124 | n = 0; |
| 6125 | while (argv[n]) |
| 6126 | p += sprintf(p, " %s", argv[n++]); |
| 6127 | *p++ = '\n'; |
| 6128 | *p = '\0'; |
| 6129 | fputs(buf, stderr); |
| 6130 | free(buf); |
| 6131 | } |
| 6132 | } |
| 6133 | #else |
| 6134 | # define dump_cmd_in_x_mode(argv) ((void)0) |
| 6135 | #endif |
| 6136 | |
| 6137 | #if BB_MMU |
| 6138 | #define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \ |
| 6139 | pseudo_exec_argv(argv, assignment_cnt, argv_expanded) |
| 6140 | #define pseudo_exec(nommu_save, command, argv_expanded) \ |
| 6141 | pseudo_exec(command, argv_expanded) |
| 6142 | #endif |
| 6143 | |
| 6144 | /* Called after [v]fork() in run_pipe, or from builtin_exec. |
| 6145 | * Never returns. |
| 6146 | * Don't exit() here. If you don't exec, use _exit instead. |
| 6147 | * The at_exit handlers apparently confuse the calling process, |
| 6148 | * in particular stdin handling. Not sure why? -- because of vfork! (vda) */ |
| 6149 | static void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6150 | char **argv, int assignment_cnt, |
| 6151 | char **argv_expanded) NORETURN; |
| 6152 | static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save, |
| 6153 | char **argv, int assignment_cnt, |
| 6154 | char **argv_expanded) |
| 6155 | { |
| 6156 | char **new_env; |
| 6157 | |
| 6158 | new_env = expand_assignments(argv, assignment_cnt); |
| 6159 | dump_cmd_in_x_mode(new_env); |
| 6160 | |
| 6161 | if (!argv[assignment_cnt]) { |
| 6162 | /* Case when we are here: ... | var=val | ... |
| 6163 | * (note that we do not exit early, i.e., do not optimize out |
| 6164 | * expand_assignments(): think about ... | var=`sleep 1` | ... |
| 6165 | */ |
| 6166 | free_strings(new_env); |
| 6167 | _exit(EXIT_SUCCESS); |
| 6168 | } |
| 6169 | |
| 6170 | #if BB_MMU |
| 6171 | set_vars_and_save_old(new_env); |
| 6172 | free(new_env); /* optional */ |
| 6173 | /* we can also destroy set_vars_and_save_old's return value, |
| 6174 | * to save memory */ |
| 6175 | #else |
| 6176 | nommu_save->new_env = new_env; |
| 6177 | nommu_save->old_vars = set_vars_and_save_old(new_env); |
| 6178 | #endif |
| 6179 | |
| 6180 | if (argv_expanded) { |
| 6181 | argv = argv_expanded; |
| 6182 | } else { |
| 6183 | argv = expand_strvec_to_strvec(argv + assignment_cnt); |
| 6184 | #if !BB_MMU |
| 6185 | nommu_save->argv = argv; |
| 6186 | #endif |
| 6187 | } |
| 6188 | dump_cmd_in_x_mode(argv); |
| 6189 | |
| 6190 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6191 | if (strchr(argv[0], '/') != NULL) |
| 6192 | goto skip; |
| 6193 | #endif |
| 6194 | |
| 6195 | /* Check if the command matches any of the builtins. |
| 6196 | * Depending on context, this might be redundant. But it's |
| 6197 | * easier to waste a few CPU cycles than it is to figure out |
| 6198 | * if this is one of those cases. |
| 6199 | */ |
| 6200 | { |
| 6201 | /* On NOMMU, it is more expensive to re-execute shell |
| 6202 | * just in order to run echo or test builtin. |
| 6203 | * It's better to skip it here and run corresponding |
| 6204 | * non-builtin later. */ |
| 6205 | const struct built_in_command *x; |
| 6206 | x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]); |
| 6207 | if (x) { |
| 6208 | exec_builtin(&nommu_save->argv_from_re_execing, x, argv); |
| 6209 | } |
| 6210 | } |
| 6211 | #if ENABLE_HUSH_FUNCTIONS |
| 6212 | /* Check if the command matches any functions */ |
| 6213 | { |
| 6214 | const struct function *funcp = find_function(argv[0]); |
| 6215 | if (funcp) { |
| 6216 | exec_function(&nommu_save->argv_from_re_execing, funcp, argv); |
| 6217 | } |
| 6218 | } |
| 6219 | #endif |
| 6220 | |
| 6221 | #if ENABLE_FEATURE_SH_STANDALONE |
| 6222 | /* Check if the command matches any busybox applets */ |
| 6223 | { |
| 6224 | int a = find_applet_by_name(argv[0]); |
| 6225 | if (a >= 0) { |
| 6226 | # if BB_MMU /* see above why on NOMMU it is not allowed */ |
| 6227 | if (APPLET_IS_NOEXEC(a)) { |
| 6228 | debug_printf_exec("running applet '%s'\n", argv[0]); |
| 6229 | run_applet_no_and_exit(a, argv); |
| 6230 | } |
| 6231 | # endif |
| 6232 | /* Re-exec ourselves */ |
| 6233 | debug_printf_exec("re-execing applet '%s'\n", argv[0]); |
| 6234 | sigprocmask(SIG_SETMASK, &G.inherited_set, NULL); |
| 6235 | execv(bb_busybox_exec_path, argv); |
| 6236 | /* If they called chroot or otherwise made the binary no longer |
| 6237 | * executable, fall through */ |
| 6238 | } |
| 6239 | } |
| 6240 | #endif |
| 6241 | |
| 6242 | #if ENABLE_FEATURE_SH_STANDALONE || BB_MMU |
| 6243 | skip: |
| 6244 | #endif |
| 6245 | execvp_or_die(argv); |
| 6246 | } |
| 6247 | |
| 6248 | /* Called after [v]fork() in run_pipe |
| 6249 | */ |
| 6250 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6251 | struct command *command, |
| 6252 | char **argv_expanded) NORETURN; |
| 6253 | static void pseudo_exec(nommu_save_t *nommu_save, |
| 6254 | struct command *command, |
| 6255 | char **argv_expanded) |
| 6256 | { |
| 6257 | if (command->argv) { |
| 6258 | pseudo_exec_argv(nommu_save, command->argv, |
| 6259 | command->assignment_cnt, argv_expanded); |
| 6260 | } |
| 6261 | |
| 6262 | if (command->group) { |
| 6263 | /* Cases when we are here: |
| 6264 | * ( list ) |
| 6265 | * { list } & |
| 6266 | * ... | ( list ) | ... |
| 6267 | * ... | { list } | ... |
| 6268 | */ |
| 6269 | #if BB_MMU |
| 6270 | int rcode; |
| 6271 | debug_printf_exec("pseudo_exec: run_list\n"); |
| 6272 | reset_traps_to_defaults(); |
| 6273 | rcode = run_list(command->group); |
| 6274 | /* OK to leak memory by not calling free_pipe_list, |
| 6275 | * since this process is about to exit */ |
| 6276 | _exit(rcode); |
| 6277 | #else |
| 6278 | re_execute_shell(&nommu_save->argv_from_re_execing, |
| 6279 | command->group_as_string, |
| 6280 | G.global_argv[0], |
| 6281 | G.global_argv + 1, |
| 6282 | NULL); |
| 6283 | #endif |
| 6284 | } |
| 6285 | |
| 6286 | /* Case when we are here: ... | >file */ |
| 6287 | debug_printf_exec("pseudo_exec'ed null command\n"); |
| 6288 | _exit(EXIT_SUCCESS); |
| 6289 | } |
| 6290 | |
| 6291 | #if ENABLE_HUSH_JOB |
| 6292 | static const char *get_cmdtext(struct pipe *pi) |
| 6293 | { |
| 6294 | char **argv; |
| 6295 | char *p; |
| 6296 | int len; |
| 6297 | |
| 6298 | /* This is subtle. ->cmdtext is created only on first backgrounding. |
| 6299 | * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...) |
| 6300 | * On subsequent bg argv is trashed, but we won't use it */ |
| 6301 | if (pi->cmdtext) |
| 6302 | return pi->cmdtext; |
| 6303 | argv = pi->cmds[0].argv; |
| 6304 | if (!argv || !argv[0]) { |
| 6305 | pi->cmdtext = xzalloc(1); |
| 6306 | return pi->cmdtext; |
| 6307 | } |
| 6308 | |
| 6309 | len = 0; |
| 6310 | do { |
| 6311 | len += strlen(*argv) + 1; |
| 6312 | } while (*++argv); |
| 6313 | p = xmalloc(len); |
| 6314 | pi->cmdtext = p; |
| 6315 | argv = pi->cmds[0].argv; |
| 6316 | do { |
| 6317 | len = strlen(*argv); |
| 6318 | memcpy(p, *argv, len); |
| 6319 | p += len; |
| 6320 | *p++ = ' '; |
| 6321 | } while (*++argv); |
| 6322 | p[-1] = '\0'; |
| 6323 | return pi->cmdtext; |
| 6324 | } |
| 6325 | |
| 6326 | static void insert_bg_job(struct pipe *pi) |
| 6327 | { |
| 6328 | struct pipe *job, **jobp; |
| 6329 | int i; |
| 6330 | |
| 6331 | /* Linear search for the ID of the job to use */ |
| 6332 | pi->jobid = 1; |
| 6333 | for (job = G.job_list; job; job = job->next) |
| 6334 | if (job->jobid >= pi->jobid) |
| 6335 | pi->jobid = job->jobid + 1; |
| 6336 | |
| 6337 | /* Add job to the list of running jobs */ |
| 6338 | jobp = &G.job_list; |
| 6339 | while ((job = *jobp) != NULL) |
| 6340 | jobp = &job->next; |
| 6341 | job = *jobp = xmalloc(sizeof(*job)); |
| 6342 | |
| 6343 | *job = *pi; /* physical copy */ |
| 6344 | job->next = NULL; |
| 6345 | job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds); |
| 6346 | /* Cannot copy entire pi->cmds[] vector! This causes double frees */ |
| 6347 | for (i = 0; i < pi->num_cmds; i++) { |
| 6348 | job->cmds[i].pid = pi->cmds[i].pid; |
| 6349 | /* all other fields are not used and stay zero */ |
| 6350 | } |
| 6351 | job->cmdtext = xstrdup(get_cmdtext(pi)); |
| 6352 | |
| 6353 | if (G_interactive_fd) |
| 6354 | printf("[%d] %d %s\n", job->jobid, job->cmds[0].pid, job->cmdtext); |
| 6355 | G.last_jobid = job->jobid; |
| 6356 | } |
| 6357 | |
| 6358 | static void remove_bg_job(struct pipe *pi) |
| 6359 | { |
| 6360 | struct pipe *prev_pipe; |
| 6361 | |
| 6362 | if (pi == G.job_list) { |
| 6363 | G.job_list = pi->next; |
| 6364 | } else { |
| 6365 | prev_pipe = G.job_list; |
| 6366 | while (prev_pipe->next != pi) |
| 6367 | prev_pipe = prev_pipe->next; |
| 6368 | prev_pipe->next = pi->next; |
| 6369 | } |
| 6370 | if (G.job_list) |
| 6371 | G.last_jobid = G.job_list->jobid; |
| 6372 | else |
| 6373 | G.last_jobid = 0; |
| 6374 | } |
| 6375 | |
| 6376 | /* Remove a backgrounded job */ |
| 6377 | static void delete_finished_bg_job(struct pipe *pi) |
| 6378 | { |
| 6379 | remove_bg_job(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6380 | free_pipe(pi); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6381 | } |
| 6382 | #endif /* JOB */ |
| 6383 | |
| 6384 | /* Check to see if any processes have exited -- if they |
| 6385 | * have, figure out why and see if a job has completed */ |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 6386 | static int checkjobs(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6387 | { |
| 6388 | int attributes; |
| 6389 | int status; |
| 6390 | #if ENABLE_HUSH_JOB |
| 6391 | struct pipe *pi; |
| 6392 | #endif |
| 6393 | pid_t childpid; |
| 6394 | int rcode = 0; |
| 6395 | |
| 6396 | debug_printf_jobs("checkjobs %p\n", fg_pipe); |
| 6397 | |
| 6398 | attributes = WUNTRACED; |
| 6399 | if (fg_pipe == NULL) |
| 6400 | attributes |= WNOHANG; |
| 6401 | |
| 6402 | errno = 0; |
| 6403 | #if ENABLE_HUSH_FAST |
| 6404 | if (G.handled_SIGCHLD == G.count_SIGCHLD) { |
| 6405 | //bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p", |
| 6406 | //getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe); |
| 6407 | /* There was neither fork nor SIGCHLD since last waitpid */ |
| 6408 | /* Avoid doing waitpid syscall if possible */ |
| 6409 | if (!G.we_have_children) { |
| 6410 | errno = ECHILD; |
| 6411 | return -1; |
| 6412 | } |
| 6413 | if (fg_pipe == NULL) { /* is WNOHANG set? */ |
| 6414 | /* We have children, but they did not exit |
| 6415 | * or stop yet (we saw no SIGCHLD) */ |
| 6416 | return 0; |
| 6417 | } |
| 6418 | /* else: !WNOHANG, waitpid will block, can't short-circuit */ |
| 6419 | } |
| 6420 | #endif |
| 6421 | |
| 6422 | /* Do we do this right? |
| 6423 | * bash-3.00# sleep 20 | false |
| 6424 | * <ctrl-Z pressed> |
| 6425 | * [3]+ Stopped sleep 20 | false |
| 6426 | * bash-3.00# echo $? |
| 6427 | * 1 <========== bg pipe is not fully done, but exitcode is already known! |
| 6428 | * [hush 1.14.0: yes we do it right] |
| 6429 | */ |
| 6430 | wait_more: |
| 6431 | while (1) { |
| 6432 | int i; |
| 6433 | int dead; |
| 6434 | |
| 6435 | #if ENABLE_HUSH_FAST |
| 6436 | i = G.count_SIGCHLD; |
| 6437 | #endif |
| 6438 | childpid = waitpid(-1, &status, attributes); |
| 6439 | if (childpid <= 0) { |
| 6440 | if (childpid && errno != ECHILD) |
| 6441 | bb_perror_msg("waitpid"); |
| 6442 | #if ENABLE_HUSH_FAST |
| 6443 | else { /* Until next SIGCHLD, waitpid's are useless */ |
| 6444 | G.we_have_children = (childpid == 0); |
| 6445 | G.handled_SIGCHLD = i; |
| 6446 | //bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6447 | } |
| 6448 | #endif |
| 6449 | break; |
| 6450 | } |
| 6451 | dead = WIFEXITED(status) || WIFSIGNALED(status); |
| 6452 | |
| 6453 | #if DEBUG_JOBS |
| 6454 | if (WIFSTOPPED(status)) |
| 6455 | debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n", |
| 6456 | childpid, WSTOPSIG(status), WEXITSTATUS(status)); |
| 6457 | if (WIFSIGNALED(status)) |
| 6458 | debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n", |
| 6459 | childpid, WTERMSIG(status), WEXITSTATUS(status)); |
| 6460 | if (WIFEXITED(status)) |
| 6461 | debug_printf_jobs("pid %d exited, exitcode %d\n", |
| 6462 | childpid, WEXITSTATUS(status)); |
| 6463 | #endif |
| 6464 | /* Were we asked to wait for fg pipe? */ |
| 6465 | if (fg_pipe) { |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6466 | i = fg_pipe->num_cmds; |
| 6467 | while (--i >= 0) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6468 | debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid); |
| 6469 | if (fg_pipe->cmds[i].pid != childpid) |
| 6470 | continue; |
| 6471 | if (dead) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6472 | int ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6473 | fg_pipe->cmds[i].pid = 0; |
| 6474 | fg_pipe->alive_cmds--; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6475 | ex = WEXITSTATUS(status); |
| 6476 | /* bash prints killer signal's name for *last* |
| 6477 | * process in pipe (prints just newline for SIGINT). |
| 6478 | * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT) |
| 6479 | */ |
| 6480 | if (WIFSIGNALED(status)) { |
| 6481 | int sig = WTERMSIG(status); |
| 6482 | if (i == fg_pipe->num_cmds-1) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6483 | printf("%s\n", sig == SIGINT ? "" : get_signame(sig)); |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6484 | /* TODO: MIPS has 128 sigs (1..128), what if sig==128 here? |
| 6485 | * Maybe we need to use sig | 128? */ |
| 6486 | ex = sig + 128; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6487 | } |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6488 | fg_pipe->cmds[i].cmd_exitcode = ex; |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6489 | } else { |
| 6490 | fg_pipe->cmds[i].is_stopped = 1; |
| 6491 | fg_pipe->stopped_cmds++; |
| 6492 | } |
| 6493 | debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n", |
| 6494 | fg_pipe->alive_cmds, fg_pipe->stopped_cmds); |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6495 | if (fg_pipe->alive_cmds == fg_pipe->stopped_cmds) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6496 | /* All processes in fg pipe have exited or stopped */ |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 6497 | i = fg_pipe->num_cmds; |
| 6498 | while (--i >= 0) { |
| 6499 | rcode = fg_pipe->cmds[i].cmd_exitcode; |
| 6500 | /* usually last process gives overall exitstatus, |
| 6501 | * but with "set -o pipefail", last *failed* process does */ |
| 6502 | if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0) |
| 6503 | break; |
| 6504 | } |
| 6505 | IF_HAS_KEYWORDS(if (fg_pipe->pi_inverted) rcode = !rcode;) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6506 | /* Note: *non-interactive* bash does not continue if all processes in fg pipe |
| 6507 | * are stopped. Testcase: "cat | cat" in a script (not on command line!) |
| 6508 | * and "killall -STOP cat" */ |
| 6509 | if (G_interactive_fd) { |
| 6510 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6511 | if (fg_pipe->alive_cmds != 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6512 | insert_bg_job(fg_pipe); |
| 6513 | #endif |
| 6514 | return rcode; |
| 6515 | } |
Denys Vlasenko | c08c3f5 | 2010-11-14 01:59:55 +0100 | [diff] [blame] | 6516 | if (fg_pipe->alive_cmds == 0) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6517 | return rcode; |
| 6518 | } |
| 6519 | /* There are still running processes in the fg pipe */ |
| 6520 | goto wait_more; /* do waitpid again */ |
| 6521 | } |
| 6522 | /* it wasnt fg_pipe, look for process in bg pipes */ |
| 6523 | } |
| 6524 | |
| 6525 | #if ENABLE_HUSH_JOB |
| 6526 | /* We asked to wait for bg or orphaned children */ |
| 6527 | /* No need to remember exitcode in this case */ |
| 6528 | for (pi = G.job_list; pi; pi = pi->next) { |
| 6529 | for (i = 0; i < pi->num_cmds; i++) { |
| 6530 | if (pi->cmds[i].pid == childpid) |
| 6531 | goto found_pi_and_prognum; |
| 6532 | } |
| 6533 | } |
| 6534 | /* Happens when shell is used as init process (init=/bin/sh) */ |
| 6535 | debug_printf("checkjobs: pid %d was not in our list!\n", childpid); |
| 6536 | continue; /* do waitpid again */ |
| 6537 | |
| 6538 | found_pi_and_prognum: |
| 6539 | if (dead) { |
| 6540 | /* child exited */ |
| 6541 | pi->cmds[i].pid = 0; |
| 6542 | pi->alive_cmds--; |
| 6543 | if (!pi->alive_cmds) { |
| 6544 | if (G_interactive_fd) |
| 6545 | printf(JOB_STATUS_FORMAT, pi->jobid, |
| 6546 | "Done", pi->cmdtext); |
| 6547 | delete_finished_bg_job(pi); |
| 6548 | } |
| 6549 | } else { |
| 6550 | /* child stopped */ |
| 6551 | pi->cmds[i].is_stopped = 1; |
| 6552 | pi->stopped_cmds++; |
| 6553 | } |
| 6554 | #endif |
| 6555 | } /* while (waitpid succeeds)... */ |
| 6556 | |
| 6557 | return rcode; |
| 6558 | } |
| 6559 | |
| 6560 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | da463fb | 2010-09-07 09:53:50 +0200 | [diff] [blame] | 6561 | static int checkjobs_and_fg_shell(struct pipe *fg_pipe) |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6562 | { |
| 6563 | pid_t p; |
| 6564 | int rcode = checkjobs(fg_pipe); |
| 6565 | if (G_saved_tty_pgrp) { |
| 6566 | /* Job finished, move the shell to the foreground */ |
| 6567 | p = getpgrp(); /* our process group id */ |
| 6568 | debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p); |
| 6569 | tcsetpgrp(G_interactive_fd, p); |
| 6570 | } |
| 6571 | return rcode; |
| 6572 | } |
| 6573 | #endif |
| 6574 | |
| 6575 | /* Start all the jobs, but don't wait for anything to finish. |
| 6576 | * See checkjobs(). |
| 6577 | * |
| 6578 | * Return code is normally -1, when the caller has to wait for children |
| 6579 | * to finish to determine the exit status of the pipe. If the pipe |
| 6580 | * is a simple builtin command, however, the action is done by the |
| 6581 | * time run_pipe returns, and the exit code is provided as the |
| 6582 | * return value. |
| 6583 | * |
| 6584 | * Returns -1 only if started some children. IOW: we have to |
| 6585 | * mask out retvals of builtins etc with 0xff! |
| 6586 | * |
| 6587 | * The only case when we do not need to [v]fork is when the pipe |
| 6588 | * is single, non-backgrounded, non-subshell command. Examples: |
| 6589 | * cmd ; ... { list } ; ... |
| 6590 | * cmd && ... { list } && ... |
| 6591 | * cmd || ... { list } || ... |
| 6592 | * If it is, then we can run cmd as a builtin, NOFORK [do we do this?], |
| 6593 | * or (if SH_STANDALONE) an applet, and we can run the { list } |
| 6594 | * with run_list. If it isn't one of these, we fork and exec cmd. |
| 6595 | * |
| 6596 | * Cases when we must fork: |
| 6597 | * non-single: cmd | cmd |
| 6598 | * backgrounded: cmd & { list } & |
| 6599 | * subshell: ( list ) [&] |
| 6600 | */ |
| 6601 | #if !ENABLE_HUSH_MODE_X |
Denys Vlasenko | 26777aa | 2010-11-22 23:49:10 +0100 | [diff] [blame] | 6602 | #define redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel, argv_expanded) \ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6603 | redirect_and_varexp_helper(new_env_p, old_vars_p, command, squirrel) |
| 6604 | #endif |
| 6605 | static int redirect_and_varexp_helper(char ***new_env_p, |
| 6606 | struct variable **old_vars_p, |
| 6607 | struct command *command, |
| 6608 | int squirrel[3], |
| 6609 | char **argv_expanded) |
| 6610 | { |
| 6611 | /* setup_redirects acts on file descriptors, not FILEs. |
| 6612 | * This is perfect for work that comes after exec(). |
| 6613 | * Is it really safe for inline use? Experimentally, |
| 6614 | * things seem to work. */ |
| 6615 | int rcode = setup_redirects(command, squirrel); |
| 6616 | if (rcode == 0) { |
| 6617 | char **new_env = expand_assignments(command->argv, command->assignment_cnt); |
| 6618 | *new_env_p = new_env; |
| 6619 | dump_cmd_in_x_mode(new_env); |
| 6620 | dump_cmd_in_x_mode(argv_expanded); |
| 6621 | if (old_vars_p) |
| 6622 | *old_vars_p = set_vars_and_save_old(new_env); |
| 6623 | } |
| 6624 | return rcode; |
| 6625 | } |
| 6626 | static NOINLINE int run_pipe(struct pipe *pi) |
| 6627 | { |
| 6628 | static const char *const null_ptr = NULL; |
| 6629 | |
| 6630 | int cmd_no; |
| 6631 | int next_infd; |
| 6632 | struct command *command; |
| 6633 | char **argv_expanded; |
| 6634 | char **argv; |
| 6635 | /* it is not always needed, but we aim to smaller code */ |
| 6636 | int squirrel[] = { -1, -1, -1 }; |
| 6637 | int rcode; |
| 6638 | |
| 6639 | debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds); |
| 6640 | debug_enter(); |
| 6641 | |
Denys Vlasenko | 1fd3d94 | 2010-09-08 13:31:53 +0200 | [diff] [blame] | 6642 | /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*" |
| 6643 | * Result should be 3 lines: q w e, qwe, q w e |
| 6644 | */ |
| 6645 | G.ifs = get_local_var_value("IFS"); |
| 6646 | if (!G.ifs) |
| 6647 | G.ifs = defifs; |
| 6648 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6649 | IF_HUSH_JOB(pi->pgrp = -1;) |
| 6650 | pi->stopped_cmds = 0; |
| 6651 | command = &pi->cmds[0]; |
| 6652 | argv_expanded = NULL; |
| 6653 | |
| 6654 | if (pi->num_cmds != 1 |
| 6655 | || pi->followup == PIPE_BG |
| 6656 | || command->cmd_type == CMD_SUBSHELL |
| 6657 | ) { |
| 6658 | goto must_fork; |
| 6659 | } |
| 6660 | |
| 6661 | pi->alive_cmds = 1; |
| 6662 | |
| 6663 | debug_printf_exec(": group:%p argv:'%s'\n", |
| 6664 | command->group, command->argv ? command->argv[0] : "NONE"); |
| 6665 | |
| 6666 | if (command->group) { |
| 6667 | #if ENABLE_HUSH_FUNCTIONS |
| 6668 | if (command->cmd_type == CMD_FUNCDEF) { |
| 6669 | /* "executing" func () { list } */ |
| 6670 | struct function *funcp; |
| 6671 | |
| 6672 | funcp = new_function(command->argv[0]); |
| 6673 | /* funcp->name is already set to argv[0] */ |
| 6674 | funcp->body = command->group; |
| 6675 | # if !BB_MMU |
| 6676 | funcp->body_as_string = command->group_as_string; |
| 6677 | command->group_as_string = NULL; |
| 6678 | # endif |
| 6679 | command->group = NULL; |
| 6680 | command->argv[0] = NULL; |
| 6681 | debug_printf_exec("cmd %p has child func at %p\n", command, funcp); |
| 6682 | funcp->parent_cmd = command; |
| 6683 | command->child_func = funcp; |
| 6684 | |
| 6685 | debug_printf_exec("run_pipe: return EXIT_SUCCESS\n"); |
| 6686 | debug_leave(); |
| 6687 | return EXIT_SUCCESS; |
| 6688 | } |
| 6689 | #endif |
| 6690 | /* { list } */ |
| 6691 | debug_printf("non-subshell group\n"); |
| 6692 | rcode = 1; /* exitcode if redir failed */ |
| 6693 | if (setup_redirects(command, squirrel) == 0) { |
| 6694 | debug_printf_exec(": run_list\n"); |
| 6695 | rcode = run_list(command->group) & 0xff; |
| 6696 | } |
| 6697 | restore_redirects(squirrel); |
| 6698 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6699 | debug_leave(); |
| 6700 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6701 | return rcode; |
| 6702 | } |
| 6703 | |
| 6704 | argv = command->argv ? command->argv : (char **) &null_ptr; |
| 6705 | { |
| 6706 | const struct built_in_command *x; |
| 6707 | #if ENABLE_HUSH_FUNCTIONS |
| 6708 | const struct function *funcp; |
| 6709 | #else |
| 6710 | enum { funcp = 0 }; |
| 6711 | #endif |
| 6712 | char **new_env = NULL; |
| 6713 | struct variable *old_vars = NULL; |
| 6714 | |
| 6715 | if (argv[command->assignment_cnt] == NULL) { |
| 6716 | /* Assignments, but no command */ |
| 6717 | /* Ensure redirects take effect (that is, create files). |
| 6718 | * Try "a=t >file" */ |
| 6719 | #if 0 /* A few cases in testsuite fail with this code. FIXME */ |
| 6720 | rcode = redirect_and_varexp_helper(&new_env, /*old_vars:*/ NULL, command, squirrel, /*argv_expanded:*/ NULL); |
| 6721 | /* Set shell variables */ |
| 6722 | if (new_env) { |
| 6723 | argv = new_env; |
| 6724 | while (*argv) { |
| 6725 | set_local_var(*argv, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6726 | /* Do we need to flag set_local_var() errors? |
| 6727 | * "assignment to readonly var" and "putenv error" |
| 6728 | */ |
| 6729 | argv++; |
| 6730 | } |
| 6731 | } |
| 6732 | /* Redirect error sets $? to 1. Otherwise, |
| 6733 | * if evaluating assignment value set $?, retain it. |
| 6734 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 6735 | if (rcode == 0) |
| 6736 | rcode = G.last_exitcode; |
| 6737 | /* Exit, _skipping_ variable restoring code: */ |
| 6738 | goto clean_up_and_ret0; |
| 6739 | |
| 6740 | #else /* Older, bigger, but more correct code */ |
| 6741 | |
| 6742 | rcode = setup_redirects(command, squirrel); |
| 6743 | restore_redirects(squirrel); |
| 6744 | /* Set shell variables */ |
| 6745 | if (G_x_mode) |
| 6746 | bb_putchar_stderr('+'); |
| 6747 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 6748 | char *p = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6749 | if (G_x_mode) |
| 6750 | fprintf(stderr, " %s", p); |
| 6751 | debug_printf_exec("set shell var:'%s'->'%s'\n", |
| 6752 | *argv, p); |
| 6753 | set_local_var(p, /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 6754 | /* Do we need to flag set_local_var() errors? |
| 6755 | * "assignment to readonly var" and "putenv error" |
| 6756 | */ |
| 6757 | argv++; |
| 6758 | } |
| 6759 | if (G_x_mode) |
| 6760 | bb_putchar_stderr('\n'); |
| 6761 | /* Redirect error sets $? to 1. Otherwise, |
| 6762 | * if evaluating assignment value set $?, retain it. |
| 6763 | * Try "false; q=`exit 2`; echo $?" - should print 2: */ |
| 6764 | if (rcode == 0) |
| 6765 | rcode = G.last_exitcode; |
| 6766 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6767 | debug_leave(); |
| 6768 | debug_printf_exec("run_pipe: return %d\n", rcode); |
| 6769 | return rcode; |
| 6770 | #endif |
| 6771 | } |
| 6772 | |
| 6773 | /* Expand the rest into (possibly) many strings each */ |
| 6774 | if (0) {} |
| 6775 | #if ENABLE_HUSH_BASH_COMPAT |
| 6776 | else if (command->cmd_type == CMD_SINGLEWORD_NOGLOB) { |
| 6777 | argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt); |
| 6778 | } |
| 6779 | #endif |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6780 | else { |
| 6781 | argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt); |
| 6782 | } |
| 6783 | |
| 6784 | /* if someone gives us an empty string: `cmd with empty output` */ |
| 6785 | if (!argv_expanded[0]) { |
| 6786 | free(argv_expanded); |
| 6787 | debug_leave(); |
| 6788 | return G.last_exitcode; |
| 6789 | } |
| 6790 | |
| 6791 | x = find_builtin(argv_expanded[0]); |
| 6792 | #if ENABLE_HUSH_FUNCTIONS |
| 6793 | funcp = NULL; |
| 6794 | if (!x) |
| 6795 | funcp = find_function(argv_expanded[0]); |
| 6796 | #endif |
| 6797 | if (x || funcp) { |
| 6798 | if (!funcp) { |
| 6799 | if (x->b_function == builtin_exec && argv_expanded[1] == NULL) { |
| 6800 | debug_printf("exec with redirects only\n"); |
| 6801 | rcode = setup_redirects(command, NULL); |
| 6802 | goto clean_up_and_ret1; |
| 6803 | } |
| 6804 | } |
| 6805 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 6806 | if (rcode == 0) { |
| 6807 | if (!funcp) { |
| 6808 | debug_printf_exec(": builtin '%s' '%s'...\n", |
| 6809 | x->b_cmd, argv_expanded[1]); |
| 6810 | rcode = x->b_function(argv_expanded) & 0xff; |
| 6811 | fflush_all(); |
| 6812 | } |
| 6813 | #if ENABLE_HUSH_FUNCTIONS |
| 6814 | else { |
| 6815 | # if ENABLE_HUSH_LOCAL |
| 6816 | struct variable **sv; |
| 6817 | sv = G.shadowed_vars_pp; |
| 6818 | G.shadowed_vars_pp = &old_vars; |
| 6819 | # endif |
| 6820 | debug_printf_exec(": function '%s' '%s'...\n", |
| 6821 | funcp->name, argv_expanded[1]); |
| 6822 | rcode = run_function(funcp, argv_expanded) & 0xff; |
| 6823 | # if ENABLE_HUSH_LOCAL |
| 6824 | G.shadowed_vars_pp = sv; |
| 6825 | # endif |
| 6826 | } |
| 6827 | #endif |
| 6828 | } |
| 6829 | clean_up_and_ret: |
| 6830 | unset_vars(new_env); |
| 6831 | add_vars(old_vars); |
| 6832 | /* clean_up_and_ret0: */ |
| 6833 | restore_redirects(squirrel); |
| 6834 | clean_up_and_ret1: |
| 6835 | free(argv_expanded); |
| 6836 | IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;) |
| 6837 | debug_leave(); |
| 6838 | debug_printf_exec("run_pipe return %d\n", rcode); |
| 6839 | return rcode; |
| 6840 | } |
| 6841 | |
| 6842 | if (ENABLE_FEATURE_SH_STANDALONE) { |
| 6843 | int n = find_applet_by_name(argv_expanded[0]); |
| 6844 | if (n >= 0 && APPLET_IS_NOFORK(n)) { |
| 6845 | rcode = redirect_and_varexp_helper(&new_env, &old_vars, command, squirrel, argv_expanded); |
| 6846 | if (rcode == 0) { |
| 6847 | debug_printf_exec(": run_nofork_applet '%s' '%s'...\n", |
| 6848 | argv_expanded[0], argv_expanded[1]); |
| 6849 | rcode = run_nofork_applet(n, argv_expanded); |
| 6850 | } |
| 6851 | goto clean_up_and_ret; |
| 6852 | } |
| 6853 | } |
| 6854 | /* It is neither builtin nor applet. We must fork. */ |
| 6855 | } |
| 6856 | |
| 6857 | must_fork: |
| 6858 | /* NB: argv_expanded may already be created, and that |
| 6859 | * might include `cmd` runs! Do not rerun it! We *must* |
| 6860 | * use argv_expanded if it's non-NULL */ |
| 6861 | |
| 6862 | /* Going to fork a child per each pipe member */ |
| 6863 | pi->alive_cmds = 0; |
| 6864 | next_infd = 0; |
| 6865 | |
| 6866 | cmd_no = 0; |
| 6867 | while (cmd_no < pi->num_cmds) { |
| 6868 | struct fd_pair pipefds; |
| 6869 | #if !BB_MMU |
| 6870 | volatile nommu_save_t nommu_save; |
| 6871 | nommu_save.new_env = NULL; |
| 6872 | nommu_save.old_vars = NULL; |
| 6873 | nommu_save.argv = NULL; |
| 6874 | nommu_save.argv_from_re_execing = NULL; |
| 6875 | #endif |
| 6876 | command = &pi->cmds[cmd_no]; |
| 6877 | cmd_no++; |
| 6878 | if (command->argv) { |
| 6879 | debug_printf_exec(": pipe member '%s' '%s'...\n", |
| 6880 | command->argv[0], command->argv[1]); |
| 6881 | } else { |
| 6882 | debug_printf_exec(": pipe member with no argv\n"); |
| 6883 | } |
| 6884 | |
| 6885 | /* pipes are inserted between pairs of commands */ |
| 6886 | pipefds.rd = 0; |
| 6887 | pipefds.wr = 1; |
| 6888 | if (cmd_no < pi->num_cmds) |
| 6889 | xpiped_pair(pipefds); |
| 6890 | |
| 6891 | command->pid = BB_MMU ? fork() : vfork(); |
| 6892 | if (!command->pid) { /* child */ |
| 6893 | #if ENABLE_HUSH_JOB |
| 6894 | disable_restore_tty_pgrp_on_exit(); |
| 6895 | CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */ |
| 6896 | |
| 6897 | /* Every child adds itself to new process group |
| 6898 | * with pgid == pid_of_first_child_in_pipe */ |
| 6899 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 6900 | pid_t pgrp; |
| 6901 | pgrp = pi->pgrp; |
| 6902 | if (pgrp < 0) /* true for 1st process only */ |
| 6903 | pgrp = getpid(); |
| 6904 | if (setpgid(0, pgrp) == 0 |
| 6905 | && pi->followup != PIPE_BG |
| 6906 | && G_saved_tty_pgrp /* we have ctty */ |
| 6907 | ) { |
| 6908 | /* We do it in *every* child, not just first, |
| 6909 | * to avoid races */ |
| 6910 | tcsetpgrp(G_interactive_fd, pgrp); |
| 6911 | } |
| 6912 | } |
| 6913 | #endif |
| 6914 | if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) { |
| 6915 | /* 1st cmd in backgrounded pipe |
| 6916 | * should have its stdin /dev/null'ed */ |
| 6917 | close(0); |
| 6918 | if (open(bb_dev_null, O_RDONLY)) |
| 6919 | xopen("/", O_RDONLY); |
| 6920 | } else { |
| 6921 | xmove_fd(next_infd, 0); |
| 6922 | } |
| 6923 | xmove_fd(pipefds.wr, 1); |
| 6924 | if (pipefds.rd > 1) |
| 6925 | close(pipefds.rd); |
| 6926 | /* Like bash, explicit redirects override pipes, |
| 6927 | * and the pipe fd is available for dup'ing. */ |
| 6928 | if (setup_redirects(command, NULL)) |
| 6929 | _exit(1); |
| 6930 | |
| 6931 | /* Restore default handlers just prior to exec */ |
| 6932 | /*signal(SIGCHLD, SIG_DFL); - so far we don't have any handlers */ |
| 6933 | |
| 6934 | /* Stores to nommu_save list of env vars putenv'ed |
| 6935 | * (NOMMU, on MMU we don't need that) */ |
| 6936 | /* cast away volatility... */ |
| 6937 | pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded); |
| 6938 | /* pseudo_exec() does not return */ |
| 6939 | } |
| 6940 | |
| 6941 | /* parent or error */ |
| 6942 | #if ENABLE_HUSH_FAST |
| 6943 | G.count_SIGCHLD++; |
| 6944 | //bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD); |
| 6945 | #endif |
| 6946 | enable_restore_tty_pgrp_on_exit(); |
| 6947 | #if !BB_MMU |
| 6948 | /* Clean up after vforked child */ |
| 6949 | free(nommu_save.argv); |
| 6950 | free(nommu_save.argv_from_re_execing); |
| 6951 | unset_vars(nommu_save.new_env); |
| 6952 | add_vars(nommu_save.old_vars); |
| 6953 | #endif |
| 6954 | free(argv_expanded); |
| 6955 | argv_expanded = NULL; |
| 6956 | if (command->pid < 0) { /* [v]fork failed */ |
| 6957 | /* Clearly indicate, was it fork or vfork */ |
| 6958 | bb_perror_msg(BB_MMU ? "vfork"+1 : "vfork"); |
| 6959 | } else { |
| 6960 | pi->alive_cmds++; |
| 6961 | #if ENABLE_HUSH_JOB |
| 6962 | /* Second and next children need to know pid of first one */ |
| 6963 | if (pi->pgrp < 0) |
| 6964 | pi->pgrp = command->pid; |
| 6965 | #endif |
| 6966 | } |
| 6967 | |
| 6968 | if (cmd_no > 1) |
| 6969 | close(next_infd); |
| 6970 | if (cmd_no < pi->num_cmds) |
| 6971 | close(pipefds.wr); |
| 6972 | /* Pass read (output) pipe end to next iteration */ |
| 6973 | next_infd = pipefds.rd; |
| 6974 | } |
| 6975 | |
| 6976 | if (!pi->alive_cmds) { |
| 6977 | debug_leave(); |
| 6978 | debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n"); |
| 6979 | return 1; |
| 6980 | } |
| 6981 | |
| 6982 | debug_leave(); |
| 6983 | debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds); |
| 6984 | return -1; |
| 6985 | } |
| 6986 | |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 6987 | /* NB: called by pseudo_exec, and therefore must not modify any |
| 6988 | * global data until exec/_exit (we can be a child after vfork!) */ |
| 6989 | static int run_list(struct pipe *pi) |
| 6990 | { |
| 6991 | #if ENABLE_HUSH_CASE |
| 6992 | char *case_word = NULL; |
| 6993 | #endif |
| 6994 | #if ENABLE_HUSH_LOOPS |
| 6995 | struct pipe *loop_top = NULL; |
| 6996 | char **for_lcur = NULL; |
| 6997 | char **for_list = NULL; |
| 6998 | #endif |
| 6999 | smallint last_followup; |
| 7000 | smalluint rcode; |
| 7001 | #if ENABLE_HUSH_IF || ENABLE_HUSH_CASE |
| 7002 | smalluint cond_code = 0; |
| 7003 | #else |
| 7004 | enum { cond_code = 0 }; |
| 7005 | #endif |
| 7006 | #if HAS_KEYWORDS |
Denys Vlasenko | 9b78255 | 2010-09-08 13:33:26 +0200 | [diff] [blame] | 7007 | smallint rword; /* RES_foo */ |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7008 | smallint last_rword; /* ditto */ |
| 7009 | #endif |
| 7010 | |
| 7011 | debug_printf_exec("run_list start lvl %d\n", G.run_list_level); |
| 7012 | debug_enter(); |
| 7013 | |
| 7014 | #if ENABLE_HUSH_LOOPS |
| 7015 | /* Check syntax for "for" */ |
| 7016 | for (struct pipe *cpipe = pi; cpipe; cpipe = cpipe->next) { |
| 7017 | if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN) |
| 7018 | continue; |
| 7019 | /* current word is FOR or IN (BOLD in comments below) */ |
| 7020 | if (cpipe->next == NULL) { |
| 7021 | syntax_error("malformed for"); |
| 7022 | debug_leave(); |
| 7023 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7024 | return 1; |
| 7025 | } |
| 7026 | /* "FOR v; do ..." and "for v IN a b; do..." are ok */ |
| 7027 | if (cpipe->next->res_word == RES_DO) |
| 7028 | continue; |
| 7029 | /* next word is not "do". It must be "in" then ("FOR v in ...") */ |
| 7030 | if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */ |
| 7031 | || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */ |
| 7032 | ) { |
| 7033 | syntax_error("malformed for"); |
| 7034 | debug_leave(); |
| 7035 | debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level); |
| 7036 | return 1; |
| 7037 | } |
| 7038 | } |
| 7039 | #endif |
| 7040 | |
| 7041 | /* Past this point, all code paths should jump to ret: label |
| 7042 | * in order to return, no direct "return" statements please. |
| 7043 | * This helps to ensure that no memory is leaked. */ |
| 7044 | |
| 7045 | #if ENABLE_HUSH_JOB |
| 7046 | G.run_list_level++; |
| 7047 | #endif |
| 7048 | |
| 7049 | #if HAS_KEYWORDS |
| 7050 | rword = RES_NONE; |
| 7051 | last_rword = RES_XXXX; |
| 7052 | #endif |
| 7053 | last_followup = PIPE_SEQ; |
| 7054 | rcode = G.last_exitcode; |
| 7055 | |
| 7056 | /* Go through list of pipes, (maybe) executing them. */ |
| 7057 | for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) { |
| 7058 | if (G.flag_SIGINT) |
| 7059 | break; |
| 7060 | |
| 7061 | IF_HAS_KEYWORDS(rword = pi->res_word;) |
| 7062 | debug_printf_exec(": rword=%d cond_code=%d last_rword=%d\n", |
| 7063 | rword, cond_code, last_rword); |
| 7064 | #if ENABLE_HUSH_LOOPS |
| 7065 | if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR) |
| 7066 | && loop_top == NULL /* avoid bumping G.depth_of_loop twice */ |
| 7067 | ) { |
| 7068 | /* start of a loop: remember where loop starts */ |
| 7069 | loop_top = pi; |
| 7070 | G.depth_of_loop++; |
| 7071 | } |
| 7072 | #endif |
| 7073 | /* Still in the same "if...", "then..." or "do..." branch? */ |
| 7074 | if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) { |
| 7075 | if ((rcode == 0 && last_followup == PIPE_OR) |
| 7076 | || (rcode != 0 && last_followup == PIPE_AND) |
| 7077 | ) { |
| 7078 | /* It is "<true> || CMD" or "<false> && CMD" |
| 7079 | * and we should not execute CMD */ |
| 7080 | debug_printf_exec("skipped cmd because of || or &&\n"); |
| 7081 | last_followup = pi->followup; |
| 7082 | continue; |
| 7083 | } |
| 7084 | } |
| 7085 | last_followup = pi->followup; |
| 7086 | IF_HAS_KEYWORDS(last_rword = rword;) |
| 7087 | #if ENABLE_HUSH_IF |
| 7088 | if (cond_code) { |
| 7089 | if (rword == RES_THEN) { |
| 7090 | /* if false; then ... fi has exitcode 0! */ |
| 7091 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7092 | /* "if <false> THEN cmd": skip cmd */ |
| 7093 | continue; |
| 7094 | } |
| 7095 | } else { |
| 7096 | if (rword == RES_ELSE || rword == RES_ELIF) { |
| 7097 | /* "if <true> then ... ELSE/ELIF cmd": |
| 7098 | * skip cmd and all following ones */ |
| 7099 | break; |
| 7100 | } |
| 7101 | } |
| 7102 | #endif |
| 7103 | #if ENABLE_HUSH_LOOPS |
| 7104 | if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */ |
| 7105 | if (!for_lcur) { |
| 7106 | /* first loop through for */ |
| 7107 | |
| 7108 | static const char encoded_dollar_at[] ALIGN1 = { |
| 7109 | SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0' |
| 7110 | }; /* encoded representation of "$@" */ |
| 7111 | static const char *const encoded_dollar_at_argv[] = { |
| 7112 | encoded_dollar_at, NULL |
| 7113 | }; /* argv list with one element: "$@" */ |
| 7114 | char **vals; |
| 7115 | |
| 7116 | vals = (char**)encoded_dollar_at_argv; |
| 7117 | if (pi->next->res_word == RES_IN) { |
| 7118 | /* if no variable values after "in" we skip "for" */ |
| 7119 | if (!pi->next->cmds[0].argv) { |
| 7120 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7121 | debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n"); |
| 7122 | break; |
| 7123 | } |
| 7124 | vals = pi->next->cmds[0].argv; |
| 7125 | } /* else: "for var; do..." -> assume "$@" list */ |
| 7126 | /* create list of variable values */ |
| 7127 | debug_print_strings("for_list made from", vals); |
| 7128 | for_list = expand_strvec_to_strvec(vals); |
| 7129 | for_lcur = for_list; |
| 7130 | debug_print_strings("for_list", for_list); |
| 7131 | } |
| 7132 | if (!*for_lcur) { |
| 7133 | /* "for" loop is over, clean up */ |
| 7134 | free(for_list); |
| 7135 | for_list = NULL; |
| 7136 | for_lcur = NULL; |
| 7137 | break; |
| 7138 | } |
| 7139 | /* Insert next value from for_lcur */ |
| 7140 | /* note: *for_lcur already has quotes removed, $var expanded, etc */ |
| 7141 | set_local_var(xasprintf("%s=%s", pi->cmds[0].argv[0], *for_lcur++), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ 0); |
| 7142 | continue; |
| 7143 | } |
| 7144 | if (rword == RES_IN) { |
| 7145 | continue; /* "for v IN list;..." - "in" has no cmds anyway */ |
| 7146 | } |
| 7147 | if (rword == RES_DONE) { |
| 7148 | continue; /* "done" has no cmds too */ |
| 7149 | } |
| 7150 | #endif |
| 7151 | #if ENABLE_HUSH_CASE |
| 7152 | if (rword == RES_CASE) { |
| 7153 | case_word = expand_strvec_to_string(pi->cmds->argv); |
| 7154 | continue; |
| 7155 | } |
| 7156 | if (rword == RES_MATCH) { |
| 7157 | char **argv; |
| 7158 | |
| 7159 | if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */ |
| 7160 | break; |
| 7161 | /* all prev words didn't match, does this one match? */ |
| 7162 | argv = pi->cmds->argv; |
| 7163 | while (*argv) { |
Denys Vlasenko | ebee410 | 2010-09-10 10:17:53 +0200 | [diff] [blame] | 7164 | char *pattern = expand_string_to_string(*argv, /*unbackslash:*/ 1); |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7165 | /* TODO: which FNM_xxx flags to use? */ |
| 7166 | cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0); |
| 7167 | free(pattern); |
| 7168 | if (cond_code == 0) { /* match! we will execute this branch */ |
| 7169 | free(case_word); /* make future "word)" stop */ |
| 7170 | case_word = NULL; |
| 7171 | break; |
| 7172 | } |
| 7173 | argv++; |
| 7174 | } |
| 7175 | continue; |
| 7176 | } |
| 7177 | if (rword == RES_CASE_BODY) { /* inside of a case branch */ |
| 7178 | if (cond_code != 0) |
| 7179 | continue; /* not matched yet, skip this pipe */ |
| 7180 | } |
| 7181 | #endif |
| 7182 | /* Just pressing <enter> in shell should check for jobs. |
| 7183 | * OTOH, in non-interactive shell this is useless |
| 7184 | * and only leads to extra job checks */ |
| 7185 | if (pi->num_cmds == 0) { |
| 7186 | if (G_interactive_fd) |
| 7187 | goto check_jobs_and_continue; |
| 7188 | continue; |
| 7189 | } |
| 7190 | |
| 7191 | /* After analyzing all keywords and conditions, we decided |
| 7192 | * to execute this pipe. NB: have to do checkjobs(NULL) |
| 7193 | * after run_pipe to collect any background children, |
| 7194 | * even if list execution is to be stopped. */ |
| 7195 | debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds); |
| 7196 | { |
| 7197 | int r; |
| 7198 | #if ENABLE_HUSH_LOOPS |
| 7199 | G.flag_break_continue = 0; |
| 7200 | #endif |
| 7201 | rcode = r = run_pipe(pi); /* NB: rcode is a smallint */ |
| 7202 | if (r != -1) { |
| 7203 | /* We ran a builtin, function, or group. |
| 7204 | * rcode is already known |
| 7205 | * and we don't need to wait for anything. */ |
| 7206 | G.last_exitcode = rcode; |
| 7207 | debug_printf_exec(": builtin/func exitcode %d\n", rcode); |
| 7208 | check_and_run_traps(0); |
| 7209 | #if ENABLE_HUSH_LOOPS |
| 7210 | /* Was it "break" or "continue"? */ |
| 7211 | if (G.flag_break_continue) { |
| 7212 | smallint fbc = G.flag_break_continue; |
| 7213 | /* We might fall into outer *loop*, |
| 7214 | * don't want to break it too */ |
| 7215 | if (loop_top) { |
| 7216 | G.depth_break_continue--; |
| 7217 | if (G.depth_break_continue == 0) |
| 7218 | G.flag_break_continue = 0; |
| 7219 | /* else: e.g. "continue 2" should *break* once, *then* continue */ |
| 7220 | } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */ |
| 7221 | if (G.depth_break_continue != 0 || fbc == BC_BREAK) |
| 7222 | goto check_jobs_and_break; |
| 7223 | /* "continue": simulate end of loop */ |
| 7224 | rword = RES_DONE; |
| 7225 | continue; |
| 7226 | } |
| 7227 | #endif |
| 7228 | #if ENABLE_HUSH_FUNCTIONS |
| 7229 | if (G.flag_return_in_progress == 1) { |
| 7230 | /* same as "goto check_jobs_and_break" */ |
| 7231 | checkjobs(NULL); |
| 7232 | break; |
| 7233 | } |
| 7234 | #endif |
| 7235 | } else if (pi->followup == PIPE_BG) { |
| 7236 | /* What does bash do with attempts to background builtins? */ |
| 7237 | /* even bash 3.2 doesn't do that well with nested bg: |
| 7238 | * try "{ { sleep 10; echo DEEP; } & echo HERE; } &". |
| 7239 | * I'm NOT treating inner &'s as jobs */ |
| 7240 | check_and_run_traps(0); |
| 7241 | #if ENABLE_HUSH_JOB |
| 7242 | if (G.run_list_level == 1) |
| 7243 | insert_bg_job(pi); |
| 7244 | #endif |
| 7245 | /* Last command's pid goes to $! */ |
| 7246 | G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid; |
| 7247 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7248 | debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n"); |
| 7249 | } else { |
| 7250 | #if ENABLE_HUSH_JOB |
| 7251 | if (G.run_list_level == 1 && G_interactive_fd) { |
| 7252 | /* Waits for completion, then fg's main shell */ |
| 7253 | rcode = checkjobs_and_fg_shell(pi); |
| 7254 | debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode); |
| 7255 | check_and_run_traps(0); |
| 7256 | } else |
| 7257 | #endif |
| 7258 | { /* This one just waits for completion */ |
| 7259 | rcode = checkjobs(pi); |
| 7260 | debug_printf_exec(": checkjobs exitcode %d\n", rcode); |
| 7261 | check_and_run_traps(0); |
| 7262 | } |
| 7263 | G.last_exitcode = rcode; |
| 7264 | } |
| 7265 | } |
| 7266 | |
| 7267 | /* Analyze how result affects subsequent commands */ |
| 7268 | #if ENABLE_HUSH_IF |
| 7269 | if (rword == RES_IF || rword == RES_ELIF) |
| 7270 | cond_code = rcode; |
| 7271 | #endif |
| 7272 | #if ENABLE_HUSH_LOOPS |
| 7273 | /* Beware of "while false; true; do ..."! */ |
| 7274 | if (pi->next && pi->next->res_word == RES_DO) { |
| 7275 | if (rword == RES_WHILE) { |
| 7276 | if (rcode) { |
| 7277 | /* "while false; do...done" - exitcode 0 */ |
| 7278 | G.last_exitcode = rcode = EXIT_SUCCESS; |
| 7279 | debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n"); |
| 7280 | goto check_jobs_and_break; |
| 7281 | } |
| 7282 | } |
| 7283 | if (rword == RES_UNTIL) { |
| 7284 | if (!rcode) { |
| 7285 | debug_printf_exec(": until expr is true: breaking\n"); |
| 7286 | check_jobs_and_break: |
| 7287 | checkjobs(NULL); |
| 7288 | break; |
| 7289 | } |
| 7290 | } |
| 7291 | } |
| 7292 | #endif |
| 7293 | |
| 7294 | check_jobs_and_continue: |
| 7295 | checkjobs(NULL); |
| 7296 | } /* for (pi) */ |
| 7297 | |
| 7298 | #if ENABLE_HUSH_JOB |
| 7299 | G.run_list_level--; |
| 7300 | #endif |
| 7301 | #if ENABLE_HUSH_LOOPS |
| 7302 | if (loop_top) |
| 7303 | G.depth_of_loop--; |
| 7304 | free(for_list); |
| 7305 | #endif |
| 7306 | #if ENABLE_HUSH_CASE |
| 7307 | free(case_word); |
| 7308 | #endif |
| 7309 | debug_leave(); |
| 7310 | debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level + 1, rcode); |
| 7311 | return rcode; |
| 7312 | } |
| 7313 | |
| 7314 | /* Select which version we will use */ |
| 7315 | static int run_and_free_list(struct pipe *pi) |
| 7316 | { |
| 7317 | int rcode = 0; |
| 7318 | debug_printf_exec("run_and_free_list entered\n"); |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7319 | if (!G.o_opt[OPT_O_NOEXEC]) { |
Denys Vlasenko | b36abf2 | 2010-09-05 14:50:59 +0200 | [diff] [blame] | 7320 | debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds); |
| 7321 | rcode = run_list(pi); |
| 7322 | } |
| 7323 | /* free_pipe_list has the side effect of clearing memory. |
| 7324 | * In the long run that function can be merged with run_list, |
| 7325 | * but doing that now would hobble the debugging effort. */ |
| 7326 | free_pipe_list(pi); |
| 7327 | debug_printf_exec("run_and_free_list return %d\n", rcode); |
| 7328 | return rcode; |
| 7329 | } |
| 7330 | |
| 7331 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7332 | /* Called a few times only (or even once if "sh -c") */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7333 | static void init_sigmasks(void) |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7334 | { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7335 | unsigned sig; |
| 7336 | unsigned mask; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7337 | sigset_t old_blocked_set; |
| 7338 | |
| 7339 | if (!G.inherited_set_is_saved) { |
| 7340 | sigprocmask(SIG_SETMASK, NULL, &G.blocked_set); |
| 7341 | G.inherited_set = G.blocked_set; |
| 7342 | } |
| 7343 | old_blocked_set = G.blocked_set; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7344 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7345 | mask = (1 << SIGQUIT); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7346 | if (G_interactive_fd) { |
Denis Vlasenko | e4bd4f2 | 2009-04-17 13:52:51 +0000 | [diff] [blame] | 7347 | mask = (1 << SIGQUIT) | SPECIAL_INTERACTIVE_SIGS; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7348 | if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */ |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7349 | mask |= SPECIAL_JOB_SIGS; |
| 7350 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7351 | G.non_DFL_mask = mask; |
Eric Andersen | 52a97ca | 2001-06-22 06:49:26 +0000 | [diff] [blame] | 7352 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7353 | sig = 0; |
| 7354 | while (mask) { |
| 7355 | if (mask & 1) |
| 7356 | sigaddset(&G.blocked_set, sig); |
| 7357 | mask >>= 1; |
| 7358 | sig++; |
| 7359 | } |
| 7360 | sigdelset(&G.blocked_set, SIGCHLD); |
| 7361 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7362 | if (memcmp(&old_blocked_set, &G.blocked_set, sizeof(old_blocked_set)) != 0) |
| 7363 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7364 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7365 | /* POSIX allows shell to re-enable SIGCHLD |
| 7366 | * even if it was SIG_IGN on entry */ |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7367 | #if ENABLE_HUSH_FAST |
| 7368 | G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7369 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7370 | signal(SIGCHLD, SIGCHLD_handler); |
| 7371 | #else |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7372 | if (!G.inherited_set_is_saved) |
Denys Vlasenko | 8d7be23 | 2009-05-25 16:38:32 +0200 | [diff] [blame] | 7373 | signal(SIGCHLD, SIG_DFL); |
| 7374 | #endif |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7375 | |
| 7376 | G.inherited_set_is_saved = 1; |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | #if ENABLE_HUSH_JOB |
| 7380 | /* helper */ |
| 7381 | static void maybe_set_to_sigexit(int sig) |
| 7382 | { |
| 7383 | void (*handler)(int); |
| 7384 | /* non_DFL_mask'ed signals are, well, masked, |
| 7385 | * no need to set handler for them. |
| 7386 | */ |
| 7387 | if (!((G.non_DFL_mask >> sig) & 1)) { |
| 7388 | handler = signal(sig, sigexit); |
| 7389 | if (handler == SIG_IGN) /* oops... restore back to IGN! */ |
| 7390 | signal(sig, handler); |
| 7391 | } |
| 7392 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7393 | /* Set handlers to restore tty pgrp and exit */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7394 | static void set_fatal_handlers(void) |
| 7395 | { |
Denis Vlasenko | a6c467f | 2007-05-05 15:10:52 +0000 | [diff] [blame] | 7396 | /* We _must_ restore tty pgrp on fatal signals */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7397 | if (HUSH_DEBUG) { |
| 7398 | maybe_set_to_sigexit(SIGILL ); |
| 7399 | maybe_set_to_sigexit(SIGFPE ); |
| 7400 | maybe_set_to_sigexit(SIGBUS ); |
| 7401 | maybe_set_to_sigexit(SIGSEGV); |
| 7402 | maybe_set_to_sigexit(SIGTRAP); |
| 7403 | } /* else: hush is perfect. what SEGV? */ |
| 7404 | maybe_set_to_sigexit(SIGABRT); |
| 7405 | /* bash 3.2 seems to handle these just like 'fatal' ones */ |
| 7406 | maybe_set_to_sigexit(SIGPIPE); |
| 7407 | maybe_set_to_sigexit(SIGALRM); |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 7408 | /* if we are interactive, SIGHUP, SIGTERM and SIGINT are masked. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7409 | * if we aren't interactive... but in this case |
| 7410 | * 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] | 7411 | /*maybe_set_to_sigexit(SIGHUP );*/ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7412 | /*maybe_set_to_sigexit(SIGTERM);*/ |
| 7413 | /*maybe_set_to_sigexit(SIGINT );*/ |
Eric Andersen | 6c947d2 | 2001-06-25 22:24:38 +0000 | [diff] [blame] | 7414 | } |
Denis Vlasenko | b81b3df | 2007-04-28 16:48:04 +0000 | [diff] [blame] | 7415 | #endif |
Eric Andersen | ada18ff | 2001-05-21 16:18:22 +0000 | [diff] [blame] | 7416 | |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7417 | static int set_mode(int state, char mode, const char *o_opt) |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7418 | { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7419 | int idx; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7420 | switch (mode) { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7421 | case 'n': |
Dan Fandrich | 85c6247 | 2010-11-20 13:05:17 -0800 | [diff] [blame] | 7422 | G.o_opt[OPT_O_NOEXEC] = state; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7423 | break; |
| 7424 | case 'x': |
| 7425 | IF_HUSH_MODE_X(G_x_mode = state;) |
| 7426 | break; |
| 7427 | case 'o': |
| 7428 | if (!o_opt) { |
| 7429 | /* "set -+o" without parameter. |
| 7430 | * in bash, set -o produces this output: |
| 7431 | * pipefail off |
| 7432 | * and set +o: |
| 7433 | * set +o pipefail |
| 7434 | * We always use the second form. |
| 7435 | */ |
| 7436 | const char *p = o_opt_strings; |
| 7437 | idx = 0; |
| 7438 | while (*p) { |
| 7439 | printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p); |
| 7440 | idx++; |
| 7441 | p += strlen(p) + 1; |
| 7442 | } |
| 7443 | break; |
| 7444 | } |
| 7445 | idx = index_in_strings(o_opt_strings, o_opt); |
| 7446 | if (idx >= 0) { |
| 7447 | G.o_opt[idx] = state; |
| 7448 | break; |
| 7449 | } |
| 7450 | default: |
| 7451 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 7452 | } |
| 7453 | return EXIT_SUCCESS; |
| 7454 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7455 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 7456 | int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Matt Kraai | 2d91deb | 2001-08-01 17:21:35 +0000 | [diff] [blame] | 7457 | int hush_main(int argc, char **argv) |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7458 | { |
| 7459 | int opt; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7460 | unsigned builtin_argc; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7461 | char **e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7462 | struct variable *cur_var; |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 7463 | struct variable shell_ver; |
Eric Andersen | bc604a2 | 2001-05-16 05:24:03 +0000 | [diff] [blame] | 7464 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 7465 | INIT_G(); |
Denys Vlasenko | cddbb61 | 2010-05-20 14:27:09 +0200 | [diff] [blame] | 7466 | if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7467 | G.last_exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7468 | #if !BB_MMU |
| 7469 | G.argv0_for_re_execing = argv[0]; |
| 7470 | #endif |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7471 | /* Deal with HUSH_VERSION */ |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 7472 | memset(&shell_ver, 0, sizeof(shell_ver)); |
| 7473 | shell_ver.flg_export = 1; |
| 7474 | shell_ver.flg_read_only = 1; |
Denys Vlasenko | 4f87049 | 2010-09-10 11:06:01 +0200 | [diff] [blame] | 7475 | /* Code which handles ${var<op>...} needs writable values for all variables, |
Denys Vlasenko | 36f774a | 2010-09-05 14:45:38 +0200 | [diff] [blame] | 7476 | * therefore we xstrdup: */ |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 7477 | shell_ver.varstr = xstrdup(hush_version_str), |
| 7478 | G.top_var = &shell_ver; |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7479 | /* Create shell local variables from the values |
| 7480 | * currently living in the environment */ |
Denis Vlasenko | f886fd2 | 2008-10-13 12:36:05 +0000 | [diff] [blame] | 7481 | debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7482 | unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7483 | cur_var = G.top_var; |
Denis Vlasenko | 0a83fc3 | 2007-05-25 11:12:32 +0000 | [diff] [blame] | 7484 | e = environ; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7485 | if (e) while (*e) { |
| 7486 | char *value = strchr(*e, '='); |
| 7487 | if (value) { /* paranoia */ |
| 7488 | cur_var->next = xzalloc(sizeof(*cur_var)); |
| 7489 | cur_var = cur_var->next; |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7490 | cur_var->varstr = *e; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7491 | cur_var->max_len = strlen(*e); |
| 7492 | cur_var->flg_export = 1; |
| 7493 | } |
| 7494 | e++; |
| 7495 | } |
Denys Vlasenko | 605067b | 2010-09-06 12:10:51 +0200 | [diff] [blame] | 7496 | /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ |
Denys Vlasenko | 52e460b | 2010-09-16 16:12:00 +0200 | [diff] [blame] | 7497 | debug_printf_env("putenv '%s'\n", shell_ver.varstr); |
| 7498 | putenv(shell_ver.varstr); |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7499 | |
| 7500 | /* Export PWD */ |
| 7501 | set_pwd_var(/*exp:*/ 1); |
| 7502 | /* bash also exports SHLVL and _, |
| 7503 | * and sets (but doesn't export) the following variables: |
| 7504 | * BASH=/bin/bash |
| 7505 | * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu") |
| 7506 | * BASH_VERSION='3.2.0(1)-release' |
| 7507 | * HOSTTYPE=i386 |
| 7508 | * MACHTYPE=i386-pc-linux-gnu |
| 7509 | * OSTYPE=linux-gnu |
| 7510 | * HOSTNAME=<xxxxxxxxxx> |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7511 | * PPID=<NNNNN> - we also do it elsewhere |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7512 | * EUID=<NNNNN> |
| 7513 | * UID=<NNNNN> |
| 7514 | * GROUPS=() |
| 7515 | * LINES=<NNN> |
| 7516 | * COLUMNS=<NNN> |
| 7517 | * BASH_ARGC=() |
| 7518 | * BASH_ARGV=() |
| 7519 | * BASH_LINENO=() |
| 7520 | * BASH_SOURCE=() |
| 7521 | * DIRSTACK=() |
| 7522 | * PIPESTATUS=([0]="0") |
| 7523 | * HISTFILE=/<xxx>/.bash_history |
| 7524 | * HISTFILESIZE=500 |
| 7525 | * HISTSIZE=500 |
| 7526 | * MAILCHECK=60 |
| 7527 | * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:. |
| 7528 | * SHELL=/bin/bash |
| 7529 | * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
| 7530 | * TERM=dumb |
| 7531 | * OPTERR=1 |
| 7532 | * OPTIND=1 |
| 7533 | * IFS=$' \t\n' |
| 7534 | * PS1='\s-\v\$ ' |
| 7535 | * PS2='> ' |
| 7536 | * PS4='+ ' |
| 7537 | */ |
| 7538 | |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7539 | #if ENABLE_FEATURE_EDITING |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7540 | G.line_input_state = new_line_input_t(FOR_SHELL); |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 7541 | # if defined MAX_HISTORY && MAX_HISTORY > 0 && ENABLE_HUSH_SAVEHISTORY |
| 7542 | { |
| 7543 | const char *hp = get_local_var_value("HISTFILE"); |
| 7544 | if (!hp) { |
| 7545 | hp = get_local_var_value("HOME"); |
| 7546 | if (hp) { |
| 7547 | G.line_input_state->hist_file = concat_path_file(hp, ".hush_history"); |
| 7548 | //set_local_var(xasprintf("HISTFILE=%s", ...)); |
| 7549 | } |
| 7550 | } |
| 7551 | } |
| 7552 | # endif |
Denis Vlasenko | 8e1c715 | 2007-01-22 07:21:38 +0000 | [diff] [blame] | 7553 | #endif |
Denys Vlasenko | 99862cb | 2010-09-12 17:34:13 +0200 | [diff] [blame] | 7554 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7555 | G.global_argc = argc; |
| 7556 | G.global_argv = argv; |
Eric Andersen | 94ac244 | 2001-05-22 19:05:18 +0000 | [diff] [blame] | 7557 | /* Initialize some more globals to non-zero values */ |
Mike Frysinger | 67c1c7b | 2009-04-24 06:26:18 +0000 | [diff] [blame] | 7558 | cmdedit_update_prompt(); |
Denis Vlasenko | c8be5ee | 2007-05-17 15:38:46 +0000 | [diff] [blame] | 7559 | |
Denis Vlasenko | ed78237 | 2009-04-10 00:45:02 +0000 | [diff] [blame] | 7560 | if (setjmp(die_jmp)) { |
| 7561 | /* xfunc has failed! die die die */ |
| 7562 | /* no EXIT traps, this is an escape hatch! */ |
| 7563 | G.exiting = 1; |
| 7564 | hush_exit(xfunc_error_retval); |
| 7565 | } |
| 7566 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7567 | /* Shell is non-interactive at first. We need to call |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7568 | * init_sigmasks() if we are going to execute "sh <script>", |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7569 | * "sh -c <cmds>" or login shell's /etc/profile and friends. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7570 | * If we later decide that we are interactive, we run init_sigmasks() |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7571 | * in order to intercept (more) signals. |
| 7572 | */ |
| 7573 | |
| 7574 | /* Parse options */ |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7575 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7576 | builtin_argc = 0; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7577 | while (1) { |
Denys Vlasenko | a67a962 | 2009-08-20 03:38:58 +0200 | [diff] [blame] | 7578 | opt = getopt(argc, argv, "+c:xins" |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7579 | #if !BB_MMU |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7580 | "<:$:R:V:" |
| 7581 | # if ENABLE_HUSH_FUNCTIONS |
| 7582 | "F:" |
| 7583 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7584 | #endif |
| 7585 | ); |
| 7586 | if (opt <= 0) |
| 7587 | break; |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7588 | switch (opt) { |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7589 | case 'c': |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7590 | /* Possibilities: |
| 7591 | * sh ... -c 'script' |
| 7592 | * sh ... -c 'script' ARG0 [ARG1...] |
| 7593 | * On NOMMU, if builtin_argc != 0, |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7594 | * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...] |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7595 | * "" needs to be replaced with NULL |
| 7596 | * and BARGV vector fed to builtin function. |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7597 | * Note: the form without ARG0 never happens: |
| 7598 | * sh ... -c 'builtin' BARGV... "" |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7599 | */ |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7600 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7601 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7602 | G.root_ppid = getppid(); |
| 7603 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7604 | G.global_argv = argv + optind; |
| 7605 | G.global_argc = argc - optind; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7606 | if (builtin_argc) { |
| 7607 | /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */ |
| 7608 | const struct built_in_command *x; |
| 7609 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7610 | init_sigmasks(); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7611 | x = find_builtin(optarg); |
| 7612 | if (x) { /* paranoia */ |
| 7613 | G.global_argc -= builtin_argc; /* skip [BARGV...] "" */ |
| 7614 | G.global_argv += builtin_argc; |
| 7615 | G.global_argv[-1] = NULL; /* replace "" */ |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 7616 | G.last_exitcode = x->b_function(argv + optind - 1); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7617 | } |
| 7618 | goto final_return; |
| 7619 | } |
| 7620 | if (!G.global_argv[0]) { |
| 7621 | /* -c 'script' (no params): prevent empty $0 */ |
| 7622 | G.global_argv--; /* points to argv[i] of 'script' */ |
| 7623 | G.global_argv[0] = argv[0]; |
Denys Vlasenko | 5ae8f1c | 2010-05-22 06:32:11 +0200 | [diff] [blame] | 7624 | G.global_argc++; |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7625 | } /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7626 | init_sigmasks(); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7627 | parse_and_run_string(optarg); |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7628 | goto final_return; |
| 7629 | case 'i': |
Denis Vlasenko | c666f71 | 2007-05-16 22:18:54 +0000 | [diff] [blame] | 7630 | /* Well, we cannot just declare interactiveness, |
| 7631 | * we have to have some stuff (ctty, etc) */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7632 | /* G_interactive_fd++; */ |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7633 | break; |
Mike Frysinger | 19a7ea1 | 2009-03-28 13:02:11 +0000 | [diff] [blame] | 7634 | case 's': |
| 7635 | /* "-s" means "read from stdin", but this is how we always |
| 7636 | * operate, so simply do nothing here. */ |
| 7637 | break; |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7638 | #if !BB_MMU |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7639 | case '<': /* "big heredoc" support */ |
Denys Vlasenko | 729ecb8 | 2010-06-07 14:14:26 +0200 | [diff] [blame] | 7640 | full_write1_str(optarg); |
Denis Vlasenko | 50f3aa4 | 2009-04-07 10:52:40 +0000 | [diff] [blame] | 7641 | _exit(0); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7642 | case '$': { |
| 7643 | unsigned long long empty_trap_mask; |
| 7644 | |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7645 | G.root_pid = bb_strtou(optarg, &optarg, 16); |
| 7646 | optarg++; |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7647 | G.root_ppid = bb_strtou(optarg, &optarg, 16); |
| 7648 | optarg++; |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7649 | G.last_bg_pid = bb_strtou(optarg, &optarg, 16); |
| 7650 | optarg++; |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7651 | G.last_exitcode = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 7652 | optarg++; |
| 7653 | builtin_argc = bb_strtou(optarg, &optarg, 16); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7654 | optarg++; |
| 7655 | empty_trap_mask = bb_strtoull(optarg, &optarg, 16); |
| 7656 | if (empty_trap_mask != 0) { |
| 7657 | int sig; |
| 7658 | init_sigmasks(); |
| 7659 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 7660 | for (sig = 1; sig < NSIG; sig++) { |
| 7661 | if (empty_trap_mask & (1LL << sig)) { |
| 7662 | G.traps[sig] = xzalloc(1); /* == xstrdup(""); */ |
| 7663 | sigaddset(&G.blocked_set, sig); |
| 7664 | } |
| 7665 | } |
| 7666 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 7667 | } |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7668 | # if ENABLE_HUSH_LOOPS |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7669 | optarg++; |
| 7670 | G.depth_of_loop = bb_strtou(optarg, &optarg, 16); |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7671 | # endif |
Denis Vlasenko | 34e573d | 2009-04-06 12:56:28 +0000 | [diff] [blame] | 7672 | break; |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7673 | } |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7674 | case 'R': |
| 7675 | case 'V': |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 7676 | set_local_var(xstrdup(optarg), /*exp:*/ 0, /*lvl:*/ 0, /*ro:*/ opt == 'R'); |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7677 | break; |
Denis Vlasenko | bc56974 | 2009-04-12 20:35:19 +0000 | [diff] [blame] | 7678 | # if ENABLE_HUSH_FUNCTIONS |
| 7679 | case 'F': { |
| 7680 | struct function *funcp = new_function(optarg); |
| 7681 | /* funcp->name is already set to optarg */ |
| 7682 | /* funcp->body is set to NULL. It's a special case. */ |
| 7683 | funcp->body_as_string = argv[optind]; |
| 7684 | optind++; |
| 7685 | break; |
| 7686 | } |
| 7687 | # endif |
Denis Vlasenko | 0bb4a23 | 2009-04-05 01:42:59 +0000 | [diff] [blame] | 7688 | #endif |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7689 | case 'n': |
| 7690 | case 'x': |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 7691 | if (set_mode(1, opt, NULL) == 0) /* no error */ |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 7692 | break; |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7693 | default: |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7694 | #ifndef BB_VER |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7695 | fprintf(stderr, "Usage: sh [FILE]...\n" |
| 7696 | " or: sh -c command [args]...\n\n"); |
| 7697 | exit(EXIT_FAILURE); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7698 | #else |
Denis Vlasenko | fbf6dea | 2007-04-13 19:56:56 +0000 | [diff] [blame] | 7699 | bb_show_usage(); |
Eric Andersen | 9ffb7dd | 2001-05-19 03:00:46 +0000 | [diff] [blame] | 7700 | #endif |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7701 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7702 | } /* option parsing loop */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7703 | |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7704 | if (!G.root_pid) { |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7705 | G.root_pid = getpid(); |
Denys Vlasenko | dea4788 | 2009-10-09 15:40:49 +0200 | [diff] [blame] | 7706 | G.root_ppid = getppid(); |
| 7707 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7708 | |
| 7709 | /* If we are login shell... */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7710 | if (argv[0] && argv[0][0] == '-') { |
| 7711 | FILE *input; |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7712 | debug_printf("sourcing /etc/profile\n"); |
| 7713 | input = fopen_for_read("/etc/profile"); |
| 7714 | if (input != NULL) { |
| 7715 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7716 | init_sigmasks(); |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7717 | parse_and_run_file(input); |
| 7718 | fclose(input); |
| 7719 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7720 | /* bash: after sourcing /etc/profile, |
| 7721 | * tries to source (in the given order): |
| 7722 | * ~/.bash_profile, ~/.bash_login, ~/.profile, |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7723 | * stopping on first found. --noprofile turns this off. |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7724 | * bash also sources ~/.bash_logout on exit. |
| 7725 | * If called as sh, skips .bash_XXX files. |
| 7726 | */ |
Denis Vlasenko | 46f9b6d | 2009-04-05 10:39:03 +0000 | [diff] [blame] | 7727 | } |
| 7728 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7729 | if (argv[optind]) { |
| 7730 | FILE *input; |
| 7731 | /* |
Denis Vlasenko | d3f973e | 2009-04-06 10:21:42 +0000 | [diff] [blame] | 7732 | * "bash <script>" (which is never interactive (unless -i?)) |
| 7733 | * sources $BASH_ENV here (without scanning $PATH). |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7734 | * If called as sh, does the same but with $ENV. |
| 7735 | */ |
| 7736 | debug_printf("running script '%s'\n", argv[optind]); |
| 7737 | G.global_argv = argv + optind; |
| 7738 | G.global_argc = argc - optind; |
| 7739 | input = xfopen_for_read(argv[optind]); |
| 7740 | close_on_exec_on(fileno(input)); |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7741 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7742 | parse_and_run_file(input); |
| 7743 | #if ENABLE_FEATURE_CLEAN_UP |
| 7744 | fclose(input); |
| 7745 | #endif |
| 7746 | goto final_return; |
| 7747 | } |
| 7748 | |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7749 | /* Up to here, shell was non-interactive. Now it may become one. |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7750 | * NB: don't forget to (re)run init_sigmasks() as needed. |
Denis Vlasenko | c4a7af5 | 2009-04-05 20:33:27 +0000 | [diff] [blame] | 7751 | */ |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7752 | |
Denys Vlasenko | 28a105d | 2009-06-01 11:26:30 +0200 | [diff] [blame] | 7753 | /* A shell is interactive if the '-i' flag was given, |
| 7754 | * or if all of the following conditions are met: |
Denis Vlasenko | 55b2de7 | 2007-04-18 17:21:28 +0000 | [diff] [blame] | 7755 | * no -c command |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7756 | * no arguments remaining or the -s flag given |
| 7757 | * standard input is a terminal |
| 7758 | * standard output is a terminal |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7759 | * Refer to Posix.2, the description of the 'sh' utility. |
| 7760 | */ |
| 7761 | #if ENABLE_HUSH_JOB |
| 7762 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7763 | G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO); |
| 7764 | debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp); |
| 7765 | if (G_saved_tty_pgrp < 0) |
| 7766 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7767 | |
| 7768 | /* try to dup stdin to high fd#, >= 255 */ |
| 7769 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7770 | if (G_interactive_fd < 0) { |
| 7771 | /* try to dup to any fd */ |
| 7772 | G_interactive_fd = dup(STDIN_FILENO); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7773 | if (G_interactive_fd < 0) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7774 | /* give up */ |
| 7775 | G_interactive_fd = 0; |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7776 | G_saved_tty_pgrp = 0; |
Denis Vlasenko | 54e7ffb | 2007-04-21 00:03:36 +0000 | [diff] [blame] | 7777 | } |
| 7778 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7779 | // TODO: track & disallow any attempts of user |
| 7780 | // to (inadvertently) close/redirect G_interactive_fd |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7781 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7782 | debug_printf("interactive_fd:%d\n", G_interactive_fd); |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7783 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7784 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7785 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7786 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7787 | /* If we were run as 'hush &', sleep until we are |
| 7788 | * in the foreground (tty pgrp == our pgrp). |
| 7789 | * If we get started under a job aware app (like bash), |
| 7790 | * make sure we are now in charge so we don't fight over |
| 7791 | * who gets the foreground */ |
| 7792 | while (1) { |
| 7793 | pid_t shell_pgrp = getpgrp(); |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7794 | G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd); |
| 7795 | if (G_saved_tty_pgrp == shell_pgrp) |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7796 | break; |
| 7797 | /* send TTIN to ourself (should stop us) */ |
| 7798 | kill(- shell_pgrp, SIGTTIN); |
| 7799 | } |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7800 | } |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7801 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7802 | /* Block some signals */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7803 | init_sigmasks(); |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7804 | |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 7805 | if (G_saved_tty_pgrp) { |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 7806 | /* Set other signals to restore saved_tty_pgrp */ |
| 7807 | set_fatal_handlers(); |
| 7808 | /* Put ourselves in our own process group |
| 7809 | * (bash, too, does this only if ctty is available) */ |
| 7810 | bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */ |
| 7811 | /* Grab control of the terminal */ |
| 7812 | tcsetpgrp(G_interactive_fd, getpid()); |
| 7813 | } |
Denis Vlasenko | 4ecfcdc | 2008-02-11 08:32:31 +0000 | [diff] [blame] | 7814 | /* -1 is special - makes xfuncs longjmp, not exit |
Denis Vlasenko | c04163a | 2008-02-11 08:30:53 +0000 | [diff] [blame] | 7815 | * (we reset die_sleep = 0 whereever we [v]fork) */ |
Denis Vlasenko | af07b7c | 2009-04-07 13:26:18 +0000 | [diff] [blame] | 7816 | enable_restore_tty_pgrp_on_exit(); /* sets die_sleep = -1 */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7817 | } else { |
| 7818 | init_sigmasks(); |
| 7819 | } |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7820 | #elif ENABLE_HUSH_INTERACTIVE |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7821 | /* No job control compiled in, only prompt/line editing */ |
| 7822 | if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) { |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7823 | G_interactive_fd = fcntl(STDIN_FILENO, F_DUPFD, 255); |
| 7824 | if (G_interactive_fd < 0) { |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7825 | /* try to dup to any fd */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7826 | G_interactive_fd = dup(STDIN_FILENO); |
| 7827 | if (G_interactive_fd < 0) |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7828 | /* give up */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7829 | G_interactive_fd = 0; |
Denis Vlasenko | e3f2f89 | 2007-04-28 16:48:27 +0000 | [diff] [blame] | 7830 | } |
| 7831 | } |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 7832 | if (G_interactive_fd) { |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7833 | close_on_exec_on(G_interactive_fd); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7834 | } |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7835 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7836 | #else |
| 7837 | /* We have interactiveness code disabled */ |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 7838 | init_sigmasks(); |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7839 | #endif |
| 7840 | /* bash: |
| 7841 | * if interactive but not a login shell, sources ~/.bashrc |
| 7842 | * (--norc turns this off, --rcfile <file> overrides) |
| 7843 | */ |
| 7844 | |
| 7845 | if (!ENABLE_FEATURE_SH_EXTRA_QUIET && G_interactive_fd) { |
Denys Vlasenko | c34c033 | 2009-09-29 12:25:30 +0200 | [diff] [blame] | 7846 | /* note: ash and hush share this string */ |
| 7847 | printf("\n\n%s %s\n" |
| 7848 | IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n") |
| 7849 | "\n", |
| 7850 | bb_banner, |
| 7851 | "hush - the humble shell" |
| 7852 | ); |
Mike Frysinger | b2705e1 | 2009-03-23 08:44:02 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
Denis Vlasenko | f937528 | 2009-04-05 19:13:39 +0000 | [diff] [blame] | 7855 | parse_and_run_file(stdin); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7856 | |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7857 | final_return: |
Denis Vlasenko | 38f6319 | 2007-01-22 09:03:07 +0000 | [diff] [blame] | 7858 | #if ENABLE_FEATURE_CLEAN_UP |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 7859 | if (G.cwd != bb_msg_unknown) |
| 7860 | free((char*)G.cwd); |
| 7861 | cur_var = G.top_var->next; |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7862 | while (cur_var) { |
| 7863 | struct variable *tmp = cur_var; |
| 7864 | if (!cur_var->max_len) |
Denis Vlasenko | 28c0f0f | 2007-05-25 02:46:01 +0000 | [diff] [blame] | 7865 | free(cur_var->varstr); |
Denis Vlasenko | d76c049 | 2007-05-25 02:16:25 +0000 | [diff] [blame] | 7866 | cur_var = cur_var->next; |
| 7867 | free(tmp); |
Eric Andersen | aeb44c4 | 2001-05-22 20:29:00 +0000 | [diff] [blame] | 7868 | } |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7869 | #endif |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7870 | hush_exit(G.last_exitcode); |
Eric Andersen | 25f2703 | 2001-04-26 23:22:31 +0000 | [diff] [blame] | 7871 | } |
Denis Vlasenko | 96702ca | 2007-11-23 23:28:55 +0000 | [diff] [blame] | 7872 | |
| 7873 | |
Denys Vlasenko | 1cc4b13 | 2009-08-21 00:05:51 +0200 | [diff] [blame] | 7874 | #if ENABLE_MSH |
| 7875 | int msh_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 7876 | int msh_main(int argc, char **argv) |
| 7877 | { |
| 7878 | //bb_error_msg("msh is deprecated, please use hush instead"); |
| 7879 | return hush_main(argc, argv); |
| 7880 | } |
| 7881 | #endif |
| 7882 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7883 | |
| 7884 | /* |
| 7885 | * Built-ins |
| 7886 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7887 | static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7888 | { |
| 7889 | return 0; |
| 7890 | } |
| 7891 | |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7892 | 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] | 7893 | { |
| 7894 | int argc = 0; |
| 7895 | while (*argv) { |
| 7896 | argc++; |
| 7897 | argv++; |
| 7898 | } |
Denys Vlasenko | 8bc7f2c | 2009-10-19 13:20:52 +0200 | [diff] [blame] | 7899 | return applet_main_func(argc, argv - argc); |
Mike Frysinger | ccb1959 | 2009-10-15 03:31:15 -0400 | [diff] [blame] | 7900 | } |
| 7901 | |
| 7902 | static int FAST_FUNC builtin_test(char **argv) |
| 7903 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7904 | return run_applet_main(argv, test_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7907 | static int FAST_FUNC builtin_echo(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7908 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7909 | return run_applet_main(argv, echo_main); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7910 | } |
| 7911 | |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7912 | #if ENABLE_PRINTF |
| 7913 | static int FAST_FUNC builtin_printf(char **argv) |
| 7914 | { |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 7915 | return run_applet_main(argv, printf_main); |
Mike Frysinger | 4ebc76c | 2009-10-15 03:32:39 -0400 | [diff] [blame] | 7916 | } |
| 7917 | #endif |
| 7918 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7919 | static char **skip_dash_dash(char **argv) |
| 7920 | { |
| 7921 | argv++; |
| 7922 | if (argv[0] && argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == '\0') |
| 7923 | argv++; |
| 7924 | return argv; |
| 7925 | } |
| 7926 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7927 | static int FAST_FUNC builtin_eval(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7928 | { |
| 7929 | int rcode = EXIT_SUCCESS; |
| 7930 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7931 | argv = skip_dash_dash(argv); |
| 7932 | if (*argv) { |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7933 | char *str = expand_strvec_to_string(argv); |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7934 | /* bash: |
| 7935 | * eval "echo Hi; done" ("done" is syntax error): |
| 7936 | * "echo Hi" will not execute too. |
| 7937 | */ |
| 7938 | parse_and_run_string(str); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7939 | free(str); |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 7940 | rcode = G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7941 | } |
| 7942 | return rcode; |
| 7943 | } |
| 7944 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7945 | static int FAST_FUNC builtin_cd(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7946 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7947 | const char *newdir; |
| 7948 | |
| 7949 | argv = skip_dash_dash(argv); |
| 7950 | newdir = argv[0]; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7951 | if (newdir == NULL) { |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7952 | /* bash does nothing (exitcode 0) if HOME is ""; if it's unset, |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 7953 | * bash says "bash: cd: HOME not set" and does nothing |
| 7954 | * (exitcode 1) |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 7955 | */ |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 7956 | const char *home = get_local_var_value("HOME"); |
| 7957 | newdir = home ? home : "/"; |
Denis Vlasenko | b0a6478 | 2009-04-06 11:33:07 +0000 | [diff] [blame] | 7958 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7959 | if (chdir(newdir)) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 7960 | /* Mimic bash message exactly */ |
| 7961 | bb_perror_msg("cd: %s", newdir); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7962 | return EXIT_FAILURE; |
| 7963 | } |
Denys Vlasenko | 6db4784 | 2009-09-05 20:15:17 +0200 | [diff] [blame] | 7964 | /* Read current dir (get_cwd(1) is inside) and set PWD. |
| 7965 | * Note: do not enforce exporting. If PWD was unset or unexported, |
| 7966 | * set it again, but do not export. bash does the same. |
| 7967 | */ |
| 7968 | set_pwd_var(/*exp:*/ 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7969 | return EXIT_SUCCESS; |
| 7970 | } |
| 7971 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7972 | static int FAST_FUNC builtin_exec(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7973 | { |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 7974 | argv = skip_dash_dash(argv); |
| 7975 | if (argv[0] == NULL) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7976 | return EXIT_SUCCESS; /* bash does this */ |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7977 | |
Denys Vlasenko | f37eb39 | 2009-10-18 11:46:35 +0200 | [diff] [blame] | 7978 | /* Careful: we can end up here after [v]fork. Do not restore |
| 7979 | * tty pgrp then, only top-level shell process does that */ |
| 7980 | if (G_saved_tty_pgrp && getpid() == G.root_pid) |
| 7981 | tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp); |
| 7982 | |
Denys Vlasenko | 3ef4f77 | 2009-10-19 23:09:06 +0200 | [diff] [blame] | 7983 | /* TODO: if exec fails, bash does NOT exit! We do. |
| 7984 | * We'll need to undo sigprocmask (it's inside execvp_or_die) |
| 7985 | * and tcsetpgrp, and this is inherently racy. |
| 7986 | */ |
| 7987 | execvp_or_die(argv); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7988 | } |
| 7989 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 7990 | static int FAST_FUNC builtin_exit(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 7991 | { |
Denis Vlasenko | cd418a2 | 2009-04-06 18:08:35 +0000 | [diff] [blame] | 7992 | debug_printf_exec("%s()\n", __func__); |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 7993 | |
| 7994 | /* interactive bash: |
| 7995 | * # trap "echo EEE" EXIT |
| 7996 | * # exit |
| 7997 | * exit |
| 7998 | * There are stopped jobs. |
| 7999 | * (if there are _stopped_ jobs, running ones don't count) |
| 8000 | * # exit |
| 8001 | * exit |
| 8002 | # EEE (then bash exits) |
| 8003 | * |
Denys Vlasenko | a110c90 | 2010-09-12 15:38:04 +0200 | [diff] [blame] | 8004 | * TODO: we can use G.exiting = -1 as indicator "last cmd was exit" |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8005 | */ |
Denis Vlasenko | efea9d2 | 2009-04-09 13:43:11 +0000 | [diff] [blame] | 8006 | |
| 8007 | /* note: EXIT trap is run by hush_exit */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8008 | argv = skip_dash_dash(argv); |
| 8009 | if (argv[0] == NULL) |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8010 | hush_exit(G.last_exitcode); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8011 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
| 8012 | xfunc_error_retval = 255; |
| 8013 | /* bash: exit -2 == exit 254, no error msg */ |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8014 | hush_exit(xatoi(argv[0]) & 0xff); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8015 | } |
| 8016 | |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8017 | static void print_escaped(const char *s) |
| 8018 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8019 | if (*s == '\'') |
| 8020 | goto squote; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8021 | do { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8022 | const char *p = strchrnul(s, '\''); |
| 8023 | /* print 'xxxx', possibly just '' */ |
| 8024 | printf("'%.*s'", (int)(p - s), s); |
| 8025 | if (*p == '\0') |
| 8026 | break; |
| 8027 | s = p; |
| 8028 | squote: |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8029 | /* s points to '; print "'''...'''" */ |
| 8030 | putchar('"'); |
| 8031 | do putchar('\''); while (*++s == '\''); |
| 8032 | putchar('"'); |
| 8033 | } while (*s); |
| 8034 | } |
| 8035 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8036 | #if !ENABLE_HUSH_LOCAL |
| 8037 | #define helper_export_local(argv, exp, lvl) \ |
| 8038 | helper_export_local(argv, exp) |
| 8039 | #endif |
| 8040 | static void helper_export_local(char **argv, int exp, int lvl) |
| 8041 | { |
| 8042 | do { |
| 8043 | char *name = *argv; |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8044 | char *name_end = strchrnul(name, '='); |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8045 | |
| 8046 | /* So far we do not check that name is valid (TODO?) */ |
| 8047 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8048 | if (*name_end == '\0') { |
| 8049 | struct variable *var, **vpp; |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8050 | |
Denys Vlasenko | 27c56f1 | 2010-09-07 09:56:34 +0200 | [diff] [blame] | 8051 | vpp = get_ptr_to_local_var(name, name_end - name); |
| 8052 | var = vpp ? *vpp : NULL; |
| 8053 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8054 | if (exp == -1) { /* unexporting? */ |
| 8055 | /* export -n NAME (without =VALUE) */ |
| 8056 | if (var) { |
| 8057 | var->flg_export = 0; |
| 8058 | debug_printf_env("%s: unsetenv '%s'\n", __func__, name); |
| 8059 | unsetenv(name); |
| 8060 | } /* else: export -n NOT_EXISTING_VAR: no-op */ |
| 8061 | continue; |
| 8062 | } |
| 8063 | if (exp == 1) { /* exporting? */ |
| 8064 | /* export NAME (without =VALUE) */ |
| 8065 | if (var) { |
| 8066 | var->flg_export = 1; |
| 8067 | debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr); |
| 8068 | putenv(var->varstr); |
| 8069 | continue; |
| 8070 | } |
| 8071 | } |
| 8072 | /* Exporting non-existing variable. |
| 8073 | * bash does not put it in environment, |
| 8074 | * but remembers that it is exported, |
| 8075 | * and does put it in env when it is set later. |
| 8076 | * We just set it to "" and export. */ |
| 8077 | /* Or, it's "local NAME" (without =VALUE). |
| 8078 | * bash sets the value to "". */ |
| 8079 | name = xasprintf("%s=", name); |
| 8080 | } else { |
| 8081 | /* (Un)exporting/making local NAME=VALUE */ |
| 8082 | name = xstrdup(name); |
| 8083 | } |
| 8084 | set_local_var(name, /*exp:*/ exp, /*lvl:*/ lvl, /*ro:*/ 0); |
| 8085 | } while (*++argv); |
| 8086 | } |
| 8087 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8088 | static int FAST_FUNC builtin_export(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8089 | { |
Denis Vlasenko | ad4bd05 | 2009-04-20 22:04:21 +0000 | [diff] [blame] | 8090 | unsigned opt_unexport; |
| 8091 | |
Denys Vlasenko | df5131c | 2009-06-07 16:04:17 +0200 | [diff] [blame] | 8092 | #if ENABLE_HUSH_EXPORT_N |
| 8093 | /* "!": do not abort on errors */ |
| 8094 | opt_unexport = getopt32(argv, "!n"); |
| 8095 | if (opt_unexport == (uint32_t)-1) |
| 8096 | return EXIT_FAILURE; |
| 8097 | argv += optind; |
| 8098 | #else |
| 8099 | opt_unexport = 0; |
| 8100 | argv++; |
| 8101 | #endif |
| 8102 | |
| 8103 | if (argv[0] == NULL) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8104 | char **e = environ; |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8105 | if (e) { |
| 8106 | while (*e) { |
| 8107 | #if 0 |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8108 | puts(*e++); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8109 | #else |
| 8110 | /* ash emits: export VAR='VAL' |
| 8111 | * bash: declare -x VAR="VAL" |
| 8112 | * we follow ash example */ |
| 8113 | const char *s = *e++; |
| 8114 | const char *p = strchr(s, '='); |
| 8115 | |
| 8116 | if (!p) /* wtf? take next variable */ |
| 8117 | continue; |
| 8118 | /* export var= */ |
| 8119 | printf("export %.*s", (int)(p - s) + 1, s); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8120 | print_escaped(p + 1); |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8121 | putchar('\n'); |
| 8122 | #endif |
| 8123 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8124 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 0b677d8 | 2009-04-10 13:49:10 +0000 | [diff] [blame] | 8125 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8126 | return EXIT_SUCCESS; |
| 8127 | } |
| 8128 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8129 | helper_export_local(argv, (opt_unexport ? -1 : 1), 0); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8130 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8131 | return EXIT_SUCCESS; |
| 8132 | } |
| 8133 | |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8134 | #if ENABLE_HUSH_LOCAL |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8135 | static int FAST_FUNC builtin_local(char **argv) |
Denys Vlasenko | 295fef8 | 2009-06-03 12:47:26 +0200 | [diff] [blame] | 8136 | { |
| 8137 | if (G.func_nest_level == 0) { |
| 8138 | bb_error_msg("%s: not in a function", argv[0]); |
| 8139 | return EXIT_FAILURE; /* bash compat */ |
| 8140 | } |
| 8141 | helper_export_local(argv, 0, G.func_nest_level); |
| 8142 | return EXIT_SUCCESS; |
| 8143 | } |
| 8144 | #endif |
| 8145 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8146 | static int FAST_FUNC builtin_trap(char **argv) |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8147 | { |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8148 | int sig; |
| 8149 | char *new_cmd; |
| 8150 | |
| 8151 | if (!G.traps) |
| 8152 | G.traps = xzalloc(sizeof(G.traps[0]) * NSIG); |
| 8153 | |
| 8154 | argv++; |
| 8155 | if (!*argv) { |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8156 | int i; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8157 | /* No args: print all trapped */ |
| 8158 | for (i = 0; i < NSIG; ++i) { |
| 8159 | if (G.traps[i]) { |
| 8160 | printf("trap -- "); |
| 8161 | print_escaped(G.traps[i]); |
Denys Vlasenko | e74aaf9 | 2009-09-27 02:05:45 +0200 | [diff] [blame] | 8162 | /* note: bash adds "SIG", but only if invoked |
| 8163 | * as "bash". If called as "sh", or if set -o posix, |
| 8164 | * then it prints short signal names. |
| 8165 | * We are printing short names: */ |
| 8166 | printf(" %s\n", get_signame(i)); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8167 | } |
| 8168 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 8169 | /*fflush_all(); - done after each builtin anyway */ |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8170 | return EXIT_SUCCESS; |
| 8171 | } |
| 8172 | |
| 8173 | new_cmd = NULL; |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8174 | /* If first arg is a number: reset all specified signals */ |
| 8175 | sig = bb_strtou(*argv, NULL, 10); |
| 8176 | if (errno == 0) { |
| 8177 | int ret; |
| 8178 | process_sig_list: |
| 8179 | ret = EXIT_SUCCESS; |
| 8180 | while (*argv) { |
| 8181 | sig = get_signum(*argv++); |
| 8182 | if (sig < 0 || sig >= NSIG) { |
| 8183 | ret = EXIT_FAILURE; |
| 8184 | /* Mimic bash message exactly */ |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8185 | bb_perror_msg("trap: %s: invalid signal specification", argv[-1]); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8186 | continue; |
| 8187 | } |
| 8188 | |
| 8189 | free(G.traps[sig]); |
| 8190 | G.traps[sig] = xstrdup(new_cmd); |
| 8191 | |
Denys Vlasenko | e89a241 | 2010-01-12 15:19:31 +0100 | [diff] [blame] | 8192 | debug_printf("trap: setting SIG%s (%i) to '%s'\n", |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8193 | get_signame(sig), sig, G.traps[sig]); |
| 8194 | |
| 8195 | /* There is no signal for 0 (EXIT) */ |
| 8196 | if (sig == 0) |
| 8197 | continue; |
| 8198 | |
| 8199 | if (new_cmd) { |
| 8200 | sigaddset(&G.blocked_set, sig); |
| 8201 | } else { |
| 8202 | /* There was a trap handler, we are removing it |
| 8203 | * (if sig has non-DFL handling, |
| 8204 | * we don't need to do anything) */ |
| 8205 | if (sig < 32 && (G.non_DFL_mask & (1 << sig))) |
| 8206 | continue; |
| 8207 | sigdelset(&G.blocked_set, sig); |
| 8208 | } |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8209 | } |
Denis Vlasenko | 6008d8a | 2009-04-18 13:05:10 +0000 | [diff] [blame] | 8210 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
Denis Vlasenko | 38e626d | 2009-04-18 12:58:19 +0000 | [diff] [blame] | 8211 | return ret; |
| 8212 | } |
| 8213 | |
| 8214 | if (!argv[1]) { /* no second arg */ |
| 8215 | bb_error_msg("trap: invalid arguments"); |
| 8216 | return EXIT_FAILURE; |
| 8217 | } |
| 8218 | |
| 8219 | /* First arg is "-": reset all specified to default */ |
| 8220 | /* First arg is "--": skip it, the rest is "handler SIGs..." */ |
| 8221 | /* Everything else: set arg as signal handler |
| 8222 | * (includes "" case, which ignores signal) */ |
| 8223 | if (argv[0][0] == '-') { |
| 8224 | if (argv[0][1] == '\0') { /* "-" */ |
| 8225 | /* new_cmd remains NULL: "reset these sigs" */ |
| 8226 | goto reset_traps; |
| 8227 | } |
| 8228 | if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */ |
| 8229 | argv++; |
| 8230 | } |
| 8231 | /* else: "-something", no special meaning */ |
| 8232 | } |
| 8233 | new_cmd = *argv; |
| 8234 | reset_traps: |
| 8235 | argv++; |
| 8236 | goto process_sig_list; |
| 8237 | } |
| 8238 | |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8239 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8240 | static int FAST_FUNC builtin_type(char **argv) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8241 | { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8242 | int ret = EXIT_SUCCESS; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8243 | |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8244 | while (*++argv) { |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8245 | const char *type; |
Denys Vlasenko | 171932d | 2009-05-28 17:07:22 +0200 | [diff] [blame] | 8246 | char *path = NULL; |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8247 | |
| 8248 | if (0) {} /* make conditional compile easier below */ |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8249 | /*else if (find_alias(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8250 | type = "an alias";*/ |
| 8251 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8252 | else if (find_function(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8253 | type = "a function"; |
| 8254 | #endif |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8255 | else if (find_builtin(*argv)) |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8256 | type = "a shell builtin"; |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8257 | else if ((path = find_in_path(*argv)) != NULL) |
| 8258 | type = path; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8259 | else { |
Denys Vlasenko | dd6b211 | 2009-05-28 09:45:50 +0200 | [diff] [blame] | 8260 | bb_error_msg("type: %s: not found", *argv); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8261 | ret = EXIT_FAILURE; |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8262 | continue; |
| 8263 | } |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8264 | |
Denys Vlasenko | 5d7cca2 | 2009-05-28 09:58:43 +0200 | [diff] [blame] | 8265 | printf("%s is %s\n", *argv, type); |
| 8266 | free(path); |
Mike Frysinger | 93cadc2 | 2009-05-27 17:06:25 -0400 | [diff] [blame] | 8267 | } |
| 8268 | |
| 8269 | return ret; |
| 8270 | } |
| 8271 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8272 | #if ENABLE_HUSH_JOB |
| 8273 | /* built-in 'fg' and 'bg' handler */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8274 | static int FAST_FUNC builtin_fg_bg(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8275 | { |
| 8276 | int i, jobnum; |
| 8277 | struct pipe *pi; |
| 8278 | |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8279 | if (!G_interactive_fd) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8280 | return EXIT_FAILURE; |
Denis Vlasenko | c8653f6 | 2009-04-27 23:29:14 +0000 | [diff] [blame] | 8281 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8282 | /* If they gave us no args, assume they want the last backgrounded task */ |
| 8283 | if (!argv[1]) { |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8284 | for (pi = G.job_list; pi; pi = pi->next) { |
| 8285 | if (pi->jobid == G.last_jobid) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8286 | goto found; |
| 8287 | } |
| 8288 | } |
| 8289 | bb_error_msg("%s: no current job", argv[0]); |
| 8290 | return EXIT_FAILURE; |
| 8291 | } |
| 8292 | if (sscanf(argv[1], "%%%d", &jobnum) != 1) { |
| 8293 | bb_error_msg("%s: bad argument '%s'", argv[0], argv[1]); |
| 8294 | return EXIT_FAILURE; |
| 8295 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8296 | for (pi = G.job_list; pi; pi = pi->next) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8297 | if (pi->jobid == jobnum) { |
| 8298 | goto found; |
| 8299 | } |
| 8300 | } |
| 8301 | bb_error_msg("%s: %d: no such job", argv[0], jobnum); |
| 8302 | return EXIT_FAILURE; |
| 8303 | found: |
Denis Vlasenko | 6b9e053 | 2009-04-18 01:23:21 +0000 | [diff] [blame] | 8304 | /* TODO: bash prints a string representation |
| 8305 | * of job being foregrounded (like "sleep 1 | cat") */ |
Mike Frysinger | 38478a6 | 2009-05-20 04:48:06 -0400 | [diff] [blame] | 8306 | if (argv[0][0] == 'f' && G_saved_tty_pgrp) { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8307 | /* Put the job into the foreground. */ |
Denis Vlasenko | 60b392f | 2009-04-03 19:14:32 +0000 | [diff] [blame] | 8308 | tcsetpgrp(G_interactive_fd, pi->pgrp); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8309 | } |
| 8310 | |
| 8311 | /* Restart the processes in the job */ |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8312 | debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp); |
| 8313 | for (i = 0; i < pi->num_cmds; i++) { |
| 8314 | debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid); |
| 8315 | pi->cmds[i].is_stopped = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8316 | } |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8317 | pi->stopped_cmds = 0; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8318 | |
| 8319 | i = kill(- pi->pgrp, SIGCONT); |
| 8320 | if (i < 0) { |
| 8321 | if (errno == ESRCH) { |
| 8322 | delete_finished_bg_job(pi); |
| 8323 | return EXIT_SUCCESS; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8324 | } |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8325 | bb_perror_msg("kill (SIGCONT)"); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8326 | } |
| 8327 | |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8328 | if (argv[0][0] == 'f') { |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8329 | remove_bg_job(pi); |
| 8330 | return checkjobs_and_fg_shell(pi); |
| 8331 | } |
| 8332 | return EXIT_SUCCESS; |
| 8333 | } |
| 8334 | #endif |
| 8335 | |
| 8336 | #if ENABLE_HUSH_HELP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8337 | static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8338 | { |
| 8339 | const struct built_in_command *x; |
| 8340 | |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8341 | printf( |
Denis Vlasenko | 34d4d89 | 2009-04-04 20:24:37 +0000 | [diff] [blame] | 8342 | "Built-in commands:\n" |
| 8343 | "------------------\n"); |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8344 | for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) { |
Denys Vlasenko | 17323a6 | 2010-01-28 01:57:05 +0100 | [diff] [blame] | 8345 | if (x->b_descr) |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8346 | printf("%-10s%s\n", x->b_cmd, x->b_descr); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8347 | } |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8348 | bb_putchar('\n'); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8349 | return EXIT_SUCCESS; |
| 8350 | } |
| 8351 | #endif |
| 8352 | |
| 8353 | #if ENABLE_HUSH_JOB |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8354 | static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8355 | { |
| 8356 | struct pipe *job; |
| 8357 | const char *status_string; |
| 8358 | |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8359 | for (job = G.job_list; job; job = job->next) { |
Denis Vlasenko | 9af22c7 | 2008-10-09 12:54:58 +0000 | [diff] [blame] | 8360 | if (job->alive_cmds == job->stopped_cmds) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8361 | status_string = "Stopped"; |
| 8362 | else |
| 8363 | status_string = "Running"; |
| 8364 | |
| 8365 | printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext); |
| 8366 | } |
| 8367 | return EXIT_SUCCESS; |
| 8368 | } |
| 8369 | #endif |
| 8370 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8371 | #if HUSH_DEBUG |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8372 | static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM) |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8373 | { |
| 8374 | void *p; |
| 8375 | unsigned long l; |
| 8376 | |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8377 | # ifdef M_TRIM_THRESHOLD |
Denys Vlasenko | 27726cb | 2009-09-12 14:48:33 +0200 | [diff] [blame] | 8378 | /* Optional. Reduces probability of false positives */ |
| 8379 | malloc_trim(0); |
Denys Vlasenko | c083653 | 2009-10-19 13:13:06 +0200 | [diff] [blame] | 8380 | # endif |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8381 | /* Crude attempt to find where "free memory" starts, |
| 8382 | * sans fragmentation. */ |
| 8383 | p = malloc(240); |
| 8384 | l = (unsigned long)p; |
| 8385 | free(p); |
| 8386 | p = malloc(3400); |
| 8387 | if (l < (unsigned long)p) l = (unsigned long)p; |
| 8388 | free(p); |
| 8389 | |
| 8390 | if (!G.memleak_value) |
| 8391 | G.memleak_value = l; |
Denys Vlasenko | 9038d6f | 2009-07-15 20:02:19 +0200 | [diff] [blame] | 8392 | |
Denis Vlasenko | c73b70c | 2009-04-08 11:48:57 +0000 | [diff] [blame] | 8393 | l -= G.memleak_value; |
| 8394 | if ((long)l < 0) |
| 8395 | l = 0; |
| 8396 | l /= 1024; |
| 8397 | if (l > 127) |
| 8398 | l = 127; |
| 8399 | |
| 8400 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ |
| 8401 | return l; |
| 8402 | } |
| 8403 | #endif |
| 8404 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8405 | static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8406 | { |
Denys Vlasenko | d6b05eb | 2009-06-06 20:59:55 +0200 | [diff] [blame] | 8407 | puts(get_cwd(0)); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8408 | return EXIT_SUCCESS; |
| 8409 | } |
| 8410 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8411 | static int FAST_FUNC builtin_read(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8412 | { |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8413 | const char *r; |
| 8414 | char *opt_n = NULL; |
| 8415 | char *opt_p = NULL; |
| 8416 | char *opt_t = NULL; |
| 8417 | char *opt_u = NULL; |
| 8418 | int read_flags; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8419 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8420 | /* "!": do not abort on errors. |
| 8421 | * Option string must start with "sr" to match BUILTIN_READ_xxx |
| 8422 | */ |
| 8423 | read_flags = getopt32(argv, "!srn:p:t:u:", &opt_n, &opt_p, &opt_t, &opt_u); |
| 8424 | if (read_flags == (uint32_t)-1) |
| 8425 | return EXIT_FAILURE; |
| 8426 | argv += optind; |
| 8427 | |
| 8428 | r = shell_builtin_read(set_local_var_from_halves, |
| 8429 | argv, |
| 8430 | get_local_var_value("IFS"), /* can be NULL */ |
| 8431 | read_flags, |
| 8432 | opt_n, |
| 8433 | opt_p, |
| 8434 | opt_t, |
| 8435 | opt_u |
| 8436 | ); |
| 8437 | |
| 8438 | if ((uintptr_t)r > 1) { |
| 8439 | bb_error_msg("%s", r); |
| 8440 | r = (char*)(uintptr_t)1; |
Denis Vlasenko | 05d3b7c | 2009-04-09 19:16:15 +0000 | [diff] [blame] | 8441 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8442 | |
Denys Vlasenko | 03dad22 | 2010-01-12 23:29:57 +0100 | [diff] [blame] | 8443 | return (uintptr_t)r; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8444 | } |
| 8445 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8446 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set |
| 8447 | * built-in 'set' handler |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8448 | * SUSv3 says: |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8449 | * set [-abCefhmnuvx] [-o option] [argument...] |
| 8450 | * set [+abCefhmnuvx] [+o option] [argument...] |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8451 | * set -- [argument...] |
| 8452 | * set -o |
| 8453 | * set +o |
| 8454 | * Implementations shall support the options in both their hyphen and |
| 8455 | * plus-sign forms. These options can also be specified as options to sh. |
| 8456 | * Examples: |
| 8457 | * Write out all variables and their values: set |
| 8458 | * Set $1, $2, and $3 and set "$#" to 3: set c a b |
| 8459 | * Turn on the -x and -v options: set -xv |
| 8460 | * Unset all positional parameters: set -- |
| 8461 | * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x" |
| 8462 | * Set the positional parameters to the expansion of x, even if x expands |
| 8463 | * with a leading '-' or '+': set -- $x |
| 8464 | * |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8465 | * 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] | 8466 | */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8467 | static int FAST_FUNC builtin_set(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8468 | { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8469 | int n; |
| 8470 | char **pp, **g_argv; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8471 | char *arg = *++argv; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8472 | |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8473 | if (arg == NULL) { |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8474 | struct variable *e; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8475 | for (e = G.top_var; e; e = e->next) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8476 | puts(e->varstr); |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8477 | return EXIT_SUCCESS; |
Denis Vlasenko | 11fb7cf | 2009-03-20 10:13:08 +0000 | [diff] [blame] | 8478 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8479 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8480 | do { |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8481 | if (strcmp(arg, "--") == 0) { |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8482 | ++argv; |
| 8483 | goto set_argv; |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8484 | } |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8485 | if (arg[0] != '+' && arg[0] != '-') |
| 8486 | break; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8487 | for (n = 1; arg[n]; ++n) { |
| 8488 | if (set_mode((arg[0] == '-'), arg[n], argv[1])) |
Denis Vlasenko | 6ba6f54 | 2009-04-10 21:57:50 +0000 | [diff] [blame] | 8489 | goto error; |
Denys Vlasenko | 6696eac | 2010-11-14 02:01:50 +0100 | [diff] [blame] | 8490 | if (arg[n] == 'o' && argv[1]) |
| 8491 | argv++; |
| 8492 | } |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8493 | } while ((arg = *++argv) != NULL); |
| 8494 | /* Now argv[0] is 1st argument */ |
| 8495 | |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8496 | if (arg == NULL) |
| 8497 | return EXIT_SUCCESS; |
| 8498 | set_argv: |
| 8499 | |
Denis Vlasenko | 424f79b | 2009-03-22 14:23:34 +0000 | [diff] [blame] | 8500 | /* NB: G.global_argv[0] ($0) is never freed/changed */ |
| 8501 | g_argv = G.global_argv; |
| 8502 | if (G.global_args_malloced) { |
| 8503 | pp = g_argv; |
| 8504 | while (*++pp) |
| 8505 | free(*pp); |
| 8506 | g_argv[1] = NULL; |
| 8507 | } else { |
| 8508 | G.global_args_malloced = 1; |
| 8509 | pp = xzalloc(sizeof(pp[0]) * 2); |
| 8510 | pp[0] = g_argv[0]; /* retain $0 */ |
| 8511 | g_argv = pp; |
| 8512 | } |
| 8513 | /* This realloc's G.global_argv */ |
| 8514 | G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1); |
| 8515 | |
| 8516 | n = 1; |
| 8517 | while (*++pp) |
| 8518 | n++; |
| 8519 | G.global_argc = n; |
| 8520 | |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8521 | return EXIT_SUCCESS; |
Mike Frysinger | ad88d5a | 2009-03-28 13:44:51 +0000 | [diff] [blame] | 8522 | |
| 8523 | /* Nothing known, so abort */ |
| 8524 | error: |
| 8525 | bb_error_msg("set: %s: invalid option", arg); |
| 8526 | return EXIT_FAILURE; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8527 | } |
| 8528 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8529 | static int FAST_FUNC builtin_shift(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8530 | { |
| 8531 | int n = 1; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8532 | argv = skip_dash_dash(argv); |
| 8533 | if (argv[0]) { |
| 8534 | n = atoi(argv[0]); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8535 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8536 | if (n >= 0 && n < G.global_argc) { |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8537 | if (G.global_args_malloced) { |
| 8538 | int m = 1; |
| 8539 | while (m <= n) |
| 8540 | free(G.global_argv[m++]); |
| 8541 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8542 | G.global_argc -= n; |
Denis Vlasenko | e1300f6 | 2009-03-22 11:41:18 +0000 | [diff] [blame] | 8543 | memmove(&G.global_argv[1], &G.global_argv[n+1], |
| 8544 | G.global_argc * sizeof(G.global_argv[0])); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8545 | return EXIT_SUCCESS; |
| 8546 | } |
| 8547 | return EXIT_FAILURE; |
| 8548 | } |
| 8549 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8550 | static int FAST_FUNC builtin_source(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8551 | { |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8552 | char *arg_path, *filename; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8553 | FILE *input; |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8554 | save_arg_t sv; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8555 | #if ENABLE_HUSH_FUNCTIONS |
| 8556 | smallint sv_flg; |
| 8557 | #endif |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8558 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8559 | argv = skip_dash_dash(argv); |
| 8560 | filename = argv[0]; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8561 | if (!filename) { |
| 8562 | /* bash says: "bash: .: filename argument required" */ |
| 8563 | return 2; /* bash compat */ |
| 8564 | } |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8565 | arg_path = NULL; |
Denys Vlasenko | e66cf82 | 2010-05-18 09:12:53 +0200 | [diff] [blame] | 8566 | if (!strchr(filename, '/')) { |
| 8567 | arg_path = find_in_path(filename); |
| 8568 | if (arg_path) |
| 8569 | filename = arg_path; |
| 8570 | } |
| 8571 | input = fopen_or_warn(filename, "r"); |
| 8572 | free(arg_path); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8573 | if (!input) { |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8574 | /* bb_perror_msg("%s", *argv); - done by fopen_or_warn */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8575 | return EXIT_FAILURE; |
| 8576 | } |
| 8577 | close_on_exec_on(fileno(input)); |
| 8578 | |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8579 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8580 | sv_flg = G.flag_return_in_progress; |
| 8581 | /* "we are inside sourced file, ok to use return" */ |
| 8582 | G.flag_return_in_progress = -1; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8583 | #endif |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8584 | save_and_replace_G_args(&sv, argv); |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8585 | |
Denis Vlasenko | b6e6556 | 2009-04-03 16:49:04 +0000 | [diff] [blame] | 8586 | parse_and_run_file(input); |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8587 | fclose(input); |
Denis Vlasenko | 270b1c3 | 2009-04-17 18:54:50 +0000 | [diff] [blame] | 8588 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8589 | restore_G_args(&sv, argv); |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8590 | #if ENABLE_HUSH_FUNCTIONS |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8591 | G.flag_return_in_progress = sv_flg; |
Mike Frysinger | 885b6f2 | 2009-04-18 21:04:25 +0000 | [diff] [blame] | 8592 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8593 | |
Denis Vlasenko | ab2b064 | 2009-04-06 18:42:11 +0000 | [diff] [blame] | 8594 | return G.last_exitcode; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8595 | } |
| 8596 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8597 | static int FAST_FUNC builtin_umask(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8598 | { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8599 | int rc; |
| 8600 | mode_t mask; |
| 8601 | |
| 8602 | mask = umask(0); |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8603 | argv = skip_dash_dash(argv); |
| 8604 | if (argv[0]) { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8605 | mode_t old_mask = mask; |
| 8606 | |
| 8607 | mask ^= 0777; |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8608 | rc = bb_parse_mode(argv[0], &mask); |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8609 | mask ^= 0777; |
| 8610 | if (rc == 0) { |
| 8611 | mask = old_mask; |
| 8612 | /* bash messages: |
| 8613 | * bash: umask: 'q': invalid symbolic mode operator |
| 8614 | * bash: umask: 999: octal number out of range |
| 8615 | */ |
Denys Vlasenko | 44c86ce | 2010-05-20 04:22:55 +0200 | [diff] [blame] | 8616 | bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]); |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8617 | } |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8618 | } else { |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8619 | rc = 1; |
| 8620 | /* Mimic bash */ |
| 8621 | printf("%04o\n", (unsigned) mask); |
| 8622 | /* fall through and restore mask which we set to 0 */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8623 | } |
Denis Vlasenko | eb85849 | 2009-04-18 02:06:54 +0000 | [diff] [blame] | 8624 | umask(mask); |
| 8625 | |
| 8626 | return !rc; /* rc != 0 - success */ |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8627 | } |
| 8628 | |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8629 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8630 | static int FAST_FUNC builtin_unset(char **argv) |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8631 | { |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8632 | int ret; |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8633 | unsigned opts; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8634 | |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8635 | /* "!": do not abort on errors */ |
| 8636 | /* "+": stop at 1st non-option */ |
| 8637 | opts = getopt32(argv, "!+vf"); |
| 8638 | if (opts == (unsigned)-1) |
| 8639 | return EXIT_FAILURE; |
| 8640 | if (opts == 3) { |
| 8641 | bb_error_msg("unset: -v and -f are exclusive"); |
| 8642 | return EXIT_FAILURE; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8643 | } |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8644 | argv += optind; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8645 | |
| 8646 | ret = EXIT_SUCCESS; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8647 | while (*argv) { |
Denis Vlasenko | 28e6796 | 2009-04-26 23:22:40 +0000 | [diff] [blame] | 8648 | if (!(opts & 2)) { /* not -f */ |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8649 | if (unset_local_var(*argv)) { |
| 8650 | /* unset <nonexistent_var> doesn't fail. |
| 8651 | * Error is when one tries to unset RO var. |
| 8652 | * Message was printed by unset_local_var. */ |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8653 | ret = EXIT_FAILURE; |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8654 | } |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8655 | } |
Denis Vlasenko | 40e8437 | 2009-04-18 11:23:38 +0000 | [diff] [blame] | 8656 | #if ENABLE_HUSH_FUNCTIONS |
| 8657 | else { |
| 8658 | unset_func(*argv); |
| 8659 | } |
| 8660 | #endif |
Denis Vlasenko | bfbc971 | 2009-04-06 12:04:42 +0000 | [diff] [blame] | 8661 | argv++; |
Mike Frysinger | d690f68 | 2009-03-30 06:50:54 +0000 | [diff] [blame] | 8662 | } |
| 8663 | return ret; |
Denis Vlasenko | c7985b7 | 2008-06-17 05:43:38 +0000 | [diff] [blame] | 8664 | } |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8665 | |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8666 | /* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8667 | static int FAST_FUNC builtin_wait(char **argv) |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8668 | { |
| 8669 | int ret = EXIT_SUCCESS; |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8670 | int status, sig; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8671 | |
Denys Vlasenko | b131cce | 2010-05-20 03:39:43 +0200 | [diff] [blame] | 8672 | argv = skip_dash_dash(argv); |
| 8673 | if (argv[0] == NULL) { |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8674 | /* Don't care about wait results */ |
| 8675 | /* Note 1: must wait until there are no more children */ |
| 8676 | /* Note 2: must be interruptible */ |
| 8677 | /* Examples: |
| 8678 | * $ sleep 3 & sleep 6 & wait |
| 8679 | * [1] 30934 sleep 3 |
| 8680 | * [2] 30935 sleep 6 |
| 8681 | * [1] Done sleep 3 |
| 8682 | * [2] Done sleep 6 |
| 8683 | * $ sleep 3 & sleep 6 & wait |
| 8684 | * [1] 30936 sleep 3 |
| 8685 | * [2] 30937 sleep 6 |
| 8686 | * [1] Done sleep 3 |
| 8687 | * ^C <-- after ~4 sec from keyboard |
| 8688 | * $ |
| 8689 | */ |
| 8690 | sigaddset(&G.blocked_set, SIGCHLD); |
| 8691 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8692 | while (1) { |
| 8693 | checkjobs(NULL); |
| 8694 | if (errno == ECHILD) |
| 8695 | break; |
| 8696 | /* Wait for SIGCHLD or any other signal of interest */ |
| 8697 | /* sigtimedwait with infinite timeout: */ |
| 8698 | sig = sigwaitinfo(&G.blocked_set, NULL); |
| 8699 | if (sig > 0) { |
| 8700 | sig = check_and_run_traps(sig); |
| 8701 | if (sig && sig != SIGCHLD) { /* see note 2 */ |
| 8702 | ret = 128 + sig; |
| 8703 | break; |
| 8704 | } |
| 8705 | } |
| 8706 | } |
| 8707 | sigdelset(&G.blocked_set, SIGCHLD); |
| 8708 | sigprocmask(SIG_SETMASK, &G.blocked_set, NULL); |
| 8709 | return ret; |
| 8710 | } |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8711 | |
Denis Vlasenko | 7566bae | 2009-03-31 17:24:49 +0000 | [diff] [blame] | 8712 | /* This is probably buggy wrt interruptible-ness */ |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8713 | while (*argv) { |
| 8714 | pid_t pid = bb_strtou(*argv, NULL, 10); |
Mike Frysinger | 40b8dc4 | 2009-03-29 00:50:30 +0000 | [diff] [blame] | 8715 | if (errno) { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8716 | /* mimic bash message */ |
| 8717 | 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] | 8718 | return EXIT_FAILURE; |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8719 | } |
| 8720 | if (waitpid(pid, &status, 0) == pid) { |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8721 | if (WIFSIGNALED(status)) |
| 8722 | ret = 128 + WTERMSIG(status); |
| 8723 | else if (WIFEXITED(status)) |
| 8724 | ret = WEXITSTATUS(status); |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8725 | else /* wtf? */ |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8726 | ret = EXIT_FAILURE; |
| 8727 | } else { |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8728 | bb_perror_msg("wait %s", *argv); |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8729 | ret = 127; |
| 8730 | } |
Denis Vlasenko | d576293 | 2009-03-31 11:22:57 +0000 | [diff] [blame] | 8731 | argv++; |
Mike Frysinger | 56bdea1 | 2009-03-28 20:01:58 +0000 | [diff] [blame] | 8732 | } |
| 8733 | |
| 8734 | return ret; |
| 8735 | } |
| 8736 | |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8737 | #if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS |
| 8738 | static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min) |
| 8739 | { |
| 8740 | if (argv[1]) { |
| 8741 | def = bb_strtou(argv[1], NULL, 10); |
| 8742 | if (errno || def < def_min || argv[2]) { |
| 8743 | bb_error_msg("%s: bad arguments", argv[0]); |
| 8744 | def = UINT_MAX; |
| 8745 | } |
| 8746 | } |
| 8747 | return def; |
| 8748 | } |
| 8749 | #endif |
| 8750 | |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8751 | #if ENABLE_HUSH_LOOPS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8752 | static int FAST_FUNC builtin_break(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8753 | { |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8754 | unsigned depth; |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8755 | if (G.depth_of_loop == 0) { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8756 | bb_error_msg("%s: only meaningful in a loop", argv[0]); |
Denis Vlasenko | fcf37c3 | 2008-07-29 11:37:15 +0000 | [diff] [blame] | 8757 | return EXIT_SUCCESS; /* bash compat */ |
| 8758 | } |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8759 | G.flag_break_continue++; /* BC_BREAK = 1 */ |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8760 | |
| 8761 | G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1); |
| 8762 | if (depth == UINT_MAX) |
| 8763 | G.flag_break_continue = BC_BREAK; |
| 8764 | if (G.depth_of_loop < depth) |
Denis Vlasenko | 87a8655 | 2008-07-29 19:43:10 +0000 | [diff] [blame] | 8765 | G.depth_break_continue = G.depth_of_loop; |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8766 | |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8767 | return EXIT_SUCCESS; |
| 8768 | } |
| 8769 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8770 | static int FAST_FUNC builtin_continue(char **argv) |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8771 | { |
Denis Vlasenko | 4f504a9 | 2008-07-29 19:48:30 +0000 | [diff] [blame] | 8772 | G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */ |
| 8773 | return builtin_break(argv); |
Denis Vlasenko | bcb2553 | 2008-07-28 23:04:34 +0000 | [diff] [blame] | 8774 | } |
Denis Vlasenko | dadfb49 | 2008-07-29 10:16:05 +0000 | [diff] [blame] | 8775 | #endif |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8776 | |
| 8777 | #if ENABLE_HUSH_FUNCTIONS |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 8778 | static int FAST_FUNC builtin_return(char **argv) |
Denis Vlasenko | 3d40d8e | 2009-04-17 23:44:18 +0000 | [diff] [blame] | 8779 | { |
| 8780 | int rc; |
| 8781 | |
| 8782 | if (G.flag_return_in_progress != -1) { |
| 8783 | bb_error_msg("%s: not in a function or sourced script", argv[0]); |
| 8784 | return EXIT_FAILURE; /* bash compat */ |
| 8785 | } |
| 8786 | |
| 8787 | G.flag_return_in_progress = 1; |
| 8788 | |
| 8789 | /* bash: |
| 8790 | * out of range: wraps around at 256, does not error out |
| 8791 | * non-numeric param: |
| 8792 | * f() { false; return qwe; }; f; echo $? |
| 8793 | * bash: return: qwe: numeric argument required <== we do this |
| 8794 | * 255 <== we also do this |
| 8795 | */ |
| 8796 | rc = parse_numeric_argv1(argv, G.last_exitcode, 0); |
| 8797 | return rc; |
| 8798 | } |
| 8799 | #endif |